blob: 9f72d9a76a387e5b20bb479370a8af13312f6d95 [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);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100108
Dave Airlie0e32b392014-05-02 14:02:48 +1000109static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
110{
111 if (!connector->mst_port)
112 return connector->encoder;
113 else
114 return &connector->mst_port->mst_encoders[pipe]->base;
115}
116
Jesse Barnes79e53942008-11-07 14:24:08 -0800117typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400118 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800119} intel_range_t;
120
121typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400122 int dot_limit;
123 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800124} intel_p2_t;
125
Ma Lingd4906092009-03-18 20:13:27 +0800126typedef struct intel_limit intel_limit_t;
127struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400128 intel_range_t dot, vco, n, m, m1, m2, p, p1;
129 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800130};
Jesse Barnes79e53942008-11-07 14:24:08 -0800131
Daniel Vetterd2acd212012-10-20 20:57:43 +0200132int
133intel_pch_rawclk(struct drm_device *dev)
134{
135 struct drm_i915_private *dev_priv = dev->dev_private;
136
137 WARN_ON(!HAS_PCH_SPLIT(dev));
138
139 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
140}
141
Chris Wilson021357a2010-09-07 20:54:59 +0100142static inline u32 /* units of 100MHz */
143intel_fdi_link_freq(struct drm_device *dev)
144{
Chris Wilson8b99e682010-10-13 09:59:17 +0100145 if (IS_GEN5(dev)) {
146 struct drm_i915_private *dev_priv = dev->dev_private;
147 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
148 } else
149 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100150}
151
Daniel Vetter5d536e22013-07-06 12:52:06 +0200152static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400153 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200154 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200155 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400156 .m = { .min = 96, .max = 140 },
157 .m1 = { .min = 18, .max = 26 },
158 .m2 = { .min = 6, .max = 16 },
159 .p = { .min = 4, .max = 128 },
160 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700161 .p2 = { .dot_limit = 165000,
162 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700163};
164
Daniel Vetter5d536e22013-07-06 12:52:06 +0200165static const intel_limit_t intel_limits_i8xx_dvo = {
166 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200167 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200168 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200169 .m = { .min = 96, .max = 140 },
170 .m1 = { .min = 18, .max = 26 },
171 .m2 = { .min = 6, .max = 16 },
172 .p = { .min = 4, .max = 128 },
173 .p1 = { .min = 2, .max = 33 },
174 .p2 = { .dot_limit = 165000,
175 .p2_slow = 4, .p2_fast = 4 },
176};
177
Keith Packarde4b36692009-06-05 19:22:17 -0700178static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400179 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200180 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200181 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400182 .m = { .min = 96, .max = 140 },
183 .m1 = { .min = 18, .max = 26 },
184 .m2 = { .min = 6, .max = 16 },
185 .p = { .min = 4, .max = 128 },
186 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700187 .p2 = { .dot_limit = 165000,
188 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700189};
Eric Anholt273e27c2011-03-30 13:01:10 -0700190
Keith Packarde4b36692009-06-05 19:22:17 -0700191static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400192 .dot = { .min = 20000, .max = 400000 },
193 .vco = { .min = 1400000, .max = 2800000 },
194 .n = { .min = 1, .max = 6 },
195 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100196 .m1 = { .min = 8, .max = 18 },
197 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400198 .p = { .min = 5, .max = 80 },
199 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700200 .p2 = { .dot_limit = 200000,
201 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700202};
203
204static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400205 .dot = { .min = 20000, .max = 400000 },
206 .vco = { .min = 1400000, .max = 2800000 },
207 .n = { .min = 1, .max = 6 },
208 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100209 .m1 = { .min = 8, .max = 18 },
210 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400211 .p = { .min = 7, .max = 98 },
212 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700213 .p2 = { .dot_limit = 112000,
214 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700215};
216
Eric Anholt273e27c2011-03-30 13:01:10 -0700217
Keith Packarde4b36692009-06-05 19:22:17 -0700218static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700219 .dot = { .min = 25000, .max = 270000 },
220 .vco = { .min = 1750000, .max = 3500000},
221 .n = { .min = 1, .max = 4 },
222 .m = { .min = 104, .max = 138 },
223 .m1 = { .min = 17, .max = 23 },
224 .m2 = { .min = 5, .max = 11 },
225 .p = { .min = 10, .max = 30 },
226 .p1 = { .min = 1, .max = 3},
227 .p2 = { .dot_limit = 270000,
228 .p2_slow = 10,
229 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800230 },
Keith Packarde4b36692009-06-05 19:22:17 -0700231};
232
233static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700234 .dot = { .min = 22000, .max = 400000 },
235 .vco = { .min = 1750000, .max = 3500000},
236 .n = { .min = 1, .max = 4 },
237 .m = { .min = 104, .max = 138 },
238 .m1 = { .min = 16, .max = 23 },
239 .m2 = { .min = 5, .max = 11 },
240 .p = { .min = 5, .max = 80 },
241 .p1 = { .min = 1, .max = 8},
242 .p2 = { .dot_limit = 165000,
243 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700244};
245
246static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700247 .dot = { .min = 20000, .max = 115000 },
248 .vco = { .min = 1750000, .max = 3500000 },
249 .n = { .min = 1, .max = 3 },
250 .m = { .min = 104, .max = 138 },
251 .m1 = { .min = 17, .max = 23 },
252 .m2 = { .min = 5, .max = 11 },
253 .p = { .min = 28, .max = 112 },
254 .p1 = { .min = 2, .max = 8 },
255 .p2 = { .dot_limit = 0,
256 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800257 },
Keith Packarde4b36692009-06-05 19:22:17 -0700258};
259
260static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700261 .dot = { .min = 80000, .max = 224000 },
262 .vco = { .min = 1750000, .max = 3500000 },
263 .n = { .min = 1, .max = 3 },
264 .m = { .min = 104, .max = 138 },
265 .m1 = { .min = 17, .max = 23 },
266 .m2 = { .min = 5, .max = 11 },
267 .p = { .min = 14, .max = 42 },
268 .p1 = { .min = 2, .max = 6 },
269 .p2 = { .dot_limit = 0,
270 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800271 },
Keith Packarde4b36692009-06-05 19:22:17 -0700272};
273
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500274static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400275 .dot = { .min = 20000, .max = 400000},
276 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700277 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400278 .n = { .min = 3, .max = 6 },
279 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700280 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400281 .m1 = { .min = 0, .max = 0 },
282 .m2 = { .min = 0, .max = 254 },
283 .p = { .min = 5, .max = 80 },
284 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700285 .p2 = { .dot_limit = 200000,
286 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700287};
288
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500289static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400290 .dot = { .min = 20000, .max = 400000 },
291 .vco = { .min = 1700000, .max = 3500000 },
292 .n = { .min = 3, .max = 6 },
293 .m = { .min = 2, .max = 256 },
294 .m1 = { .min = 0, .max = 0 },
295 .m2 = { .min = 0, .max = 254 },
296 .p = { .min = 7, .max = 112 },
297 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700298 .p2 = { .dot_limit = 112000,
299 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700300};
301
Eric Anholt273e27c2011-03-30 13:01:10 -0700302/* Ironlake / Sandybridge
303 *
304 * We calculate clock using (register_value + 2) for N/M1/M2, so here
305 * the range value for them is (actual_value - 2).
306 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800307static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700308 .dot = { .min = 25000, .max = 350000 },
309 .vco = { .min = 1760000, .max = 3510000 },
310 .n = { .min = 1, .max = 5 },
311 .m = { .min = 79, .max = 127 },
312 .m1 = { .min = 12, .max = 22 },
313 .m2 = { .min = 5, .max = 9 },
314 .p = { .min = 5, .max = 80 },
315 .p1 = { .min = 1, .max = 8 },
316 .p2 = { .dot_limit = 225000,
317 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700318};
319
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800320static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700321 .dot = { .min = 25000, .max = 350000 },
322 .vco = { .min = 1760000, .max = 3510000 },
323 .n = { .min = 1, .max = 3 },
324 .m = { .min = 79, .max = 118 },
325 .m1 = { .min = 12, .max = 22 },
326 .m2 = { .min = 5, .max = 9 },
327 .p = { .min = 28, .max = 112 },
328 .p1 = { .min = 2, .max = 8 },
329 .p2 = { .dot_limit = 225000,
330 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800331};
332
333static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700334 .dot = { .min = 25000, .max = 350000 },
335 .vco = { .min = 1760000, .max = 3510000 },
336 .n = { .min = 1, .max = 3 },
337 .m = { .min = 79, .max = 127 },
338 .m1 = { .min = 12, .max = 22 },
339 .m2 = { .min = 5, .max = 9 },
340 .p = { .min = 14, .max = 56 },
341 .p1 = { .min = 2, .max = 8 },
342 .p2 = { .dot_limit = 225000,
343 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800344};
345
Eric Anholt273e27c2011-03-30 13:01:10 -0700346/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800347static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700348 .dot = { .min = 25000, .max = 350000 },
349 .vco = { .min = 1760000, .max = 3510000 },
350 .n = { .min = 1, .max = 2 },
351 .m = { .min = 79, .max = 126 },
352 .m1 = { .min = 12, .max = 22 },
353 .m2 = { .min = 5, .max = 9 },
354 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400355 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700356 .p2 = { .dot_limit = 225000,
357 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800358};
359
360static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700361 .dot = { .min = 25000, .max = 350000 },
362 .vco = { .min = 1760000, .max = 3510000 },
363 .n = { .min = 1, .max = 3 },
364 .m = { .min = 79, .max = 126 },
365 .m1 = { .min = 12, .max = 22 },
366 .m2 = { .min = 5, .max = 9 },
367 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400368 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700369 .p2 = { .dot_limit = 225000,
370 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800371};
372
Ville Syrjälädc730512013-09-24 21:26:30 +0300373static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300374 /*
375 * These are the data rate limits (measured in fast clocks)
376 * since those are the strictest limits we have. The fast
377 * clock and actual rate limits are more relaxed, so checking
378 * them would make no difference.
379 */
380 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200381 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700382 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700383 .m1 = { .min = 2, .max = 3 },
384 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300385 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300386 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700387};
388
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300389static const intel_limit_t intel_limits_chv = {
390 /*
391 * These are the data rate limits (measured in fast clocks)
392 * since those are the strictest limits we have. The fast
393 * clock and actual rate limits are more relaxed, so checking
394 * them would make no difference.
395 */
396 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200397 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300398 .n = { .min = 1, .max = 1 },
399 .m1 = { .min = 2, .max = 2 },
400 .m2 = { .min = 24 << 22, .max = 175 << 22 },
401 .p1 = { .min = 2, .max = 4 },
402 .p2 = { .p2_slow = 1, .p2_fast = 14 },
403};
404
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300405static void vlv_clock(int refclk, intel_clock_t *clock)
406{
407 clock->m = clock->m1 * clock->m2;
408 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200409 if (WARN_ON(clock->n == 0 || clock->p == 0))
410 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300411 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
412 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300413}
414
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300415/**
416 * Returns whether any output on the specified pipe is of the specified type
417 */
Damien Lespiau40935612014-10-29 11:16:59 +0000418bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300419{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300420 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300421 struct intel_encoder *encoder;
422
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300423 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300424 if (encoder->type == type)
425 return true;
426
427 return false;
428}
429
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200430/**
431 * Returns whether any output on the specified pipe will have the specified
432 * type after a staged modeset is complete, i.e., the same as
433 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
434 * encoder->crtc.
435 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200436static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
437 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200438{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200439 struct drm_atomic_state *state = crtc_state->base.state;
440 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200441 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200442 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200443
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200444 for (i = 0; i < state->num_connector; i++) {
445 if (!state->connectors[i])
446 continue;
447
448 connector_state = state->connector_states[i];
449 if (connector_state->crtc != crtc_state->base.crtc)
450 continue;
451
452 num_connectors++;
453
454 encoder = to_intel_encoder(connector_state->best_encoder);
455 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200456 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200457 }
458
459 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200460
461 return false;
462}
463
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200464static const intel_limit_t *
465intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800466{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200467 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800468 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800469
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200470 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100471 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000472 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800473 limit = &intel_limits_ironlake_dual_lvds_100m;
474 else
475 limit = &intel_limits_ironlake_dual_lvds;
476 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000477 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800478 limit = &intel_limits_ironlake_single_lvds_100m;
479 else
480 limit = &intel_limits_ironlake_single_lvds;
481 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200482 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800483 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800484
485 return limit;
486}
487
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200488static const intel_limit_t *
489intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800490{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200491 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800492 const intel_limit_t *limit;
493
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200494 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100495 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700496 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800497 else
Keith Packarde4b36692009-06-05 19:22:17 -0700498 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200499 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
500 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700501 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200502 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700503 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800504 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700505 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800506
507 return limit;
508}
509
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200510static const intel_limit_t *
511intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800512{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200513 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800514 const intel_limit_t *limit;
515
Eric Anholtbad720f2009-10-22 16:11:14 -0700516 if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200517 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800518 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200519 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500520 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200521 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500522 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800523 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500524 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300525 } else if (IS_CHERRYVIEW(dev)) {
526 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700527 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300528 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100529 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200530 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100531 limit = &intel_limits_i9xx_lvds;
532 else
533 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800534 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200535 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700536 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200537 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700538 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200539 else
540 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800541 }
542 return limit;
543}
544
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500545/* m1 is reserved as 0 in Pineview, n is a ring counter */
546static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800547{
Shaohua Li21778322009-02-23 15:19:16 +0800548 clock->m = clock->m2 + 2;
549 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200550 if (WARN_ON(clock->n == 0 || clock->p == 0))
551 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300552 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
553 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800554}
555
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200556static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
557{
558 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
559}
560
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200561static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800562{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200563 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800564 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200565 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
566 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300567 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
568 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800569}
570
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300571static void chv_clock(int refclk, intel_clock_t *clock)
572{
573 clock->m = clock->m1 * clock->m2;
574 clock->p = clock->p1 * clock->p2;
575 if (WARN_ON(clock->n == 0 || clock->p == 0))
576 return;
577 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
578 clock->n << 22);
579 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
580}
581
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800582#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800583/**
584 * Returns whether the given set of divisors are valid for a given refclk with
585 * the given connectors.
586 */
587
Chris Wilson1b894b52010-12-14 20:04:54 +0000588static bool intel_PLL_is_valid(struct drm_device *dev,
589 const intel_limit_t *limit,
590 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800591{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300592 if (clock->n < limit->n.min || limit->n.max < clock->n)
593 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800594 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400595 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800596 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400597 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800598 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400599 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300600
601 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
602 if (clock->m1 <= clock->m2)
603 INTELPllInvalid("m1 <= m2\n");
604
605 if (!IS_VALLEYVIEW(dev)) {
606 if (clock->p < limit->p.min || limit->p.max < clock->p)
607 INTELPllInvalid("p out of range\n");
608 if (clock->m < limit->m.min || limit->m.max < clock->m)
609 INTELPllInvalid("m out of range\n");
610 }
611
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400613 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800614 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
615 * connector, etc., rather than just a single range.
616 */
617 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400618 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800619
620 return true;
621}
622
Ma Lingd4906092009-03-18 20:13:27 +0800623static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200624i9xx_find_best_dpll(const intel_limit_t *limit,
625 struct intel_crtc_state *crtc_state,
Sean Paulcec2f352012-01-10 15:09:36 -0800626 int target, int refclk, intel_clock_t *match_clock,
627 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800628{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200629 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300630 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800631 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800632 int err = target;
633
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200634 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800635 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100636 * For LVDS just rely on its current settings for dual-channel.
637 * We haven't figured out how to reliably set up different
638 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800639 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100640 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800641 clock.p2 = limit->p2.p2_fast;
642 else
643 clock.p2 = limit->p2.p2_slow;
644 } else {
645 if (target < limit->p2.dot_limit)
646 clock.p2 = limit->p2.p2_slow;
647 else
648 clock.p2 = limit->p2.p2_fast;
649 }
650
Akshay Joshi0206e352011-08-16 15:34:10 -0400651 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800652
Zhao Yakui42158662009-11-20 11:24:18 +0800653 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
654 clock.m1++) {
655 for (clock.m2 = limit->m2.min;
656 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200657 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800658 break;
659 for (clock.n = limit->n.min;
660 clock.n <= limit->n.max; clock.n++) {
661 for (clock.p1 = limit->p1.min;
662 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800663 int this_err;
664
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200665 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000666 if (!intel_PLL_is_valid(dev, limit,
667 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800668 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800669 if (match_clock &&
670 clock.p != match_clock->p)
671 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800672
673 this_err = abs(clock.dot - target);
674 if (this_err < err) {
675 *best_clock = clock;
676 err = this_err;
677 }
678 }
679 }
680 }
681 }
682
683 return (err != target);
684}
685
Ma Lingd4906092009-03-18 20:13:27 +0800686static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200687pnv_find_best_dpll(const intel_limit_t *limit,
688 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200689 int target, int refclk, intel_clock_t *match_clock,
690 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200691{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200692 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300693 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200694 intel_clock_t clock;
695 int err = target;
696
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200697 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200698 /*
699 * For LVDS just rely on its current settings for dual-channel.
700 * We haven't figured out how to reliably set up different
701 * single/dual channel state, if we even can.
702 */
703 if (intel_is_dual_link_lvds(dev))
704 clock.p2 = limit->p2.p2_fast;
705 else
706 clock.p2 = limit->p2.p2_slow;
707 } else {
708 if (target < limit->p2.dot_limit)
709 clock.p2 = limit->p2.p2_slow;
710 else
711 clock.p2 = limit->p2.p2_fast;
712 }
713
714 memset(best_clock, 0, sizeof(*best_clock));
715
716 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
717 clock.m1++) {
718 for (clock.m2 = limit->m2.min;
719 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200720 for (clock.n = limit->n.min;
721 clock.n <= limit->n.max; clock.n++) {
722 for (clock.p1 = limit->p1.min;
723 clock.p1 <= limit->p1.max; clock.p1++) {
724 int this_err;
725
726 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800727 if (!intel_PLL_is_valid(dev, limit,
728 &clock))
729 continue;
730 if (match_clock &&
731 clock.p != match_clock->p)
732 continue;
733
734 this_err = abs(clock.dot - target);
735 if (this_err < err) {
736 *best_clock = clock;
737 err = this_err;
738 }
739 }
740 }
741 }
742 }
743
744 return (err != target);
745}
746
Ma Lingd4906092009-03-18 20:13:27 +0800747static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200748g4x_find_best_dpll(const intel_limit_t *limit,
749 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200750 int target, int refclk, intel_clock_t *match_clock,
751 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800752{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200753 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300754 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800755 intel_clock_t clock;
756 int max_n;
757 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400758 /* approximately equals target * 0.00585 */
759 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800760 found = false;
761
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200762 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100763 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800764 clock.p2 = limit->p2.p2_fast;
765 else
766 clock.p2 = limit->p2.p2_slow;
767 } else {
768 if (target < limit->p2.dot_limit)
769 clock.p2 = limit->p2.p2_slow;
770 else
771 clock.p2 = limit->p2.p2_fast;
772 }
773
774 memset(best_clock, 0, sizeof(*best_clock));
775 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200776 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800777 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200778 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800779 for (clock.m1 = limit->m1.max;
780 clock.m1 >= limit->m1.min; clock.m1--) {
781 for (clock.m2 = limit->m2.max;
782 clock.m2 >= limit->m2.min; clock.m2--) {
783 for (clock.p1 = limit->p1.max;
784 clock.p1 >= limit->p1.min; clock.p1--) {
785 int this_err;
786
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200787 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000788 if (!intel_PLL_is_valid(dev, limit,
789 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800790 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000791
792 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800793 if (this_err < err_most) {
794 *best_clock = clock;
795 err_most = this_err;
796 max_n = clock.n;
797 found = true;
798 }
799 }
800 }
801 }
802 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800803 return found;
804}
Ma Lingd4906092009-03-18 20:13:27 +0800805
Imre Deakd5dd62b2015-03-17 11:40:03 +0200806/*
807 * Check if the calculated PLL configuration is more optimal compared to the
808 * best configuration and error found so far. Return the calculated error.
809 */
810static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
811 const intel_clock_t *calculated_clock,
812 const intel_clock_t *best_clock,
813 unsigned int best_error_ppm,
814 unsigned int *error_ppm)
815{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200816 /*
817 * For CHV ignore the error and consider only the P value.
818 * Prefer a bigger P value based on HW requirements.
819 */
820 if (IS_CHERRYVIEW(dev)) {
821 *error_ppm = 0;
822
823 return calculated_clock->p > best_clock->p;
824 }
825
Imre Deak24be4e42015-03-17 11:40:04 +0200826 if (WARN_ON_ONCE(!target_freq))
827 return false;
828
Imre Deakd5dd62b2015-03-17 11:40:03 +0200829 *error_ppm = div_u64(1000000ULL *
830 abs(target_freq - calculated_clock->dot),
831 target_freq);
832 /*
833 * Prefer a better P value over a better (smaller) error if the error
834 * is small. Ensure this preference for future configurations too by
835 * setting the error to 0.
836 */
837 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
838 *error_ppm = 0;
839
840 return true;
841 }
842
843 return *error_ppm + 10 < best_error_ppm;
844}
845
Zhenyu Wang2c072452009-06-05 15:38:42 +0800846static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200847vlv_find_best_dpll(const intel_limit_t *limit,
848 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200849 int target, int refclk, intel_clock_t *match_clock,
850 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700851{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200852 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300853 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300854 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300855 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300856 /* min update 19.2 MHz */
857 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300858 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700859
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300860 target *= 5; /* fast clock */
861
862 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700863
864 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300865 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300866 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300867 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300868 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300869 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700870 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300871 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200872 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300873
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300874 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
875 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300876
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300877 vlv_clock(refclk, &clock);
878
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300879 if (!intel_PLL_is_valid(dev, limit,
880 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300881 continue;
882
Imre Deakd5dd62b2015-03-17 11:40:03 +0200883 if (!vlv_PLL_is_optimal(dev, target,
884 &clock,
885 best_clock,
886 bestppm, &ppm))
887 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300888
Imre Deakd5dd62b2015-03-17 11:40:03 +0200889 *best_clock = clock;
890 bestppm = ppm;
891 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700892 }
893 }
894 }
895 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700896
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300897 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700898}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700899
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300900static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200901chv_find_best_dpll(const intel_limit_t *limit,
902 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300903 int target, int refclk, intel_clock_t *match_clock,
904 intel_clock_t *best_clock)
905{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200906 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300907 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200908 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300909 intel_clock_t clock;
910 uint64_t m2;
911 int found = false;
912
913 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200914 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300915
916 /*
917 * Based on hardware doc, the n always set to 1, and m1 always
918 * set to 2. If requires to support 200Mhz refclk, we need to
919 * revisit this because n may not 1 anymore.
920 */
921 clock.n = 1, clock.m1 = 2;
922 target *= 5; /* fast clock */
923
924 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
925 for (clock.p2 = limit->p2.p2_fast;
926 clock.p2 >= limit->p2.p2_slow;
927 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200928 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300929
930 clock.p = clock.p1 * clock.p2;
931
932 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
933 clock.n) << 22, refclk * clock.m1);
934
935 if (m2 > INT_MAX/clock.m1)
936 continue;
937
938 clock.m2 = m2;
939
940 chv_clock(refclk, &clock);
941
942 if (!intel_PLL_is_valid(dev, limit, &clock))
943 continue;
944
Imre Deak9ca3ba02015-03-17 11:40:05 +0200945 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
946 best_error_ppm, &error_ppm))
947 continue;
948
949 *best_clock = clock;
950 best_error_ppm = error_ppm;
951 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300952 }
953 }
954
955 return found;
956}
957
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300958bool intel_crtc_active(struct drm_crtc *crtc)
959{
960 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
961
962 /* Be paranoid as we can arrive here with only partial
963 * state retrieved from the hardware during setup.
964 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100965 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300966 * as Haswell has gained clock readout/fastboot support.
967 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000968 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300969 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700970 *
971 * FIXME: The intel_crtc->active here should be switched to
972 * crtc->state->active once we have proper CRTC states wired up
973 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300974 */
Matt Roperc3d1f432015-03-09 10:19:23 -0700975 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200976 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300977}
978
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200979enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
980 enum pipe pipe)
981{
982 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
983 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
984
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200985 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200986}
987
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300988static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
989{
990 struct drm_i915_private *dev_priv = dev->dev_private;
991 u32 reg = PIPEDSL(pipe);
992 u32 line1, line2;
993 u32 line_mask;
994
995 if (IS_GEN2(dev))
996 line_mask = DSL_LINEMASK_GEN2;
997 else
998 line_mask = DSL_LINEMASK_GEN3;
999
1000 line1 = I915_READ(reg) & line_mask;
1001 mdelay(5);
1002 line2 = I915_READ(reg) & line_mask;
1003
1004 return line1 == line2;
1005}
1006
Keith Packardab7ad7f2010-10-03 00:33:06 -07001007/*
1008 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001009 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001010 *
1011 * After disabling a pipe, we can't wait for vblank in the usual way,
1012 * spinning on the vblank interrupt status bit, since we won't actually
1013 * see an interrupt when the pipe is disabled.
1014 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001015 * On Gen4 and above:
1016 * wait for the pipe register state bit to turn off
1017 *
1018 * Otherwise:
1019 * wait for the display line value to settle (it usually
1020 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001021 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001022 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001023static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001024{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001025 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001026 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001027 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001028 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001029
Keith Packardab7ad7f2010-10-03 00:33:06 -07001030 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001031 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001032
Keith Packardab7ad7f2010-10-03 00:33:06 -07001033 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001034 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1035 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001036 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001037 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001038 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001039 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001040 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001041 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001042}
1043
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001044/*
1045 * ibx_digital_port_connected - is the specified port connected?
1046 * @dev_priv: i915 private structure
1047 * @port: the port to test
1048 *
1049 * Returns true if @port is connected, false otherwise.
1050 */
1051bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1052 struct intel_digital_port *port)
1053{
1054 u32 bit;
1055
Damien Lespiauc36346e2012-12-13 16:09:03 +00001056 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001057 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001058 case PORT_B:
1059 bit = SDE_PORTB_HOTPLUG;
1060 break;
1061 case PORT_C:
1062 bit = SDE_PORTC_HOTPLUG;
1063 break;
1064 case PORT_D:
1065 bit = SDE_PORTD_HOTPLUG;
1066 break;
1067 default:
1068 return true;
1069 }
1070 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001071 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001072 case PORT_B:
1073 bit = SDE_PORTB_HOTPLUG_CPT;
1074 break;
1075 case PORT_C:
1076 bit = SDE_PORTC_HOTPLUG_CPT;
1077 break;
1078 case PORT_D:
1079 bit = SDE_PORTD_HOTPLUG_CPT;
1080 break;
1081 default:
1082 return true;
1083 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001084 }
1085
1086 return I915_READ(SDEISR) & bit;
1087}
1088
Jesse Barnesb24e7172011-01-04 15:09:30 -08001089static const char *state_string(bool enabled)
1090{
1091 return enabled ? "on" : "off";
1092}
1093
1094/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001095void assert_pll(struct drm_i915_private *dev_priv,
1096 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001097{
1098 int reg;
1099 u32 val;
1100 bool cur_state;
1101
1102 reg = DPLL(pipe);
1103 val = I915_READ(reg);
1104 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001105 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001106 "PLL state assertion failure (expected %s, current %s)\n",
1107 state_string(state), state_string(cur_state));
1108}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001109
Jani Nikula23538ef2013-08-27 15:12:22 +03001110/* XXX: the dsi pll is shared between MIPI DSI ports */
1111static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1112{
1113 u32 val;
1114 bool cur_state;
1115
1116 mutex_lock(&dev_priv->dpio_lock);
1117 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1118 mutex_unlock(&dev_priv->dpio_lock);
1119
1120 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001121 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001122 "DSI PLL state assertion failure (expected %s, current %s)\n",
1123 state_string(state), state_string(cur_state));
1124}
1125#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1126#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1127
Daniel Vetter55607e82013-06-16 21:42:39 +02001128struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001129intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001130{
Daniel Vettere2b78262013-06-07 23:10:03 +02001131 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1132
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001133 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001134 return NULL;
1135
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001136 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001137}
1138
Jesse Barnesb24e7172011-01-04 15:09:30 -08001139/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001140void assert_shared_dpll(struct drm_i915_private *dev_priv,
1141 struct intel_shared_dpll *pll,
1142 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001143{
Jesse Barnes040484a2011-01-03 12:14:26 -08001144 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001145 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001146
Chris Wilson92b27b02012-05-20 18:10:50 +01001147 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001148 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001149 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001150
Daniel Vetter53589012013-06-05 13:34:16 +02001151 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001152 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001153 "%s assertion failure (expected %s, current %s)\n",
1154 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001155}
Jesse Barnes040484a2011-01-03 12:14:26 -08001156
1157static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1158 enum pipe pipe, bool state)
1159{
1160 int reg;
1161 u32 val;
1162 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001163 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1164 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001165
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001166 if (HAS_DDI(dev_priv->dev)) {
1167 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001168 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001169 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001170 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001171 } else {
1172 reg = FDI_TX_CTL(pipe);
1173 val = I915_READ(reg);
1174 cur_state = !!(val & FDI_TX_ENABLE);
1175 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001176 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001177 "FDI TX state assertion failure (expected %s, current %s)\n",
1178 state_string(state), state_string(cur_state));
1179}
1180#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1181#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1182
1183static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1184 enum pipe pipe, bool state)
1185{
1186 int reg;
1187 u32 val;
1188 bool cur_state;
1189
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001190 reg = FDI_RX_CTL(pipe);
1191 val = I915_READ(reg);
1192 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001193 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001194 "FDI RX state assertion failure (expected %s, current %s)\n",
1195 state_string(state), state_string(cur_state));
1196}
1197#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1198#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1199
1200static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1201 enum pipe pipe)
1202{
1203 int reg;
1204 u32 val;
1205
1206 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001207 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001208 return;
1209
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001210 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001211 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001212 return;
1213
Jesse Barnes040484a2011-01-03 12:14:26 -08001214 reg = FDI_TX_CTL(pipe);
1215 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001216 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 -08001217}
1218
Daniel Vetter55607e82013-06-16 21:42:39 +02001219void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1220 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001221{
1222 int reg;
1223 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001224 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001225
1226 reg = FDI_RX_CTL(pipe);
1227 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001228 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001229 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001230 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1231 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001232}
1233
Daniel Vetterb680c372014-09-19 18:27:27 +02001234void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1235 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001236{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001237 struct drm_device *dev = dev_priv->dev;
1238 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001239 u32 val;
1240 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001241 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001242
Jani Nikulabedd4db2014-08-22 15:04:13 +03001243 if (WARN_ON(HAS_DDI(dev)))
1244 return;
1245
1246 if (HAS_PCH_SPLIT(dev)) {
1247 u32 port_sel;
1248
Jesse Barnesea0760c2011-01-04 15:09:32 -08001249 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001250 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1251
1252 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1253 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1254 panel_pipe = PIPE_B;
1255 /* XXX: else fix for eDP */
1256 } else if (IS_VALLEYVIEW(dev)) {
1257 /* presumably write lock depends on pipe, not port select */
1258 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1259 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001260 } else {
1261 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001262 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1263 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001264 }
1265
1266 val = I915_READ(pp_reg);
1267 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001268 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001269 locked = false;
1270
Rob Clarke2c719b2014-12-15 13:56:32 -05001271 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001272 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001273 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001274}
1275
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001276static void assert_cursor(struct drm_i915_private *dev_priv,
1277 enum pipe pipe, bool state)
1278{
1279 struct drm_device *dev = dev_priv->dev;
1280 bool cur_state;
1281
Paulo Zanonid9d82082014-02-27 16:30:56 -03001282 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001283 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001284 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001285 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001286
Rob Clarke2c719b2014-12-15 13:56:32 -05001287 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001288 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1289 pipe_name(pipe), state_string(state), state_string(cur_state));
1290}
1291#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1292#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1293
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001294void assert_pipe(struct drm_i915_private *dev_priv,
1295 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001296{
1297 int reg;
1298 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001299 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001300 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1301 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001302
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001303 /* if we need the pipe quirk it must be always on */
1304 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1305 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001306 state = true;
1307
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001308 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001309 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001310 cur_state = false;
1311 } else {
1312 reg = PIPECONF(cpu_transcoder);
1313 val = I915_READ(reg);
1314 cur_state = !!(val & PIPECONF_ENABLE);
1315 }
1316
Rob Clarke2c719b2014-12-15 13:56:32 -05001317 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001318 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001319 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001320}
1321
Chris Wilson931872f2012-01-16 23:01:13 +00001322static void assert_plane(struct drm_i915_private *dev_priv,
1323 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001324{
1325 int reg;
1326 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001327 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001328
1329 reg = DSPCNTR(plane);
1330 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001331 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001332 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001333 "plane %c assertion failure (expected %s, current %s)\n",
1334 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001335}
1336
Chris Wilson931872f2012-01-16 23:01:13 +00001337#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1338#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1339
Jesse Barnesb24e7172011-01-04 15:09:30 -08001340static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1341 enum pipe pipe)
1342{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001343 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001344 int reg, i;
1345 u32 val;
1346 int cur_pipe;
1347
Ville Syrjälä653e1022013-06-04 13:49:05 +03001348 /* Primary planes are fixed to pipes on gen4+ */
1349 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001350 reg = DSPCNTR(pipe);
1351 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001352 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001353 "plane %c assertion failure, should be disabled but not\n",
1354 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001355 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001356 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001357
Jesse Barnesb24e7172011-01-04 15:09:30 -08001358 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001359 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001360 reg = DSPCNTR(i);
1361 val = I915_READ(reg);
1362 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1363 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001364 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001365 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1366 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001367 }
1368}
1369
Jesse Barnes19332d72013-03-28 09:55:38 -07001370static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1371 enum pipe pipe)
1372{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001373 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001374 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001375 u32 val;
1376
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001377 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001378 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001379 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001380 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001381 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1382 sprite, pipe_name(pipe));
1383 }
1384 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001385 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001386 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001387 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001388 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001389 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001390 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001391 }
1392 } else if (INTEL_INFO(dev)->gen >= 7) {
1393 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001394 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001395 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001396 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001397 plane_name(pipe), pipe_name(pipe));
1398 } else if (INTEL_INFO(dev)->gen >= 5) {
1399 reg = DVSCNTR(pipe);
1400 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001401 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001402 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1403 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001404 }
1405}
1406
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001407static void assert_vblank_disabled(struct drm_crtc *crtc)
1408{
Rob Clarke2c719b2014-12-15 13:56:32 -05001409 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001410 drm_crtc_vblank_put(crtc);
1411}
1412
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001413static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001414{
1415 u32 val;
1416 bool enabled;
1417
Rob Clarke2c719b2014-12-15 13:56:32 -05001418 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001419
Jesse Barnes92f25842011-01-04 15:09:34 -08001420 val = I915_READ(PCH_DREF_CONTROL);
1421 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1422 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001423 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001424}
1425
Daniel Vetterab9412b2013-05-03 11:49:46 +02001426static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1427 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001428{
1429 int reg;
1430 u32 val;
1431 bool enabled;
1432
Daniel Vetterab9412b2013-05-03 11:49:46 +02001433 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001434 val = I915_READ(reg);
1435 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001436 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001437 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1438 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001439}
1440
Keith Packard4e634382011-08-06 10:39:45 -07001441static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1442 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001443{
1444 if ((val & DP_PORT_EN) == 0)
1445 return false;
1446
1447 if (HAS_PCH_CPT(dev_priv->dev)) {
1448 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1449 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1450 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1451 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001452 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1453 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1454 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001455 } else {
1456 if ((val & DP_PIPE_MASK) != (pipe << 30))
1457 return false;
1458 }
1459 return true;
1460}
1461
Keith Packard1519b992011-08-06 10:35:34 -07001462static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1463 enum pipe pipe, u32 val)
1464{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001465 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001466 return false;
1467
1468 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001469 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001470 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001471 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1472 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1473 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001474 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001475 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001476 return false;
1477 }
1478 return true;
1479}
1480
1481static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1482 enum pipe pipe, u32 val)
1483{
1484 if ((val & LVDS_PORT_EN) == 0)
1485 return false;
1486
1487 if (HAS_PCH_CPT(dev_priv->dev)) {
1488 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1489 return false;
1490 } else {
1491 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1492 return false;
1493 }
1494 return true;
1495}
1496
1497static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1498 enum pipe pipe, u32 val)
1499{
1500 if ((val & ADPA_DAC_ENABLE) == 0)
1501 return false;
1502 if (HAS_PCH_CPT(dev_priv->dev)) {
1503 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1504 return false;
1505 } else {
1506 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1507 return false;
1508 }
1509 return true;
1510}
1511
Jesse Barnes291906f2011-02-02 12:28:03 -08001512static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001513 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001514{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001515 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001516 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001517 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001518 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001519
Rob Clarke2c719b2014-12-15 13:56:32 -05001520 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001521 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001522 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001523}
1524
1525static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1526 enum pipe pipe, int reg)
1527{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001528 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001529 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001530 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001531 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001532
Rob Clarke2c719b2014-12-15 13:56:32 -05001533 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001534 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001535 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001536}
1537
1538static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1539 enum pipe pipe)
1540{
1541 int reg;
1542 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001543
Keith Packardf0575e92011-07-25 22:12:43 -07001544 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1545 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1546 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001547
1548 reg = PCH_ADPA;
1549 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001550 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001551 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001552 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001553
1554 reg = PCH_LVDS;
1555 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001556 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001557 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001558 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001559
Paulo Zanonie2debe92013-02-18 19:00:27 -03001560 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1561 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1562 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001563}
1564
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001565static void intel_init_dpio(struct drm_device *dev)
1566{
1567 struct drm_i915_private *dev_priv = dev->dev_private;
1568
1569 if (!IS_VALLEYVIEW(dev))
1570 return;
1571
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001572 /*
1573 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1574 * CHV x1 PHY (DP/HDMI D)
1575 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1576 */
1577 if (IS_CHERRYVIEW(dev)) {
1578 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1579 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1580 } else {
1581 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1582 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001583}
1584
Ville Syrjäläd288f652014-10-28 13:20:22 +02001585static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001586 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001587{
Daniel Vetter426115c2013-07-11 22:13:42 +02001588 struct drm_device *dev = crtc->base.dev;
1589 struct drm_i915_private *dev_priv = dev->dev_private;
1590 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001591 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001592
Daniel Vetter426115c2013-07-11 22:13:42 +02001593 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001594
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001595 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001596 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1597
1598 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001599 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001600 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001601
Daniel Vetter426115c2013-07-11 22:13:42 +02001602 I915_WRITE(reg, dpll);
1603 POSTING_READ(reg);
1604 udelay(150);
1605
1606 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1607 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1608
Ville Syrjäläd288f652014-10-28 13:20:22 +02001609 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001610 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001611
1612 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001613 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001614 POSTING_READ(reg);
1615 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001616 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001617 POSTING_READ(reg);
1618 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001619 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001620 POSTING_READ(reg);
1621 udelay(150); /* wait for warmup */
1622}
1623
Ville Syrjäläd288f652014-10-28 13:20:22 +02001624static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001625 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001626{
1627 struct drm_device *dev = crtc->base.dev;
1628 struct drm_i915_private *dev_priv = dev->dev_private;
1629 int pipe = crtc->pipe;
1630 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001631 u32 tmp;
1632
1633 assert_pipe_disabled(dev_priv, crtc->pipe);
1634
1635 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1636
1637 mutex_lock(&dev_priv->dpio_lock);
1638
1639 /* Enable back the 10bit clock to display controller */
1640 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1641 tmp |= DPIO_DCLKP_EN;
1642 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1643
1644 /*
1645 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1646 */
1647 udelay(1);
1648
1649 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001650 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001651
1652 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001653 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001654 DRM_ERROR("PLL %d failed to lock\n", pipe);
1655
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001656 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001657 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001658 POSTING_READ(DPLL_MD(pipe));
1659
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001660 mutex_unlock(&dev_priv->dpio_lock);
1661}
1662
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001663static int intel_num_dvo_pipes(struct drm_device *dev)
1664{
1665 struct intel_crtc *crtc;
1666 int count = 0;
1667
1668 for_each_intel_crtc(dev, crtc)
1669 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001670 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001671
1672 return count;
1673}
1674
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001675static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001676{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001677 struct drm_device *dev = crtc->base.dev;
1678 struct drm_i915_private *dev_priv = dev->dev_private;
1679 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001680 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001681
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001682 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001683
1684 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001685 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001686
1687 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001688 if (IS_MOBILE(dev) && !IS_I830(dev))
1689 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001690
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001691 /* Enable DVO 2x clock on both PLLs if necessary */
1692 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1693 /*
1694 * It appears to be important that we don't enable this
1695 * for the current pipe before otherwise configuring the
1696 * PLL. No idea how this should be handled if multiple
1697 * DVO outputs are enabled simultaneosly.
1698 */
1699 dpll |= DPLL_DVO_2X_MODE;
1700 I915_WRITE(DPLL(!crtc->pipe),
1701 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1702 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001703
1704 /* Wait for the clocks to stabilize. */
1705 POSTING_READ(reg);
1706 udelay(150);
1707
1708 if (INTEL_INFO(dev)->gen >= 4) {
1709 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001710 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001711 } else {
1712 /* The pixel multiplier can only be updated once the
1713 * DPLL is enabled and the clocks are stable.
1714 *
1715 * So write it again.
1716 */
1717 I915_WRITE(reg, dpll);
1718 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001719
1720 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001721 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001722 POSTING_READ(reg);
1723 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001724 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001725 POSTING_READ(reg);
1726 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001727 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001728 POSTING_READ(reg);
1729 udelay(150); /* wait for warmup */
1730}
1731
1732/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001733 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001734 * @dev_priv: i915 private structure
1735 * @pipe: pipe PLL to disable
1736 *
1737 * Disable the PLL for @pipe, making sure the pipe is off first.
1738 *
1739 * Note! This is for pre-ILK only.
1740 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001741static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001742{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001743 struct drm_device *dev = crtc->base.dev;
1744 struct drm_i915_private *dev_priv = dev->dev_private;
1745 enum pipe pipe = crtc->pipe;
1746
1747 /* Disable DVO 2x clock on both PLLs if necessary */
1748 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001749 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001750 intel_num_dvo_pipes(dev) == 1) {
1751 I915_WRITE(DPLL(PIPE_B),
1752 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1753 I915_WRITE(DPLL(PIPE_A),
1754 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1755 }
1756
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001757 /* Don't disable pipe or pipe PLLs if needed */
1758 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1759 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001760 return;
1761
1762 /* Make sure the pipe isn't still relying on us */
1763 assert_pipe_disabled(dev_priv, pipe);
1764
Daniel Vetter50b44a42013-06-05 13:34:33 +02001765 I915_WRITE(DPLL(pipe), 0);
1766 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001767}
1768
Jesse Barnesf6071162013-10-01 10:41:38 -07001769static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1770{
1771 u32 val = 0;
1772
1773 /* Make sure the pipe isn't still relying on us */
1774 assert_pipe_disabled(dev_priv, pipe);
1775
Imre Deake5cbfbf2014-01-09 17:08:16 +02001776 /*
1777 * Leave integrated clock source and reference clock enabled for pipe B.
1778 * The latter is needed for VGA hotplug / manual detection.
1779 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001780 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001781 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001782 I915_WRITE(DPLL(pipe), val);
1783 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001784
1785}
1786
1787static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1788{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001789 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001790 u32 val;
1791
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001792 /* Make sure the pipe isn't still relying on us */
1793 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001794
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001795 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001796 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001797 if (pipe != PIPE_A)
1798 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1799 I915_WRITE(DPLL(pipe), val);
1800 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001801
1802 mutex_lock(&dev_priv->dpio_lock);
1803
1804 /* Disable 10bit clock to display controller */
1805 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1806 val &= ~DPIO_DCLKP_EN;
1807 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1808
Ville Syrjälä61407f62014-05-27 16:32:55 +03001809 /* disable left/right clock distribution */
1810 if (pipe != PIPE_B) {
1811 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1812 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1813 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1814 } else {
1815 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1816 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1817 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1818 }
1819
Ville Syrjäläd7520482014-04-09 13:28:59 +03001820 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001821}
1822
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001823void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1824 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001825{
1826 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001827 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001828
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001829 switch (dport->port) {
1830 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001831 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001832 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001833 break;
1834 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001835 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001836 dpll_reg = DPLL(0);
1837 break;
1838 case PORT_D:
1839 port_mask = DPLL_PORTD_READY_MASK;
1840 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001841 break;
1842 default:
1843 BUG();
1844 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001845
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001846 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
Jesse Barnes89b667f2013-04-18 14:51:36 -07001847 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001848 port_name(dport->port), I915_READ(dpll_reg));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001849}
1850
Daniel Vetterb14b1052014-04-24 23:55:13 +02001851static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1852{
1853 struct drm_device *dev = crtc->base.dev;
1854 struct drm_i915_private *dev_priv = dev->dev_private;
1855 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1856
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001857 if (WARN_ON(pll == NULL))
1858 return;
1859
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001860 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001861 if (pll->active == 0) {
1862 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1863 WARN_ON(pll->on);
1864 assert_shared_dpll_disabled(dev_priv, pll);
1865
1866 pll->mode_set(dev_priv, pll);
1867 }
1868}
1869
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001870/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001871 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001872 * @dev_priv: i915 private structure
1873 * @pipe: pipe PLL to enable
1874 *
1875 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1876 * drives the transcoder clock.
1877 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001878static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001879{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001880 struct drm_device *dev = crtc->base.dev;
1881 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001882 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001883
Daniel Vetter87a875b2013-06-05 13:34:19 +02001884 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001885 return;
1886
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001887 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001888 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001889
Damien Lespiau74dd6922014-07-29 18:06:17 +01001890 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001891 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001892 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001893
Daniel Vettercdbd2312013-06-05 13:34:03 +02001894 if (pll->active++) {
1895 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001896 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001897 return;
1898 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001899 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001900
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001901 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1902
Daniel Vetter46edb022013-06-05 13:34:12 +02001903 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001904 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001905 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001906}
1907
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001908static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001909{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001910 struct drm_device *dev = crtc->base.dev;
1911 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001912 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001913
Jesse Barnes92f25842011-01-04 15:09:34 -08001914 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001915 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001916 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001917 return;
1918
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001919 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001920 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001921
Daniel Vetter46edb022013-06-05 13:34:12 +02001922 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1923 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001924 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001925
Chris Wilson48da64a2012-05-13 20:16:12 +01001926 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001927 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001928 return;
1929 }
1930
Daniel Vettere9d69442013-06-05 13:34:15 +02001931 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001932 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001933 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001934 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001935
Daniel Vetter46edb022013-06-05 13:34:12 +02001936 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001937 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001938 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001939
1940 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001941}
1942
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001943static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1944 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001945{
Daniel Vetter23670b322012-11-01 09:15:30 +01001946 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001947 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001948 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001949 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001950
1951 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001952 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001953
1954 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001955 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001956 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001957
1958 /* FDI must be feeding us bits for PCH ports */
1959 assert_fdi_tx_enabled(dev_priv, pipe);
1960 assert_fdi_rx_enabled(dev_priv, pipe);
1961
Daniel Vetter23670b322012-11-01 09:15:30 +01001962 if (HAS_PCH_CPT(dev)) {
1963 /* Workaround: Set the timing override bit before enabling the
1964 * pch transcoder. */
1965 reg = TRANS_CHICKEN2(pipe);
1966 val = I915_READ(reg);
1967 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1968 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001969 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001970
Daniel Vetterab9412b2013-05-03 11:49:46 +02001971 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001972 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001973 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001974
1975 if (HAS_PCH_IBX(dev_priv->dev)) {
1976 /*
1977 * make the BPC in transcoder be consistent with
1978 * that in pipeconf reg.
1979 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001980 val &= ~PIPECONF_BPC_MASK;
1981 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001982 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001983
1984 val &= ~TRANS_INTERLACE_MASK;
1985 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001986 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001987 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001988 val |= TRANS_LEGACY_INTERLACED_ILK;
1989 else
1990 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001991 else
1992 val |= TRANS_PROGRESSIVE;
1993
Jesse Barnes040484a2011-01-03 12:14:26 -08001994 I915_WRITE(reg, val | TRANS_ENABLE);
1995 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001996 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001997}
1998
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001999static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002000 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002001{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002002 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002003
2004 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002005 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002006
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002007 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002008 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002009 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002010
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002011 /* Workaround: set timing override bit. */
2012 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002013 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002014 I915_WRITE(_TRANSA_CHICKEN2, val);
2015
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002016 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002017 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002018
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002019 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2020 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002021 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002022 else
2023 val |= TRANS_PROGRESSIVE;
2024
Daniel Vetterab9412b2013-05-03 11:49:46 +02002025 I915_WRITE(LPT_TRANSCONF, val);
2026 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002027 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002028}
2029
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002030static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2031 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002032{
Daniel Vetter23670b322012-11-01 09:15:30 +01002033 struct drm_device *dev = dev_priv->dev;
2034 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002035
2036 /* FDI relies on the transcoder */
2037 assert_fdi_tx_disabled(dev_priv, pipe);
2038 assert_fdi_rx_disabled(dev_priv, pipe);
2039
Jesse Barnes291906f2011-02-02 12:28:03 -08002040 /* Ports must be off as well */
2041 assert_pch_ports_disabled(dev_priv, pipe);
2042
Daniel Vetterab9412b2013-05-03 11:49:46 +02002043 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002044 val = I915_READ(reg);
2045 val &= ~TRANS_ENABLE;
2046 I915_WRITE(reg, val);
2047 /* wait for PCH transcoder off, transcoder state */
2048 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002049 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002050
2051 if (!HAS_PCH_IBX(dev)) {
2052 /* Workaround: Clear the timing override chicken bit again. */
2053 reg = TRANS_CHICKEN2(pipe);
2054 val = I915_READ(reg);
2055 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2056 I915_WRITE(reg, val);
2057 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002058}
2059
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002060static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002061{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002062 u32 val;
2063
Daniel Vetterab9412b2013-05-03 11:49:46 +02002064 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002065 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002066 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002067 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002068 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002069 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002070
2071 /* Workaround: clear timing override bit. */
2072 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002073 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002074 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002075}
2076
2077/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002078 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002079 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002080 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002081 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002082 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002083 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002084static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002085{
Paulo Zanoni03722642014-01-17 13:51:09 -02002086 struct drm_device *dev = crtc->base.dev;
2087 struct drm_i915_private *dev_priv = dev->dev_private;
2088 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002089 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2090 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002091 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002092 int reg;
2093 u32 val;
2094
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002095 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002096 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002097 assert_sprites_disabled(dev_priv, pipe);
2098
Paulo Zanoni681e5812012-12-06 11:12:38 -02002099 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002100 pch_transcoder = TRANSCODER_A;
2101 else
2102 pch_transcoder = pipe;
2103
Jesse Barnesb24e7172011-01-04 15:09:30 -08002104 /*
2105 * A pipe without a PLL won't actually be able to drive bits from
2106 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2107 * need the check.
2108 */
2109 if (!HAS_PCH_SPLIT(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002110 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002111 assert_dsi_pll_enabled(dev_priv);
2112 else
2113 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002114 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002115 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002116 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002117 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002118 assert_fdi_tx_pll_enabled(dev_priv,
2119 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002120 }
2121 /* FIXME: assert CPU port conditions for SNB+ */
2122 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002123
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002124 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002125 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002126 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002127 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2128 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002129 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002130 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002131
2132 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002133 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002134}
2135
2136/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002137 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002138 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002139 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002140 * Disable the pipe of @crtc, making sure that various hardware
2141 * specific requirements are met, if applicable, e.g. plane
2142 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002143 *
2144 * Will wait until the pipe has shut down before returning.
2145 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002146static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002147{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002148 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002149 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002150 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002151 int reg;
2152 u32 val;
2153
2154 /*
2155 * Make sure planes won't keep trying to pump pixels to us,
2156 * or we might hang the display.
2157 */
2158 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002159 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002160 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002161
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002162 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002163 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002164 if ((val & PIPECONF_ENABLE) == 0)
2165 return;
2166
Ville Syrjälä67adc642014-08-15 01:21:57 +03002167 /*
2168 * Double wide has implications for planes
2169 * so best keep it disabled when not needed.
2170 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002171 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002172 val &= ~PIPECONF_DOUBLE_WIDE;
2173
2174 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002175 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2176 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002177 val &= ~PIPECONF_ENABLE;
2178
2179 I915_WRITE(reg, val);
2180 if ((val & PIPECONF_ENABLE) == 0)
2181 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002182}
2183
Keith Packardd74362c2011-07-28 14:47:14 -07002184/*
2185 * Plane regs are double buffered, going from enabled->disabled needs a
2186 * trigger in order to latch. The display address reg provides this.
2187 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002188void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2189 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002190{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002191 struct drm_device *dev = dev_priv->dev;
2192 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002193
2194 I915_WRITE(reg, I915_READ(reg));
2195 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002196}
2197
Jesse Barnesb24e7172011-01-04 15:09:30 -08002198/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002199 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002200 * @plane: plane to be enabled
2201 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002202 *
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002203 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002204 */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002205static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2206 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002207{
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002208 struct drm_device *dev = plane->dev;
2209 struct drm_i915_private *dev_priv = dev->dev_private;
2210 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002211
2212 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002213 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002214
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002215 if (intel_crtc->primary_enabled)
2216 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002217
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002218 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002219
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002220 dev_priv->display.update_primary_plane(crtc, plane->fb,
2221 crtc->x, crtc->y);
Ville Syrjälä33c3b0d2014-06-24 13:59:28 +03002222
2223 /*
2224 * BDW signals flip done immediately if the plane
2225 * is disabled, even if the plane enable is already
2226 * armed to occur at the next vblank :(
2227 */
2228 if (IS_BROADWELL(dev))
2229 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002230}
2231
Jesse Barnesb24e7172011-01-04 15:09:30 -08002232/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002233 * intel_disable_primary_hw_plane - disable the primary hardware plane
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002234 * @plane: plane to be disabled
2235 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002236 *
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002237 * Disable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002238 */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002239static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2240 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002241{
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002242 struct drm_device *dev = plane->dev;
2243 struct drm_i915_private *dev_priv = dev->dev_private;
2244 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2245
Matt Roper32b7eee2014-12-24 07:59:06 -08002246 if (WARN_ON(!intel_crtc->active))
2247 return;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002248
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002249 if (!intel_crtc->primary_enabled)
2250 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002251
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002252 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002253
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002254 dev_priv->display.update_primary_plane(crtc, plane->fb,
2255 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002256}
2257
Chris Wilson693db182013-03-05 14:52:39 +00002258static bool need_vtd_wa(struct drm_device *dev)
2259{
2260#ifdef CONFIG_INTEL_IOMMU
2261 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2262 return true;
2263#endif
2264 return false;
2265}
2266
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002267unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002268intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2269 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002270{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002271 unsigned int tile_height;
2272 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002273
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002274 switch (fb_format_modifier) {
2275 case DRM_FORMAT_MOD_NONE:
2276 tile_height = 1;
2277 break;
2278 case I915_FORMAT_MOD_X_TILED:
2279 tile_height = IS_GEN2(dev) ? 16 : 8;
2280 break;
2281 case I915_FORMAT_MOD_Y_TILED:
2282 tile_height = 32;
2283 break;
2284 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002285 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2286 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002287 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002288 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002289 tile_height = 64;
2290 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002291 case 2:
2292 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002293 tile_height = 32;
2294 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002295 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002296 tile_height = 16;
2297 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002298 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002299 WARN_ONCE(1,
2300 "128-bit pixels are not supported for display!");
2301 tile_height = 16;
2302 break;
2303 }
2304 break;
2305 default:
2306 MISSING_CASE(fb_format_modifier);
2307 tile_height = 1;
2308 break;
2309 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002310
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002311 return tile_height;
2312}
2313
2314unsigned int
2315intel_fb_align_height(struct drm_device *dev, unsigned int height,
2316 uint32_t pixel_format, uint64_t fb_format_modifier)
2317{
2318 return ALIGN(height, intel_tile_height(dev, pixel_format,
2319 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002320}
2321
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002322static int
2323intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2324 const struct drm_plane_state *plane_state)
2325{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002326 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002327
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002328 *view = i915_ggtt_view_normal;
2329
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002330 if (!plane_state)
2331 return 0;
2332
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002333 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002334 return 0;
2335
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002336 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002337
2338 info->height = fb->height;
2339 info->pixel_format = fb->pixel_format;
2340 info->pitch = fb->pitches[0];
2341 info->fb_modifier = fb->modifier[0];
2342
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002343 return 0;
2344}
2345
Chris Wilson127bd2a2010-07-23 23:32:05 +01002346int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002347intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2348 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002349 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002350 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002351{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002352 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002353 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002354 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002355 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002356 u32 alignment;
2357 int ret;
2358
Matt Roperebcdd392014-07-09 16:22:11 -07002359 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2360
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002361 switch (fb->modifier[0]) {
2362 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002363 if (INTEL_INFO(dev)->gen >= 9)
2364 alignment = 256 * 1024;
2365 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002366 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002367 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002368 alignment = 4 * 1024;
2369 else
2370 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002371 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002372 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002373 if (INTEL_INFO(dev)->gen >= 9)
2374 alignment = 256 * 1024;
2375 else {
2376 /* pin() will align the object as required by fence */
2377 alignment = 0;
2378 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002379 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002380 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002381 case I915_FORMAT_MOD_Yf_TILED:
2382 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2383 "Y tiling bo slipped through, driver bug!\n"))
2384 return -EINVAL;
2385 alignment = 1 * 1024 * 1024;
2386 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002387 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002388 MISSING_CASE(fb->modifier[0]);
2389 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002390 }
2391
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002392 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2393 if (ret)
2394 return ret;
2395
Chris Wilson693db182013-03-05 14:52:39 +00002396 /* Note that the w/a also requires 64 PTE of padding following the
2397 * bo. We currently fill all unused PTE with the shadow page and so
2398 * we should always have valid PTE following the scanout preventing
2399 * the VT-d warning.
2400 */
2401 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2402 alignment = 256 * 1024;
2403
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002404 /*
2405 * Global gtt pte registers are special registers which actually forward
2406 * writes to a chunk of system memory. Which means that there is no risk
2407 * that the register values disappear as soon as we call
2408 * intel_runtime_pm_put(), so it is correct to wrap only the
2409 * pin/unpin/fence and not more.
2410 */
2411 intel_runtime_pm_get(dev_priv);
2412
Chris Wilsonce453d82011-02-21 14:43:56 +00002413 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002414 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002415 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002416 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002417 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002418
2419 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2420 * fence, whereas 965+ only requires a fence if using
2421 * framebuffer compression. For simplicity, we always install
2422 * a fence as the cost is not that onerous.
2423 */
Chris Wilson06d98132012-04-17 15:31:24 +01002424 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002425 if (ret)
2426 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002427
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002428 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002429
Chris Wilsonce453d82011-02-21 14:43:56 +00002430 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002431 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002432 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002433
2434err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002435 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002436err_interruptible:
2437 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002438 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002439 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002440}
2441
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002442static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2443 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002444{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002445 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002446 struct i915_ggtt_view view;
2447 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002448
Matt Roperebcdd392014-07-09 16:22:11 -07002449 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2450
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002451 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2452 WARN_ONCE(ret, "Couldn't get view from plane state!");
2453
Chris Wilson1690e1e2011-12-14 13:57:08 +01002454 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002455 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002456}
2457
Daniel Vetterc2c75132012-07-05 12:17:30 +02002458/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2459 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002460unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2461 unsigned int tiling_mode,
2462 unsigned int cpp,
2463 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002464{
Chris Wilsonbc752862013-02-21 20:04:31 +00002465 if (tiling_mode != I915_TILING_NONE) {
2466 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002467
Chris Wilsonbc752862013-02-21 20:04:31 +00002468 tile_rows = *y / 8;
2469 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002470
Chris Wilsonbc752862013-02-21 20:04:31 +00002471 tiles = *x / (512/cpp);
2472 *x %= 512/cpp;
2473
2474 return tile_rows * pitch * 8 + tiles * 4096;
2475 } else {
2476 unsigned int offset;
2477
2478 offset = *y * pitch + *x * cpp;
2479 *y = 0;
2480 *x = (offset & 4095) / cpp;
2481 return offset & -4096;
2482 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002483}
2484
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002485static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002486{
2487 switch (format) {
2488 case DISPPLANE_8BPP:
2489 return DRM_FORMAT_C8;
2490 case DISPPLANE_BGRX555:
2491 return DRM_FORMAT_XRGB1555;
2492 case DISPPLANE_BGRX565:
2493 return DRM_FORMAT_RGB565;
2494 default:
2495 case DISPPLANE_BGRX888:
2496 return DRM_FORMAT_XRGB8888;
2497 case DISPPLANE_RGBX888:
2498 return DRM_FORMAT_XBGR8888;
2499 case DISPPLANE_BGRX101010:
2500 return DRM_FORMAT_XRGB2101010;
2501 case DISPPLANE_RGBX101010:
2502 return DRM_FORMAT_XBGR2101010;
2503 }
2504}
2505
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002506static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2507{
2508 switch (format) {
2509 case PLANE_CTL_FORMAT_RGB_565:
2510 return DRM_FORMAT_RGB565;
2511 default:
2512 case PLANE_CTL_FORMAT_XRGB_8888:
2513 if (rgb_order) {
2514 if (alpha)
2515 return DRM_FORMAT_ABGR8888;
2516 else
2517 return DRM_FORMAT_XBGR8888;
2518 } else {
2519 if (alpha)
2520 return DRM_FORMAT_ARGB8888;
2521 else
2522 return DRM_FORMAT_XRGB8888;
2523 }
2524 case PLANE_CTL_FORMAT_XRGB_2101010:
2525 if (rgb_order)
2526 return DRM_FORMAT_XBGR2101010;
2527 else
2528 return DRM_FORMAT_XRGB2101010;
2529 }
2530}
2531
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002532static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002533intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2534 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002535{
2536 struct drm_device *dev = crtc->base.dev;
2537 struct drm_i915_gem_object *obj = NULL;
2538 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002539 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002540 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2541 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2542 PAGE_SIZE);
2543
2544 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002545
Chris Wilsonff2652e2014-03-10 08:07:02 +00002546 if (plane_config->size == 0)
2547 return false;
2548
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002549 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2550 base_aligned,
2551 base_aligned,
2552 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002553 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002554 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002555
Damien Lespiau49af4492015-01-20 12:51:44 +00002556 obj->tiling_mode = plane_config->tiling;
2557 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002558 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002559
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002560 mode_cmd.pixel_format = fb->pixel_format;
2561 mode_cmd.width = fb->width;
2562 mode_cmd.height = fb->height;
2563 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002564 mode_cmd.modifier[0] = fb->modifier[0];
2565 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002566
2567 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002568 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002569 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002570 DRM_DEBUG_KMS("intel fb init failed\n");
2571 goto out_unref_obj;
2572 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002573 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002574
Daniel Vetterf6936e22015-03-26 12:17:05 +01002575 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002576 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002577
2578out_unref_obj:
2579 drm_gem_object_unreference(&obj->base);
2580 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002581 return false;
2582}
2583
Matt Roperafd65eb2015-02-03 13:10:04 -08002584/* Update plane->state->fb to match plane->fb after driver-internal updates */
2585static void
2586update_state_fb(struct drm_plane *plane)
2587{
2588 if (plane->fb == plane->state->fb)
2589 return;
2590
2591 if (plane->state->fb)
2592 drm_framebuffer_unreference(plane->state->fb);
2593 plane->state->fb = plane->fb;
2594 if (plane->state->fb)
2595 drm_framebuffer_reference(plane->state->fb);
2596}
2597
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002598static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002599intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2600 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002601{
2602 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002603 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002604 struct drm_crtc *c;
2605 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002606 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002607 struct drm_plane *primary = intel_crtc->base.primary;
2608 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002609
Damien Lespiau2d140302015-02-05 17:22:18 +00002610 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002611 return;
2612
Daniel Vetterf6936e22015-03-26 12:17:05 +01002613 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002614 fb = &plane_config->fb->base;
2615 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002616 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002617
Damien Lespiau2d140302015-02-05 17:22:18 +00002618 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002619
2620 /*
2621 * Failed to alloc the obj, check to see if we should share
2622 * an fb with another CRTC instead
2623 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002624 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002625 i = to_intel_crtc(c);
2626
2627 if (c == &intel_crtc->base)
2628 continue;
2629
Matt Roper2ff8fde2014-07-08 07:50:07 -07002630 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002631 continue;
2632
Daniel Vetter88595ac2015-03-26 12:42:24 +01002633 fb = c->primary->fb;
2634 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002635 continue;
2636
Daniel Vetter88595ac2015-03-26 12:42:24 +01002637 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002638 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002639 drm_framebuffer_reference(fb);
2640 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002641 }
2642 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002643
2644 return;
2645
2646valid_fb:
2647 obj = intel_fb_obj(fb);
2648 if (obj->tiling_mode != I915_TILING_NONE)
2649 dev_priv->preserve_bios_swizzle = true;
2650
2651 primary->fb = fb;
2652 primary->state->crtc = &intel_crtc->base;
2653 primary->crtc = &intel_crtc->base;
2654 update_state_fb(primary);
2655 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002656}
2657
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002658static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2659 struct drm_framebuffer *fb,
2660 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002661{
2662 struct drm_device *dev = crtc->dev;
2663 struct drm_i915_private *dev_priv = dev->dev_private;
2664 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002665 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002666 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002667 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002668 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002669 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302670 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002671
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002672 if (!intel_crtc->primary_enabled) {
2673 I915_WRITE(reg, 0);
2674 if (INTEL_INFO(dev)->gen >= 4)
2675 I915_WRITE(DSPSURF(plane), 0);
2676 else
2677 I915_WRITE(DSPADDR(plane), 0);
2678 POSTING_READ(reg);
2679 return;
2680 }
2681
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002682 obj = intel_fb_obj(fb);
2683 if (WARN_ON(obj == NULL))
2684 return;
2685
2686 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2687
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002688 dspcntr = DISPPLANE_GAMMA_ENABLE;
2689
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002690 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002691
2692 if (INTEL_INFO(dev)->gen < 4) {
2693 if (intel_crtc->pipe == PIPE_B)
2694 dspcntr |= DISPPLANE_SEL_PIPE_B;
2695
2696 /* pipesrc and dspsize control the size that is scaled from,
2697 * which should always be the user's requested size.
2698 */
2699 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002700 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2701 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002702 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002703 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2704 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002705 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2706 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002707 I915_WRITE(PRIMPOS(plane), 0);
2708 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002709 }
2710
Ville Syrjälä57779d02012-10-31 17:50:14 +02002711 switch (fb->pixel_format) {
2712 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002713 dspcntr |= DISPPLANE_8BPP;
2714 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002715 case DRM_FORMAT_XRGB1555:
2716 case DRM_FORMAT_ARGB1555:
2717 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002718 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002719 case DRM_FORMAT_RGB565:
2720 dspcntr |= DISPPLANE_BGRX565;
2721 break;
2722 case DRM_FORMAT_XRGB8888:
2723 case DRM_FORMAT_ARGB8888:
2724 dspcntr |= DISPPLANE_BGRX888;
2725 break;
2726 case DRM_FORMAT_XBGR8888:
2727 case DRM_FORMAT_ABGR8888:
2728 dspcntr |= DISPPLANE_RGBX888;
2729 break;
2730 case DRM_FORMAT_XRGB2101010:
2731 case DRM_FORMAT_ARGB2101010:
2732 dspcntr |= DISPPLANE_BGRX101010;
2733 break;
2734 case DRM_FORMAT_XBGR2101010:
2735 case DRM_FORMAT_ABGR2101010:
2736 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002737 break;
2738 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002739 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002740 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002741
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002742 if (INTEL_INFO(dev)->gen >= 4 &&
2743 obj->tiling_mode != I915_TILING_NONE)
2744 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002745
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002746 if (IS_G4X(dev))
2747 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2748
Ville Syrjäläb98971272014-08-27 16:51:22 +03002749 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002750
Daniel Vetterc2c75132012-07-05 12:17:30 +02002751 if (INTEL_INFO(dev)->gen >= 4) {
2752 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002753 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002754 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002755 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002756 linear_offset -= intel_crtc->dspaddr_offset;
2757 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002758 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002759 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002760
Matt Roper8e7d6882015-01-21 16:35:41 -08002761 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302762 dspcntr |= DISPPLANE_ROTATE_180;
2763
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002764 x += (intel_crtc->config->pipe_src_w - 1);
2765 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302766
2767 /* Finding the last pixel of the last line of the display
2768 data and adding to linear_offset*/
2769 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002770 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2771 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302772 }
2773
2774 I915_WRITE(reg, dspcntr);
2775
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002776 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002777 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002778 I915_WRITE(DSPSURF(plane),
2779 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002780 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002781 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002782 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002783 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002784 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002785}
2786
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002787static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2788 struct drm_framebuffer *fb,
2789 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002790{
2791 struct drm_device *dev = crtc->dev;
2792 struct drm_i915_private *dev_priv = dev->dev_private;
2793 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002794 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002795 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002796 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002797 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002798 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302799 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002800
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002801 if (!intel_crtc->primary_enabled) {
2802 I915_WRITE(reg, 0);
2803 I915_WRITE(DSPSURF(plane), 0);
2804 POSTING_READ(reg);
2805 return;
2806 }
2807
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002808 obj = intel_fb_obj(fb);
2809 if (WARN_ON(obj == NULL))
2810 return;
2811
2812 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2813
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002814 dspcntr = DISPPLANE_GAMMA_ENABLE;
2815
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002816 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002817
2818 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2819 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2820
Ville Syrjälä57779d02012-10-31 17:50:14 +02002821 switch (fb->pixel_format) {
2822 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002823 dspcntr |= DISPPLANE_8BPP;
2824 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002825 case DRM_FORMAT_RGB565:
2826 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002827 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002828 case DRM_FORMAT_XRGB8888:
2829 case DRM_FORMAT_ARGB8888:
2830 dspcntr |= DISPPLANE_BGRX888;
2831 break;
2832 case DRM_FORMAT_XBGR8888:
2833 case DRM_FORMAT_ABGR8888:
2834 dspcntr |= DISPPLANE_RGBX888;
2835 break;
2836 case DRM_FORMAT_XRGB2101010:
2837 case DRM_FORMAT_ARGB2101010:
2838 dspcntr |= DISPPLANE_BGRX101010;
2839 break;
2840 case DRM_FORMAT_XBGR2101010:
2841 case DRM_FORMAT_ABGR2101010:
2842 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002843 break;
2844 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002845 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002846 }
2847
2848 if (obj->tiling_mode != I915_TILING_NONE)
2849 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002850
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002851 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002852 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002853
Ville Syrjäläb98971272014-08-27 16:51:22 +03002854 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002855 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002856 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002857 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002858 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002859 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002860 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302861 dspcntr |= DISPPLANE_ROTATE_180;
2862
2863 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002864 x += (intel_crtc->config->pipe_src_w - 1);
2865 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302866
2867 /* Finding the last pixel of the last line of the display
2868 data and adding to linear_offset*/
2869 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002870 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2871 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302872 }
2873 }
2874
2875 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002876
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002877 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002878 I915_WRITE(DSPSURF(plane),
2879 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002880 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002881 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2882 } else {
2883 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2884 I915_WRITE(DSPLINOFF(plane), linear_offset);
2885 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002886 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002887}
2888
Damien Lespiaub3218032015-02-27 11:15:18 +00002889u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2890 uint32_t pixel_format)
2891{
2892 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2893
2894 /*
2895 * The stride is either expressed as a multiple of 64 bytes
2896 * chunks for linear buffers or in number of tiles for tiled
2897 * buffers.
2898 */
2899 switch (fb_modifier) {
2900 case DRM_FORMAT_MOD_NONE:
2901 return 64;
2902 case I915_FORMAT_MOD_X_TILED:
2903 if (INTEL_INFO(dev)->gen == 2)
2904 return 128;
2905 return 512;
2906 case I915_FORMAT_MOD_Y_TILED:
2907 /* No need to check for old gens and Y tiling since this is
2908 * about the display engine and those will be blocked before
2909 * we get here.
2910 */
2911 return 128;
2912 case I915_FORMAT_MOD_Yf_TILED:
2913 if (bits_per_pixel == 8)
2914 return 64;
2915 else
2916 return 128;
2917 default:
2918 MISSING_CASE(fb_modifier);
2919 return 64;
2920 }
2921}
2922
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002923unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2924 struct drm_i915_gem_object *obj)
2925{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002926 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002927
2928 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002929 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002930
2931 return i915_gem_obj_ggtt_offset_view(obj, view);
2932}
2933
Chandra Kondurua1b22782015-04-07 15:28:45 -07002934/*
2935 * This function detaches (aka. unbinds) unused scalers in hardware
2936 */
2937void skl_detach_scalers(struct intel_crtc *intel_crtc)
2938{
2939 struct drm_device *dev;
2940 struct drm_i915_private *dev_priv;
2941 struct intel_crtc_scaler_state *scaler_state;
2942 int i;
2943
2944 if (!intel_crtc || !intel_crtc->config)
2945 return;
2946
2947 dev = intel_crtc->base.dev;
2948 dev_priv = dev->dev_private;
2949 scaler_state = &intel_crtc->config->scaler_state;
2950
2951 /* loop through and disable scalers that aren't in use */
2952 for (i = 0; i < intel_crtc->num_scalers; i++) {
2953 if (!scaler_state->scalers[i].in_use) {
2954 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2955 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2956 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2957 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2958 intel_crtc->base.base.id, intel_crtc->pipe, i);
2959 }
2960 }
2961}
2962
Damien Lespiau70d21f02013-07-03 21:06:04 +01002963static void skylake_update_primary_plane(struct drm_crtc *crtc,
2964 struct drm_framebuffer *fb,
2965 int x, int y)
2966{
2967 struct drm_device *dev = crtc->dev;
2968 struct drm_i915_private *dev_priv = dev->dev_private;
2969 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002970 struct drm_i915_gem_object *obj;
2971 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302972 u32 plane_ctl, stride_div, stride;
2973 u32 tile_height, plane_offset, plane_size;
2974 unsigned int rotation;
2975 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002976 unsigned long surf_addr;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302977 struct drm_plane *plane;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002978
2979 if (!intel_crtc->primary_enabled) {
2980 I915_WRITE(PLANE_CTL(pipe, 0), 0);
2981 I915_WRITE(PLANE_SURF(pipe, 0), 0);
2982 POSTING_READ(PLANE_CTL(pipe, 0));
2983 return;
2984 }
2985
2986 plane_ctl = PLANE_CTL_ENABLE |
2987 PLANE_CTL_PIPE_GAMMA_ENABLE |
2988 PLANE_CTL_PIPE_CSC_ENABLE;
2989
2990 switch (fb->pixel_format) {
2991 case DRM_FORMAT_RGB565:
2992 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
2993 break;
2994 case DRM_FORMAT_XRGB8888:
2995 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2996 break;
Jani Nikulaf75fb422015-02-10 13:15:49 +02002997 case DRM_FORMAT_ARGB8888:
2998 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2999 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
3000 break;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003001 case DRM_FORMAT_XBGR8888:
3002 plane_ctl |= PLANE_CTL_ORDER_RGBX;
3003 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
3004 break;
Jani Nikulaf75fb422015-02-10 13:15:49 +02003005 case DRM_FORMAT_ABGR8888:
3006 plane_ctl |= PLANE_CTL_ORDER_RGBX;
3007 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
3008 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
3009 break;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003010 case DRM_FORMAT_XRGB2101010:
3011 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
3012 break;
3013 case DRM_FORMAT_XBGR2101010:
3014 plane_ctl |= PLANE_CTL_ORDER_RGBX;
3015 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
3016 break;
3017 default:
3018 BUG();
3019 }
3020
Daniel Vetter30af77c2015-02-10 17:16:11 +00003021 switch (fb->modifier[0]) {
3022 case DRM_FORMAT_MOD_NONE:
Damien Lespiau70d21f02013-07-03 21:06:04 +01003023 break;
Daniel Vetter30af77c2015-02-10 17:16:11 +00003024 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau70d21f02013-07-03 21:06:04 +01003025 plane_ctl |= PLANE_CTL_TILED_X;
Damien Lespiaub3218032015-02-27 11:15:18 +00003026 break;
3027 case I915_FORMAT_MOD_Y_TILED:
3028 plane_ctl |= PLANE_CTL_TILED_Y;
3029 break;
3030 case I915_FORMAT_MOD_Yf_TILED:
3031 plane_ctl |= PLANE_CTL_TILED_YF;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003032 break;
3033 default:
Damien Lespiaub3218032015-02-27 11:15:18 +00003034 MISSING_CASE(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003035 }
3036
3037 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303038
3039 plane = crtc->primary;
3040 rotation = plane->state->rotation;
3041 switch (rotation) {
3042 case BIT(DRM_ROTATE_90):
3043 plane_ctl |= PLANE_CTL_ROTATE_90;
3044 break;
3045
3046 case BIT(DRM_ROTATE_180):
Sonika Jindal1447dde2014-10-04 10:53:31 +01003047 plane_ctl |= PLANE_CTL_ROTATE_180;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303048 break;
3049
3050 case BIT(DRM_ROTATE_270):
3051 plane_ctl |= PLANE_CTL_ROTATE_270;
3052 break;
3053 }
Damien Lespiau70d21f02013-07-03 21:06:04 +01003054
Damien Lespiaub3218032015-02-27 11:15:18 +00003055 obj = intel_fb_obj(fb);
3056 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3057 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303058 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3059
3060 if (intel_rotation_90_or_270(rotation)) {
3061 /* stride = Surface height in tiles */
3062 tile_height = intel_tile_height(dev, fb->bits_per_pixel,
3063 fb->modifier[0]);
3064 stride = DIV_ROUND_UP(fb->height, tile_height);
3065 x_offset = stride * tile_height - y - (plane->state->src_h >> 16);
3066 y_offset = x;
3067 plane_size = ((plane->state->src_w >> 16) - 1) << 16 |
3068 ((plane->state->src_h >> 16) - 1);
3069 } else {
3070 stride = fb->pitches[0] / stride_div;
3071 x_offset = x;
3072 y_offset = y;
3073 plane_size = ((plane->state->src_h >> 16) - 1) << 16 |
3074 ((plane->state->src_w >> 16) - 1);
3075 }
3076 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003077
Damien Lespiau70d21f02013-07-03 21:06:04 +01003078 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003079 I915_WRITE(PLANE_POS(pipe, 0), 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303080 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3081 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3082 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003083 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003084
3085 POSTING_READ(PLANE_SURF(pipe, 0));
3086}
3087
Jesse Barnes17638cd2011-06-24 12:19:23 -07003088/* Assume fb object is pinned & idle & fenced and just update base pointers */
3089static int
3090intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3091 int x, int y, enum mode_set_atomic state)
3092{
3093 struct drm_device *dev = crtc->dev;
3094 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003095
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003096 if (dev_priv->display.disable_fbc)
3097 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003098
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003099 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3100
3101 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003102}
3103
Ville Syrjälä75147472014-11-24 18:28:11 +02003104static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003105{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003106 struct drm_crtc *crtc;
3107
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003108 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003109 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3110 enum plane plane = intel_crtc->plane;
3111
3112 intel_prepare_page_flip(dev, plane);
3113 intel_finish_page_flip_plane(dev, plane);
3114 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003115}
3116
3117static void intel_update_primary_planes(struct drm_device *dev)
3118{
3119 struct drm_i915_private *dev_priv = dev->dev_private;
3120 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003121
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003122 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003123 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3124
Rob Clark51fd3712013-11-19 12:10:12 -05003125 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003126 /*
3127 * FIXME: Once we have proper support for primary planes (and
3128 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003129 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003130 */
Matt Roperf4510a22014-04-01 15:22:40 -07003131 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003132 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003133 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003134 crtc->x,
3135 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003136 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003137 }
3138}
3139
Ville Syrjälä75147472014-11-24 18:28:11 +02003140void intel_prepare_reset(struct drm_device *dev)
3141{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003142 struct drm_i915_private *dev_priv = to_i915(dev);
3143 struct intel_crtc *crtc;
3144
Ville Syrjälä75147472014-11-24 18:28:11 +02003145 /* no reset support for gen2 */
3146 if (IS_GEN2(dev))
3147 return;
3148
3149 /* reset doesn't touch the display */
3150 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3151 return;
3152
3153 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003154
3155 /*
3156 * Disabling the crtcs gracefully seems nicer. Also the
3157 * g33 docs say we should at least disable all the planes.
3158 */
3159 for_each_intel_crtc(dev, crtc) {
3160 if (crtc->active)
3161 dev_priv->display.crtc_disable(&crtc->base);
3162 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003163}
3164
3165void intel_finish_reset(struct drm_device *dev)
3166{
3167 struct drm_i915_private *dev_priv = to_i915(dev);
3168
3169 /*
3170 * Flips in the rings will be nuked by the reset,
3171 * so complete all pending flips so that user space
3172 * will get its events and not get stuck.
3173 */
3174 intel_complete_page_flips(dev);
3175
3176 /* no reset support for gen2 */
3177 if (IS_GEN2(dev))
3178 return;
3179
3180 /* reset doesn't touch the display */
3181 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3182 /*
3183 * Flips in the rings have been nuked by the reset,
3184 * so update the base address of all primary
3185 * planes to the the last fb to make sure we're
3186 * showing the correct fb after a reset.
3187 */
3188 intel_update_primary_planes(dev);
3189 return;
3190 }
3191
3192 /*
3193 * The display has been reset as well,
3194 * so need a full re-initialization.
3195 */
3196 intel_runtime_pm_disable_interrupts(dev_priv);
3197 intel_runtime_pm_enable_interrupts(dev_priv);
3198
3199 intel_modeset_init_hw(dev);
3200
3201 spin_lock_irq(&dev_priv->irq_lock);
3202 if (dev_priv->display.hpd_irq_setup)
3203 dev_priv->display.hpd_irq_setup(dev);
3204 spin_unlock_irq(&dev_priv->irq_lock);
3205
3206 intel_modeset_setup_hw_state(dev, true);
3207
3208 intel_hpd_init(dev_priv);
3209
3210 drm_modeset_unlock_all(dev);
3211}
3212
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003213static int
Chris Wilson14667a42012-04-03 17:58:35 +01003214intel_finish_fb(struct drm_framebuffer *old_fb)
3215{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003216 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01003217 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3218 bool was_interruptible = dev_priv->mm.interruptible;
3219 int ret;
3220
Chris Wilson14667a42012-04-03 17:58:35 +01003221 /* Big Hammer, we also need to ensure that any pending
3222 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3223 * current scanout is retired before unpinning the old
3224 * framebuffer.
3225 *
3226 * This should only fail upon a hung GPU, in which case we
3227 * can safely continue.
3228 */
3229 dev_priv->mm.interruptible = false;
3230 ret = i915_gem_object_finish_gpu(obj);
3231 dev_priv->mm.interruptible = was_interruptible;
3232
3233 return ret;
3234}
3235
Chris Wilson7d5e3792014-03-04 13:15:08 +00003236static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3237{
3238 struct drm_device *dev = crtc->dev;
3239 struct drm_i915_private *dev_priv = dev->dev_private;
3240 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003241 bool pending;
3242
3243 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3244 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3245 return false;
3246
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003247 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003248 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003249 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003250
3251 return pending;
3252}
3253
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003254static void intel_update_pipe_size(struct intel_crtc *crtc)
3255{
3256 struct drm_device *dev = crtc->base.dev;
3257 struct drm_i915_private *dev_priv = dev->dev_private;
3258 const struct drm_display_mode *adjusted_mode;
3259
3260 if (!i915.fastboot)
3261 return;
3262
3263 /*
3264 * Update pipe size and adjust fitter if needed: the reason for this is
3265 * that in compute_mode_changes we check the native mode (not the pfit
3266 * mode) to see if we can flip rather than do a full mode set. In the
3267 * fastboot case, we'll flip, but if we don't update the pipesrc and
3268 * pfit state, we'll end up with a big fb scanned out into the wrong
3269 * sized surface.
3270 *
3271 * To fix this properly, we need to hoist the checks up into
3272 * compute_mode_changes (or above), check the actual pfit state and
3273 * whether the platform allows pfit disable with pipe active, and only
3274 * then update the pipesrc and pfit state, even on the flip path.
3275 */
3276
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003277 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003278
3279 I915_WRITE(PIPESRC(crtc->pipe),
3280 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3281 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003282 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003283 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3284 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003285 I915_WRITE(PF_CTL(crtc->pipe), 0);
3286 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3287 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3288 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003289 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3290 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003291}
3292
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003293static void intel_fdi_normal_train(struct drm_crtc *crtc)
3294{
3295 struct drm_device *dev = crtc->dev;
3296 struct drm_i915_private *dev_priv = dev->dev_private;
3297 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3298 int pipe = intel_crtc->pipe;
3299 u32 reg, temp;
3300
3301 /* enable normal train */
3302 reg = FDI_TX_CTL(pipe);
3303 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003304 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003305 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3306 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003307 } else {
3308 temp &= ~FDI_LINK_TRAIN_NONE;
3309 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003310 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003311 I915_WRITE(reg, temp);
3312
3313 reg = FDI_RX_CTL(pipe);
3314 temp = I915_READ(reg);
3315 if (HAS_PCH_CPT(dev)) {
3316 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3317 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3318 } else {
3319 temp &= ~FDI_LINK_TRAIN_NONE;
3320 temp |= FDI_LINK_TRAIN_NONE;
3321 }
3322 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3323
3324 /* wait one idle pattern time */
3325 POSTING_READ(reg);
3326 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003327
3328 /* IVB wants error correction enabled */
3329 if (IS_IVYBRIDGE(dev))
3330 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3331 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003332}
3333
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003334/* The FDI link training functions for ILK/Ibexpeak. */
3335static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3336{
3337 struct drm_device *dev = crtc->dev;
3338 struct drm_i915_private *dev_priv = dev->dev_private;
3339 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3340 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003341 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003342
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003343 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003344 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003345
Adam Jacksone1a44742010-06-25 15:32:14 -04003346 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3347 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003348 reg = FDI_RX_IMR(pipe);
3349 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003350 temp &= ~FDI_RX_SYMBOL_LOCK;
3351 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003352 I915_WRITE(reg, temp);
3353 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003354 udelay(150);
3355
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003356 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003357 reg = FDI_TX_CTL(pipe);
3358 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003359 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003360 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003361 temp &= ~FDI_LINK_TRAIN_NONE;
3362 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003363 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003364
Chris Wilson5eddb702010-09-11 13:48:45 +01003365 reg = FDI_RX_CTL(pipe);
3366 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003367 temp &= ~FDI_LINK_TRAIN_NONE;
3368 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003369 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3370
3371 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003372 udelay(150);
3373
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003374 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003375 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3376 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3377 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003378
Chris Wilson5eddb702010-09-11 13:48:45 +01003379 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003380 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003381 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003382 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3383
3384 if ((temp & FDI_RX_BIT_LOCK)) {
3385 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003386 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003387 break;
3388 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003389 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003390 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003391 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003392
3393 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003394 reg = FDI_TX_CTL(pipe);
3395 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003396 temp &= ~FDI_LINK_TRAIN_NONE;
3397 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003398 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003399
Chris Wilson5eddb702010-09-11 13:48:45 +01003400 reg = FDI_RX_CTL(pipe);
3401 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003402 temp &= ~FDI_LINK_TRAIN_NONE;
3403 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003404 I915_WRITE(reg, temp);
3405
3406 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003407 udelay(150);
3408
Chris Wilson5eddb702010-09-11 13:48:45 +01003409 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003410 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003411 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003412 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3413
3414 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003415 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003416 DRM_DEBUG_KMS("FDI train 2 done.\n");
3417 break;
3418 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003419 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003420 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003421 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003422
3423 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003424
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003425}
3426
Akshay Joshi0206e352011-08-16 15:34:10 -04003427static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003428 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3429 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3430 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3431 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3432};
3433
3434/* The FDI link training functions for SNB/Cougarpoint. */
3435static void gen6_fdi_link_train(struct drm_crtc *crtc)
3436{
3437 struct drm_device *dev = crtc->dev;
3438 struct drm_i915_private *dev_priv = dev->dev_private;
3439 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3440 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003441 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003442
Adam Jacksone1a44742010-06-25 15:32:14 -04003443 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3444 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003445 reg = FDI_RX_IMR(pipe);
3446 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003447 temp &= ~FDI_RX_SYMBOL_LOCK;
3448 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003449 I915_WRITE(reg, temp);
3450
3451 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003452 udelay(150);
3453
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003454 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003455 reg = FDI_TX_CTL(pipe);
3456 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003457 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003458 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003459 temp &= ~FDI_LINK_TRAIN_NONE;
3460 temp |= FDI_LINK_TRAIN_PATTERN_1;
3461 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3462 /* SNB-B */
3463 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003464 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003465
Daniel Vetterd74cf322012-10-26 10:58:13 +02003466 I915_WRITE(FDI_RX_MISC(pipe),
3467 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3468
Chris Wilson5eddb702010-09-11 13:48:45 +01003469 reg = FDI_RX_CTL(pipe);
3470 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003471 if (HAS_PCH_CPT(dev)) {
3472 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3473 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3474 } else {
3475 temp &= ~FDI_LINK_TRAIN_NONE;
3476 temp |= FDI_LINK_TRAIN_PATTERN_1;
3477 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003478 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3479
3480 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003481 udelay(150);
3482
Akshay Joshi0206e352011-08-16 15:34:10 -04003483 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003484 reg = FDI_TX_CTL(pipe);
3485 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003486 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3487 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003488 I915_WRITE(reg, temp);
3489
3490 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003491 udelay(500);
3492
Sean Paulfa37d392012-03-02 12:53:39 -05003493 for (retry = 0; retry < 5; retry++) {
3494 reg = FDI_RX_IIR(pipe);
3495 temp = I915_READ(reg);
3496 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3497 if (temp & FDI_RX_BIT_LOCK) {
3498 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3499 DRM_DEBUG_KMS("FDI train 1 done.\n");
3500 break;
3501 }
3502 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003503 }
Sean Paulfa37d392012-03-02 12:53:39 -05003504 if (retry < 5)
3505 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003506 }
3507 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003508 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003509
3510 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003511 reg = FDI_TX_CTL(pipe);
3512 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003513 temp &= ~FDI_LINK_TRAIN_NONE;
3514 temp |= FDI_LINK_TRAIN_PATTERN_2;
3515 if (IS_GEN6(dev)) {
3516 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3517 /* SNB-B */
3518 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3519 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003520 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003521
Chris Wilson5eddb702010-09-11 13:48:45 +01003522 reg = FDI_RX_CTL(pipe);
3523 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003524 if (HAS_PCH_CPT(dev)) {
3525 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3526 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3527 } else {
3528 temp &= ~FDI_LINK_TRAIN_NONE;
3529 temp |= FDI_LINK_TRAIN_PATTERN_2;
3530 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003531 I915_WRITE(reg, temp);
3532
3533 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003534 udelay(150);
3535
Akshay Joshi0206e352011-08-16 15:34:10 -04003536 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003537 reg = FDI_TX_CTL(pipe);
3538 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003539 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3540 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003541 I915_WRITE(reg, temp);
3542
3543 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003544 udelay(500);
3545
Sean Paulfa37d392012-03-02 12:53:39 -05003546 for (retry = 0; retry < 5; retry++) {
3547 reg = FDI_RX_IIR(pipe);
3548 temp = I915_READ(reg);
3549 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3550 if (temp & FDI_RX_SYMBOL_LOCK) {
3551 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3552 DRM_DEBUG_KMS("FDI train 2 done.\n");
3553 break;
3554 }
3555 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003556 }
Sean Paulfa37d392012-03-02 12:53:39 -05003557 if (retry < 5)
3558 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003559 }
3560 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003561 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003562
3563 DRM_DEBUG_KMS("FDI train done.\n");
3564}
3565
Jesse Barnes357555c2011-04-28 15:09:55 -07003566/* Manual link training for Ivy Bridge A0 parts */
3567static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3568{
3569 struct drm_device *dev = crtc->dev;
3570 struct drm_i915_private *dev_priv = dev->dev_private;
3571 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3572 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003573 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003574
3575 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3576 for train result */
3577 reg = FDI_RX_IMR(pipe);
3578 temp = I915_READ(reg);
3579 temp &= ~FDI_RX_SYMBOL_LOCK;
3580 temp &= ~FDI_RX_BIT_LOCK;
3581 I915_WRITE(reg, temp);
3582
3583 POSTING_READ(reg);
3584 udelay(150);
3585
Daniel Vetter01a415f2012-10-27 15:58:40 +02003586 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3587 I915_READ(FDI_RX_IIR(pipe)));
3588
Jesse Barnes139ccd32013-08-19 11:04:55 -07003589 /* Try each vswing and preemphasis setting twice before moving on */
3590 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3591 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003592 reg = FDI_TX_CTL(pipe);
3593 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003594 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3595 temp &= ~FDI_TX_ENABLE;
3596 I915_WRITE(reg, temp);
3597
3598 reg = FDI_RX_CTL(pipe);
3599 temp = I915_READ(reg);
3600 temp &= ~FDI_LINK_TRAIN_AUTO;
3601 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3602 temp &= ~FDI_RX_ENABLE;
3603 I915_WRITE(reg, temp);
3604
3605 /* enable CPU FDI TX and PCH FDI RX */
3606 reg = FDI_TX_CTL(pipe);
3607 temp = I915_READ(reg);
3608 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003609 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003610 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003611 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003612 temp |= snb_b_fdi_train_param[j/2];
3613 temp |= FDI_COMPOSITE_SYNC;
3614 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3615
3616 I915_WRITE(FDI_RX_MISC(pipe),
3617 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3618
3619 reg = FDI_RX_CTL(pipe);
3620 temp = I915_READ(reg);
3621 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3622 temp |= FDI_COMPOSITE_SYNC;
3623 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3624
3625 POSTING_READ(reg);
3626 udelay(1); /* should be 0.5us */
3627
3628 for (i = 0; i < 4; i++) {
3629 reg = FDI_RX_IIR(pipe);
3630 temp = I915_READ(reg);
3631 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3632
3633 if (temp & FDI_RX_BIT_LOCK ||
3634 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3635 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3636 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3637 i);
3638 break;
3639 }
3640 udelay(1); /* should be 0.5us */
3641 }
3642 if (i == 4) {
3643 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3644 continue;
3645 }
3646
3647 /* Train 2 */
3648 reg = FDI_TX_CTL(pipe);
3649 temp = I915_READ(reg);
3650 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3651 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3652 I915_WRITE(reg, temp);
3653
3654 reg = FDI_RX_CTL(pipe);
3655 temp = I915_READ(reg);
3656 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3657 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003658 I915_WRITE(reg, temp);
3659
3660 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003661 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003662
Jesse Barnes139ccd32013-08-19 11:04:55 -07003663 for (i = 0; i < 4; i++) {
3664 reg = FDI_RX_IIR(pipe);
3665 temp = I915_READ(reg);
3666 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003667
Jesse Barnes139ccd32013-08-19 11:04:55 -07003668 if (temp & FDI_RX_SYMBOL_LOCK ||
3669 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3670 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3671 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3672 i);
3673 goto train_done;
3674 }
3675 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003676 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003677 if (i == 4)
3678 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003679 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003680
Jesse Barnes139ccd32013-08-19 11:04:55 -07003681train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003682 DRM_DEBUG_KMS("FDI train done.\n");
3683}
3684
Daniel Vetter88cefb62012-08-12 19:27:14 +02003685static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003686{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003687 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003688 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003689 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003690 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003691
Jesse Barnesc64e3112010-09-10 11:27:03 -07003692
Jesse Barnes0e23b992010-09-10 11:10:00 -07003693 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003694 reg = FDI_RX_CTL(pipe);
3695 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003696 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003697 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003698 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003699 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3700
3701 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003702 udelay(200);
3703
3704 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003705 temp = I915_READ(reg);
3706 I915_WRITE(reg, temp | FDI_PCDCLK);
3707
3708 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003709 udelay(200);
3710
Paulo Zanoni20749732012-11-23 15:30:38 -02003711 /* Enable CPU FDI TX PLL, always on for Ironlake */
3712 reg = FDI_TX_CTL(pipe);
3713 temp = I915_READ(reg);
3714 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3715 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003716
Paulo Zanoni20749732012-11-23 15:30:38 -02003717 POSTING_READ(reg);
3718 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003719 }
3720}
3721
Daniel Vetter88cefb62012-08-12 19:27:14 +02003722static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3723{
3724 struct drm_device *dev = intel_crtc->base.dev;
3725 struct drm_i915_private *dev_priv = dev->dev_private;
3726 int pipe = intel_crtc->pipe;
3727 u32 reg, temp;
3728
3729 /* Switch from PCDclk to Rawclk */
3730 reg = FDI_RX_CTL(pipe);
3731 temp = I915_READ(reg);
3732 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3733
3734 /* Disable CPU FDI TX PLL */
3735 reg = FDI_TX_CTL(pipe);
3736 temp = I915_READ(reg);
3737 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3738
3739 POSTING_READ(reg);
3740 udelay(100);
3741
3742 reg = FDI_RX_CTL(pipe);
3743 temp = I915_READ(reg);
3744 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3745
3746 /* Wait for the clocks to turn off. */
3747 POSTING_READ(reg);
3748 udelay(100);
3749}
3750
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003751static void ironlake_fdi_disable(struct drm_crtc *crtc)
3752{
3753 struct drm_device *dev = crtc->dev;
3754 struct drm_i915_private *dev_priv = dev->dev_private;
3755 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3756 int pipe = intel_crtc->pipe;
3757 u32 reg, temp;
3758
3759 /* disable CPU FDI tx and PCH FDI rx */
3760 reg = FDI_TX_CTL(pipe);
3761 temp = I915_READ(reg);
3762 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3763 POSTING_READ(reg);
3764
3765 reg = FDI_RX_CTL(pipe);
3766 temp = I915_READ(reg);
3767 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003768 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003769 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3770
3771 POSTING_READ(reg);
3772 udelay(100);
3773
3774 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003775 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003776 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003777
3778 /* still set train pattern 1 */
3779 reg = FDI_TX_CTL(pipe);
3780 temp = I915_READ(reg);
3781 temp &= ~FDI_LINK_TRAIN_NONE;
3782 temp |= FDI_LINK_TRAIN_PATTERN_1;
3783 I915_WRITE(reg, temp);
3784
3785 reg = FDI_RX_CTL(pipe);
3786 temp = I915_READ(reg);
3787 if (HAS_PCH_CPT(dev)) {
3788 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3789 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3790 } else {
3791 temp &= ~FDI_LINK_TRAIN_NONE;
3792 temp |= FDI_LINK_TRAIN_PATTERN_1;
3793 }
3794 /* BPC in FDI rx is consistent with that in PIPECONF */
3795 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003796 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003797 I915_WRITE(reg, temp);
3798
3799 POSTING_READ(reg);
3800 udelay(100);
3801}
3802
Chris Wilson5dce5b932014-01-20 10:17:36 +00003803bool intel_has_pending_fb_unpin(struct drm_device *dev)
3804{
3805 struct intel_crtc *crtc;
3806
3807 /* Note that we don't need to be called with mode_config.lock here
3808 * as our list of CRTC objects is static for the lifetime of the
3809 * device and so cannot disappear as we iterate. Similarly, we can
3810 * happily treat the predicates as racy, atomic checks as userspace
3811 * cannot claim and pin a new fb without at least acquring the
3812 * struct_mutex and so serialising with us.
3813 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003814 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003815 if (atomic_read(&crtc->unpin_work_count) == 0)
3816 continue;
3817
3818 if (crtc->unpin_work)
3819 intel_wait_for_vblank(dev, crtc->pipe);
3820
3821 return true;
3822 }
3823
3824 return false;
3825}
3826
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003827static void page_flip_completed(struct intel_crtc *intel_crtc)
3828{
3829 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3830 struct intel_unpin_work *work = intel_crtc->unpin_work;
3831
3832 /* ensure that the unpin work is consistent wrt ->pending. */
3833 smp_rmb();
3834 intel_crtc->unpin_work = NULL;
3835
3836 if (work->event)
3837 drm_send_vblank_event(intel_crtc->base.dev,
3838 intel_crtc->pipe,
3839 work->event);
3840
3841 drm_crtc_vblank_put(&intel_crtc->base);
3842
3843 wake_up_all(&dev_priv->pending_flip_queue);
3844 queue_work(dev_priv->wq, &work->work);
3845
3846 trace_i915_flip_complete(intel_crtc->plane,
3847 work->pending_flip_obj);
3848}
3849
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003850void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003851{
Chris Wilson0f911282012-04-17 10:05:38 +01003852 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003853 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003854
Daniel Vetter2c10d572012-12-20 21:24:07 +01003855 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003856 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3857 !intel_crtc_has_pending_flip(crtc),
3858 60*HZ) == 0)) {
3859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003860
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003861 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003862 if (intel_crtc->unpin_work) {
3863 WARN_ONCE(1, "Removing stuck page flip\n");
3864 page_flip_completed(intel_crtc);
3865 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003866 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003867 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003868
Chris Wilson975d5682014-08-20 13:13:34 +01003869 if (crtc->primary->fb) {
3870 mutex_lock(&dev->struct_mutex);
3871 intel_finish_fb(crtc->primary->fb);
3872 mutex_unlock(&dev->struct_mutex);
3873 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003874}
3875
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003876/* Program iCLKIP clock to the desired frequency */
3877static void lpt_program_iclkip(struct drm_crtc *crtc)
3878{
3879 struct drm_device *dev = crtc->dev;
3880 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003881 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003882 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3883 u32 temp;
3884
Daniel Vetter09153002012-12-12 14:06:44 +01003885 mutex_lock(&dev_priv->dpio_lock);
3886
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003887 /* It is necessary to ungate the pixclk gate prior to programming
3888 * the divisors, and gate it back when it is done.
3889 */
3890 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3891
3892 /* Disable SSCCTL */
3893 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003894 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3895 SBI_SSCCTL_DISABLE,
3896 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003897
3898 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003899 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003900 auxdiv = 1;
3901 divsel = 0x41;
3902 phaseinc = 0x20;
3903 } else {
3904 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003905 * but the adjusted_mode->crtc_clock in in KHz. To get the
3906 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003907 * convert the virtual clock precision to KHz here for higher
3908 * precision.
3909 */
3910 u32 iclk_virtual_root_freq = 172800 * 1000;
3911 u32 iclk_pi_range = 64;
3912 u32 desired_divisor, msb_divisor_value, pi_value;
3913
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003914 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003915 msb_divisor_value = desired_divisor / iclk_pi_range;
3916 pi_value = desired_divisor % iclk_pi_range;
3917
3918 auxdiv = 0;
3919 divsel = msb_divisor_value - 2;
3920 phaseinc = pi_value;
3921 }
3922
3923 /* This should not happen with any sane values */
3924 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3925 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3926 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3927 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3928
3929 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 +03003930 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003931 auxdiv,
3932 divsel,
3933 phasedir,
3934 phaseinc);
3935
3936 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003937 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003938 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3939 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3940 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3941 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3942 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3943 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003944 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003945
3946 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003947 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003948 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3949 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003950 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003951
3952 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003953 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003954 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003955 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003956
3957 /* Wait for initialization time */
3958 udelay(24);
3959
3960 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003961
3962 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003963}
3964
Daniel Vetter275f01b22013-05-03 11:49:47 +02003965static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3966 enum pipe pch_transcoder)
3967{
3968 struct drm_device *dev = crtc->base.dev;
3969 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003970 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003971
3972 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3973 I915_READ(HTOTAL(cpu_transcoder)));
3974 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3975 I915_READ(HBLANK(cpu_transcoder)));
3976 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3977 I915_READ(HSYNC(cpu_transcoder)));
3978
3979 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3980 I915_READ(VTOTAL(cpu_transcoder)));
3981 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3982 I915_READ(VBLANK(cpu_transcoder)));
3983 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3984 I915_READ(VSYNC(cpu_transcoder)));
3985 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3986 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3987}
3988
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003989static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003990{
3991 struct drm_i915_private *dev_priv = dev->dev_private;
3992 uint32_t temp;
3993
3994 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003995 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003996 return;
3997
3998 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3999 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4000
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004001 temp &= ~FDI_BC_BIFURCATION_SELECT;
4002 if (enable)
4003 temp |= FDI_BC_BIFURCATION_SELECT;
4004
4005 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004006 I915_WRITE(SOUTH_CHICKEN1, temp);
4007 POSTING_READ(SOUTH_CHICKEN1);
4008}
4009
4010static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4011{
4012 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004013
4014 switch (intel_crtc->pipe) {
4015 case PIPE_A:
4016 break;
4017 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004018 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004019 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004020 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004021 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004022
4023 break;
4024 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004025 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004026
4027 break;
4028 default:
4029 BUG();
4030 }
4031}
4032
Jesse Barnesf67a5592011-01-05 10:31:48 -08004033/*
4034 * Enable PCH resources required for PCH ports:
4035 * - PCH PLLs
4036 * - FDI training & RX/TX
4037 * - update transcoder timings
4038 * - DP transcoding bits
4039 * - transcoder
4040 */
4041static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004042{
4043 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004044 struct drm_i915_private *dev_priv = dev->dev_private;
4045 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4046 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004047 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004048
Daniel Vetterab9412b2013-05-03 11:49:46 +02004049 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004050
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004051 if (IS_IVYBRIDGE(dev))
4052 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4053
Daniel Vettercd986ab2012-10-26 10:58:12 +02004054 /* Write the TU size bits before fdi link training, so that error
4055 * detection works. */
4056 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4057 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4058
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004059 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004060 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004061
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004062 /* We need to program the right clock selection before writing the pixel
4063 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004064 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004065 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004066
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004067 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004068 temp |= TRANS_DPLL_ENABLE(pipe);
4069 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004070 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004071 temp |= sel;
4072 else
4073 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004074 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004075 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004076
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004077 /* XXX: pch pll's can be enabled any time before we enable the PCH
4078 * transcoder, and we actually should do this to not upset any PCH
4079 * transcoder that already use the clock when we share it.
4080 *
4081 * Note that enable_shared_dpll tries to do the right thing, but
4082 * get_shared_dpll unconditionally resets the pll - we need that to have
4083 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004084 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004085
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004086 /* set transcoder timing, panel must allow it */
4087 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004088 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004089
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004090 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004091
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004092 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004093 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004094 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004095 reg = TRANS_DP_CTL(pipe);
4096 temp = I915_READ(reg);
4097 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004098 TRANS_DP_SYNC_MASK |
4099 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01004100 temp |= (TRANS_DP_OUTPUT_ENABLE |
4101 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004102 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004103
4104 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004105 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004106 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004107 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004108
4109 switch (intel_trans_dp_port_sel(crtc)) {
4110 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004111 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004112 break;
4113 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004114 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004115 break;
4116 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004117 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004118 break;
4119 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004120 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004121 }
4122
Chris Wilson5eddb702010-09-11 13:48:45 +01004123 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004124 }
4125
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004126 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004127}
4128
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004129static void lpt_pch_enable(struct drm_crtc *crtc)
4130{
4131 struct drm_device *dev = crtc->dev;
4132 struct drm_i915_private *dev_priv = dev->dev_private;
4133 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004134 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004135
Daniel Vetterab9412b2013-05-03 11:49:46 +02004136 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004137
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004138 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004139
Paulo Zanoni0540e482012-10-31 18:12:40 -02004140 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004141 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004142
Paulo Zanoni937bb612012-10-31 18:12:47 -02004143 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004144}
4145
Daniel Vetter716c2e52014-06-25 22:02:02 +03004146void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004147{
Daniel Vettere2b78262013-06-07 23:10:03 +02004148 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004149
4150 if (pll == NULL)
4151 return;
4152
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004153 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004154 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004155 return;
4156 }
4157
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004158 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4159 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004160 WARN_ON(pll->on);
4161 WARN_ON(pll->active);
4162 }
4163
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004164 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004165}
4166
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004167struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4168 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004169{
Daniel Vettere2b78262013-06-07 23:10:03 +02004170 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004171 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004172 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004173
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004174 if (HAS_PCH_IBX(dev_priv->dev)) {
4175 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004176 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004177 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004178
Daniel Vetter46edb022013-06-05 13:34:12 +02004179 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4180 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004181
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004182 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004183
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004184 goto found;
4185 }
4186
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004187 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4188 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004189
4190 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004191 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004192 continue;
4193
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004194 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004195 &pll->new_config->hw_state,
4196 sizeof(pll->new_config->hw_state)) == 0) {
4197 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004198 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004199 pll->new_config->crtc_mask,
4200 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004201 goto found;
4202 }
4203 }
4204
4205 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004206 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4207 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004208 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004209 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4210 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004211 goto found;
4212 }
4213 }
4214
4215 return NULL;
4216
4217found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004218 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004219 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004220
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004221 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004222 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4223 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004224
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004225 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004226
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004227 return pll;
4228}
4229
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004230/**
4231 * intel_shared_dpll_start_config - start a new PLL staged config
4232 * @dev_priv: DRM device
4233 * @clear_pipes: mask of pipes that will have their PLLs freed
4234 *
4235 * Starts a new PLL staged config, copying the current config but
4236 * releasing the references of pipes specified in clear_pipes.
4237 */
4238static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4239 unsigned clear_pipes)
4240{
4241 struct intel_shared_dpll *pll;
4242 enum intel_dpll_id i;
4243
4244 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4245 pll = &dev_priv->shared_dplls[i];
4246
4247 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4248 GFP_KERNEL);
4249 if (!pll->new_config)
4250 goto cleanup;
4251
4252 pll->new_config->crtc_mask &= ~clear_pipes;
4253 }
4254
4255 return 0;
4256
4257cleanup:
4258 while (--i >= 0) {
4259 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004260 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004261 pll->new_config = NULL;
4262 }
4263
4264 return -ENOMEM;
4265}
4266
4267static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4268{
4269 struct intel_shared_dpll *pll;
4270 enum intel_dpll_id i;
4271
4272 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4273 pll = &dev_priv->shared_dplls[i];
4274
4275 WARN_ON(pll->new_config == &pll->config);
4276
4277 pll->config = *pll->new_config;
4278 kfree(pll->new_config);
4279 pll->new_config = NULL;
4280 }
4281}
4282
4283static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4284{
4285 struct intel_shared_dpll *pll;
4286 enum intel_dpll_id i;
4287
4288 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4289 pll = &dev_priv->shared_dplls[i];
4290
4291 WARN_ON(pll->new_config == &pll->config);
4292
4293 kfree(pll->new_config);
4294 pll->new_config = NULL;
4295 }
4296}
4297
Daniel Vettera1520312013-05-03 11:49:50 +02004298static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004299{
4300 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004301 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004302 u32 temp;
4303
4304 temp = I915_READ(dslreg);
4305 udelay(500);
4306 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004307 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004308 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004309 }
4310}
4311
Chandra Kondurua1b22782015-04-07 15:28:45 -07004312/**
4313 * skl_update_scaler_users - Stages update to crtc's scaler state
4314 * @intel_crtc: crtc
4315 * @crtc_state: crtc_state
4316 * @plane: plane (NULL indicates crtc is requesting update)
4317 * @plane_state: plane's state
4318 * @force_detach: request unconditional detachment of scaler
4319 *
4320 * This function updates scaler state for requested plane or crtc.
4321 * To request scaler usage update for a plane, caller shall pass plane pointer.
4322 * To request scaler usage update for crtc, caller shall pass plane pointer
4323 * as NULL.
4324 *
4325 * Return
4326 * 0 - scaler_usage updated successfully
4327 * error - requested scaling cannot be supported or other error condition
4328 */
4329int
4330skl_update_scaler_users(
4331 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4332 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4333 int force_detach)
4334{
4335 int need_scaling;
4336 int idx;
4337 int src_w, src_h, dst_w, dst_h;
4338 int *scaler_id;
4339 struct drm_framebuffer *fb;
4340 struct intel_crtc_scaler_state *scaler_state;
4341
4342 if (!intel_crtc || !crtc_state)
4343 return 0;
4344
4345 scaler_state = &crtc_state->scaler_state;
4346
4347 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4348 fb = intel_plane ? plane_state->base.fb : NULL;
4349
4350 if (intel_plane) {
4351 src_w = drm_rect_width(&plane_state->src) >> 16;
4352 src_h = drm_rect_height(&plane_state->src) >> 16;
4353 dst_w = drm_rect_width(&plane_state->dst);
4354 dst_h = drm_rect_height(&plane_state->dst);
4355 scaler_id = &plane_state->scaler_id;
4356 } else {
4357 struct drm_display_mode *adjusted_mode =
4358 &crtc_state->base.adjusted_mode;
4359 src_w = crtc_state->pipe_src_w;
4360 src_h = crtc_state->pipe_src_h;
4361 dst_w = adjusted_mode->hdisplay;
4362 dst_h = adjusted_mode->vdisplay;
4363 scaler_id = &scaler_state->scaler_id;
4364 }
4365 need_scaling = (src_w != dst_w || src_h != dst_h);
4366
4367 /*
4368 * if plane is being disabled or scaler is no more required or force detach
4369 * - free scaler binded to this plane/crtc
4370 * - in order to do this, update crtc->scaler_usage
4371 *
4372 * Here scaler state in crtc_state is set free so that
4373 * scaler can be assigned to other user. Actual register
4374 * update to free the scaler is done in plane/panel-fit programming.
4375 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4376 */
4377 if (force_detach || !need_scaling || (intel_plane &&
4378 (!fb || !plane_state->visible))) {
4379 if (*scaler_id >= 0) {
4380 scaler_state->scaler_users &= ~(1 << idx);
4381 scaler_state->scalers[*scaler_id].in_use = 0;
4382
4383 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4384 "crtc_state = %p scaler_users = 0x%x\n",
4385 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4386 intel_plane ? intel_plane->base.base.id :
4387 intel_crtc->base.base.id, crtc_state,
4388 scaler_state->scaler_users);
4389 *scaler_id = -1;
4390 }
4391 return 0;
4392 }
4393
4394 /* range checks */
4395 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4396 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4397
4398 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4399 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4400 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4401 "size is out of scaler range\n",
4402 intel_plane ? "PLANE" : "CRTC",
4403 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4404 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4405 return -EINVAL;
4406 }
4407
4408 /* check colorkey */
4409 if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
4410 DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
4411 intel_plane->base.base.id);
4412 return -EINVAL;
4413 }
4414
4415 /* Check src format */
4416 if (intel_plane) {
4417 switch (fb->pixel_format) {
4418 case DRM_FORMAT_RGB565:
4419 case DRM_FORMAT_XBGR8888:
4420 case DRM_FORMAT_XRGB8888:
4421 case DRM_FORMAT_ABGR8888:
4422 case DRM_FORMAT_ARGB8888:
4423 case DRM_FORMAT_XRGB2101010:
4424 case DRM_FORMAT_ARGB2101010:
4425 case DRM_FORMAT_XBGR2101010:
4426 case DRM_FORMAT_ABGR2101010:
4427 case DRM_FORMAT_YUYV:
4428 case DRM_FORMAT_YVYU:
4429 case DRM_FORMAT_UYVY:
4430 case DRM_FORMAT_VYUY:
4431 break;
4432 default:
4433 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4434 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4435 return -EINVAL;
4436 }
4437 }
4438
4439 /* mark this plane as a scaler user in crtc_state */
4440 scaler_state->scaler_users |= (1 << idx);
4441 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4442 "crtc_state = %p scaler_users = 0x%x\n",
4443 intel_plane ? "PLANE" : "CRTC",
4444 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4445 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4446 return 0;
4447}
4448
4449static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004450{
4451 struct drm_device *dev = crtc->base.dev;
4452 struct drm_i915_private *dev_priv = dev->dev_private;
4453 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004454 struct intel_crtc_scaler_state *scaler_state =
4455 &crtc->config->scaler_state;
4456
4457 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4458
4459 /* To update pfit, first update scaler state */
4460 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4461 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4462 skl_detach_scalers(crtc);
4463 if (!enable)
4464 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004465
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004466 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004467 int id;
4468
4469 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4470 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4471 return;
4472 }
4473
4474 id = scaler_state->scaler_id;
4475 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4476 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4477 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4478 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4479
4480 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004481 }
4482}
4483
Jesse Barnesb074cec2013-04-25 12:55:02 -07004484static void ironlake_pfit_enable(struct intel_crtc *crtc)
4485{
4486 struct drm_device *dev = crtc->base.dev;
4487 struct drm_i915_private *dev_priv = dev->dev_private;
4488 int pipe = crtc->pipe;
4489
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004490 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004491 /* Force use of hard-coded filter coefficients
4492 * as some pre-programmed values are broken,
4493 * e.g. x201.
4494 */
4495 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4496 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4497 PF_PIPE_SEL_IVB(pipe));
4498 else
4499 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004500 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4501 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004502 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004503}
4504
Matt Roper4a3b8762014-12-23 10:41:51 -08004505static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004506{
4507 struct drm_device *dev = crtc->dev;
4508 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004509 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004510 struct intel_plane *intel_plane;
4511
Matt Roperaf2b6532014-04-01 15:22:32 -07004512 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4513 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004514 if (intel_plane->pipe == pipe)
4515 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004516 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004517}
4518
Matt Roper0d703d42015-03-04 10:49:04 -08004519/*
4520 * Disable a plane internally without actually modifying the plane's state.
4521 * This will allow us to easily restore the plane later by just reprogramming
4522 * its state.
4523 */
4524static void disable_plane_internal(struct drm_plane *plane)
4525{
4526 struct intel_plane *intel_plane = to_intel_plane(plane);
4527 struct drm_plane_state *state =
4528 plane->funcs->atomic_duplicate_state(plane);
4529 struct intel_plane_state *intel_state = to_intel_plane_state(state);
4530
4531 intel_state->visible = false;
4532 intel_plane->commit_plane(plane, intel_state);
4533
4534 intel_plane_destroy_state(plane, state);
4535}
4536
Matt Roper4a3b8762014-12-23 10:41:51 -08004537static void intel_disable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004538{
4539 struct drm_device *dev = crtc->dev;
4540 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004541 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004542 struct intel_plane *intel_plane;
4543
Matt Roperaf2b6532014-04-01 15:22:32 -07004544 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4545 intel_plane = to_intel_plane(plane);
Matt Roper0d703d42015-03-04 10:49:04 -08004546 if (plane->fb && intel_plane->pipe == pipe)
4547 disable_plane_internal(plane);
Matt Roperaf2b6532014-04-01 15:22:32 -07004548 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004549}
4550
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004551void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004552{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004553 struct drm_device *dev = crtc->base.dev;
4554 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004555
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004556 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004557 return;
4558
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004559 /* We can only enable IPS after we enable a plane and wait for a vblank */
4560 intel_wait_for_vblank(dev, crtc->pipe);
4561
Paulo Zanonid77e4532013-09-24 13:52:55 -03004562 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004563 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004564 mutex_lock(&dev_priv->rps.hw_lock);
4565 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4566 mutex_unlock(&dev_priv->rps.hw_lock);
4567 /* Quoting Art Runyan: "its not safe to expect any particular
4568 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004569 * mailbox." Moreover, the mailbox may return a bogus state,
4570 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004571 */
4572 } else {
4573 I915_WRITE(IPS_CTL, IPS_ENABLE);
4574 /* The bit only becomes 1 in the next vblank, so this wait here
4575 * is essentially intel_wait_for_vblank. If we don't have this
4576 * and don't wait for vblanks until the end of crtc_enable, then
4577 * the HW state readout code will complain that the expected
4578 * IPS_CTL value is not the one we read. */
4579 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4580 DRM_ERROR("Timed out waiting for IPS enable\n");
4581 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004582}
4583
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004584void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004585{
4586 struct drm_device *dev = crtc->base.dev;
4587 struct drm_i915_private *dev_priv = dev->dev_private;
4588
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004589 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004590 return;
4591
4592 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004593 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004594 mutex_lock(&dev_priv->rps.hw_lock);
4595 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4596 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004597 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4598 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4599 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004600 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004601 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004602 POSTING_READ(IPS_CTL);
4603 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004604
4605 /* We need to wait for a vblank before we can disable the plane. */
4606 intel_wait_for_vblank(dev, crtc->pipe);
4607}
4608
4609/** Loads the palette/gamma unit for the CRTC with the prepared values */
4610static void intel_crtc_load_lut(struct drm_crtc *crtc)
4611{
4612 struct drm_device *dev = crtc->dev;
4613 struct drm_i915_private *dev_priv = dev->dev_private;
4614 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4615 enum pipe pipe = intel_crtc->pipe;
4616 int palreg = PALETTE(pipe);
4617 int i;
4618 bool reenable_ips = false;
4619
4620 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004621 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004622 return;
4623
4624 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004625 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004626 assert_dsi_pll_enabled(dev_priv);
4627 else
4628 assert_pll_enabled(dev_priv, pipe);
4629 }
4630
4631 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304632 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004633 palreg = LGC_PALETTE(pipe);
4634
4635 /* Workaround : Do not read or write the pipe palette/gamma data while
4636 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4637 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004638 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004639 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4640 GAMMA_MODE_MODE_SPLIT)) {
4641 hsw_disable_ips(intel_crtc);
4642 reenable_ips = true;
4643 }
4644
4645 for (i = 0; i < 256; i++) {
4646 I915_WRITE(palreg + 4 * i,
4647 (intel_crtc->lut_r[i] << 16) |
4648 (intel_crtc->lut_g[i] << 8) |
4649 intel_crtc->lut_b[i]);
4650 }
4651
4652 if (reenable_ips)
4653 hsw_enable_ips(intel_crtc);
4654}
4655
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004656static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4657{
4658 if (!enable && intel_crtc->overlay) {
4659 struct drm_device *dev = intel_crtc->base.dev;
4660 struct drm_i915_private *dev_priv = dev->dev_private;
4661
4662 mutex_lock(&dev->struct_mutex);
4663 dev_priv->mm.interruptible = false;
4664 (void) intel_overlay_switch_off(intel_crtc->overlay);
4665 dev_priv->mm.interruptible = true;
4666 mutex_unlock(&dev->struct_mutex);
4667 }
4668
4669 /* Let userspace switch the overlay on again. In most cases userspace
4670 * has to recompute where to put it anyway.
4671 */
4672}
4673
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004674static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004675{
4676 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004677 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4678 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004679
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03004680 intel_enable_primary_hw_plane(crtc->primary, crtc);
Matt Roper4a3b8762014-12-23 10:41:51 -08004681 intel_enable_sprite_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004682 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004683 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004684
4685 hsw_enable_ips(intel_crtc);
4686
4687 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004688 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004689 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004690
4691 /*
4692 * FIXME: Once we grow proper nuclear flip support out of this we need
4693 * to compute the mask of flip planes precisely. For the time being
4694 * consider this a flip from a NULL plane.
4695 */
4696 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004697}
4698
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004699static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004700{
4701 struct drm_device *dev = crtc->dev;
4702 struct drm_i915_private *dev_priv = dev->dev_private;
4703 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4704 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004705
4706 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004707
Paulo Zanonie35fef22015-02-09 14:46:29 -02004708 if (dev_priv->fbc.crtc == intel_crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004709 intel_fbc_disable(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004710
4711 hsw_disable_ips(intel_crtc);
4712
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004713 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004714 intel_crtc_update_cursor(crtc, false);
Matt Roper4a3b8762014-12-23 10:41:51 -08004715 intel_disable_sprite_planes(crtc);
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03004716 intel_disable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004717
Daniel Vetterf99d7062014-06-19 16:01:59 +02004718 /*
4719 * FIXME: Once we grow proper nuclear flip support out of this we need
4720 * to compute the mask of flip planes precisely. For the time being
4721 * consider this a flip to a NULL plane.
4722 */
4723 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004724}
4725
Jesse Barnesf67a5592011-01-05 10:31:48 -08004726static void ironlake_crtc_enable(struct drm_crtc *crtc)
4727{
4728 struct drm_device *dev = crtc->dev;
4729 struct drm_i915_private *dev_priv = dev->dev_private;
4730 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004731 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004732 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004733
Matt Roper83d65732015-02-25 13:12:16 -08004734 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004735
Jesse Barnesf67a5592011-01-05 10:31:48 -08004736 if (intel_crtc->active)
4737 return;
4738
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004739 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004740 intel_prepare_shared_dpll(intel_crtc);
4741
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004742 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304743 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004744
4745 intel_set_pipe_timings(intel_crtc);
4746
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004747 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004748 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004749 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004750 }
4751
4752 ironlake_set_pipeconf(crtc);
4753
Jesse Barnesf67a5592011-01-05 10:31:48 -08004754 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004755
Daniel Vettera72e4c92014-09-30 10:56:47 +02004756 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4757 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004758
Daniel Vetterf6736a12013-06-05 13:34:30 +02004759 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004760 if (encoder->pre_enable)
4761 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004762
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004763 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004764 /* Note: FDI PLL enabling _must_ be done before we enable the
4765 * cpu pipes, hence this is separate from all the other fdi/pch
4766 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004767 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004768 } else {
4769 assert_fdi_tx_disabled(dev_priv, pipe);
4770 assert_fdi_rx_disabled(dev_priv, pipe);
4771 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004772
Jesse Barnesb074cec2013-04-25 12:55:02 -07004773 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004774
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004775 /*
4776 * On ILK+ LUT must be loaded before the pipe is running but with
4777 * clocks enabled
4778 */
4779 intel_crtc_load_lut(crtc);
4780
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004781 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004782 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004783
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004784 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004785 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004786
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004787 assert_vblank_disabled(crtc);
4788 drm_crtc_vblank_on(crtc);
4789
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004790 for_each_encoder_on_crtc(dev, crtc, encoder)
4791 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004792
4793 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004794 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004795
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004796 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004797}
4798
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004799/* IPS only exists on ULT machines and is tied to pipe A. */
4800static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4801{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004802 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004803}
4804
Paulo Zanonie4916942013-09-20 16:21:19 -03004805/*
4806 * This implements the workaround described in the "notes" section of the mode
4807 * set sequence documentation. When going from no pipes or single pipe to
4808 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4809 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4810 */
4811static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4812{
4813 struct drm_device *dev = crtc->base.dev;
4814 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4815
4816 /* We want to get the other_active_crtc only if there's only 1 other
4817 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004818 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004819 if (!crtc_it->active || crtc_it == crtc)
4820 continue;
4821
4822 if (other_active_crtc)
4823 return;
4824
4825 other_active_crtc = crtc_it;
4826 }
4827 if (!other_active_crtc)
4828 return;
4829
4830 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4831 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4832}
4833
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004834static void haswell_crtc_enable(struct drm_crtc *crtc)
4835{
4836 struct drm_device *dev = crtc->dev;
4837 struct drm_i915_private *dev_priv = dev->dev_private;
4838 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4839 struct intel_encoder *encoder;
4840 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004841
Matt Roper83d65732015-02-25 13:12:16 -08004842 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004843
4844 if (intel_crtc->active)
4845 return;
4846
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004847 if (intel_crtc_to_shared_dpll(intel_crtc))
4848 intel_enable_shared_dpll(intel_crtc);
4849
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004850 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304851 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004852
4853 intel_set_pipe_timings(intel_crtc);
4854
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004855 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4856 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4857 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004858 }
4859
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004860 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004861 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004862 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004863 }
4864
4865 haswell_set_pipeconf(crtc);
4866
4867 intel_set_pipe_csc(crtc);
4868
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004869 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004870
Daniel Vettera72e4c92014-09-30 10:56:47 +02004871 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004872 for_each_encoder_on_crtc(dev, crtc, encoder)
4873 if (encoder->pre_enable)
4874 encoder->pre_enable(encoder);
4875
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004876 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004877 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4878 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004879 dev_priv->display.fdi_link_train(crtc);
4880 }
4881
Paulo Zanoni1f544382012-10-24 11:32:00 -02004882 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004883
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004884 if (IS_SKYLAKE(dev))
Chandra Kondurua1b22782015-04-07 15:28:45 -07004885 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004886 else
4887 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004888
4889 /*
4890 * On ILK+ LUT must be loaded before the pipe is running but with
4891 * clocks enabled
4892 */
4893 intel_crtc_load_lut(crtc);
4894
Paulo Zanoni1f544382012-10-24 11:32:00 -02004895 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004896 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004897
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004898 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004899 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004900
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004901 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004902 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004903
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004904 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004905 intel_ddi_set_vc_payload_alloc(crtc, true);
4906
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004907 assert_vblank_disabled(crtc);
4908 drm_crtc_vblank_on(crtc);
4909
Jani Nikula8807e552013-08-30 19:40:32 +03004910 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004911 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004912 intel_opregion_notify_encoder(encoder, true);
4913 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004914
Paulo Zanonie4916942013-09-20 16:21:19 -03004915 /* If we change the relative order between pipe/planes enabling, we need
4916 * to change the workaround. */
4917 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004918 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004919}
4920
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004921static void ironlake_pfit_disable(struct intel_crtc *crtc)
4922{
4923 struct drm_device *dev = crtc->base.dev;
4924 struct drm_i915_private *dev_priv = dev->dev_private;
4925 int pipe = crtc->pipe;
4926
4927 /* To avoid upsetting the power well on haswell only disable the pfit if
4928 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004929 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004930 I915_WRITE(PF_CTL(pipe), 0);
4931 I915_WRITE(PF_WIN_POS(pipe), 0);
4932 I915_WRITE(PF_WIN_SZ(pipe), 0);
4933 }
4934}
4935
Jesse Barnes6be4a602010-09-10 10:26:01 -07004936static void ironlake_crtc_disable(struct drm_crtc *crtc)
4937{
4938 struct drm_device *dev = crtc->dev;
4939 struct drm_i915_private *dev_priv = dev->dev_private;
4940 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004941 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004942 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004943 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004944
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004945 if (!intel_crtc->active)
4946 return;
4947
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004948 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004949
Daniel Vetterea9d7582012-07-10 10:42:52 +02004950 for_each_encoder_on_crtc(dev, crtc, encoder)
4951 encoder->disable(encoder);
4952
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004953 drm_crtc_vblank_off(crtc);
4954 assert_vblank_disabled(crtc);
4955
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004956 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004957 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02004958
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004959 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004960
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004961 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004962
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004963 for_each_encoder_on_crtc(dev, crtc, encoder)
4964 if (encoder->post_disable)
4965 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004966
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004967 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004968 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004969
Daniel Vetterd925c592013-06-05 13:34:04 +02004970 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004971
Daniel Vetterd925c592013-06-05 13:34:04 +02004972 if (HAS_PCH_CPT(dev)) {
4973 /* disable TRANS_DP_CTL */
4974 reg = TRANS_DP_CTL(pipe);
4975 temp = I915_READ(reg);
4976 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4977 TRANS_DP_PORT_SEL_MASK);
4978 temp |= TRANS_DP_PORT_SEL_NONE;
4979 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004980
Daniel Vetterd925c592013-06-05 13:34:04 +02004981 /* disable DPLL_SEL */
4982 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004983 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004984 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004985 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004986
4987 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004988 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02004989
4990 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004991 }
4992
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004993 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004994 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004995
4996 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004997 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004998 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004999}
5000
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005001static void haswell_crtc_disable(struct drm_crtc *crtc)
5002{
5003 struct drm_device *dev = crtc->dev;
5004 struct drm_i915_private *dev_priv = dev->dev_private;
5005 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5006 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005007 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005008
5009 if (!intel_crtc->active)
5010 return;
5011
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005012 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03005013
Jani Nikula8807e552013-08-30 19:40:32 +03005014 for_each_encoder_on_crtc(dev, crtc, encoder) {
5015 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005016 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005017 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005018
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005019 drm_crtc_vblank_off(crtc);
5020 assert_vblank_disabled(crtc);
5021
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005022 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005023 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5024 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005025 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005026
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005027 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005028 intel_ddi_set_vc_payload_alloc(crtc, false);
5029
Paulo Zanoniad80a812012-10-24 16:06:19 -02005030 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005031
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005032 if (IS_SKYLAKE(dev))
Chandra Kondurua1b22782015-04-07 15:28:45 -07005033 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005034 else
5035 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005036
Paulo Zanoni1f544382012-10-24 11:32:00 -02005037 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005038
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005039 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005040 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005041 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005042 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005043
Imre Deak97b040a2014-06-25 22:01:50 +03005044 for_each_encoder_on_crtc(dev, crtc, encoder)
5045 if (encoder->post_disable)
5046 encoder->post_disable(encoder);
5047
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005048 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005049 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005050
5051 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005052 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005053 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005054
5055 if (intel_crtc_to_shared_dpll(intel_crtc))
5056 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005057}
5058
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005059static void ironlake_crtc_off(struct drm_crtc *crtc)
5060{
5061 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005062 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005063}
5064
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005065
Jesse Barnes2dd24552013-04-25 12:55:01 -07005066static void i9xx_pfit_enable(struct intel_crtc *crtc)
5067{
5068 struct drm_device *dev = crtc->base.dev;
5069 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005070 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005071
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005072 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005073 return;
5074
Daniel Vetterc0b03412013-05-28 12:05:54 +02005075 /*
5076 * The panel fitter should only be adjusted whilst the pipe is disabled,
5077 * according to register description and PRM.
5078 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005079 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5080 assert_pipe_disabled(dev_priv, crtc->pipe);
5081
Jesse Barnesb074cec2013-04-25 12:55:02 -07005082 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5083 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005084
5085 /* Border color in case we don't scale up to the full screen. Black by
5086 * default, change to something else for debugging. */
5087 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005088}
5089
Dave Airlied05410f2014-06-05 13:22:59 +10005090static enum intel_display_power_domain port_to_power_domain(enum port port)
5091{
5092 switch (port) {
5093 case PORT_A:
5094 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5095 case PORT_B:
5096 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5097 case PORT_C:
5098 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5099 case PORT_D:
5100 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5101 default:
5102 WARN_ON_ONCE(1);
5103 return POWER_DOMAIN_PORT_OTHER;
5104 }
5105}
5106
Imre Deak77d22dc2014-03-05 16:20:52 +02005107#define for_each_power_domain(domain, mask) \
5108 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5109 if ((1 << (domain)) & (mask))
5110
Imre Deak319be8a2014-03-04 19:22:57 +02005111enum intel_display_power_domain
5112intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005113{
Imre Deak319be8a2014-03-04 19:22:57 +02005114 struct drm_device *dev = intel_encoder->base.dev;
5115 struct intel_digital_port *intel_dig_port;
5116
5117 switch (intel_encoder->type) {
5118 case INTEL_OUTPUT_UNKNOWN:
5119 /* Only DDI platforms should ever use this output type */
5120 WARN_ON_ONCE(!HAS_DDI(dev));
5121 case INTEL_OUTPUT_DISPLAYPORT:
5122 case INTEL_OUTPUT_HDMI:
5123 case INTEL_OUTPUT_EDP:
5124 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005125 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005126 case INTEL_OUTPUT_DP_MST:
5127 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5128 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005129 case INTEL_OUTPUT_ANALOG:
5130 return POWER_DOMAIN_PORT_CRT;
5131 case INTEL_OUTPUT_DSI:
5132 return POWER_DOMAIN_PORT_DSI;
5133 default:
5134 return POWER_DOMAIN_PORT_OTHER;
5135 }
5136}
5137
5138static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5139{
5140 struct drm_device *dev = crtc->dev;
5141 struct intel_encoder *intel_encoder;
5142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5143 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005144 unsigned long mask;
5145 enum transcoder transcoder;
5146
5147 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5148
5149 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5150 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005151 if (intel_crtc->config->pch_pfit.enabled ||
5152 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005153 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5154
Imre Deak319be8a2014-03-04 19:22:57 +02005155 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5156 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5157
Imre Deak77d22dc2014-03-05 16:20:52 +02005158 return mask;
5159}
5160
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005161static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005162{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005163 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005164 struct drm_i915_private *dev_priv = dev->dev_private;
5165 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5166 struct intel_crtc *crtc;
5167
5168 /*
5169 * First get all needed power domains, then put all unneeded, to avoid
5170 * any unnecessary toggling of the power wells.
5171 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005172 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005173 enum intel_display_power_domain domain;
5174
Matt Roper83d65732015-02-25 13:12:16 -08005175 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005176 continue;
5177
Imre Deak319be8a2014-03-04 19:22:57 +02005178 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005179
5180 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5181 intel_display_power_get(dev_priv, domain);
5182 }
5183
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005184 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005185 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005186
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005187 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005188 enum intel_display_power_domain domain;
5189
5190 for_each_power_domain(domain, crtc->enabled_power_domains)
5191 intel_display_power_put(dev_priv, domain);
5192
5193 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5194 }
5195
5196 intel_display_set_init_power(dev_priv, false);
5197}
5198
Ville Syrjälädfcab172014-06-13 13:37:47 +03005199/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005200static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005201{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005202 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005203
Jesse Barnes586f49d2013-11-04 16:06:59 -08005204 /* Obtain SKU information */
5205 mutex_lock(&dev_priv->dpio_lock);
5206 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5207 CCK_FUSE_HPLL_FREQ_MASK;
5208 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005209
Ville Syrjälädfcab172014-06-13 13:37:47 +03005210 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005211}
5212
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005213static void vlv_update_cdclk(struct drm_device *dev)
5214{
5215 struct drm_i915_private *dev_priv = dev->dev_private;
5216
5217 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03005218 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005219 dev_priv->vlv_cdclk_freq);
5220
5221 /*
5222 * Program the gmbus_freq based on the cdclk frequency.
5223 * BSpec erroneously claims we should aim for 4MHz, but
5224 * in fact 1MHz is the correct frequency.
5225 */
Ville Syrjälä6be1e3d2014-10-16 20:52:31 +03005226 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005227}
5228
Jesse Barnes30a970c2013-11-04 13:48:12 -08005229/* Adjust CDclk dividers to allow high res or save power if possible */
5230static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5231{
5232 struct drm_i915_private *dev_priv = dev->dev_private;
5233 u32 val, cmd;
5234
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005235 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005236
Ville Syrjälädfcab172014-06-13 13:37:47 +03005237 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005238 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005239 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005240 cmd = 1;
5241 else
5242 cmd = 0;
5243
5244 mutex_lock(&dev_priv->rps.hw_lock);
5245 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5246 val &= ~DSPFREQGUAR_MASK;
5247 val |= (cmd << DSPFREQGUAR_SHIFT);
5248 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5249 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5250 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5251 50)) {
5252 DRM_ERROR("timed out waiting for CDclk change\n");
5253 }
5254 mutex_unlock(&dev_priv->rps.hw_lock);
5255
Ville Syrjälädfcab172014-06-13 13:37:47 +03005256 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005257 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005258
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005259 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005260
5261 mutex_lock(&dev_priv->dpio_lock);
5262 /* adjust cdclk divider */
5263 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005264 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005265 val |= divider;
5266 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005267
5268 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5269 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5270 50))
5271 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005272 mutex_unlock(&dev_priv->dpio_lock);
5273 }
5274
5275 mutex_lock(&dev_priv->dpio_lock);
5276 /* adjust self-refresh exit latency value */
5277 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5278 val &= ~0x7f;
5279
5280 /*
5281 * For high bandwidth configs, we set a higher latency in the bunit
5282 * so that the core display fetch happens in time to avoid underruns.
5283 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005284 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005285 val |= 4500 / 250; /* 4.5 usec */
5286 else
5287 val |= 3000 / 250; /* 3.0 usec */
5288 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5289 mutex_unlock(&dev_priv->dpio_lock);
5290
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005291 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005292}
5293
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005294static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5295{
5296 struct drm_i915_private *dev_priv = dev->dev_private;
5297 u32 val, cmd;
5298
5299 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
5300
5301 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005302 case 333333:
5303 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005304 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005305 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005306 break;
5307 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005308 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005309 return;
5310 }
5311
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005312 /*
5313 * Specs are full of misinformation, but testing on actual
5314 * hardware has shown that we just need to write the desired
5315 * CCK divider into the Punit register.
5316 */
5317 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5318
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005319 mutex_lock(&dev_priv->rps.hw_lock);
5320 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5321 val &= ~DSPFREQGUAR_MASK_CHV;
5322 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5323 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5324 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5325 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5326 50)) {
5327 DRM_ERROR("timed out waiting for CDclk change\n");
5328 }
5329 mutex_unlock(&dev_priv->rps.hw_lock);
5330
5331 vlv_update_cdclk(dev);
5332}
5333
Jesse Barnes30a970c2013-11-04 13:48:12 -08005334static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5335 int max_pixclk)
5336{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005337 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005338 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005339
Jesse Barnes30a970c2013-11-04 13:48:12 -08005340 /*
5341 * Really only a few cases to deal with, as only 4 CDclks are supported:
5342 * 200MHz
5343 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005344 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005345 * 400MHz (VLV only)
5346 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5347 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005348 *
5349 * We seem to get an unstable or solid color picture at 200MHz.
5350 * Not sure what's wrong. For now use 200MHz only when all pipes
5351 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005352 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005353 if (!IS_CHERRYVIEW(dev_priv) &&
5354 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005355 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005356 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005357 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005358 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005359 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005360 else
5361 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005362}
5363
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005364/* compute the max pixel clock for new configuration */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005365static int intel_mode_max_pixclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005366{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005367 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005368 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005369 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005370 int max_pixclk = 0;
5371
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005372 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005373 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5374 if (IS_ERR(crtc_state))
5375 return PTR_ERR(crtc_state);
5376
5377 if (!crtc_state->base.enable)
5378 continue;
5379
5380 max_pixclk = max(max_pixclk,
5381 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005382 }
5383
5384 return max_pixclk;
5385}
5386
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005387static int valleyview_modeset_global_pipes(struct drm_atomic_state *state,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005388 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005389{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005390 struct drm_i915_private *dev_priv = to_i915(state->dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005391 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005392 int max_pixclk = intel_mode_max_pixclk(state);
5393
5394 if (max_pixclk < 0)
5395 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005396
Imre Deakd60c4472014-03-27 17:45:10 +02005397 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
5398 dev_priv->vlv_cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005399 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005400
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005401 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005402 for_each_intel_crtc(state->dev, intel_crtc)
Matt Roper83d65732015-02-25 13:12:16 -08005403 if (intel_crtc->base.state->enable)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005404 *prepare_pipes |= (1 << intel_crtc->pipe);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005405
5406 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005407}
5408
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005409static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5410{
5411 unsigned int credits, default_credits;
5412
5413 if (IS_CHERRYVIEW(dev_priv))
5414 default_credits = PFI_CREDIT(12);
5415 else
5416 default_credits = PFI_CREDIT(8);
5417
5418 if (DIV_ROUND_CLOSEST(dev_priv->vlv_cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
5419 /* CHV suggested value is 31 or 63 */
5420 if (IS_CHERRYVIEW(dev_priv))
5421 credits = PFI_CREDIT_31;
5422 else
5423 credits = PFI_CREDIT(15);
5424 } else {
5425 credits = default_credits;
5426 }
5427
5428 /*
5429 * WA - write default credits before re-programming
5430 * FIXME: should we also set the resend bit here?
5431 */
5432 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5433 default_credits);
5434
5435 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5436 credits | PFI_CREDIT_RESEND);
5437
5438 /*
5439 * FIXME is this guaranteed to clear
5440 * immediately or should we poll for it?
5441 */
5442 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5443}
5444
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005445static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005446{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005447 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005448 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005449 int max_pixclk = intel_mode_max_pixclk(state);
5450 int req_cdclk;
5451
5452 /* The only reason this can fail is if we fail to add the crtc_state
5453 * to the atomic state. But that can't happen since the call to
5454 * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
5455 * can't have failed otherwise the mode set would be aborted) added all
5456 * the states already. */
5457 if (WARN_ON(max_pixclk < 0))
5458 return;
5459
5460 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005461
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005462 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005463 /*
5464 * FIXME: We can end up here with all power domains off, yet
5465 * with a CDCLK frequency other than the minimum. To account
5466 * for this take the PIPE-A power domain, which covers the HW
5467 * blocks needed for the following programming. This can be
5468 * removed once it's guaranteed that we get here either with
5469 * the minimum CDCLK set, or the required power domains
5470 * enabled.
5471 */
5472 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5473
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005474 if (IS_CHERRYVIEW(dev))
5475 cherryview_set_cdclk(dev, req_cdclk);
5476 else
5477 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005478
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005479 vlv_program_pfi_credits(dev_priv);
5480
Imre Deak738c05c2014-11-19 16:25:37 +02005481 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005482 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005483}
5484
Jesse Barnes89b667f2013-04-18 14:51:36 -07005485static void valleyview_crtc_enable(struct drm_crtc *crtc)
5486{
5487 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005488 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005489 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5490 struct intel_encoder *encoder;
5491 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005492 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005493
Matt Roper83d65732015-02-25 13:12:16 -08005494 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005495
5496 if (intel_crtc->active)
5497 return;
5498
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005499 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305500
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005501 if (!is_dsi) {
5502 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005503 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005504 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005505 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005506 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005507
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005508 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305509 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005510
5511 intel_set_pipe_timings(intel_crtc);
5512
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005513 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5514 struct drm_i915_private *dev_priv = dev->dev_private;
5515
5516 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5517 I915_WRITE(CHV_CANVAS(pipe), 0);
5518 }
5519
Daniel Vetter5b18e572014-04-24 23:55:06 +02005520 i9xx_set_pipeconf(intel_crtc);
5521
Jesse Barnes89b667f2013-04-18 14:51:36 -07005522 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005523
Daniel Vettera72e4c92014-09-30 10:56:47 +02005524 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005525
Jesse Barnes89b667f2013-04-18 14:51:36 -07005526 for_each_encoder_on_crtc(dev, crtc, encoder)
5527 if (encoder->pre_pll_enable)
5528 encoder->pre_pll_enable(encoder);
5529
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005530 if (!is_dsi) {
5531 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005532 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005533 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005534 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005535 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005536
5537 for_each_encoder_on_crtc(dev, crtc, encoder)
5538 if (encoder->pre_enable)
5539 encoder->pre_enable(encoder);
5540
Jesse Barnes2dd24552013-04-25 12:55:01 -07005541 i9xx_pfit_enable(intel_crtc);
5542
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005543 intel_crtc_load_lut(crtc);
5544
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005545 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005546 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005547
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005548 assert_vblank_disabled(crtc);
5549 drm_crtc_vblank_on(crtc);
5550
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005551 for_each_encoder_on_crtc(dev, crtc, encoder)
5552 encoder->enable(encoder);
5553
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005554 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005555
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005556 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005557 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005558}
5559
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005560static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5561{
5562 struct drm_device *dev = crtc->base.dev;
5563 struct drm_i915_private *dev_priv = dev->dev_private;
5564
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005565 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5566 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005567}
5568
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005569static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005570{
5571 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005572 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005573 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005574 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005575 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005576
Matt Roper83d65732015-02-25 13:12:16 -08005577 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02005578
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005579 if (intel_crtc->active)
5580 return;
5581
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005582 i9xx_set_pll_dividers(intel_crtc);
5583
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005584 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305585 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005586
5587 intel_set_pipe_timings(intel_crtc);
5588
Daniel Vetter5b18e572014-04-24 23:55:06 +02005589 i9xx_set_pipeconf(intel_crtc);
5590
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005591 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005592
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005593 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005594 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005595
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005596 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005597 if (encoder->pre_enable)
5598 encoder->pre_enable(encoder);
5599
Daniel Vetterf6736a12013-06-05 13:34:30 +02005600 i9xx_enable_pll(intel_crtc);
5601
Jesse Barnes2dd24552013-04-25 12:55:01 -07005602 i9xx_pfit_enable(intel_crtc);
5603
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005604 intel_crtc_load_lut(crtc);
5605
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005606 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005607 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005608
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005609 assert_vblank_disabled(crtc);
5610 drm_crtc_vblank_on(crtc);
5611
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005612 for_each_encoder_on_crtc(dev, crtc, encoder)
5613 encoder->enable(encoder);
5614
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005615 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005616
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005617 /*
5618 * Gen2 reports pipe underruns whenever all planes are disabled.
5619 * So don't enable underrun reporting before at least some planes
5620 * are enabled.
5621 * FIXME: Need to fix the logic to work when we turn off all planes
5622 * but leave the pipe running.
5623 */
5624 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005625 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005626
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005627 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005628 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005629}
5630
Daniel Vetter87476d62013-04-11 16:29:06 +02005631static void i9xx_pfit_disable(struct intel_crtc *crtc)
5632{
5633 struct drm_device *dev = crtc->base.dev;
5634 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02005635
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005636 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005637 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02005638
5639 assert_pipe_disabled(dev_priv, crtc->pipe);
5640
Daniel Vetter328d8e82013-05-08 10:36:31 +02005641 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5642 I915_READ(PFIT_CONTROL));
5643 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02005644}
5645
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005646static void i9xx_crtc_disable(struct drm_crtc *crtc)
5647{
5648 struct drm_device *dev = crtc->dev;
5649 struct drm_i915_private *dev_priv = dev->dev_private;
5650 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005651 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005652 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005653
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005654 if (!intel_crtc->active)
5655 return;
5656
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005657 /*
5658 * Gen2 reports pipe underruns whenever all planes are disabled.
5659 * So diasble underrun reporting before all the planes get disabled.
5660 * FIXME: Need to fix the logic to work when we turn off all planes
5661 * but leave the pipe running.
5662 */
5663 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005664 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005665
Imre Deak564ed192014-06-13 14:54:21 +03005666 /*
5667 * Vblank time updates from the shadow to live plane control register
5668 * are blocked if the memory self-refresh mode is active at that
5669 * moment. So to make sure the plane gets truly disabled, disable
5670 * first the self-refresh mode. The self-refresh enable bit in turn
5671 * will be checked/applied by the HW only at the next frame start
5672 * event which is after the vblank start event, so we need to have a
5673 * wait-for-vblank between disabling the plane and the pipe.
5674 */
5675 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005676 intel_crtc_disable_planes(crtc);
5677
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005678 /*
5679 * On gen2 planes are double buffered but the pipe isn't, so we must
5680 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03005681 * We also need to wait on all gmch platforms because of the
5682 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005683 */
Imre Deak564ed192014-06-13 14:54:21 +03005684 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005685
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005686 for_each_encoder_on_crtc(dev, crtc, encoder)
5687 encoder->disable(encoder);
5688
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005689 drm_crtc_vblank_off(crtc);
5690 assert_vblank_disabled(crtc);
5691
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005692 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005693
Daniel Vetter87476d62013-04-11 16:29:06 +02005694 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005695
Jesse Barnes89b667f2013-04-18 14:51:36 -07005696 for_each_encoder_on_crtc(dev, crtc, encoder)
5697 if (encoder->post_disable)
5698 encoder->post_disable(encoder);
5699
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005700 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005701 if (IS_CHERRYVIEW(dev))
5702 chv_disable_pll(dev_priv, pipe);
5703 else if (IS_VALLEYVIEW(dev))
5704 vlv_disable_pll(dev_priv, pipe);
5705 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03005706 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005707 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005708
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005709 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005710 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005711
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005712 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005713 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005714
Daniel Vetterefa96242014-04-24 23:55:02 +02005715 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005716 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02005717 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005718}
5719
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005720static void i9xx_crtc_off(struct drm_crtc *crtc)
5721{
5722}
5723
Borun Fub04c5bd2014-07-12 10:02:27 +05305724/* Master function to enable/disable CRTC and corresponding power wells */
5725void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01005726{
Chris Wilsoncdd59982010-09-08 16:30:16 +01005727 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005728 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005729 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005730 enum intel_display_power_domain domain;
5731 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005732
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005733 if (enable) {
5734 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005735 domains = get_crtc_power_domains(crtc);
5736 for_each_power_domain(domain, domains)
5737 intel_display_power_get(dev_priv, domain);
5738 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005739
5740 dev_priv->display.crtc_enable(crtc);
5741 }
5742 } else {
5743 if (intel_crtc->active) {
5744 dev_priv->display.crtc_disable(crtc);
5745
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005746 domains = intel_crtc->enabled_power_domains;
5747 for_each_power_domain(domain, domains)
5748 intel_display_power_put(dev_priv, domain);
5749 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005750 }
5751 }
Borun Fub04c5bd2014-07-12 10:02:27 +05305752}
5753
5754/**
5755 * Sets the power management mode of the pipe and plane.
5756 */
5757void intel_crtc_update_dpms(struct drm_crtc *crtc)
5758{
5759 struct drm_device *dev = crtc->dev;
5760 struct intel_encoder *intel_encoder;
5761 bool enable = false;
5762
5763 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5764 enable |= intel_encoder->connectors_active;
5765
5766 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005767}
5768
Daniel Vetter976f8a22012-07-08 22:34:21 +02005769static void intel_crtc_disable(struct drm_crtc *crtc)
5770{
5771 struct drm_device *dev = crtc->dev;
5772 struct drm_connector *connector;
5773 struct drm_i915_private *dev_priv = dev->dev_private;
5774
5775 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08005776 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005777
5778 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005779 dev_priv->display.off(crtc);
5780
Matt Roper70a101f2015-04-08 18:56:53 -07005781 drm_plane_helper_disable(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005782
5783 /* Update computed state. */
5784 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5785 if (!connector->encoder || !connector->encoder->crtc)
5786 continue;
5787
5788 if (connector->encoder->crtc != crtc)
5789 continue;
5790
5791 connector->dpms = DRM_MODE_DPMS_OFF;
5792 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01005793 }
5794}
5795
Chris Wilsonea5b2132010-08-04 13:50:23 +01005796void intel_encoder_destroy(struct drm_encoder *encoder)
5797{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005798 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01005799
Chris Wilsonea5b2132010-08-04 13:50:23 +01005800 drm_encoder_cleanup(encoder);
5801 kfree(intel_encoder);
5802}
5803
Damien Lespiau92373292013-08-08 22:28:57 +01005804/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005805 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5806 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01005807static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005808{
5809 if (mode == DRM_MODE_DPMS_ON) {
5810 encoder->connectors_active = true;
5811
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005812 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005813 } else {
5814 encoder->connectors_active = false;
5815
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005816 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005817 }
5818}
5819
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005820/* Cross check the actual hw state with our own modeset state tracking (and it's
5821 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02005822static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005823{
5824 if (connector->get_hw_state(connector)) {
5825 struct intel_encoder *encoder = connector->encoder;
5826 struct drm_crtc *crtc;
5827 bool encoder_enabled;
5828 enum pipe pipe;
5829
5830 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5831 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03005832 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005833
Dave Airlie0e32b392014-05-02 14:02:48 +10005834 /* there is no real hw state for MST connectors */
5835 if (connector->mst_port)
5836 return;
5837
Rob Clarke2c719b2014-12-15 13:56:32 -05005838 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005839 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05005840 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005841 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005842
Dave Airlie36cd7442014-05-02 13:44:18 +10005843 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05005844 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10005845 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005846
Dave Airlie36cd7442014-05-02 13:44:18 +10005847 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05005848 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
5849 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10005850 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005851
Dave Airlie36cd7442014-05-02 13:44:18 +10005852 crtc = encoder->base.crtc;
5853
Matt Roper83d65732015-02-25 13:12:16 -08005854 I915_STATE_WARN(!crtc->state->enable,
5855 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05005856 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5857 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10005858 "encoder active on the wrong pipe\n");
5859 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005860 }
5861}
5862
Ander Conselvan de Oliveira9bdbd0b2015-04-10 10:59:10 +03005863int intel_connector_init(struct intel_connector *connector)
5864{
5865 struct drm_connector_state *connector_state;
5866
5867 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
5868 if (!connector_state)
5869 return -ENOMEM;
5870
5871 connector->base.state = connector_state;
5872 return 0;
5873}
5874
5875struct intel_connector *intel_connector_alloc(void)
5876{
5877 struct intel_connector *connector;
5878
5879 connector = kzalloc(sizeof *connector, GFP_KERNEL);
5880 if (!connector)
5881 return NULL;
5882
5883 if (intel_connector_init(connector) < 0) {
5884 kfree(connector);
5885 return NULL;
5886 }
5887
5888 return connector;
5889}
5890
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005891/* Even simpler default implementation, if there's really no special case to
5892 * consider. */
5893void intel_connector_dpms(struct drm_connector *connector, int mode)
5894{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005895 /* All the simple cases only support two dpms states. */
5896 if (mode != DRM_MODE_DPMS_ON)
5897 mode = DRM_MODE_DPMS_OFF;
5898
5899 if (mode == connector->dpms)
5900 return;
5901
5902 connector->dpms = mode;
5903
5904 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01005905 if (connector->encoder)
5906 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005907
Daniel Vetterb9805142012-08-31 17:37:33 +02005908 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005909}
5910
Daniel Vetterf0947c32012-07-02 13:10:34 +02005911/* Simple connector->get_hw_state implementation for encoders that support only
5912 * one connector and no cloning and hence the encoder state determines the state
5913 * of the connector. */
5914bool intel_connector_get_hw_state(struct intel_connector *connector)
5915{
Daniel Vetter24929352012-07-02 20:28:59 +02005916 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02005917 struct intel_encoder *encoder = connector->encoder;
5918
5919 return encoder->get_hw_state(encoder, &pipe);
5920}
5921
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005922static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02005923{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005924 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
5925 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02005926
5927 return 0;
5928}
5929
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005930static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005931 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005932{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005933 struct drm_atomic_state *state = pipe_config->base.state;
5934 struct intel_crtc *other_crtc;
5935 struct intel_crtc_state *other_crtc_state;
5936
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005937 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5938 pipe_name(pipe), pipe_config->fdi_lanes);
5939 if (pipe_config->fdi_lanes > 4) {
5940 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5941 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005942 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005943 }
5944
Paulo Zanonibafb6552013-11-02 21:07:44 -07005945 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005946 if (pipe_config->fdi_lanes > 2) {
5947 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5948 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005949 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005950 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005951 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005952 }
5953 }
5954
5955 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005956 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005957
5958 /* Ivybridge 3 pipe is really complicated */
5959 switch (pipe) {
5960 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005961 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005962 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005963 if (pipe_config->fdi_lanes <= 2)
5964 return 0;
5965
5966 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
5967 other_crtc_state =
5968 intel_atomic_get_crtc_state(state, other_crtc);
5969 if (IS_ERR(other_crtc_state))
5970 return PTR_ERR(other_crtc_state);
5971
5972 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005973 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5974 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005975 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005976 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005977 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005978 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02005979 if (pipe_config->fdi_lanes > 2) {
5980 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
5981 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005982 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02005983 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005984
5985 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
5986 other_crtc_state =
5987 intel_atomic_get_crtc_state(state, other_crtc);
5988 if (IS_ERR(other_crtc_state))
5989 return PTR_ERR(other_crtc_state);
5990
5991 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005992 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005993 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005994 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005995 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005996 default:
5997 BUG();
5998 }
5999}
6000
Daniel Vettere29c22c2013-02-21 00:00:16 +01006001#define RETRY 1
6002static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006003 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006004{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006005 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006006 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006007 int lane, link_bw, fdi_dotclock, ret;
6008 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006009
Daniel Vettere29c22c2013-02-21 00:00:16 +01006010retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006011 /* FDI is a binary signal running at ~2.7GHz, encoding
6012 * each output octet as 10 bits. The actual frequency
6013 * is stored as a divider into a 100MHz clock, and the
6014 * mode pixel clock is stored in units of 1KHz.
6015 * Hence the bw of each lane in terms of the mode signal
6016 * is:
6017 */
6018 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6019
Damien Lespiau241bfc32013-09-25 16:45:37 +01006020 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006021
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006022 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006023 pipe_config->pipe_bpp);
6024
6025 pipe_config->fdi_lanes = lane;
6026
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006027 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006028 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006029
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006030 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6031 intel_crtc->pipe, pipe_config);
6032 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006033 pipe_config->pipe_bpp -= 2*3;
6034 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6035 pipe_config->pipe_bpp);
6036 needs_recompute = true;
6037 pipe_config->bw_constrained = true;
6038
6039 goto retry;
6040 }
6041
6042 if (needs_recompute)
6043 return RETRY;
6044
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006045 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006046}
6047
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006048static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006049 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006050{
Jani Nikulad330a952014-01-21 11:24:25 +02006051 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03006052 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07006053 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006054}
6055
Daniel Vettera43f6e02013-06-07 23:10:32 +02006056static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006057 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006058{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006059 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006060 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006061 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006062 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006063
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006064 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006065 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006066 int clock_limit =
6067 dev_priv->display.get_display_clock_speed(dev);
6068
6069 /*
6070 * Enable pixel doubling when the dot clock
6071 * is > 90% of the (display) core speed.
6072 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006073 * GDG double wide on either pipe,
6074 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006075 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006076 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006077 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006078 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006079 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006080 }
6081
Damien Lespiau241bfc32013-09-25 16:45:37 +01006082 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006083 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006084 }
Chris Wilson89749352010-09-12 18:25:19 +01006085
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006086 /*
6087 * Pipe horizontal size must be even in:
6088 * - DVO ganged mode
6089 * - LVDS dual channel mode
6090 * - Double wide pipe
6091 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006092 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006093 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6094 pipe_config->pipe_src_w &= ~1;
6095
Damien Lespiau8693a822013-05-03 18:48:11 +01006096 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6097 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006098 */
6099 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6100 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006101 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006102
Daniel Vetterbd080ee2013-04-17 20:01:39 +02006103 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01006104 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02006105 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01006106 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
6107 * for lvds. */
6108 pipe_config->pipe_bpp = 8*3;
6109 }
6110
Damien Lespiauf5adf942013-06-24 18:29:34 +01006111 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006112 hsw_compute_ips_config(crtc, pipe_config);
6113
Daniel Vetter877d48d2013-04-19 11:24:43 +02006114 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006115 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006116
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006117 /* FIXME: remove below call once atomic mode set is place and all crtc
6118 * related checks called from atomic_crtc_check function */
6119 ret = 0;
6120 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6121 crtc, pipe_config->base.state);
6122 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6123
6124 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006125}
6126
Ville Syrjälä1652d192015-03-31 14:12:01 +03006127static int skylake_get_display_clock_speed(struct drm_device *dev)
6128{
6129 struct drm_i915_private *dev_priv = to_i915(dev);
6130 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6131 uint32_t cdctl = I915_READ(CDCLK_CTL);
6132 uint32_t linkrate;
6133
6134 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6135 WARN(1, "LCPLL1 not enabled\n");
6136 return 24000; /* 24MHz is the cd freq with NSSC ref */
6137 }
6138
6139 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6140 return 540000;
6141
6142 linkrate = (I915_READ(DPLL_CTRL1) &
6143 DPLL_CRTL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
6144
6145 if (linkrate == DPLL_CRTL1_LINK_RATE_2160 ||
6146 linkrate == DPLL_CRTL1_LINK_RATE_1080) {
6147 /* vco 8640 */
6148 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6149 case CDCLK_FREQ_450_432:
6150 return 432000;
6151 case CDCLK_FREQ_337_308:
6152 return 308570;
6153 case CDCLK_FREQ_675_617:
6154 return 617140;
6155 default:
6156 WARN(1, "Unknown cd freq selection\n");
6157 }
6158 } else {
6159 /* vco 8100 */
6160 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6161 case CDCLK_FREQ_450_432:
6162 return 450000;
6163 case CDCLK_FREQ_337_308:
6164 return 337500;
6165 case CDCLK_FREQ_675_617:
6166 return 675000;
6167 default:
6168 WARN(1, "Unknown cd freq selection\n");
6169 }
6170 }
6171
6172 /* error case, do as if DPLL0 isn't enabled */
6173 return 24000;
6174}
6175
6176static int broadwell_get_display_clock_speed(struct drm_device *dev)
6177{
6178 struct drm_i915_private *dev_priv = dev->dev_private;
6179 uint32_t lcpll = I915_READ(LCPLL_CTL);
6180 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6181
6182 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6183 return 800000;
6184 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6185 return 450000;
6186 else if (freq == LCPLL_CLK_FREQ_450)
6187 return 450000;
6188 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6189 return 540000;
6190 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6191 return 337500;
6192 else
6193 return 675000;
6194}
6195
6196static int haswell_get_display_clock_speed(struct drm_device *dev)
6197{
6198 struct drm_i915_private *dev_priv = dev->dev_private;
6199 uint32_t lcpll = I915_READ(LCPLL_CTL);
6200 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6201
6202 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6203 return 800000;
6204 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6205 return 450000;
6206 else if (freq == LCPLL_CLK_FREQ_450)
6207 return 450000;
6208 else if (IS_HSW_ULT(dev))
6209 return 337500;
6210 else
6211 return 540000;
6212}
6213
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006214static int valleyview_get_display_clock_speed(struct drm_device *dev)
6215{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006216 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006217 u32 val;
6218 int divider;
6219
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006220 if (dev_priv->hpll_freq == 0)
6221 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6222
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006223 mutex_lock(&dev_priv->dpio_lock);
6224 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6225 mutex_unlock(&dev_priv->dpio_lock);
6226
6227 divider = val & DISPLAY_FREQUENCY_VALUES;
6228
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006229 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6230 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6231 "cdclk change in progress\n");
6232
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006233 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006234}
6235
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006236static int ilk_get_display_clock_speed(struct drm_device *dev)
6237{
6238 return 450000;
6239}
6240
Jesse Barnese70236a2009-09-21 10:42:27 -07006241static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006242{
Jesse Barnese70236a2009-09-21 10:42:27 -07006243 return 400000;
6244}
Jesse Barnes79e53942008-11-07 14:24:08 -08006245
Jesse Barnese70236a2009-09-21 10:42:27 -07006246static int i915_get_display_clock_speed(struct drm_device *dev)
6247{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006248 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006249}
Jesse Barnes79e53942008-11-07 14:24:08 -08006250
Jesse Barnese70236a2009-09-21 10:42:27 -07006251static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6252{
6253 return 200000;
6254}
Jesse Barnes79e53942008-11-07 14:24:08 -08006255
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006256static int pnv_get_display_clock_speed(struct drm_device *dev)
6257{
6258 u16 gcfgc = 0;
6259
6260 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6261
6262 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6263 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006264 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006265 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006266 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006267 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006268 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006269 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6270 return 200000;
6271 default:
6272 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6273 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006274 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006275 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006276 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006277 }
6278}
6279
Jesse Barnese70236a2009-09-21 10:42:27 -07006280static int i915gm_get_display_clock_speed(struct drm_device *dev)
6281{
6282 u16 gcfgc = 0;
6283
6284 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6285
6286 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006287 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006288 else {
6289 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6290 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006291 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006292 default:
6293 case GC_DISPLAY_CLOCK_190_200_MHZ:
6294 return 190000;
6295 }
6296 }
6297}
Jesse Barnes79e53942008-11-07 14:24:08 -08006298
Jesse Barnese70236a2009-09-21 10:42:27 -07006299static int i865_get_display_clock_speed(struct drm_device *dev)
6300{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006301 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006302}
6303
6304static int i855_get_display_clock_speed(struct drm_device *dev)
6305{
6306 u16 hpllcc = 0;
6307 /* Assume that the hardware is in the high speed state. This
6308 * should be the default.
6309 */
6310 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6311 case GC_CLOCK_133_200:
6312 case GC_CLOCK_100_200:
6313 return 200000;
6314 case GC_CLOCK_166_250:
6315 return 250000;
6316 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006317 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006318 }
6319
6320 /* Shouldn't happen */
6321 return 0;
6322}
6323
6324static int i830_get_display_clock_speed(struct drm_device *dev)
6325{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006326 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006327}
6328
Zhenyu Wang2c072452009-06-05 15:38:42 +08006329static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006330intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006331{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006332 while (*num > DATA_LINK_M_N_MASK ||
6333 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006334 *num >>= 1;
6335 *den >>= 1;
6336 }
6337}
6338
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006339static void compute_m_n(unsigned int m, unsigned int n,
6340 uint32_t *ret_m, uint32_t *ret_n)
6341{
6342 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6343 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6344 intel_reduce_m_n_ratio(ret_m, ret_n);
6345}
6346
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006347void
6348intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6349 int pixel_clock, int link_clock,
6350 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006351{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006352 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006353
6354 compute_m_n(bits_per_pixel * pixel_clock,
6355 link_clock * nlanes * 8,
6356 &m_n->gmch_m, &m_n->gmch_n);
6357
6358 compute_m_n(pixel_clock, link_clock,
6359 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006360}
6361
Chris Wilsona7615032011-01-12 17:04:08 +00006362static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6363{
Jani Nikulad330a952014-01-21 11:24:25 +02006364 if (i915.panel_use_ssc >= 0)
6365 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006366 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006367 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006368}
6369
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006370static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6371 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006372{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006373 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006374 struct drm_i915_private *dev_priv = dev->dev_private;
6375 int refclk;
6376
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006377 WARN_ON(!crtc_state->base.state);
6378
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006379 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006380 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006381 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006382 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006383 refclk = dev_priv->vbt.lvds_ssc_freq;
6384 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006385 } else if (!IS_GEN2(dev)) {
6386 refclk = 96000;
6387 } else {
6388 refclk = 48000;
6389 }
6390
6391 return refclk;
6392}
6393
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006394static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006395{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006396 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006397}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006398
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006399static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6400{
6401 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006402}
6403
Daniel Vetterf47709a2013-03-28 10:42:02 +01006404static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006405 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08006406 intel_clock_t *reduced_clock)
6407{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006408 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006409 u32 fp, fp2 = 0;
6410
6411 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006412 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006413 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006414 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006415 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006416 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006417 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006418 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006419 }
6420
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006421 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006422
Daniel Vetterf47709a2013-03-28 10:42:02 +01006423 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006424 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006425 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006426 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006427 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006428 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006429 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006430 }
6431}
6432
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006433static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6434 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006435{
6436 u32 reg_val;
6437
6438 /*
6439 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6440 * and set it to a reasonable value instead.
6441 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006442 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006443 reg_val &= 0xffffff00;
6444 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006445 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006446
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006447 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006448 reg_val &= 0x8cffffff;
6449 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006450 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006451
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006452 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006453 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006454 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006455
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006456 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006457 reg_val &= 0x00ffffff;
6458 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006459 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006460}
6461
Daniel Vetterb5518422013-05-03 11:49:48 +02006462static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6463 struct intel_link_m_n *m_n)
6464{
6465 struct drm_device *dev = crtc->base.dev;
6466 struct drm_i915_private *dev_priv = dev->dev_private;
6467 int pipe = crtc->pipe;
6468
Daniel Vettere3b95f12013-05-03 11:49:49 +02006469 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6470 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6471 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6472 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006473}
6474
6475static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006476 struct intel_link_m_n *m_n,
6477 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006478{
6479 struct drm_device *dev = crtc->base.dev;
6480 struct drm_i915_private *dev_priv = dev->dev_private;
6481 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006482 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006483
6484 if (INTEL_INFO(dev)->gen >= 5) {
6485 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6486 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6487 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6488 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006489 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6490 * for gen < 8) and if DRRS is supported (to make sure the
6491 * registers are not unnecessarily accessed).
6492 */
Durgadoss R44395bf2015-02-13 15:33:02 +05306493 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006494 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006495 I915_WRITE(PIPE_DATA_M2(transcoder),
6496 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6497 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6498 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6499 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6500 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006501 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006502 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6503 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6504 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6505 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006506 }
6507}
6508
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306509void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006510{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306511 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6512
6513 if (m_n == M1_N1) {
6514 dp_m_n = &crtc->config->dp_m_n;
6515 dp_m2_n2 = &crtc->config->dp_m2_n2;
6516 } else if (m_n == M2_N2) {
6517
6518 /*
6519 * M2_N2 registers are not supported. Hence m2_n2 divider value
6520 * needs to be programmed into M1_N1.
6521 */
6522 dp_m_n = &crtc->config->dp_m2_n2;
6523 } else {
6524 DRM_ERROR("Unsupported divider value\n");
6525 return;
6526 }
6527
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006528 if (crtc->config->has_pch_encoder)
6529 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006530 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306531 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006532}
6533
Ville Syrjäläd288f652014-10-28 13:20:22 +02006534static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006535 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006536{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006537 u32 dpll, dpll_md;
6538
6539 /*
6540 * Enable DPIO clock input. We should never disable the reference
6541 * clock for pipe B, since VGA hotplug / manual detection depends
6542 * on it.
6543 */
6544 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6545 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6546 /* We should never disable this, set it here for state tracking */
6547 if (crtc->pipe == PIPE_B)
6548 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6549 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006550 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006551
Ville Syrjäläd288f652014-10-28 13:20:22 +02006552 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006553 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006554 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006555}
6556
Ville Syrjäläd288f652014-10-28 13:20:22 +02006557static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006558 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006559{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006560 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006561 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006562 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006563 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006564 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006565 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006566
Daniel Vetter09153002012-12-12 14:06:44 +01006567 mutex_lock(&dev_priv->dpio_lock);
6568
Ville Syrjäläd288f652014-10-28 13:20:22 +02006569 bestn = pipe_config->dpll.n;
6570 bestm1 = pipe_config->dpll.m1;
6571 bestm2 = pipe_config->dpll.m2;
6572 bestp1 = pipe_config->dpll.p1;
6573 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006574
Jesse Barnes89b667f2013-04-18 14:51:36 -07006575 /* See eDP HDMI DPIO driver vbios notes doc */
6576
6577 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006578 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006579 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006580
6581 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006582 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006583
6584 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006585 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006586 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006587 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006588
6589 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006590 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006591
6592 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006593 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6594 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6595 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006596 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006597
6598 /*
6599 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6600 * but we don't support that).
6601 * Note: don't use the DAC post divider as it seems unstable.
6602 */
6603 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006604 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006605
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006606 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006607 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006608
Jesse Barnes89b667f2013-04-18 14:51:36 -07006609 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006610 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006611 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6612 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006613 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03006614 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006615 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006616 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006617 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006618
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02006619 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006620 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006621 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006622 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006623 0x0df40000);
6624 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006625 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006626 0x0df70000);
6627 } else { /* HDMI or VGA */
6628 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006629 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006630 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006631 0x0df70000);
6632 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006633 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006634 0x0df40000);
6635 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006636
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006637 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006638 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006639 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6640 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006641 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006642 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006643
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006644 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01006645 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006646}
6647
Ville Syrjäläd288f652014-10-28 13:20:22 +02006648static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006649 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006650{
Ville Syrjäläd288f652014-10-28 13:20:22 +02006651 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006652 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6653 DPLL_VCO_ENABLE;
6654 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006655 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006656
Ville Syrjäläd288f652014-10-28 13:20:22 +02006657 pipe_config->dpll_hw_state.dpll_md =
6658 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006659}
6660
Ville Syrjäläd288f652014-10-28 13:20:22 +02006661static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006662 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006663{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006664 struct drm_device *dev = crtc->base.dev;
6665 struct drm_i915_private *dev_priv = dev->dev_private;
6666 int pipe = crtc->pipe;
6667 int dpll_reg = DPLL(crtc->pipe);
6668 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306669 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006670 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306671 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306672 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006673
Ville Syrjäläd288f652014-10-28 13:20:22 +02006674 bestn = pipe_config->dpll.n;
6675 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6676 bestm1 = pipe_config->dpll.m1;
6677 bestm2 = pipe_config->dpll.m2 >> 22;
6678 bestp1 = pipe_config->dpll.p1;
6679 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306680 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306681 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306682 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006683
6684 /*
6685 * Enable Refclk and SSC
6686 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006687 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02006688 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006689
6690 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006691
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006692 /* p1 and p2 divider */
6693 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6694 5 << DPIO_CHV_S1_DIV_SHIFT |
6695 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6696 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6697 1 << DPIO_CHV_K_DIV_SHIFT);
6698
6699 /* Feedback post-divider - m2 */
6700 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6701
6702 /* Feedback refclk divider - n and m1 */
6703 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6704 DPIO_CHV_M1_DIV_BY_2 |
6705 1 << DPIO_CHV_N_DIV_SHIFT);
6706
6707 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306708 if (bestm2_frac)
6709 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006710
6711 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306712 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6713 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6714 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6715 if (bestm2_frac)
6716 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6717 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006718
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05306719 /* Program digital lock detect threshold */
6720 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6721 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6722 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6723 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6724 if (!bestm2_frac)
6725 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6726 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6727
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006728 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306729 if (vco == 5400000) {
6730 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6731 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6732 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6733 tribuf_calcntr = 0x9;
6734 } else if (vco <= 6200000) {
6735 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6736 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6737 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6738 tribuf_calcntr = 0x9;
6739 } else if (vco <= 6480000) {
6740 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6741 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6742 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6743 tribuf_calcntr = 0x8;
6744 } else {
6745 /* Not supported. Apply the same limits as in the max case */
6746 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6747 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6748 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6749 tribuf_calcntr = 0;
6750 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006751 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6752
Ville Syrjälä968040b2015-03-11 22:52:08 +02006753 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306754 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6755 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6756 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6757
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006758 /* AFC Recal */
6759 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6760 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6761 DPIO_AFC_RECAL);
6762
6763 mutex_unlock(&dev_priv->dpio_lock);
6764}
6765
Ville Syrjäläd288f652014-10-28 13:20:22 +02006766/**
6767 * vlv_force_pll_on - forcibly enable just the PLL
6768 * @dev_priv: i915 private structure
6769 * @pipe: pipe PLL to enable
6770 * @dpll: PLL configuration
6771 *
6772 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6773 * in cases where we need the PLL enabled even when @pipe is not going to
6774 * be enabled.
6775 */
6776void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
6777 const struct dpll *dpll)
6778{
6779 struct intel_crtc *crtc =
6780 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006781 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006782 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02006783 .pixel_multiplier = 1,
6784 .dpll = *dpll,
6785 };
6786
6787 if (IS_CHERRYVIEW(dev)) {
6788 chv_update_pll(crtc, &pipe_config);
6789 chv_prepare_pll(crtc, &pipe_config);
6790 chv_enable_pll(crtc, &pipe_config);
6791 } else {
6792 vlv_update_pll(crtc, &pipe_config);
6793 vlv_prepare_pll(crtc, &pipe_config);
6794 vlv_enable_pll(crtc, &pipe_config);
6795 }
6796}
6797
6798/**
6799 * vlv_force_pll_off - forcibly disable just the PLL
6800 * @dev_priv: i915 private structure
6801 * @pipe: pipe PLL to disable
6802 *
6803 * Disable the PLL for @pipe. To be used in cases where we need
6804 * the PLL enabled even when @pipe is not going to be enabled.
6805 */
6806void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
6807{
6808 if (IS_CHERRYVIEW(dev))
6809 chv_disable_pll(to_i915(dev), pipe);
6810 else
6811 vlv_disable_pll(to_i915(dev), pipe);
6812}
6813
Daniel Vetterf47709a2013-03-28 10:42:02 +01006814static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006815 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01006816 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006817 int num_connectors)
6818{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006819 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006820 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006821 u32 dpll;
6822 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006823 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006824
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006825 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306826
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006827 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
6828 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006829
6830 dpll = DPLL_VGA_MODE_DIS;
6831
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006832 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006833 dpll |= DPLLB_MODE_LVDS;
6834 else
6835 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006836
Daniel Vetteref1b4602013-06-01 17:17:04 +02006837 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006838 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02006839 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006840 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02006841
6842 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006843 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006844
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006845 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006846 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006847
6848 /* compute bitmask from p1 value */
6849 if (IS_PINEVIEW(dev))
6850 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6851 else {
6852 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6853 if (IS_G4X(dev) && reduced_clock)
6854 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6855 }
6856 switch (clock->p2) {
6857 case 5:
6858 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6859 break;
6860 case 7:
6861 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6862 break;
6863 case 10:
6864 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6865 break;
6866 case 14:
6867 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6868 break;
6869 }
6870 if (INTEL_INFO(dev)->gen >= 4)
6871 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6872
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006873 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006874 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006875 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006876 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6877 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6878 else
6879 dpll |= PLL_REF_INPUT_DREFCLK;
6880
6881 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006882 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006883
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006884 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006885 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02006886 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006887 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006888 }
6889}
6890
Daniel Vetterf47709a2013-03-28 10:42:02 +01006891static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006892 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01006893 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006894 int num_connectors)
6895{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006896 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006897 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006898 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006899 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006900
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006901 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306902
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006903 dpll = DPLL_VGA_MODE_DIS;
6904
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006905 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006906 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6907 } else {
6908 if (clock->p1 == 2)
6909 dpll |= PLL_P1_DIVIDE_BY_TWO;
6910 else
6911 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6912 if (clock->p2 == 4)
6913 dpll |= PLL_P2_DIVIDE_BY_4;
6914 }
6915
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006916 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02006917 dpll |= DPLL_DVO_2X_MODE;
6918
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006919 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006920 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6921 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6922 else
6923 dpll |= PLL_REF_INPUT_DREFCLK;
6924
6925 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006926 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006927}
6928
Daniel Vetter8a654f32013-06-01 17:16:22 +02006929static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006930{
6931 struct drm_device *dev = intel_crtc->base.dev;
6932 struct drm_i915_private *dev_priv = dev->dev_private;
6933 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006934 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02006935 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006936 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006937 uint32_t crtc_vtotal, crtc_vblank_end;
6938 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006939
6940 /* We need to be careful not to changed the adjusted mode, for otherwise
6941 * the hw state checker will get angry at the mismatch. */
6942 crtc_vtotal = adjusted_mode->crtc_vtotal;
6943 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006944
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006945 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006946 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006947 crtc_vtotal -= 1;
6948 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006949
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006950 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006951 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6952 else
6953 vsyncshift = adjusted_mode->crtc_hsync_start -
6954 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006955 if (vsyncshift < 0)
6956 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006957 }
6958
6959 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006960 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006961
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006962 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006963 (adjusted_mode->crtc_hdisplay - 1) |
6964 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006965 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006966 (adjusted_mode->crtc_hblank_start - 1) |
6967 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006968 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006969 (adjusted_mode->crtc_hsync_start - 1) |
6970 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6971
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006972 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006973 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006974 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006975 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006976 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006977 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006978 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006979 (adjusted_mode->crtc_vsync_start - 1) |
6980 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6981
Paulo Zanonib5e508d2012-10-24 11:34:43 -02006982 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6983 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6984 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6985 * bits. */
6986 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6987 (pipe == PIPE_B || pipe == PIPE_C))
6988 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6989
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006990 /* pipesrc controls the size that is scaled from, which should
6991 * always be the user's requested size.
6992 */
6993 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006994 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6995 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006996}
6997
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006998static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006999 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007000{
7001 struct drm_device *dev = crtc->base.dev;
7002 struct drm_i915_private *dev_priv = dev->dev_private;
7003 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7004 uint32_t tmp;
7005
7006 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007007 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7008 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007009 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007010 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7011 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007012 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007013 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7014 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007015
7016 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007017 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7018 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007019 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007020 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7021 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007022 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007023 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7024 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007025
7026 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007027 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7028 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7029 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007030 }
7031
7032 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007033 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7034 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7035
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007036 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7037 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007038}
7039
Daniel Vetterf6a83282014-02-11 15:28:57 -08007040void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007041 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007042{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007043 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7044 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7045 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7046 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007047
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007048 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7049 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7050 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7051 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007052
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007053 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007054
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007055 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7056 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007057}
7058
Daniel Vetter84b046f2013-02-19 18:48:54 +01007059static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7060{
7061 struct drm_device *dev = intel_crtc->base.dev;
7062 struct drm_i915_private *dev_priv = dev->dev_private;
7063 uint32_t pipeconf;
7064
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007065 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007066
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007067 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7068 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7069 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007070
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007071 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007072 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007073
Daniel Vetterff9ce462013-04-24 14:57:17 +02007074 /* only g4x and later have fancy bpc/dither controls */
7075 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007076 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007077 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007078 pipeconf |= PIPECONF_DITHER_EN |
7079 PIPECONF_DITHER_TYPE_SP;
7080
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007081 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007082 case 18:
7083 pipeconf |= PIPECONF_6BPC;
7084 break;
7085 case 24:
7086 pipeconf |= PIPECONF_8BPC;
7087 break;
7088 case 30:
7089 pipeconf |= PIPECONF_10BPC;
7090 break;
7091 default:
7092 /* Case prevented by intel_choose_pipe_bpp_dither. */
7093 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007094 }
7095 }
7096
7097 if (HAS_PIPE_CXSR(dev)) {
7098 if (intel_crtc->lowfreq_avail) {
7099 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7100 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7101 } else {
7102 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007103 }
7104 }
7105
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007106 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007107 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007108 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007109 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7110 else
7111 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7112 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007113 pipeconf |= PIPECONF_PROGRESSIVE;
7114
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007115 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007116 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007117
Daniel Vetter84b046f2013-02-19 18:48:54 +01007118 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7119 POSTING_READ(PIPECONF(intel_crtc->pipe));
7120}
7121
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007122static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7123 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007124{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007125 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007126 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007127 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007128 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007129 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007130 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007131 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007132 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007133 struct drm_atomic_state *state = crtc_state->base.state;
7134 struct drm_connector_state *connector_state;
7135 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007136
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007137 for (i = 0; i < state->num_connector; i++) {
7138 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007139 continue;
7140
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007141 connector_state = state->connector_states[i];
7142 if (connector_state->crtc != &crtc->base)
7143 continue;
7144
7145 encoder = to_intel_encoder(connector_state->best_encoder);
7146
Chris Wilson5eddb702010-09-11 13:48:45 +01007147 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007148 case INTEL_OUTPUT_LVDS:
7149 is_lvds = true;
7150 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007151 case INTEL_OUTPUT_DSI:
7152 is_dsi = true;
7153 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007154 default:
7155 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007156 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007157
Eric Anholtc751ce42010-03-25 11:48:48 -07007158 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007159 }
7160
Jani Nikulaf2335332013-09-13 11:03:09 +03007161 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007162 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007163
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007164 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007165 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007166
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007167 /*
7168 * Returns a set of divisors for the desired target clock with
7169 * the given refclk, or FALSE. The returned values represent
7170 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7171 * 2) / p1 / p2.
7172 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007173 limit = intel_limit(crtc_state, refclk);
7174 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007175 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007176 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007177 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007178 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7179 return -EINVAL;
7180 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007181
Jani Nikulaf2335332013-09-13 11:03:09 +03007182 if (is_lvds && dev_priv->lvds_downclock_avail) {
7183 /*
7184 * Ensure we match the reduced clock's P to the target
7185 * clock. If the clocks don't match, we can't switch
7186 * the display clock by using the FP0/FP1. In such case
7187 * we will disable the LVDS downclock feature.
7188 */
7189 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007190 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007191 dev_priv->lvds_downclock,
7192 refclk, &clock,
7193 &reduced_clock);
7194 }
7195 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007196 crtc_state->dpll.n = clock.n;
7197 crtc_state->dpll.m1 = clock.m1;
7198 crtc_state->dpll.m2 = clock.m2;
7199 crtc_state->dpll.p1 = clock.p1;
7200 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007201 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007202
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007203 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007204 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307205 has_reduced_clock ? &reduced_clock : NULL,
7206 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007207 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007208 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007209 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007210 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007211 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007212 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007213 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007214 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007215 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007216
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007217 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007218}
7219
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007220static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007221 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007222{
7223 struct drm_device *dev = crtc->base.dev;
7224 struct drm_i915_private *dev_priv = dev->dev_private;
7225 uint32_t tmp;
7226
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007227 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7228 return;
7229
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007230 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007231 if (!(tmp & PFIT_ENABLE))
7232 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007233
Daniel Vetter06922822013-07-11 13:35:40 +02007234 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007235 if (INTEL_INFO(dev)->gen < 4) {
7236 if (crtc->pipe != PIPE_B)
7237 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007238 } else {
7239 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7240 return;
7241 }
7242
Daniel Vetter06922822013-07-11 13:35:40 +02007243 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007244 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7245 if (INTEL_INFO(dev)->gen < 5)
7246 pipe_config->gmch_pfit.lvds_border_bits =
7247 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7248}
7249
Jesse Barnesacbec812013-09-20 11:29:32 -07007250static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007251 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007252{
7253 struct drm_device *dev = crtc->base.dev;
7254 struct drm_i915_private *dev_priv = dev->dev_private;
7255 int pipe = pipe_config->cpu_transcoder;
7256 intel_clock_t clock;
7257 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007258 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007259
Shobhit Kumarf573de52014-07-30 20:32:37 +05307260 /* In case of MIPI DPLL will not even be used */
7261 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7262 return;
7263
Jesse Barnesacbec812013-09-20 11:29:32 -07007264 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007265 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07007266 mutex_unlock(&dev_priv->dpio_lock);
7267
7268 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7269 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7270 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7271 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7272 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7273
Ville Syrjäläf6466282013-10-14 14:50:31 +03007274 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007275
Ville Syrjäläf6466282013-10-14 14:50:31 +03007276 /* clock.dot is the fast clock */
7277 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007278}
7279
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007280static void
7281i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7282 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007283{
7284 struct drm_device *dev = crtc->base.dev;
7285 struct drm_i915_private *dev_priv = dev->dev_private;
7286 u32 val, base, offset;
7287 int pipe = crtc->pipe, plane = crtc->plane;
7288 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007289 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007290 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007291 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007292
Damien Lespiau42a7b082015-02-05 19:35:13 +00007293 val = I915_READ(DSPCNTR(plane));
7294 if (!(val & DISPLAY_PLANE_ENABLE))
7295 return;
7296
Damien Lespiaud9806c92015-01-21 14:07:19 +00007297 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007298 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007299 DRM_DEBUG_KMS("failed to alloc fb\n");
7300 return;
7301 }
7302
Damien Lespiau1b842c82015-01-21 13:50:54 +00007303 fb = &intel_fb->base;
7304
Daniel Vetter18c52472015-02-10 17:16:09 +00007305 if (INTEL_INFO(dev)->gen >= 4) {
7306 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007307 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007308 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7309 }
7310 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007311
7312 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007313 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007314 fb->pixel_format = fourcc;
7315 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007316
7317 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007318 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007319 offset = I915_READ(DSPTILEOFF(plane));
7320 else
7321 offset = I915_READ(DSPLINOFF(plane));
7322 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7323 } else {
7324 base = I915_READ(DSPADDR(plane));
7325 }
7326 plane_config->base = base;
7327
7328 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007329 fb->width = ((val >> 16) & 0xfff) + 1;
7330 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007331
7332 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007333 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007334
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007335 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007336 fb->pixel_format,
7337 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007338
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007339 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007340
Damien Lespiau2844a922015-01-20 12:51:48 +00007341 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7342 pipe_name(pipe), plane, fb->width, fb->height,
7343 fb->bits_per_pixel, base, fb->pitches[0],
7344 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007345
Damien Lespiau2d140302015-02-05 17:22:18 +00007346 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007347}
7348
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007349static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007350 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007351{
7352 struct drm_device *dev = crtc->base.dev;
7353 struct drm_i915_private *dev_priv = dev->dev_private;
7354 int pipe = pipe_config->cpu_transcoder;
7355 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7356 intel_clock_t clock;
7357 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7358 int refclk = 100000;
7359
7360 mutex_lock(&dev_priv->dpio_lock);
7361 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7362 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7363 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7364 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7365 mutex_unlock(&dev_priv->dpio_lock);
7366
7367 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7368 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7369 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7370 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7371 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7372
7373 chv_clock(refclk, &clock);
7374
7375 /* clock.dot is the fast clock */
7376 pipe_config->port_clock = clock.dot / 5;
7377}
7378
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007379static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007380 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007381{
7382 struct drm_device *dev = crtc->base.dev;
7383 struct drm_i915_private *dev_priv = dev->dev_private;
7384 uint32_t tmp;
7385
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007386 if (!intel_display_power_is_enabled(dev_priv,
7387 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007388 return false;
7389
Daniel Vettere143a212013-07-04 12:01:15 +02007390 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007391 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007392
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007393 tmp = I915_READ(PIPECONF(crtc->pipe));
7394 if (!(tmp & PIPECONF_ENABLE))
7395 return false;
7396
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007397 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7398 switch (tmp & PIPECONF_BPC_MASK) {
7399 case PIPECONF_6BPC:
7400 pipe_config->pipe_bpp = 18;
7401 break;
7402 case PIPECONF_8BPC:
7403 pipe_config->pipe_bpp = 24;
7404 break;
7405 case PIPECONF_10BPC:
7406 pipe_config->pipe_bpp = 30;
7407 break;
7408 default:
7409 break;
7410 }
7411 }
7412
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007413 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7414 pipe_config->limited_color_range = true;
7415
Ville Syrjälä282740f2013-09-04 18:30:03 +03007416 if (INTEL_INFO(dev)->gen < 4)
7417 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7418
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007419 intel_get_pipe_timings(crtc, pipe_config);
7420
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007421 i9xx_get_pfit_config(crtc, pipe_config);
7422
Daniel Vetter6c49f242013-06-06 12:45:25 +02007423 if (INTEL_INFO(dev)->gen >= 4) {
7424 tmp = I915_READ(DPLL_MD(crtc->pipe));
7425 pipe_config->pixel_multiplier =
7426 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7427 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007428 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02007429 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7430 tmp = I915_READ(DPLL(crtc->pipe));
7431 pipe_config->pixel_multiplier =
7432 ((tmp & SDVO_MULTIPLIER_MASK)
7433 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7434 } else {
7435 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7436 * port and will be fixed up in the encoder->get_config
7437 * function. */
7438 pipe_config->pixel_multiplier = 1;
7439 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007440 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7441 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007442 /*
7443 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7444 * on 830. Filter it out here so that we don't
7445 * report errors due to that.
7446 */
7447 if (IS_I830(dev))
7448 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7449
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007450 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7451 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007452 } else {
7453 /* Mask out read-only status bits. */
7454 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7455 DPLL_PORTC_READY_MASK |
7456 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007457 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007458
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007459 if (IS_CHERRYVIEW(dev))
7460 chv_crtc_clock_get(crtc, pipe_config);
7461 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07007462 vlv_crtc_clock_get(crtc, pipe_config);
7463 else
7464 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007465
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007466 return true;
7467}
7468
Paulo Zanonidde86e22012-12-01 12:04:25 -02007469static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007470{
7471 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007472 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007473 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007474 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007475 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007476 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007477 bool has_ck505 = false;
7478 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007479
7480 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01007481 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007482 switch (encoder->type) {
7483 case INTEL_OUTPUT_LVDS:
7484 has_panel = true;
7485 has_lvds = true;
7486 break;
7487 case INTEL_OUTPUT_EDP:
7488 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03007489 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007490 has_cpu_edp = true;
7491 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007492 default:
7493 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007494 }
7495 }
7496
Keith Packard99eb6a02011-09-26 14:29:12 -07007497 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007498 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007499 can_ssc = has_ck505;
7500 } else {
7501 has_ck505 = false;
7502 can_ssc = true;
7503 }
7504
Imre Deak2de69052013-05-08 13:14:04 +03007505 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7506 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007507
7508 /* Ironlake: try to setup display ref clock before DPLL
7509 * enabling. This is only under driver's control after
7510 * PCH B stepping, previous chipset stepping should be
7511 * ignoring this setting.
7512 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007513 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007514
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007515 /* As we must carefully and slowly disable/enable each source in turn,
7516 * compute the final state we want first and check if we need to
7517 * make any changes at all.
7518 */
7519 final = val;
7520 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007521 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007522 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007523 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007524 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7525
7526 final &= ~DREF_SSC_SOURCE_MASK;
7527 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7528 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007529
Keith Packard199e5d72011-09-22 12:01:57 -07007530 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007531 final |= DREF_SSC_SOURCE_ENABLE;
7532
7533 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7534 final |= DREF_SSC1_ENABLE;
7535
7536 if (has_cpu_edp) {
7537 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7538 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7539 else
7540 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7541 } else
7542 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7543 } else {
7544 final |= DREF_SSC_SOURCE_DISABLE;
7545 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7546 }
7547
7548 if (final == val)
7549 return;
7550
7551 /* Always enable nonspread source */
7552 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7553
7554 if (has_ck505)
7555 val |= DREF_NONSPREAD_CK505_ENABLE;
7556 else
7557 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7558
7559 if (has_panel) {
7560 val &= ~DREF_SSC_SOURCE_MASK;
7561 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007562
Keith Packard199e5d72011-09-22 12:01:57 -07007563 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007564 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007565 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007566 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007567 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007568 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007569
7570 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007571 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007572 POSTING_READ(PCH_DREF_CONTROL);
7573 udelay(200);
7574
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007575 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007576
7577 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007578 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007579 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007580 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007581 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007582 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007583 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007584 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007585 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007586
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007587 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007588 POSTING_READ(PCH_DREF_CONTROL);
7589 udelay(200);
7590 } else {
7591 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7592
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007593 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007594
7595 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007596 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007597
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007598 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007599 POSTING_READ(PCH_DREF_CONTROL);
7600 udelay(200);
7601
7602 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007603 val &= ~DREF_SSC_SOURCE_MASK;
7604 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007605
7606 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007607 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007608
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007609 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007610 POSTING_READ(PCH_DREF_CONTROL);
7611 udelay(200);
7612 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007613
7614 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007615}
7616
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007617static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007618{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007619 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007620
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007621 tmp = I915_READ(SOUTH_CHICKEN2);
7622 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7623 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007624
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007625 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7626 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7627 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007628
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007629 tmp = I915_READ(SOUTH_CHICKEN2);
7630 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7631 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007632
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007633 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7634 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7635 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007636}
7637
7638/* WaMPhyProgramming:hsw */
7639static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7640{
7641 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007642
7643 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7644 tmp &= ~(0xFF << 24);
7645 tmp |= (0x12 << 24);
7646 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7647
Paulo Zanonidde86e22012-12-01 12:04:25 -02007648 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7649 tmp |= (1 << 11);
7650 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7651
7652 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7653 tmp |= (1 << 11);
7654 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7655
Paulo Zanonidde86e22012-12-01 12:04:25 -02007656 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7657 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7658 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7659
7660 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7661 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7662 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7663
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007664 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7665 tmp &= ~(7 << 13);
7666 tmp |= (5 << 13);
7667 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007668
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007669 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7670 tmp &= ~(7 << 13);
7671 tmp |= (5 << 13);
7672 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007673
7674 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7675 tmp &= ~0xFF;
7676 tmp |= 0x1C;
7677 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7678
7679 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7680 tmp &= ~0xFF;
7681 tmp |= 0x1C;
7682 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7683
7684 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7685 tmp &= ~(0xFF << 16);
7686 tmp |= (0x1C << 16);
7687 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7688
7689 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7690 tmp &= ~(0xFF << 16);
7691 tmp |= (0x1C << 16);
7692 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7693
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007694 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7695 tmp |= (1 << 27);
7696 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007697
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007698 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7699 tmp |= (1 << 27);
7700 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007701
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007702 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7703 tmp &= ~(0xF << 28);
7704 tmp |= (4 << 28);
7705 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007706
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007707 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7708 tmp &= ~(0xF << 28);
7709 tmp |= (4 << 28);
7710 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007711}
7712
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007713/* Implements 3 different sequences from BSpec chapter "Display iCLK
7714 * Programming" based on the parameters passed:
7715 * - Sequence to enable CLKOUT_DP
7716 * - Sequence to enable CLKOUT_DP without spread
7717 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7718 */
7719static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
7720 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007721{
7722 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007723 uint32_t reg, tmp;
7724
7725 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7726 with_spread = true;
7727 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
7728 with_fdi, "LP PCH doesn't have FDI\n"))
7729 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007730
7731 mutex_lock(&dev_priv->dpio_lock);
7732
7733 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7734 tmp &= ~SBI_SSCCTL_DISABLE;
7735 tmp |= SBI_SSCCTL_PATHALT;
7736 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7737
7738 udelay(24);
7739
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007740 if (with_spread) {
7741 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7742 tmp &= ~SBI_SSCCTL_PATHALT;
7743 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007744
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007745 if (with_fdi) {
7746 lpt_reset_fdi_mphy(dev_priv);
7747 lpt_program_fdi_mphy(dev_priv);
7748 }
7749 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02007750
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007751 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7752 SBI_GEN0 : SBI_DBUFF0;
7753 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7754 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7755 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01007756
7757 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007758}
7759
Paulo Zanoni47701c32013-07-23 11:19:25 -03007760/* Sequence to disable CLKOUT_DP */
7761static void lpt_disable_clkout_dp(struct drm_device *dev)
7762{
7763 struct drm_i915_private *dev_priv = dev->dev_private;
7764 uint32_t reg, tmp;
7765
7766 mutex_lock(&dev_priv->dpio_lock);
7767
7768 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7769 SBI_GEN0 : SBI_DBUFF0;
7770 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7771 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7772 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7773
7774 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7775 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7776 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7777 tmp |= SBI_SSCCTL_PATHALT;
7778 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7779 udelay(32);
7780 }
7781 tmp |= SBI_SSCCTL_DISABLE;
7782 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7783 }
7784
7785 mutex_unlock(&dev_priv->dpio_lock);
7786}
7787
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007788static void lpt_init_pch_refclk(struct drm_device *dev)
7789{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007790 struct intel_encoder *encoder;
7791 bool has_vga = false;
7792
Damien Lespiaub2784e12014-08-05 11:29:37 +01007793 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007794 switch (encoder->type) {
7795 case INTEL_OUTPUT_ANALOG:
7796 has_vga = true;
7797 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007798 default:
7799 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007800 }
7801 }
7802
Paulo Zanoni47701c32013-07-23 11:19:25 -03007803 if (has_vga)
7804 lpt_enable_clkout_dp(dev, true, true);
7805 else
7806 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007807}
7808
Paulo Zanonidde86e22012-12-01 12:04:25 -02007809/*
7810 * Initialize reference clocks when the driver loads
7811 */
7812void intel_init_pch_refclk(struct drm_device *dev)
7813{
7814 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7815 ironlake_init_pch_refclk(dev);
7816 else if (HAS_PCH_LPT(dev))
7817 lpt_init_pch_refclk(dev);
7818}
7819
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007820static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007821{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007822 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007823 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007824 struct drm_atomic_state *state = crtc_state->base.state;
7825 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007826 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007827 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007828 bool is_lvds = false;
7829
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007830 for (i = 0; i < state->num_connector; i++) {
7831 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007832 continue;
7833
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007834 connector_state = state->connector_states[i];
7835 if (connector_state->crtc != crtc_state->base.crtc)
7836 continue;
7837
7838 encoder = to_intel_encoder(connector_state->best_encoder);
7839
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007840 switch (encoder->type) {
7841 case INTEL_OUTPUT_LVDS:
7842 is_lvds = true;
7843 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007844 default:
7845 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007846 }
7847 num_connectors++;
7848 }
7849
7850 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007851 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007852 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007853 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007854 }
7855
7856 return 120000;
7857}
7858
Daniel Vetter6ff93602013-04-19 11:24:36 +02007859static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03007860{
7861 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
7862 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7863 int pipe = intel_crtc->pipe;
7864 uint32_t val;
7865
Daniel Vetter78114072013-06-13 00:54:57 +02007866 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03007867
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007868 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03007869 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007870 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007871 break;
7872 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007873 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007874 break;
7875 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007876 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007877 break;
7878 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007879 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007880 break;
7881 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03007882 /* Case prevented by intel_choose_pipe_bpp_dither. */
7883 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03007884 }
7885
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007886 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03007887 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7888
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007889 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03007890 val |= PIPECONF_INTERLACED_ILK;
7891 else
7892 val |= PIPECONF_PROGRESSIVE;
7893
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007894 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007895 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007896
Paulo Zanonic8203562012-09-12 10:06:29 -03007897 I915_WRITE(PIPECONF(pipe), val);
7898 POSTING_READ(PIPECONF(pipe));
7899}
7900
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007901/*
7902 * Set up the pipe CSC unit.
7903 *
7904 * Currently only full range RGB to limited range RGB conversion
7905 * is supported, but eventually this should handle various
7906 * RGB<->YCbCr scenarios as well.
7907 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01007908static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007909{
7910 struct drm_device *dev = crtc->dev;
7911 struct drm_i915_private *dev_priv = dev->dev_private;
7912 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7913 int pipe = intel_crtc->pipe;
7914 uint16_t coeff = 0x7800; /* 1.0 */
7915
7916 /*
7917 * TODO: Check what kind of values actually come out of the pipe
7918 * with these coeff/postoff values and adjust to get the best
7919 * accuracy. Perhaps we even need to take the bpc value into
7920 * consideration.
7921 */
7922
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007923 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007924 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
7925
7926 /*
7927 * GY/GU and RY/RU should be the other way around according
7928 * to BSpec, but reality doesn't agree. Just set them up in
7929 * a way that results in the correct picture.
7930 */
7931 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
7932 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
7933
7934 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
7935 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
7936
7937 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
7938 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
7939
7940 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
7941 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
7942 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
7943
7944 if (INTEL_INFO(dev)->gen > 6) {
7945 uint16_t postoff = 0;
7946
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007947 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02007948 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007949
7950 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
7951 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
7952 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
7953
7954 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
7955 } else {
7956 uint32_t mode = CSC_MODE_YUV_TO_RGB;
7957
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007958 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007959 mode |= CSC_BLACK_SCREEN_OFFSET;
7960
7961 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
7962 }
7963}
7964
Daniel Vetter6ff93602013-04-19 11:24:36 +02007965static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007966{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007967 struct drm_device *dev = crtc->dev;
7968 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007969 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007970 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007971 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007972 uint32_t val;
7973
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007974 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007975
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007976 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007977 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7978
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007979 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007980 val |= PIPECONF_INTERLACED_ILK;
7981 else
7982 val |= PIPECONF_PROGRESSIVE;
7983
Paulo Zanoni702e7a52012-10-23 18:29:59 -02007984 I915_WRITE(PIPECONF(cpu_transcoder), val);
7985 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007986
7987 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
7988 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007989
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05307990 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007991 val = 0;
7992
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007993 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007994 case 18:
7995 val |= PIPEMISC_DITHER_6_BPC;
7996 break;
7997 case 24:
7998 val |= PIPEMISC_DITHER_8_BPC;
7999 break;
8000 case 30:
8001 val |= PIPEMISC_DITHER_10_BPC;
8002 break;
8003 case 36:
8004 val |= PIPEMISC_DITHER_12_BPC;
8005 break;
8006 default:
8007 /* Case prevented by pipe_config_set_bpp. */
8008 BUG();
8009 }
8010
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008011 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008012 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8013
8014 I915_WRITE(PIPEMISC(pipe), val);
8015 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008016}
8017
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008018static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008019 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008020 intel_clock_t *clock,
8021 bool *has_reduced_clock,
8022 intel_clock_t *reduced_clock)
8023{
8024 struct drm_device *dev = crtc->dev;
8025 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008026 int refclk;
8027 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008028 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008029
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008030 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008031
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008032 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008033
8034 /*
8035 * Returns a set of divisors for the desired target clock with the given
8036 * refclk, or FALSE. The returned values represent the clock equation:
8037 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8038 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008039 limit = intel_limit(crtc_state, refclk);
8040 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008041 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008042 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008043 if (!ret)
8044 return false;
8045
8046 if (is_lvds && dev_priv->lvds_downclock_avail) {
8047 /*
8048 * Ensure we match the reduced clock's P to the target clock.
8049 * If the clocks don't match, we can't switch the display clock
8050 * by using the FP0/FP1. In such case we will disable the LVDS
8051 * downclock feature.
8052 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008053 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008054 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008055 dev_priv->lvds_downclock,
8056 refclk, clock,
8057 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008058 }
8059
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008060 return true;
8061}
8062
Paulo Zanonid4b19312012-11-29 11:29:32 -02008063int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8064{
8065 /*
8066 * Account for spread spectrum to avoid
8067 * oversubscribing the link. Max center spread
8068 * is 2.5%; use 5% for safety's sake.
8069 */
8070 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008071 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008072}
8073
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008074static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008075{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008076 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008077}
8078
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008079static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008080 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008081 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008082 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008083{
8084 struct drm_crtc *crtc = &intel_crtc->base;
8085 struct drm_device *dev = crtc->dev;
8086 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008087 struct drm_atomic_state *state = crtc_state->base.state;
8088 struct drm_connector_state *connector_state;
8089 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008090 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008091 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008092 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008093
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008094 for (i = 0; i < state->num_connector; i++) {
8095 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02008096 continue;
8097
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008098 connector_state = state->connector_states[i];
8099 if (connector_state->crtc != crtc_state->base.crtc)
8100 continue;
8101
8102 encoder = to_intel_encoder(connector_state->best_encoder);
8103
8104 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008105 case INTEL_OUTPUT_LVDS:
8106 is_lvds = true;
8107 break;
8108 case INTEL_OUTPUT_SDVO:
8109 case INTEL_OUTPUT_HDMI:
8110 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008111 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008112 default:
8113 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008114 }
8115
8116 num_connectors++;
8117 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008118
Chris Wilsonc1858122010-12-03 21:35:48 +00008119 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008120 factor = 21;
8121 if (is_lvds) {
8122 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008123 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008124 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008125 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008126 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008127 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008128
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008129 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008130 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008131
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008132 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8133 *fp2 |= FP_CB_TUNE;
8134
Chris Wilson5eddb702010-09-11 13:48:45 +01008135 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008136
Eric Anholta07d6782011-03-30 13:01:08 -07008137 if (is_lvds)
8138 dpll |= DPLLB_MODE_LVDS;
8139 else
8140 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008141
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008142 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008143 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008144
8145 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008146 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008147 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008148 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008149
Eric Anholta07d6782011-03-30 13:01:08 -07008150 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008151 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008152 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008153 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008154
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008155 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008156 case 5:
8157 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8158 break;
8159 case 7:
8160 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8161 break;
8162 case 10:
8163 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8164 break;
8165 case 14:
8166 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8167 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008168 }
8169
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008170 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008171 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008172 else
8173 dpll |= PLL_REF_INPUT_DREFCLK;
8174
Daniel Vetter959e16d2013-06-05 13:34:21 +02008175 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008176}
8177
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008178static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8179 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008180{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008181 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008182 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008183 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008184 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008185 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008186 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008187
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008188 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008189
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008190 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8191 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8192
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008193 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008194 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008195 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008196 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8197 return -EINVAL;
8198 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008199 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008200 if (!crtc_state->clock_set) {
8201 crtc_state->dpll.n = clock.n;
8202 crtc_state->dpll.m1 = clock.m1;
8203 crtc_state->dpll.m2 = clock.m2;
8204 crtc_state->dpll.p1 = clock.p1;
8205 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008206 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008207
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008208 /* 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 +02008209 if (crtc_state->has_pch_encoder) {
8210 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008211 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008212 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008213
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008214 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008215 &fp, &reduced_clock,
8216 has_reduced_clock ? &fp2 : NULL);
8217
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008218 crtc_state->dpll_hw_state.dpll = dpll;
8219 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008220 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008221 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008222 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008223 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008224
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008225 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008226 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008227 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008228 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008229 return -EINVAL;
8230 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008231 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008232
Rodrigo Viviab585de2015-03-24 12:40:09 -07008233 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008234 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008235 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008236 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008237
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008238 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008239}
8240
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008241static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8242 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008243{
8244 struct drm_device *dev = crtc->base.dev;
8245 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008246 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008247
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008248 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8249 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8250 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8251 & ~TU_SIZE_MASK;
8252 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8253 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8254 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8255}
8256
8257static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8258 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008259 struct intel_link_m_n *m_n,
8260 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008261{
8262 struct drm_device *dev = crtc->base.dev;
8263 struct drm_i915_private *dev_priv = dev->dev_private;
8264 enum pipe pipe = crtc->pipe;
8265
8266 if (INTEL_INFO(dev)->gen >= 5) {
8267 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8268 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8269 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8270 & ~TU_SIZE_MASK;
8271 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8272 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8273 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008274 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8275 * gen < 8) and if DRRS is supported (to make sure the
8276 * registers are not unnecessarily read).
8277 */
8278 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008279 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008280 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8281 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8282 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8283 & ~TU_SIZE_MASK;
8284 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8285 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8286 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8287 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008288 } else {
8289 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8290 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8291 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8292 & ~TU_SIZE_MASK;
8293 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8294 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8295 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8296 }
8297}
8298
8299void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008300 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008301{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008302 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008303 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8304 else
8305 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008306 &pipe_config->dp_m_n,
8307 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008308}
8309
Daniel Vetter72419202013-04-04 13:28:53 +02008310static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008311 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008312{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008313 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008314 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008315}
8316
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008317static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008318 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008319{
8320 struct drm_device *dev = crtc->base.dev;
8321 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008322 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8323 uint32_t ps_ctrl = 0;
8324 int id = -1;
8325 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008326
Chandra Kondurua1b22782015-04-07 15:28:45 -07008327 /* find scaler attached to this pipe */
8328 for (i = 0; i < crtc->num_scalers; i++) {
8329 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8330 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8331 id = i;
8332 pipe_config->pch_pfit.enabled = true;
8333 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8334 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8335 break;
8336 }
8337 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008338
Chandra Kondurua1b22782015-04-07 15:28:45 -07008339 scaler_state->scaler_id = id;
8340 if (id >= 0) {
8341 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8342 } else {
8343 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008344 }
8345}
8346
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008347static void
8348skylake_get_initial_plane_config(struct intel_crtc *crtc,
8349 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008350{
8351 struct drm_device *dev = crtc->base.dev;
8352 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008353 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008354 int pipe = crtc->pipe;
8355 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008356 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008357 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008358 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008359
Damien Lespiaud9806c92015-01-21 14:07:19 +00008360 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008361 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008362 DRM_DEBUG_KMS("failed to alloc fb\n");
8363 return;
8364 }
8365
Damien Lespiau1b842c82015-01-21 13:50:54 +00008366 fb = &intel_fb->base;
8367
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008368 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008369 if (!(val & PLANE_CTL_ENABLE))
8370 goto error;
8371
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008372 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8373 fourcc = skl_format_to_fourcc(pixel_format,
8374 val & PLANE_CTL_ORDER_RGBX,
8375 val & PLANE_CTL_ALPHA_MASK);
8376 fb->pixel_format = fourcc;
8377 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8378
Damien Lespiau40f46282015-02-27 11:15:21 +00008379 tiling = val & PLANE_CTL_TILED_MASK;
8380 switch (tiling) {
8381 case PLANE_CTL_TILED_LINEAR:
8382 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8383 break;
8384 case PLANE_CTL_TILED_X:
8385 plane_config->tiling = I915_TILING_X;
8386 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8387 break;
8388 case PLANE_CTL_TILED_Y:
8389 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8390 break;
8391 case PLANE_CTL_TILED_YF:
8392 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8393 break;
8394 default:
8395 MISSING_CASE(tiling);
8396 goto error;
8397 }
8398
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008399 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8400 plane_config->base = base;
8401
8402 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8403
8404 val = I915_READ(PLANE_SIZE(pipe, 0));
8405 fb->height = ((val >> 16) & 0xfff) + 1;
8406 fb->width = ((val >> 0) & 0x1fff) + 1;
8407
8408 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00008409 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8410 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008411 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8412
8413 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008414 fb->pixel_format,
8415 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008416
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008417 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008418
8419 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8420 pipe_name(pipe), fb->width, fb->height,
8421 fb->bits_per_pixel, base, fb->pitches[0],
8422 plane_config->size);
8423
Damien Lespiau2d140302015-02-05 17:22:18 +00008424 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008425 return;
8426
8427error:
8428 kfree(fb);
8429}
8430
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008431static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008432 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008433{
8434 struct drm_device *dev = crtc->base.dev;
8435 struct drm_i915_private *dev_priv = dev->dev_private;
8436 uint32_t tmp;
8437
8438 tmp = I915_READ(PF_CTL(crtc->pipe));
8439
8440 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008441 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008442 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8443 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008444
8445 /* We currently do not free assignements of panel fitters on
8446 * ivb/hsw (since we don't use the higher upscaling modes which
8447 * differentiates them) so just WARN about this case for now. */
8448 if (IS_GEN7(dev)) {
8449 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8450 PF_PIPE_SEL_IVB(crtc->pipe));
8451 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008452 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008453}
8454
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008455static void
8456ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8457 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008458{
8459 struct drm_device *dev = crtc->base.dev;
8460 struct drm_i915_private *dev_priv = dev->dev_private;
8461 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008462 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008463 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008464 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008465 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008466 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008467
Damien Lespiau42a7b082015-02-05 19:35:13 +00008468 val = I915_READ(DSPCNTR(pipe));
8469 if (!(val & DISPLAY_PLANE_ENABLE))
8470 return;
8471
Damien Lespiaud9806c92015-01-21 14:07:19 +00008472 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008473 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008474 DRM_DEBUG_KMS("failed to alloc fb\n");
8475 return;
8476 }
8477
Damien Lespiau1b842c82015-01-21 13:50:54 +00008478 fb = &intel_fb->base;
8479
Daniel Vetter18c52472015-02-10 17:16:09 +00008480 if (INTEL_INFO(dev)->gen >= 4) {
8481 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008482 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008483 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8484 }
8485 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008486
8487 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008488 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008489 fb->pixel_format = fourcc;
8490 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008491
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008492 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008493 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008494 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008495 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00008496 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008497 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008498 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008499 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008500 }
8501 plane_config->base = base;
8502
8503 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008504 fb->width = ((val >> 16) & 0xfff) + 1;
8505 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008506
8507 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008508 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008509
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008510 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008511 fb->pixel_format,
8512 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008513
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008514 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008515
Damien Lespiau2844a922015-01-20 12:51:48 +00008516 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8517 pipe_name(pipe), fb->width, fb->height,
8518 fb->bits_per_pixel, base, fb->pitches[0],
8519 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008520
Damien Lespiau2d140302015-02-05 17:22:18 +00008521 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008522}
8523
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008524static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008525 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008526{
8527 struct drm_device *dev = crtc->base.dev;
8528 struct drm_i915_private *dev_priv = dev->dev_private;
8529 uint32_t tmp;
8530
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008531 if (!intel_display_power_is_enabled(dev_priv,
8532 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008533 return false;
8534
Daniel Vettere143a212013-07-04 12:01:15 +02008535 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008536 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008537
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008538 tmp = I915_READ(PIPECONF(crtc->pipe));
8539 if (!(tmp & PIPECONF_ENABLE))
8540 return false;
8541
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008542 switch (tmp & PIPECONF_BPC_MASK) {
8543 case PIPECONF_6BPC:
8544 pipe_config->pipe_bpp = 18;
8545 break;
8546 case PIPECONF_8BPC:
8547 pipe_config->pipe_bpp = 24;
8548 break;
8549 case PIPECONF_10BPC:
8550 pipe_config->pipe_bpp = 30;
8551 break;
8552 case PIPECONF_12BPC:
8553 pipe_config->pipe_bpp = 36;
8554 break;
8555 default:
8556 break;
8557 }
8558
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008559 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8560 pipe_config->limited_color_range = true;
8561
Daniel Vetterab9412b2013-05-03 11:49:46 +02008562 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008563 struct intel_shared_dpll *pll;
8564
Daniel Vetter88adfff2013-03-28 10:42:01 +01008565 pipe_config->has_pch_encoder = true;
8566
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008567 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8568 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8569 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008570
8571 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008572
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008573 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02008574 pipe_config->shared_dpll =
8575 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008576 } else {
8577 tmp = I915_READ(PCH_DPLL_SEL);
8578 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8579 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8580 else
8581 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8582 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008583
8584 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8585
8586 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8587 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008588
8589 tmp = pipe_config->dpll_hw_state.dpll;
8590 pipe_config->pixel_multiplier =
8591 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8592 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008593
8594 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008595 } else {
8596 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008597 }
8598
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008599 intel_get_pipe_timings(crtc, pipe_config);
8600
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008601 ironlake_get_pfit_config(crtc, pipe_config);
8602
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008603 return true;
8604}
8605
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008606static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8607{
8608 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008609 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008610
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008611 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008612 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008613 pipe_name(crtc->pipe));
8614
Rob Clarke2c719b2014-12-15 13:56:32 -05008615 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8616 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8617 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8618 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8619 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8620 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008621 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03008622 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05008623 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008624 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008625 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008626 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008627 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008628 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008629 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008630
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008631 /*
8632 * In theory we can still leave IRQs enabled, as long as only the HPD
8633 * interrupts remain enabled. We used to check for that, but since it's
8634 * gen-specific and since we only disable LCPLL after we fully disable
8635 * the interrupts, the check below should be enough.
8636 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008637 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008638}
8639
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008640static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8641{
8642 struct drm_device *dev = dev_priv->dev;
8643
8644 if (IS_HASWELL(dev))
8645 return I915_READ(D_COMP_HSW);
8646 else
8647 return I915_READ(D_COMP_BDW);
8648}
8649
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008650static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8651{
8652 struct drm_device *dev = dev_priv->dev;
8653
8654 if (IS_HASWELL(dev)) {
8655 mutex_lock(&dev_priv->rps.hw_lock);
8656 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8657 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03008658 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008659 mutex_unlock(&dev_priv->rps.hw_lock);
8660 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008661 I915_WRITE(D_COMP_BDW, val);
8662 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008663 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008664}
8665
8666/*
8667 * This function implements pieces of two sequences from BSpec:
8668 * - Sequence for display software to disable LCPLL
8669 * - Sequence for display software to allow package C8+
8670 * The steps implemented here are just the steps that actually touch the LCPLL
8671 * register. Callers should take care of disabling all the display engine
8672 * functions, doing the mode unset, fixing interrupts, etc.
8673 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008674static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8675 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008676{
8677 uint32_t val;
8678
8679 assert_can_disable_lcpll(dev_priv);
8680
8681 val = I915_READ(LCPLL_CTL);
8682
8683 if (switch_to_fclk) {
8684 val |= LCPLL_CD_SOURCE_FCLK;
8685 I915_WRITE(LCPLL_CTL, val);
8686
8687 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
8688 LCPLL_CD_SOURCE_FCLK_DONE, 1))
8689 DRM_ERROR("Switching to FCLK failed\n");
8690
8691 val = I915_READ(LCPLL_CTL);
8692 }
8693
8694 val |= LCPLL_PLL_DISABLE;
8695 I915_WRITE(LCPLL_CTL, val);
8696 POSTING_READ(LCPLL_CTL);
8697
8698 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
8699 DRM_ERROR("LCPLL still locked\n");
8700
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008701 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008702 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008703 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008704 ndelay(100);
8705
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008706 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8707 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008708 DRM_ERROR("D_COMP RCOMP still in progress\n");
8709
8710 if (allow_power_down) {
8711 val = I915_READ(LCPLL_CTL);
8712 val |= LCPLL_POWER_DOWN_ALLOW;
8713 I915_WRITE(LCPLL_CTL, val);
8714 POSTING_READ(LCPLL_CTL);
8715 }
8716}
8717
8718/*
8719 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8720 * source.
8721 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008722static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008723{
8724 uint32_t val;
8725
8726 val = I915_READ(LCPLL_CTL);
8727
8728 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8729 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8730 return;
8731
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008732 /*
8733 * Make sure we're not on PC8 state before disabling PC8, otherwise
8734 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008735 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02008736 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03008737
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008738 if (val & LCPLL_POWER_DOWN_ALLOW) {
8739 val &= ~LCPLL_POWER_DOWN_ALLOW;
8740 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02008741 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008742 }
8743
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008744 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008745 val |= D_COMP_COMP_FORCE;
8746 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008747 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008748
8749 val = I915_READ(LCPLL_CTL);
8750 val &= ~LCPLL_PLL_DISABLE;
8751 I915_WRITE(LCPLL_CTL, val);
8752
8753 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
8754 DRM_ERROR("LCPLL not locked yet\n");
8755
8756 if (val & LCPLL_CD_SOURCE_FCLK) {
8757 val = I915_READ(LCPLL_CTL);
8758 val &= ~LCPLL_CD_SOURCE_FCLK;
8759 I915_WRITE(LCPLL_CTL, val);
8760
8761 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
8762 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
8763 DRM_ERROR("Switching back to LCPLL failed\n");
8764 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03008765
Mika Kuoppala59bad942015-01-16 11:34:40 +02008766 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008767}
8768
Paulo Zanoni765dab672014-03-07 20:08:18 -03008769/*
8770 * Package states C8 and deeper are really deep PC states that can only be
8771 * reached when all the devices on the system allow it, so even if the graphics
8772 * device allows PC8+, it doesn't mean the system will actually get to these
8773 * states. Our driver only allows PC8+ when going into runtime PM.
8774 *
8775 * The requirements for PC8+ are that all the outputs are disabled, the power
8776 * well is disabled and most interrupts are disabled, and these are also
8777 * requirements for runtime PM. When these conditions are met, we manually do
8778 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8779 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8780 * hang the machine.
8781 *
8782 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8783 * the state of some registers, so when we come back from PC8+ we need to
8784 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8785 * need to take care of the registers kept by RC6. Notice that this happens even
8786 * if we don't put the device in PCI D3 state (which is what currently happens
8787 * because of the runtime PM support).
8788 *
8789 * For more, read "Display Sequences for Package C8" on the hardware
8790 * documentation.
8791 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008792void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008793{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008794 struct drm_device *dev = dev_priv->dev;
8795 uint32_t val;
8796
Paulo Zanonic67a4702013-08-19 13:18:09 -03008797 DRM_DEBUG_KMS("Enabling package C8+\n");
8798
Paulo Zanonic67a4702013-08-19 13:18:09 -03008799 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8800 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8801 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8802 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8803 }
8804
8805 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008806 hsw_disable_lcpll(dev_priv, true, true);
8807}
8808
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008809void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008810{
8811 struct drm_device *dev = dev_priv->dev;
8812 uint32_t val;
8813
Paulo Zanonic67a4702013-08-19 13:18:09 -03008814 DRM_DEBUG_KMS("Disabling package C8+\n");
8815
8816 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008817 lpt_init_pch_refclk(dev);
8818
8819 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8820 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8821 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8822 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8823 }
8824
8825 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008826}
8827
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008828static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8829 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008830{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008831 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008832 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03008833
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008834 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02008835
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008836 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008837}
8838
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008839static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8840 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008841 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008842{
Damien Lespiau3148ade2014-11-21 16:14:56 +00008843 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008844
8845 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
8846 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
8847
8848 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00008849 case SKL_DPLL0:
8850 /*
8851 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
8852 * of the shared DPLL framework and thus needs to be read out
8853 * separately
8854 */
8855 dpll_ctl1 = I915_READ(DPLL_CTRL1);
8856 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
8857 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008858 case SKL_DPLL1:
8859 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
8860 break;
8861 case SKL_DPLL2:
8862 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
8863 break;
8864 case SKL_DPLL3:
8865 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
8866 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008867 }
8868}
8869
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008870static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8871 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008872 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008873{
8874 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8875
8876 switch (pipe_config->ddi_pll_sel) {
8877 case PORT_CLK_SEL_WRPLL1:
8878 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
8879 break;
8880 case PORT_CLK_SEL_WRPLL2:
8881 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
8882 break;
8883 }
8884}
8885
Daniel Vetter26804af2014-06-25 22:01:55 +03008886static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008887 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03008888{
8889 struct drm_device *dev = crtc->base.dev;
8890 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03008891 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03008892 enum port port;
8893 uint32_t tmp;
8894
8895 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
8896
8897 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
8898
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008899 if (IS_SKYLAKE(dev))
8900 skylake_get_ddi_pll(dev_priv, port, pipe_config);
8901 else
8902 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03008903
Daniel Vetterd452c5b2014-07-04 11:27:39 -03008904 if (pipe_config->shared_dpll >= 0) {
8905 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8906
8907 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8908 &pipe_config->dpll_hw_state));
8909 }
8910
Daniel Vetter26804af2014-06-25 22:01:55 +03008911 /*
8912 * Haswell has only FDI/PCH transcoder A. It is which is connected to
8913 * DDI E. So just check whether this pipe is wired to DDI E and whether
8914 * the PCH transcoder is on.
8915 */
Damien Lespiauca370452013-12-03 13:56:24 +00008916 if (INTEL_INFO(dev)->gen < 9 &&
8917 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03008918 pipe_config->has_pch_encoder = true;
8919
8920 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
8921 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8922 FDI_DP_PORT_WIDTH_SHIFT) + 1;
8923
8924 ironlake_get_fdi_m_n_config(crtc, pipe_config);
8925 }
8926}
8927
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008928static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008929 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008930{
8931 struct drm_device *dev = crtc->base.dev;
8932 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008933 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008934 uint32_t tmp;
8935
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008936 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02008937 POWER_DOMAIN_PIPE(crtc->pipe)))
8938 return false;
8939
Daniel Vettere143a212013-07-04 12:01:15 +02008940 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008941 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
8942
Daniel Vettereccb1402013-05-22 00:50:22 +02008943 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8944 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8945 enum pipe trans_edp_pipe;
8946 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8947 default:
8948 WARN(1, "unknown pipe linked to edp transcoder\n");
8949 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8950 case TRANS_DDI_EDP_INPUT_A_ON:
8951 trans_edp_pipe = PIPE_A;
8952 break;
8953 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8954 trans_edp_pipe = PIPE_B;
8955 break;
8956 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8957 trans_edp_pipe = PIPE_C;
8958 break;
8959 }
8960
8961 if (trans_edp_pipe == crtc->pipe)
8962 pipe_config->cpu_transcoder = TRANSCODER_EDP;
8963 }
8964
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008965 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02008966 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03008967 return false;
8968
Daniel Vettereccb1402013-05-22 00:50:22 +02008969 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008970 if (!(tmp & PIPECONF_ENABLE))
8971 return false;
8972
Daniel Vetter26804af2014-06-25 22:01:55 +03008973 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008974
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008975 intel_get_pipe_timings(crtc, pipe_config);
8976
Chandra Kondurua1b22782015-04-07 15:28:45 -07008977 if (INTEL_INFO(dev)->gen >= 9) {
8978 skl_init_scalers(dev, crtc, pipe_config);
8979 }
8980
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008981 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008982 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
8983 if (IS_SKYLAKE(dev))
8984 skylake_get_pfit_config(crtc, pipe_config);
8985 else
8986 ironlake_get_pfit_config(crtc, pipe_config);
Chandra Kondurua1b22782015-04-07 15:28:45 -07008987 } else {
8988 pipe_config->scaler_state.scaler_id = -1;
8989 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008990 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01008991
Jesse Barnese59150d2014-01-07 13:30:45 -08008992 if (IS_HASWELL(dev))
8993 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
8994 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008995
Clint Taylorebb69c92014-09-30 10:30:22 -07008996 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
8997 pipe_config->pixel_multiplier =
8998 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
8999 } else {
9000 pipe_config->pixel_multiplier = 1;
9001 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009002
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009003 return true;
9004}
9005
Chris Wilson560b85b2010-08-07 11:01:38 +01009006static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9007{
9008 struct drm_device *dev = crtc->dev;
9009 struct drm_i915_private *dev_priv = dev->dev_private;
9010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009011 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009012
Ville Syrjälädc41c152014-08-13 11:57:05 +03009013 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009014 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9015 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009016 unsigned int stride = roundup_pow_of_two(width) * 4;
9017
9018 switch (stride) {
9019 default:
9020 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9021 width, stride);
9022 stride = 256;
9023 /* fallthrough */
9024 case 256:
9025 case 512:
9026 case 1024:
9027 case 2048:
9028 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009029 }
9030
Ville Syrjälädc41c152014-08-13 11:57:05 +03009031 cntl |= CURSOR_ENABLE |
9032 CURSOR_GAMMA_ENABLE |
9033 CURSOR_FORMAT_ARGB |
9034 CURSOR_STRIDE(stride);
9035
9036 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009037 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009038
Ville Syrjälädc41c152014-08-13 11:57:05 +03009039 if (intel_crtc->cursor_cntl != 0 &&
9040 (intel_crtc->cursor_base != base ||
9041 intel_crtc->cursor_size != size ||
9042 intel_crtc->cursor_cntl != cntl)) {
9043 /* On these chipsets we can only modify the base/size/stride
9044 * whilst the cursor is disabled.
9045 */
9046 I915_WRITE(_CURACNTR, 0);
9047 POSTING_READ(_CURACNTR);
9048 intel_crtc->cursor_cntl = 0;
9049 }
9050
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009051 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009052 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009053 intel_crtc->cursor_base = base;
9054 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009055
9056 if (intel_crtc->cursor_size != size) {
9057 I915_WRITE(CURSIZE, size);
9058 intel_crtc->cursor_size = size;
9059 }
9060
Chris Wilson4b0e3332014-05-30 16:35:26 +03009061 if (intel_crtc->cursor_cntl != cntl) {
9062 I915_WRITE(_CURACNTR, cntl);
9063 POSTING_READ(_CURACNTR);
9064 intel_crtc->cursor_cntl = cntl;
9065 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009066}
9067
9068static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9069{
9070 struct drm_device *dev = crtc->dev;
9071 struct drm_i915_private *dev_priv = dev->dev_private;
9072 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9073 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009074 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009075
Chris Wilson4b0e3332014-05-30 16:35:26 +03009076 cntl = 0;
9077 if (base) {
9078 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009079 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309080 case 64:
9081 cntl |= CURSOR_MODE_64_ARGB_AX;
9082 break;
9083 case 128:
9084 cntl |= CURSOR_MODE_128_ARGB_AX;
9085 break;
9086 case 256:
9087 cntl |= CURSOR_MODE_256_ARGB_AX;
9088 break;
9089 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009090 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309091 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009092 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009093 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009094
9095 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9096 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009097 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009098
Matt Roper8e7d6882015-01-21 16:35:41 -08009099 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009100 cntl |= CURSOR_ROTATE_180;
9101
Chris Wilson4b0e3332014-05-30 16:35:26 +03009102 if (intel_crtc->cursor_cntl != cntl) {
9103 I915_WRITE(CURCNTR(pipe), cntl);
9104 POSTING_READ(CURCNTR(pipe));
9105 intel_crtc->cursor_cntl = cntl;
9106 }
9107
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009108 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009109 I915_WRITE(CURBASE(pipe), base);
9110 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009111
9112 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009113}
9114
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009115/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009116static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9117 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009118{
9119 struct drm_device *dev = crtc->dev;
9120 struct drm_i915_private *dev_priv = dev->dev_private;
9121 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9122 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009123 int x = crtc->cursor_x;
9124 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009125 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009126
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009127 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009128 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009129
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009130 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009131 base = 0;
9132
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009133 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009134 base = 0;
9135
9136 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009137 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009138 base = 0;
9139
9140 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9141 x = -x;
9142 }
9143 pos |= x << CURSOR_X_SHIFT;
9144
9145 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009146 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009147 base = 0;
9148
9149 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9150 y = -y;
9151 }
9152 pos |= y << CURSOR_Y_SHIFT;
9153
Chris Wilson4b0e3332014-05-30 16:35:26 +03009154 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009155 return;
9156
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009157 I915_WRITE(CURPOS(pipe), pos);
9158
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009159 /* ILK+ do this automagically */
9160 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009161 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009162 base += (intel_crtc->base.cursor->state->crtc_h *
9163 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009164 }
9165
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009166 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009167 i845_update_cursor(crtc, base);
9168 else
9169 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009170}
9171
Ville Syrjälädc41c152014-08-13 11:57:05 +03009172static bool cursor_size_ok(struct drm_device *dev,
9173 uint32_t width, uint32_t height)
9174{
9175 if (width == 0 || height == 0)
9176 return false;
9177
9178 /*
9179 * 845g/865g are special in that they are only limited by
9180 * the width of their cursors, the height is arbitrary up to
9181 * the precision of the register. Everything else requires
9182 * square cursors, limited to a few power-of-two sizes.
9183 */
9184 if (IS_845G(dev) || IS_I865G(dev)) {
9185 if ((width & 63) != 0)
9186 return false;
9187
9188 if (width > (IS_845G(dev) ? 64 : 512))
9189 return false;
9190
9191 if (height > 1023)
9192 return false;
9193 } else {
9194 switch (width | height) {
9195 case 256:
9196 case 128:
9197 if (IS_GEN2(dev))
9198 return false;
9199 case 64:
9200 break;
9201 default:
9202 return false;
9203 }
9204 }
9205
9206 return true;
9207}
9208
Jesse Barnes79e53942008-11-07 14:24:08 -08009209static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01009210 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08009211{
James Simmons72034252010-08-03 01:33:19 +01009212 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08009213 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08009214
James Simmons72034252010-08-03 01:33:19 +01009215 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009216 intel_crtc->lut_r[i] = red[i] >> 8;
9217 intel_crtc->lut_g[i] = green[i] >> 8;
9218 intel_crtc->lut_b[i] = blue[i] >> 8;
9219 }
9220
9221 intel_crtc_load_lut(crtc);
9222}
9223
Jesse Barnes79e53942008-11-07 14:24:08 -08009224/* VESA 640x480x72Hz mode to set on the pipe */
9225static struct drm_display_mode load_detect_mode = {
9226 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9227 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9228};
9229
Daniel Vettera8bb6812014-02-10 18:00:39 +01009230struct drm_framebuffer *
9231__intel_framebuffer_create(struct drm_device *dev,
9232 struct drm_mode_fb_cmd2 *mode_cmd,
9233 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01009234{
9235 struct intel_framebuffer *intel_fb;
9236 int ret;
9237
9238 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9239 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009240 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01009241 return ERR_PTR(-ENOMEM);
9242 }
9243
9244 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009245 if (ret)
9246 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009247
9248 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009249err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009250 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009251 kfree(intel_fb);
9252
9253 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009254}
9255
Daniel Vetterb5ea6422014-03-02 21:18:00 +01009256static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01009257intel_framebuffer_create(struct drm_device *dev,
9258 struct drm_mode_fb_cmd2 *mode_cmd,
9259 struct drm_i915_gem_object *obj)
9260{
9261 struct drm_framebuffer *fb;
9262 int ret;
9263
9264 ret = i915_mutex_lock_interruptible(dev);
9265 if (ret)
9266 return ERR_PTR(ret);
9267 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9268 mutex_unlock(&dev->struct_mutex);
9269
9270 return fb;
9271}
9272
Chris Wilsond2dff872011-04-19 08:36:26 +01009273static u32
9274intel_framebuffer_pitch_for_width(int width, int bpp)
9275{
9276 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9277 return ALIGN(pitch, 64);
9278}
9279
9280static u32
9281intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9282{
9283 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009284 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009285}
9286
9287static struct drm_framebuffer *
9288intel_framebuffer_create_for_mode(struct drm_device *dev,
9289 struct drm_display_mode *mode,
9290 int depth, int bpp)
9291{
9292 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009293 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009294
9295 obj = i915_gem_alloc_object(dev,
9296 intel_framebuffer_size_for_mode(mode, bpp));
9297 if (obj == NULL)
9298 return ERR_PTR(-ENOMEM);
9299
9300 mode_cmd.width = mode->hdisplay;
9301 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009302 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9303 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009304 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009305
9306 return intel_framebuffer_create(dev, &mode_cmd, obj);
9307}
9308
9309static struct drm_framebuffer *
9310mode_fits_in_fbdev(struct drm_device *dev,
9311 struct drm_display_mode *mode)
9312{
Daniel Vetter4520f532013-10-09 09:18:51 +02009313#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01009314 struct drm_i915_private *dev_priv = dev->dev_private;
9315 struct drm_i915_gem_object *obj;
9316 struct drm_framebuffer *fb;
9317
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009318 if (!dev_priv->fbdev)
9319 return NULL;
9320
9321 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009322 return NULL;
9323
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009324 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009325 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009326
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009327 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009328 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9329 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01009330 return NULL;
9331
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009332 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009333 return NULL;
9334
9335 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009336#else
9337 return NULL;
9338#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009339}
9340
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009341bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01009342 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05009343 struct intel_load_detect_pipe *old,
9344 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009345{
9346 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009347 struct intel_encoder *intel_encoder =
9348 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009349 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009350 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009351 struct drm_crtc *crtc = NULL;
9352 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02009353 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009354 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009355 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009356 struct drm_connector_state *connector_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009357 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009358
Chris Wilsond2dff872011-04-19 08:36:26 +01009359 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009360 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009361 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009362
Rob Clark51fd3712013-11-19 12:10:12 -05009363retry:
9364 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9365 if (ret)
9366 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009367
Jesse Barnes79e53942008-11-07 14:24:08 -08009368 /*
9369 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009370 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009371 * - if the connector already has an assigned crtc, use it (but make
9372 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009373 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009374 * - try to find the first unused crtc that can drive this connector,
9375 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009376 */
9377
9378 /* See if we already have a CRTC for this connector */
9379 if (encoder->crtc) {
9380 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009381
Rob Clark51fd3712013-11-19 12:10:12 -05009382 ret = drm_modeset_lock(&crtc->mutex, ctx);
9383 if (ret)
9384 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009385 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9386 if (ret)
9387 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01009388
Daniel Vetter24218aa2012-08-12 19:27:11 +02009389 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009390 old->load_detect_temp = false;
9391
9392 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009393 if (connector->dpms != DRM_MODE_DPMS_ON)
9394 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01009395
Chris Wilson71731882011-04-19 23:10:58 +01009396 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08009397 }
9398
9399 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009400 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009401 i++;
9402 if (!(encoder->possible_crtcs & (1 << i)))
9403 continue;
Matt Roper83d65732015-02-25 13:12:16 -08009404 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +03009405 continue;
9406 /* This can occur when applying the pipe A quirk on resume. */
9407 if (to_intel_crtc(possible_crtc)->new_enabled)
9408 continue;
9409
9410 crtc = possible_crtc;
9411 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009412 }
9413
9414 /*
9415 * If we didn't find an unused CRTC, don't use any.
9416 */
9417 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01009418 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05009419 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08009420 }
9421
Rob Clark51fd3712013-11-19 12:10:12 -05009422 ret = drm_modeset_lock(&crtc->mutex, ctx);
9423 if (ret)
9424 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009425 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9426 if (ret)
9427 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02009428 intel_encoder->new_crtc = to_intel_crtc(crtc);
9429 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009430
9431 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009432 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +02009433 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009434 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01009435 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08009436
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009437 state = drm_atomic_state_alloc(dev);
9438 if (!state)
9439 return false;
9440
9441 state->acquire_ctx = ctx;
9442
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009443 connector_state = drm_atomic_get_connector_state(state, connector);
9444 if (IS_ERR(connector_state)) {
9445 ret = PTR_ERR(connector_state);
9446 goto fail;
9447 }
9448
9449 connector_state->crtc = crtc;
9450 connector_state->best_encoder = &intel_encoder->base;
9451
Chris Wilson64927112011-04-20 07:25:26 +01009452 if (!mode)
9453 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08009454
Chris Wilsond2dff872011-04-19 08:36:26 +01009455 /* We need a framebuffer large enough to accommodate all accesses
9456 * that the plane may generate whilst we perform load detection.
9457 * We can not rely on the fbcon either being present (we get called
9458 * during its initialisation to detect all boot displays, or it may
9459 * not even exist) or that it is large enough to satisfy the
9460 * requested mode.
9461 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02009462 fb = mode_fits_in_fbdev(dev, mode);
9463 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009464 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009465 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9466 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009467 } else
9468 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009469 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009470 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009471 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009472 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009473
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009474 if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
Chris Wilson64927112011-04-20 07:25:26 +01009475 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01009476 if (old->release_fb)
9477 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009478 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009479 }
Daniel Vetter9128b042015-03-03 17:31:21 +01009480 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +01009481
Jesse Barnes79e53942008-11-07 14:24:08 -08009482 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009483 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01009484 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009485
9486 fail:
Matt Roper83d65732015-02-25 13:12:16 -08009487 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -05009488fail_unlock:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009489 if (state) {
9490 drm_atomic_state_free(state);
9491 state = NULL;
9492 }
9493
Rob Clark51fd3712013-11-19 12:10:12 -05009494 if (ret == -EDEADLK) {
9495 drm_modeset_backoff(ctx);
9496 goto retry;
9497 }
9498
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009499 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009500}
9501
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009502void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +02009503 struct intel_load_detect_pipe *old,
9504 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009505{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009506 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009507 struct intel_encoder *intel_encoder =
9508 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01009509 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01009510 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009511 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009512 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009513 struct drm_connector_state *connector_state;
Jesse Barnes79e53942008-11-07 14:24:08 -08009514
Chris Wilsond2dff872011-04-19 08:36:26 +01009515 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009516 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009517 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009518
Chris Wilson8261b192011-04-19 23:18:09 +01009519 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009520 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009521 if (!state)
9522 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009523
9524 state->acquire_ctx = ctx;
9525
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009526 connector_state = drm_atomic_get_connector_state(state, connector);
9527 if (IS_ERR(connector_state))
9528 goto fail;
9529
Daniel Vetterfc303102012-07-09 10:40:58 +02009530 to_intel_connector(connector)->new_encoder = NULL;
9531 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009532 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009533
9534 connector_state->best_encoder = NULL;
9535 connector_state->crtc = NULL;
9536
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009537 intel_set_mode(crtc, NULL, 0, 0, NULL, state);
9538
9539 drm_atomic_state_free(state);
Chris Wilsond2dff872011-04-19 08:36:26 +01009540
Daniel Vetter36206362012-12-10 20:42:17 +01009541 if (old->release_fb) {
9542 drm_framebuffer_unregister_private(old->release_fb);
9543 drm_framebuffer_unreference(old->release_fb);
9544 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009545
Chris Wilson0622a532011-04-21 09:32:11 +01009546 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009547 }
9548
Eric Anholtc751ce42010-03-25 11:48:48 -07009549 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009550 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9551 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009552
9553 return;
9554fail:
9555 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9556 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -08009557}
9558
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009559static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009560 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009561{
9562 struct drm_i915_private *dev_priv = dev->dev_private;
9563 u32 dpll = pipe_config->dpll_hw_state.dpll;
9564
9565 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009566 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009567 else if (HAS_PCH_SPLIT(dev))
9568 return 120000;
9569 else if (!IS_GEN2(dev))
9570 return 96000;
9571 else
9572 return 48000;
9573}
9574
Jesse Barnes79e53942008-11-07 14:24:08 -08009575/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009576static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009577 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08009578{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009579 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08009580 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009581 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009582 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08009583 u32 fp;
9584 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009585 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08009586
9587 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03009588 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009589 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03009590 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009591
9592 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009593 if (IS_PINEVIEW(dev)) {
9594 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9595 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08009596 } else {
9597 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9598 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9599 }
9600
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009601 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009602 if (IS_PINEVIEW(dev))
9603 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9604 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08009605 else
9606 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08009607 DPLL_FPA01_P1_POST_DIV_SHIFT);
9608
9609 switch (dpll & DPLL_MODE_MASK) {
9610 case DPLLB_MODE_DAC_SERIAL:
9611 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9612 5 : 10;
9613 break;
9614 case DPLLB_MODE_LVDS:
9615 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9616 7 : 14;
9617 break;
9618 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08009619 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08009620 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009621 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009622 }
9623
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009624 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009625 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009626 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009627 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009628 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02009629 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009630 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08009631
9632 if (is_lvds) {
9633 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
9634 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009635
9636 if (lvds & LVDS_CLKB_POWER_UP)
9637 clock.p2 = 7;
9638 else
9639 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08009640 } else {
9641 if (dpll & PLL_P1_DIVIDE_BY_TWO)
9642 clock.p1 = 2;
9643 else {
9644 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
9645 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
9646 }
9647 if (dpll & PLL_P2_DIVIDE_BY_4)
9648 clock.p2 = 4;
9649 else
9650 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08009651 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009652
9653 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009654 }
9655
Ville Syrjälä18442d02013-09-13 16:00:08 +03009656 /*
9657 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01009658 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03009659 * encoder's get_config() function.
9660 */
9661 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009662}
9663
Ville Syrjälä6878da02013-09-13 15:59:11 +03009664int intel_dotclock_calculate(int link_freq,
9665 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009666{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009667 /*
9668 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009669 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009670 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009671 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009672 *
9673 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009674 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08009675 */
9676
Ville Syrjälä6878da02013-09-13 15:59:11 +03009677 if (!m_n->link_n)
9678 return 0;
9679
9680 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
9681}
9682
Ville Syrjälä18442d02013-09-13 16:00:08 +03009683static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009684 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03009685{
9686 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009687
9688 /* read out port_clock from the DPLL */
9689 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03009690
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009691 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03009692 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01009693 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03009694 * agree once we know their relationship in the encoder's
9695 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009696 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02009697 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03009698 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
9699 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08009700}
9701
9702/** Returns the currently programmed mode of the given pipe. */
9703struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9704 struct drm_crtc *crtc)
9705{
Jesse Barnes548f2452011-02-17 10:40:53 -08009706 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08009707 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009708 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009709 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009710 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02009711 int htot = I915_READ(HTOTAL(cpu_transcoder));
9712 int hsync = I915_READ(HSYNC(cpu_transcoder));
9713 int vtot = I915_READ(VTOTAL(cpu_transcoder));
9714 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03009715 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08009716
9717 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
9718 if (!mode)
9719 return NULL;
9720
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009721 /*
9722 * Construct a pipe_config sufficient for getting the clock info
9723 * back out of crtc_clock_get.
9724 *
9725 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
9726 * to use a real value here instead.
9727 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03009728 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009729 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009730 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
9731 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
9732 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009733 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
9734
Ville Syrjälä773ae032013-09-23 17:48:20 +03009735 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08009736 mode->hdisplay = (htot & 0xffff) + 1;
9737 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
9738 mode->hsync_start = (hsync & 0xffff) + 1;
9739 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
9740 mode->vdisplay = (vtot & 0xffff) + 1;
9741 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
9742 mode->vsync_start = (vsync & 0xffff) + 1;
9743 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
9744
9745 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08009746
9747 return mode;
9748}
9749
Jesse Barnes652c3932009-08-17 13:31:43 -07009750static void intel_decrease_pllclock(struct drm_crtc *crtc)
9751{
9752 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03009753 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07009754 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009755
Sonika Jindalbaff2962014-07-22 11:16:35 +05309756 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07009757 return;
9758
9759 if (!dev_priv->lvds_downclock_avail)
9760 return;
9761
9762 /*
9763 * Since this is called by a timer, we should never get here in
9764 * the manual case.
9765 */
9766 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01009767 int pipe = intel_crtc->pipe;
9768 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02009769 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01009770
Zhao Yakui44d98a62009-10-09 11:39:40 +08009771 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009772
Sean Paul8ac5a6d2012-02-13 13:14:51 -05009773 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009774
Chris Wilson074b5e12012-05-02 12:07:06 +01009775 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07009776 dpll |= DISPLAY_RATE_SELECT_FPA1;
9777 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009778 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009779 dpll = I915_READ(dpll_reg);
9780 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08009781 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009782 }
9783
9784}
9785
Chris Wilsonf047e392012-07-21 12:31:41 +01009786void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07009787{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009788 struct drm_i915_private *dev_priv = dev->dev_private;
9789
Chris Wilsonf62a0072014-02-21 17:55:39 +00009790 if (dev_priv->mm.busy)
9791 return;
9792
Paulo Zanoni43694d62014-03-07 20:08:08 -03009793 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009794 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +00009795 if (INTEL_INFO(dev)->gen >= 6)
9796 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +00009797 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +01009798}
9799
9800void intel_mark_idle(struct drm_device *dev)
9801{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009802 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00009803 struct drm_crtc *crtc;
9804
Chris Wilsonf62a0072014-02-21 17:55:39 +00009805 if (!dev_priv->mm.busy)
9806 return;
9807
9808 dev_priv->mm.busy = false;
9809
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009810 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07009811 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +00009812 continue;
9813
9814 intel_decrease_pllclock(crtc);
9815 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009816
Damien Lespiau3d13ef22014-02-07 19:12:47 +00009817 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009818 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03009819
Paulo Zanoni43694d62014-03-07 20:08:08 -03009820 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01009821}
9822
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009823static void intel_crtc_set_state(struct intel_crtc *crtc,
9824 struct intel_crtc_state *crtc_state)
9825{
9826 kfree(crtc->config);
9827 crtc->config = crtc_state;
Ander Conselvan de Oliveira16f3f652015-01-15 14:55:27 +02009828 crtc->base.state = &crtc_state->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009829}
9830
Jesse Barnes79e53942008-11-07 14:24:08 -08009831static void intel_crtc_destroy(struct drm_crtc *crtc)
9832{
9833 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009834 struct drm_device *dev = crtc->dev;
9835 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +02009836
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009837 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009838 work = intel_crtc->unpin_work;
9839 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009840 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009841
9842 if (work) {
9843 cancel_work_sync(&work->work);
9844 kfree(work);
9845 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009846
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009847 intel_crtc_set_state(intel_crtc, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08009848 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009849
Jesse Barnes79e53942008-11-07 14:24:08 -08009850 kfree(intel_crtc);
9851}
9852
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009853static void intel_unpin_work_fn(struct work_struct *__work)
9854{
9855 struct intel_unpin_work *work =
9856 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009857 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +02009858 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009859
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009860 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00009861 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +00009862 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00009863
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02009864 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +00009865
9866 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +00009867 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009868 mutex_unlock(&dev->struct_mutex);
9869
Daniel Vetterf99d7062014-06-19 16:01:59 +02009870 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +00009871 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +02009872
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009873 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9874 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9875
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009876 kfree(work);
9877}
9878
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009879static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01009880 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009881{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9883 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009884 unsigned long flags;
9885
9886 /* Ignore early vblank irqs */
9887 if (intel_crtc == NULL)
9888 return;
9889
Daniel Vetterf3260382014-09-15 14:55:23 +02009890 /*
9891 * This is called both by irq handlers and the reset code (to complete
9892 * lost pageflips) so needs the full irqsave spinlocks.
9893 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009894 spin_lock_irqsave(&dev->event_lock, flags);
9895 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00009896
9897 /* Ensure we don't miss a work->pending update ... */
9898 smp_rmb();
9899
9900 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009901 spin_unlock_irqrestore(&dev->event_lock, flags);
9902 return;
9903 }
9904
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009905 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01009906
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009907 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009908}
9909
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009910void intel_finish_page_flip(struct drm_device *dev, int pipe)
9911{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009912 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009913 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9914
Mario Kleiner49b14a52010-12-09 07:00:07 +01009915 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009916}
9917
9918void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9919{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009920 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009921 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9922
Mario Kleiner49b14a52010-12-09 07:00:07 +01009923 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009924}
9925
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009926/* Is 'a' after or equal to 'b'? */
9927static bool g4x_flip_count_after_eq(u32 a, u32 b)
9928{
9929 return !((a - b) & 0x80000000);
9930}
9931
9932static bool page_flip_finished(struct intel_crtc *crtc)
9933{
9934 struct drm_device *dev = crtc->base.dev;
9935 struct drm_i915_private *dev_priv = dev->dev_private;
9936
Ville Syrjäläbdfa7542014-05-27 21:33:09 +03009937 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
9938 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
9939 return true;
9940
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009941 /*
9942 * The relevant registers doen't exist on pre-ctg.
9943 * As the flip done interrupt doesn't trigger for mmio
9944 * flips on gmch platforms, a flip count check isn't
9945 * really needed there. But since ctg has the registers,
9946 * include it in the check anyway.
9947 */
9948 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9949 return true;
9950
9951 /*
9952 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9953 * used the same base address. In that case the mmio flip might
9954 * have completed, but the CS hasn't even executed the flip yet.
9955 *
9956 * A flip count check isn't enough as the CS might have updated
9957 * the base address just after start of vblank, but before we
9958 * managed to process the interrupt. This means we'd complete the
9959 * CS flip too soon.
9960 *
9961 * Combining both checks should get us a good enough result. It may
9962 * still happen that the CS flip has been executed, but has not
9963 * yet actually completed. But in case the base address is the same
9964 * anyway, we don't really care.
9965 */
9966 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9967 crtc->unpin_work->gtt_offset &&
9968 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9969 crtc->unpin_work->flip_count);
9970}
9971
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009972void intel_prepare_page_flip(struct drm_device *dev, int plane)
9973{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009974 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009975 struct intel_crtc *intel_crtc =
9976 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9977 unsigned long flags;
9978
Daniel Vetterf3260382014-09-15 14:55:23 +02009979
9980 /*
9981 * This is called both by irq handlers and the reset code (to complete
9982 * lost pageflips) so needs the full irqsave spinlocks.
9983 *
9984 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +00009985 * generate a page-flip completion irq, i.e. every modeset
9986 * is also accompanied by a spurious intel_prepare_page_flip().
9987 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009988 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009989 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +00009990 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009991 spin_unlock_irqrestore(&dev->event_lock, flags);
9992}
9993
Robin Schroereba905b2014-05-18 02:24:50 +02009994static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +00009995{
9996 /* Ensure that the work item is consistent when activating it ... */
9997 smp_wmb();
9998 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9999 /* and that it is marked active as soon as the irq could fire. */
10000 smp_wmb();
10001}
10002
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010003static int intel_gen2_queue_flip(struct drm_device *dev,
10004 struct drm_crtc *crtc,
10005 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010006 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010007 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010008 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010009{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010011 u32 flip_mask;
10012 int ret;
10013
Daniel Vetter6d90c952012-04-26 23:28:05 +020010014 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010015 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010016 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010017
10018 /* Can't queue multiple flips, so wait for the previous
10019 * one to finish before executing the next.
10020 */
10021 if (intel_crtc->plane)
10022 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10023 else
10024 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010025 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10026 intel_ring_emit(ring, MI_NOOP);
10027 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10028 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10029 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010030 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010031 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010032
10033 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010034 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010035 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010036}
10037
10038static int intel_gen3_queue_flip(struct drm_device *dev,
10039 struct drm_crtc *crtc,
10040 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010041 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010042 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010043 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010044{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010045 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010046 u32 flip_mask;
10047 int ret;
10048
Daniel Vetter6d90c952012-04-26 23:28:05 +020010049 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010050 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010051 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010052
10053 if (intel_crtc->plane)
10054 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10055 else
10056 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010057 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10058 intel_ring_emit(ring, MI_NOOP);
10059 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10060 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10061 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010062 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010063 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010064
Chris Wilsone7d841c2012-12-03 11:36:30 +000010065 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010066 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010067 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010068}
10069
10070static int intel_gen4_queue_flip(struct drm_device *dev,
10071 struct drm_crtc *crtc,
10072 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010073 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010074 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010075 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010076{
10077 struct drm_i915_private *dev_priv = dev->dev_private;
10078 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10079 uint32_t pf, pipesrc;
10080 int ret;
10081
Daniel Vetter6d90c952012-04-26 23:28:05 +020010082 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010083 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010084 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010085
10086 /* i965+ uses the linear or tiled offsets from the
10087 * Display Registers (which do not change across a page-flip)
10088 * so we need only reprogram the base address.
10089 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010090 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10091 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10092 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010093 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010094 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010095
10096 /* XXX Enabling the panel-fitter across page-flip is so far
10097 * untested on non-native modes, so ignore it for now.
10098 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10099 */
10100 pf = 0;
10101 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010102 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010103
10104 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010105 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010106 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010107}
10108
10109static int intel_gen6_queue_flip(struct drm_device *dev,
10110 struct drm_crtc *crtc,
10111 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010112 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010113 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010114 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010115{
10116 struct drm_i915_private *dev_priv = dev->dev_private;
10117 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10118 uint32_t pf, pipesrc;
10119 int ret;
10120
Daniel Vetter6d90c952012-04-26 23:28:05 +020010121 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010122 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010123 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010124
Daniel Vetter6d90c952012-04-26 23:28:05 +020010125 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10126 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10127 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010128 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010129
Chris Wilson99d9acd2012-04-17 20:37:00 +010010130 /* Contrary to the suggestions in the documentation,
10131 * "Enable Panel Fitter" does not seem to be required when page
10132 * flipping with a non-native mode, and worse causes a normal
10133 * modeset to fail.
10134 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10135 */
10136 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010137 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010138 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010139
10140 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010141 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010142 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010143}
10144
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010145static int intel_gen7_queue_flip(struct drm_device *dev,
10146 struct drm_crtc *crtc,
10147 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010148 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010149 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010150 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010151{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010152 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010153 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010154 int len, ret;
10155
Robin Schroereba905b2014-05-18 02:24:50 +020010156 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010157 case PLANE_A:
10158 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10159 break;
10160 case PLANE_B:
10161 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10162 break;
10163 case PLANE_C:
10164 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10165 break;
10166 default:
10167 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010168 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010169 }
10170
Chris Wilsonffe74d72013-08-26 20:58:12 +010010171 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010172 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010173 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010174 /*
10175 * On Gen 8, SRM is now taking an extra dword to accommodate
10176 * 48bits addresses, and we need a NOOP for the batch size to
10177 * stay even.
10178 */
10179 if (IS_GEN8(dev))
10180 len += 2;
10181 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010182
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010183 /*
10184 * BSpec MI_DISPLAY_FLIP for IVB:
10185 * "The full packet must be contained within the same cache line."
10186 *
10187 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10188 * cacheline, if we ever start emitting more commands before
10189 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10190 * then do the cacheline alignment, and finally emit the
10191 * MI_DISPLAY_FLIP.
10192 */
10193 ret = intel_ring_cacheline_align(ring);
10194 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010195 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010196
Chris Wilsonffe74d72013-08-26 20:58:12 +010010197 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010198 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010199 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010200
Chris Wilsonffe74d72013-08-26 20:58:12 +010010201 /* Unmask the flip-done completion message. Note that the bspec says that
10202 * we should do this for both the BCS and RCS, and that we must not unmask
10203 * more than one flip event at any time (or ensure that one flip message
10204 * can be sent by waiting for flip-done prior to queueing new flips).
10205 * Experimentation says that BCS works despite DERRMR masking all
10206 * flip-done completion events and that unmasking all planes at once
10207 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10208 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10209 */
10210 if (ring->id == RCS) {
10211 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10212 intel_ring_emit(ring, DERRMR);
10213 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10214 DERRMR_PIPEB_PRI_FLIP_DONE |
10215 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010010216 if (IS_GEN8(dev))
10217 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10218 MI_SRM_LRM_GLOBAL_GTT);
10219 else
10220 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10221 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010010222 intel_ring_emit(ring, DERRMR);
10223 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010010224 if (IS_GEN8(dev)) {
10225 intel_ring_emit(ring, 0);
10226 intel_ring_emit(ring, MI_NOOP);
10227 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010228 }
10229
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010230 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010231 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010232 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010233 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000010234
10235 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010236 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010237 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010238}
10239
Sourab Gupta84c33a62014-06-02 16:47:17 +053010240static bool use_mmio_flip(struct intel_engine_cs *ring,
10241 struct drm_i915_gem_object *obj)
10242{
10243 /*
10244 * This is not being used for older platforms, because
10245 * non-availability of flip done interrupt forces us to use
10246 * CS flips. Older platforms derive flip done using some clever
10247 * tricks involving the flip_pending status bits and vblank irqs.
10248 * So using MMIO flips there would disrupt this mechanism.
10249 */
10250
Chris Wilson8e09bf82014-07-08 10:40:30 +010010251 if (ring == NULL)
10252 return true;
10253
Sourab Gupta84c33a62014-06-02 16:47:17 +053010254 if (INTEL_INFO(ring->dev)->gen < 5)
10255 return false;
10256
10257 if (i915.use_mmio_flip < 0)
10258 return false;
10259 else if (i915.use_mmio_flip > 0)
10260 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010010261 else if (i915.enable_execlists)
10262 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010263 else
John Harrison41c52412014-11-24 18:49:43 +000010264 return ring != i915_gem_request_get_ring(obj->last_read_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010265}
10266
Damien Lespiauff944562014-11-20 14:58:16 +000010267static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10268{
10269 struct drm_device *dev = intel_crtc->base.dev;
10270 struct drm_i915_private *dev_priv = dev->dev_private;
10271 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
10272 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
10273 struct drm_i915_gem_object *obj = intel_fb->obj;
10274 const enum pipe pipe = intel_crtc->pipe;
10275 u32 ctl, stride;
10276
10277 ctl = I915_READ(PLANE_CTL(pipe, 0));
10278 ctl &= ~PLANE_CTL_TILED_MASK;
10279 if (obj->tiling_mode == I915_TILING_X)
10280 ctl |= PLANE_CTL_TILED_X;
10281
10282 /*
10283 * The stride is either expressed as a multiple of 64 bytes chunks for
10284 * linear buffers or in number of tiles for tiled buffers.
10285 */
10286 stride = fb->pitches[0] >> 6;
10287 if (obj->tiling_mode == I915_TILING_X)
10288 stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
10289
10290 /*
10291 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10292 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10293 */
10294 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10295 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10296
10297 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10298 POSTING_READ(PLANE_SURF(pipe, 0));
10299}
10300
10301static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010302{
10303 struct drm_device *dev = intel_crtc->base.dev;
10304 struct drm_i915_private *dev_priv = dev->dev_private;
10305 struct intel_framebuffer *intel_fb =
10306 to_intel_framebuffer(intel_crtc->base.primary->fb);
10307 struct drm_i915_gem_object *obj = intel_fb->obj;
10308 u32 dspcntr;
10309 u32 reg;
10310
Sourab Gupta84c33a62014-06-02 16:47:17 +053010311 reg = DSPCNTR(intel_crtc->plane);
10312 dspcntr = I915_READ(reg);
10313
Damien Lespiauc5d97472014-10-25 00:11:11 +010010314 if (obj->tiling_mode != I915_TILING_NONE)
10315 dspcntr |= DISPPLANE_TILED;
10316 else
10317 dspcntr &= ~DISPPLANE_TILED;
10318
Sourab Gupta84c33a62014-06-02 16:47:17 +053010319 I915_WRITE(reg, dspcntr);
10320
10321 I915_WRITE(DSPSURF(intel_crtc->plane),
10322 intel_crtc->unpin_work->gtt_offset);
10323 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010324
Damien Lespiauff944562014-11-20 14:58:16 +000010325}
10326
10327/*
10328 * XXX: This is the temporary way to update the plane registers until we get
10329 * around to using the usual plane update functions for MMIO flips
10330 */
10331static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10332{
10333 struct drm_device *dev = intel_crtc->base.dev;
10334 bool atomic_update;
10335 u32 start_vbl_count;
10336
10337 intel_mark_page_flip_active(intel_crtc);
10338
10339 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10340
10341 if (INTEL_INFO(dev)->gen >= 9)
10342 skl_do_mmio_flip(intel_crtc);
10343 else
10344 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10345 ilk_do_mmio_flip(intel_crtc);
10346
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010347 if (atomic_update)
10348 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010349}
10350
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010351static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010352{
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010353 struct intel_crtc *crtc =
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010354 container_of(work, struct intel_crtc, mmio_flip.work);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010355 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010356
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010357 mmio_flip = &crtc->mmio_flip;
10358 if (mmio_flip->req)
John Harrison9c654812014-11-24 18:49:35 +000010359 WARN_ON(__i915_wait_request(mmio_flip->req,
10360 crtc->reset_counter,
10361 false, NULL, NULL) != 0);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010362
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010363 intel_do_mmio_flip(crtc);
10364 if (mmio_flip->req) {
10365 mutex_lock(&crtc->base.dev->struct_mutex);
John Harrison146d84f2014-12-05 13:49:33 +000010366 i915_gem_request_assign(&mmio_flip->req, NULL);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010367 mutex_unlock(&crtc->base.dev->struct_mutex);
10368 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053010369}
10370
10371static int intel_queue_mmio_flip(struct drm_device *dev,
10372 struct drm_crtc *crtc,
10373 struct drm_framebuffer *fb,
10374 struct drm_i915_gem_object *obj,
10375 struct intel_engine_cs *ring,
10376 uint32_t flags)
10377{
Sourab Gupta84c33a62014-06-02 16:47:17 +053010378 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010379
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010380 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
10381 obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010382
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020010383 schedule_work(&intel_crtc->mmio_flip.work);
10384
Sourab Gupta84c33a62014-06-02 16:47:17 +053010385 return 0;
10386}
10387
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010388static int intel_default_queue_flip(struct drm_device *dev,
10389 struct drm_crtc *crtc,
10390 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010391 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010392 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010393 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010394{
10395 return -ENODEV;
10396}
10397
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010398static bool __intel_pageflip_stall_check(struct drm_device *dev,
10399 struct drm_crtc *crtc)
10400{
10401 struct drm_i915_private *dev_priv = dev->dev_private;
10402 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10403 struct intel_unpin_work *work = intel_crtc->unpin_work;
10404 u32 addr;
10405
10406 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10407 return true;
10408
10409 if (!work->enable_stall_check)
10410 return false;
10411
10412 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010010413 if (work->flip_queued_req &&
10414 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010415 return false;
10416
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010417 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010418 }
10419
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010420 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010421 return false;
10422
10423 /* Potential stall - if we see that the flip has happened,
10424 * assume a missed interrupt. */
10425 if (INTEL_INFO(dev)->gen >= 4)
10426 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10427 else
10428 addr = I915_READ(DSPADDR(intel_crtc->plane));
10429
10430 /* There is a potential issue here with a false positive after a flip
10431 * to the same address. We could address this by checking for a
10432 * non-incrementing frame counter.
10433 */
10434 return addr == work->gtt_offset;
10435}
10436
10437void intel_check_page_flip(struct drm_device *dev, int pipe)
10438{
10439 struct drm_i915_private *dev_priv = dev->dev_private;
10440 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10441 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010442 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020010443
Dave Gordon6c51d462015-03-06 15:34:26 +000010444 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010445
10446 if (crtc == NULL)
10447 return;
10448
Daniel Vetterf3260382014-09-15 14:55:23 +020010449 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010450 work = intel_crtc->unpin_work;
10451 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010452 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010010453 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010454 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010455 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010456 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010010457 if (work != NULL &&
10458 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10459 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020010460 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010461}
10462
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010463static int intel_crtc_page_flip(struct drm_crtc *crtc,
10464 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010465 struct drm_pending_vblank_event *event,
10466 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010467{
10468 struct drm_device *dev = crtc->dev;
10469 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070010470 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070010471 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010472 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080010473 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020010474 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010475 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010476 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010477 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010010478 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010479
Matt Roper2ff8fde2014-07-08 07:50:07 -070010480 /*
10481 * drm_mode_page_flip_ioctl() should already catch this, but double
10482 * check to be safe. In the future we may enable pageflipping from
10483 * a disabled primary plane.
10484 */
10485 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10486 return -EBUSY;
10487
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010488 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070010489 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010490 return -EINVAL;
10491
10492 /*
10493 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10494 * Note that pitch changes could also affect these register.
10495 */
10496 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070010497 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10498 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010499 return -EINVAL;
10500
Chris Wilsonf900db42014-02-20 09:26:13 +000010501 if (i915_terminally_wedged(&dev_priv->gpu_error))
10502 goto out_hang;
10503
Daniel Vetterb14c5672013-09-19 12:18:32 +020010504 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010505 if (work == NULL)
10506 return -ENOMEM;
10507
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010508 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010509 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010510 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010511 INIT_WORK(&work->work, intel_unpin_work_fn);
10512
Daniel Vetter87b6b102014-05-15 15:33:46 +020010513 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010514 if (ret)
10515 goto free_work;
10516
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010517 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010518 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010519 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010520 /* Before declaring the flip queue wedged, check if
10521 * the hardware completed the operation behind our backs.
10522 */
10523 if (__intel_pageflip_stall_check(dev, crtc)) {
10524 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10525 page_flip_completed(intel_crtc);
10526 } else {
10527 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010528 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010010529
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010530 drm_crtc_vblank_put(crtc);
10531 kfree(work);
10532 return -EBUSY;
10533 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010534 }
10535 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010536 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010537
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010538 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10539 flush_workqueue(dev_priv->wq);
10540
Jesse Barnes75dfca82010-02-10 15:09:44 -080010541 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010542 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010543 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010544
Matt Roperf4510a22014-04-01 15:22:40 -070010545 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010546 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080010547
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010548 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010549
Chris Wilson89ed88b2015-02-16 14:31:49 +000010550 ret = i915_mutex_lock_interruptible(dev);
10551 if (ret)
10552 goto cleanup;
10553
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010554 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020010555 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010556
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010557 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020010558 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010559
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010560 if (IS_VALLEYVIEW(dev)) {
10561 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010562 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010010563 /* vlv: DISPLAY_FLIP fails to change tiling */
10564 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000010565 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010010566 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010567 } else if (INTEL_INFO(dev)->gen >= 7) {
John Harrison41c52412014-11-24 18:49:43 +000010568 ring = i915_gem_request_get_ring(obj->last_read_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010569 if (ring == NULL || ring->id != RCS)
10570 ring = &dev_priv->ring[BCS];
10571 } else {
10572 ring = &dev_priv->ring[RCS];
10573 }
10574
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010575 mmio_flip = use_mmio_flip(ring, obj);
10576
10577 /* When using CS flips, we want to emit semaphores between rings.
10578 * However, when using mmio flips we will create a task to do the
10579 * synchronisation, so all we want here is to pin the framebuffer
10580 * into the display plane and skip any waits.
10581 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010582 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010583 crtc->primary->state,
10584 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010585 if (ret)
10586 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010587
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000010588 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
10589 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010590
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010591 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010592 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10593 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010594 if (ret)
10595 goto cleanup_unpin;
10596
John Harrisonf06cc1b2014-11-24 18:49:37 +000010597 i915_gem_request_assign(&work->flip_queued_req,
10598 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010599 } else {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010600 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010601 page_flip_flags);
10602 if (ret)
10603 goto cleanup_unpin;
10604
John Harrisonf06cc1b2014-11-24 18:49:37 +000010605 i915_gem_request_assign(&work->flip_queued_req,
10606 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010607 }
10608
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010609 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010610 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010611
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010612 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020010613 INTEL_FRONTBUFFER_PRIMARY(pipe));
10614
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010615 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010616 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010617 mutex_unlock(&dev->struct_mutex);
10618
Jesse Barnese5510fa2010-07-01 16:48:37 -070010619 trace_i915_flip_request(intel_crtc->plane, obj);
10620
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010621 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010010622
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010623cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010624 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010625cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010626 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010627 mutex_unlock(&dev->struct_mutex);
10628cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070010629 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010630 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010010631
Chris Wilson89ed88b2015-02-16 14:31:49 +000010632 drm_gem_object_unreference_unlocked(&obj->base);
10633 drm_framebuffer_unreference(work->old_fb);
10634
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010635 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010636 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010637 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010638
Daniel Vetter87b6b102014-05-15 15:33:46 +020010639 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010640free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010010641 kfree(work);
10642
Chris Wilsonf900db42014-02-20 09:26:13 +000010643 if (ret == -EIO) {
10644out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080010645 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010646 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010647 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020010648 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010649 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010650 }
Chris Wilsonf900db42014-02-20 09:26:13 +000010651 }
Chris Wilson96b099f2010-06-07 14:03:04 +010010652 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010653}
10654
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010655static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010656 .mode_set_base_atomic = intel_pipe_set_base_atomic,
10657 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080010658 .atomic_begin = intel_begin_crtc_commit,
10659 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010660};
10661
Daniel Vetter9a935852012-07-05 22:34:27 +020010662/**
10663 * intel_modeset_update_staged_output_state
10664 *
10665 * Updates the staged output configuration state, e.g. after we've read out the
10666 * current hw state.
10667 */
10668static void intel_modeset_update_staged_output_state(struct drm_device *dev)
10669{
Ville Syrjälä76688512014-01-10 11:28:06 +020010670 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010671 struct intel_encoder *encoder;
10672 struct intel_connector *connector;
10673
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010674 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010675 connector->new_encoder =
10676 to_intel_encoder(connector->base.encoder);
10677 }
10678
Damien Lespiaub2784e12014-08-05 11:29:37 +010010679 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010680 encoder->new_crtc =
10681 to_intel_crtc(encoder->base.crtc);
10682 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010683
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010684 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010685 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020010686 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010687}
10688
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010689/* Transitional helper to copy current connector/encoder state to
10690 * connector->state. This is needed so that code that is partially
10691 * converted to atomic does the right thing.
10692 */
10693static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
10694{
10695 struct intel_connector *connector;
10696
10697 for_each_intel_connector(dev, connector) {
10698 if (connector->base.encoder) {
10699 connector->base.state->best_encoder =
10700 connector->base.encoder;
10701 connector->base.state->crtc =
10702 connector->base.encoder->crtc;
10703 } else {
10704 connector->base.state->best_encoder = NULL;
10705 connector->base.state->crtc = NULL;
10706 }
10707 }
10708}
10709
Daniel Vetter9a935852012-07-05 22:34:27 +020010710/**
10711 * intel_modeset_commit_output_state
10712 *
10713 * This function copies the stage display pipe configuration to the real one.
10714 */
10715static void intel_modeset_commit_output_state(struct drm_device *dev)
10716{
Ville Syrjälä76688512014-01-10 11:28:06 +020010717 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010718 struct intel_encoder *encoder;
10719 struct intel_connector *connector;
10720
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010721 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010722 connector->base.encoder = &connector->new_encoder->base;
10723 }
10724
Damien Lespiaub2784e12014-08-05 11:29:37 +010010725 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010726 encoder->base.crtc = &encoder->new_crtc->base;
10727 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010728
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010729 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010730 crtc->base.state->enable = crtc->new_enabled;
Ville Syrjälä76688512014-01-10 11:28:06 +020010731 crtc->base.enabled = crtc->new_enabled;
10732 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010733
10734 intel_modeset_update_connector_atomic_state(dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020010735}
10736
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010737static void
Robin Schroereba905b2014-05-18 02:24:50 +020010738connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010739 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010740{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010741 int bpp = pipe_config->pipe_bpp;
10742
10743 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10744 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010745 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010746
10747 /* Don't use an invalid EDID bpc value */
10748 if (connector->base.display_info.bpc &&
10749 connector->base.display_info.bpc * 3 < bpp) {
10750 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10751 bpp, connector->base.display_info.bpc*3);
10752 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10753 }
10754
10755 /* Clamp bpp to 8 on screens without EDID 1.4 */
10756 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10757 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10758 bpp);
10759 pipe_config->pipe_bpp = 24;
10760 }
10761}
10762
10763static int
10764compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10765 struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010766 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010767{
10768 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010769 struct drm_atomic_state *state;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010770 struct intel_connector *connector;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010771 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010772
Daniel Vetterd42264b2013-03-28 16:38:08 +010010773 switch (fb->pixel_format) {
10774 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010775 bpp = 8*3; /* since we go through a colormap */
10776 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010777 case DRM_FORMAT_XRGB1555:
10778 case DRM_FORMAT_ARGB1555:
10779 /* checked in intel_framebuffer_init already */
10780 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10781 return -EINVAL;
10782 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010783 bpp = 6*3; /* min is 18bpp */
10784 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010785 case DRM_FORMAT_XBGR8888:
10786 case DRM_FORMAT_ABGR8888:
10787 /* checked in intel_framebuffer_init already */
10788 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10789 return -EINVAL;
10790 case DRM_FORMAT_XRGB8888:
10791 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010792 bpp = 8*3;
10793 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010794 case DRM_FORMAT_XRGB2101010:
10795 case DRM_FORMAT_ARGB2101010:
10796 case DRM_FORMAT_XBGR2101010:
10797 case DRM_FORMAT_ABGR2101010:
10798 /* checked in intel_framebuffer_init already */
10799 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +010010800 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010801 bpp = 10*3;
10802 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +010010803 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010804 default:
10805 DRM_DEBUG_KMS("unsupported depth\n");
10806 return -EINVAL;
10807 }
10808
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010809 pipe_config->pipe_bpp = bpp;
10810
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010811 state = pipe_config->base.state;
10812
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010813 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010814 for (i = 0; i < state->num_connector; i++) {
10815 if (!state->connectors[i])
10816 continue;
10817
10818 connector = to_intel_connector(state->connectors[i]);
10819 if (state->connector_states[i]->crtc != &crtc->base)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010820 continue;
10821
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010822 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010823 }
10824
10825 return bpp;
10826}
10827
Daniel Vetter644db712013-09-19 14:53:58 +020010828static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10829{
10830 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10831 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010010832 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020010833 mode->crtc_hdisplay, mode->crtc_hsync_start,
10834 mode->crtc_hsync_end, mode->crtc_htotal,
10835 mode->crtc_vdisplay, mode->crtc_vsync_start,
10836 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10837}
10838
Daniel Vetterc0b03412013-05-28 12:05:54 +020010839static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010840 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020010841 const char *context)
10842{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070010843 struct drm_device *dev = crtc->base.dev;
10844 struct drm_plane *plane;
10845 struct intel_plane *intel_plane;
10846 struct intel_plane_state *state;
10847 struct drm_framebuffer *fb;
10848
10849 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
10850 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020010851
10852 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10853 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10854 pipe_config->pipe_bpp, pipe_config->dither);
10855 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10856 pipe_config->has_pch_encoder,
10857 pipe_config->fdi_lanes,
10858 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10859 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10860 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010861 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10862 pipe_config->has_dp_encoder,
10863 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10864 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10865 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010866
10867 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10868 pipe_config->has_dp_encoder,
10869 pipe_config->dp_m2_n2.gmch_m,
10870 pipe_config->dp_m2_n2.gmch_n,
10871 pipe_config->dp_m2_n2.link_m,
10872 pipe_config->dp_m2_n2.link_n,
10873 pipe_config->dp_m2_n2.tu);
10874
Daniel Vetter55072d12014-11-20 16:10:28 +010010875 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
10876 pipe_config->has_audio,
10877 pipe_config->has_infoframe);
10878
Daniel Vetterc0b03412013-05-28 12:05:54 +020010879 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010880 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010881 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010882 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
10883 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030010884 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010885 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10886 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070010887 DRM_DEBUG_KMS("num_scalers: %d\n", crtc->num_scalers);
10888 DRM_DEBUG_KMS("scaler_users: 0x%x\n", pipe_config->scaler_state.scaler_users);
10889 DRM_DEBUG_KMS("scaler id: %d\n", pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010890 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10891 pipe_config->gmch_pfit.control,
10892 pipe_config->gmch_pfit.pgm_ratios,
10893 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010894 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020010895 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010896 pipe_config->pch_pfit.size,
10897 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010898 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030010899 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070010900
10901 DRM_DEBUG_KMS("planes on this crtc\n");
10902 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
10903 intel_plane = to_intel_plane(plane);
10904 if (intel_plane->pipe != crtc->pipe)
10905 continue;
10906
10907 state = to_intel_plane_state(plane->state);
10908 fb = state->base.fb;
10909 if (!fb) {
10910 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
10911 "disabled, scaler_id = %d\n",
10912 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
10913 plane->base.id, intel_plane->pipe,
10914 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
10915 drm_plane_index(plane), state->scaler_id);
10916 continue;
10917 }
10918
10919 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
10920 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
10921 plane->base.id, intel_plane->pipe,
10922 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
10923 drm_plane_index(plane));
10924 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
10925 fb->base.id, fb->width, fb->height, fb->pixel_format);
10926 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
10927 state->scaler_id,
10928 state->src.x1 >> 16, state->src.y1 >> 16,
10929 drm_rect_width(&state->src) >> 16,
10930 drm_rect_height(&state->src) >> 16,
10931 state->dst.x1, state->dst.y1,
10932 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
10933 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020010934}
10935
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010936static bool encoders_cloneable(const struct intel_encoder *a,
10937 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010938{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010939 /* masks could be asymmetric, so check both ways */
10940 return a == b || (a->cloneable & (1 << b->type) &&
10941 b->cloneable & (1 << a->type));
10942}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010943
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010944static bool check_single_encoder_cloning(struct drm_atomic_state *state,
10945 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010946 struct intel_encoder *encoder)
10947{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010948 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010949 struct drm_connector_state *connector_state;
10950 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010951
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010952 for (i = 0; i < state->num_connector; i++) {
10953 if (!state->connectors[i])
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010954 continue;
10955
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010956 connector_state = state->connector_states[i];
10957 if (connector_state->crtc != &crtc->base)
10958 continue;
10959
10960 source_encoder =
10961 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010962 if (!encoders_cloneable(encoder, source_encoder))
10963 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010964 }
10965
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010966 return true;
10967}
10968
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010969static bool check_encoder_cloning(struct drm_atomic_state *state,
10970 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010971{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010972 struct intel_encoder *encoder;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010973 struct drm_connector_state *connector_state;
10974 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010975
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010976 for (i = 0; i < state->num_connector; i++) {
10977 if (!state->connectors[i])
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010978 continue;
10979
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010980 connector_state = state->connector_states[i];
10981 if (connector_state->crtc != &crtc->base)
10982 continue;
10983
10984 encoder = to_intel_encoder(connector_state->best_encoder);
10985 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010986 return false;
10987 }
10988
10989 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010990}
10991
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010992static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010993{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010994 struct drm_device *dev = state->dev;
10995 struct intel_encoder *encoder;
10996 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010997 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010998 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010999
11000 /*
11001 * Walk the connector list instead of the encoder
11002 * list to detect the problem on ddi platforms
11003 * where there's just one encoder per digital port.
11004 */
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011005 for (i = 0; i < state->num_connector; i++) {
11006 if (!state->connectors[i])
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011007 continue;
11008
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011009 connector_state = state->connector_states[i];
11010 if (!connector_state->best_encoder)
11011 continue;
11012
11013 encoder = to_intel_encoder(connector_state->best_encoder);
11014
11015 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011016
11017 switch (encoder->type) {
11018 unsigned int port_mask;
11019 case INTEL_OUTPUT_UNKNOWN:
11020 if (WARN_ON(!HAS_DDI(dev)))
11021 break;
11022 case INTEL_OUTPUT_DISPLAYPORT:
11023 case INTEL_OUTPUT_HDMI:
11024 case INTEL_OUTPUT_EDP:
11025 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11026
11027 /* the same port mustn't appear more than once */
11028 if (used_ports & port_mask)
11029 return false;
11030
11031 used_ports |= port_mask;
11032 default:
11033 break;
11034 }
11035 }
11036
11037 return true;
11038}
11039
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011040static void
11041clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11042{
11043 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011044 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011045
Chandra Konduru663a3642015-04-07 15:28:41 -070011046 /* Clear only the intel specific part of the crtc state excluding scalers */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011047 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070011048 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011049 memset(crtc_state, 0, sizeof *crtc_state);
11050 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011051 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011052}
11053
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011054static struct intel_crtc_state *
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011055intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011056 struct drm_framebuffer *fb,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011057 struct drm_display_mode *mode,
11058 struct drm_atomic_state *state)
Daniel Vetter7758a112012-07-08 19:40:39 +020011059{
Daniel Vetter7758a112012-07-08 19:40:39 +020011060 struct intel_encoder *encoder;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011061 struct intel_connector *connector;
11062 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011063 struct intel_crtc_state *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011064 int plane_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011065 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011066 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011067
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011068 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011069 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11070 return ERR_PTR(-EINVAL);
11071 }
11072
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011073 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011074 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
11075 return ERR_PTR(-EINVAL);
11076 }
11077
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011078 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
11079 if (IS_ERR(pipe_config))
11080 return pipe_config;
11081
11082 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011083
Matt Roper07878242015-02-25 11:43:26 -080011084 pipe_config->base.crtc = crtc;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011085 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
11086 drm_mode_copy(&pipe_config->base.mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011087
Daniel Vettere143a212013-07-04 12:01:15 +020011088 pipe_config->cpu_transcoder =
11089 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011090 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011091
Imre Deak2960bc92013-07-30 13:36:32 +030011092 /*
11093 * Sanitize sync polarity flags based on requested ones. If neither
11094 * positive or negative polarity is requested, treat this as meaning
11095 * negative polarity.
11096 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011097 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011098 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011099 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011100
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011101 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011102 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011103 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011104
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011105 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11106 * plane pixel format and any sink constraints into account. Returns the
11107 * source plane bpp so that dithering can be selected on mismatches
11108 * after encoders and crtc also have had their say. */
11109 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11110 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011111 if (plane_bpp < 0)
11112 goto fail;
11113
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011114 /*
11115 * Determine the real pipe dimensions. Note that stereo modes can
11116 * increase the actual pipe size due to the frame doubling and
11117 * insertion of additional space for blanks between the frame. This
11118 * is stored in the crtc timings. We use the requested mode to do this
11119 * computation to clearly distinguish it from the adjusted mode, which
11120 * can be changed by the connectors in the below retry loop.
11121 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011122 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011123 &pipe_config->pipe_src_w,
11124 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011125
Daniel Vettere29c22c2013-02-21 00:00:16 +010011126encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011127 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011128 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011129 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011130
Daniel Vetter135c81b2013-07-21 21:37:09 +020011131 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011132 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11133 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011134
Daniel Vetter7758a112012-07-08 19:40:39 +020011135 /* Pass our mode to the connectors and the CRTC to give them a chance to
11136 * adjust it according to limitations or connector properties, and also
11137 * a chance to reject the mode entirely.
11138 */
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011139 for (i = 0; i < state->num_connector; i++) {
11140 connector = to_intel_connector(state->connectors[i]);
11141 if (!connector)
Daniel Vetter7758a112012-07-08 19:40:39 +020011142 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +010011143
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011144 connector_state = state->connector_states[i];
11145 if (connector_state->crtc != crtc)
11146 continue;
11147
11148 encoder = to_intel_encoder(connector_state->best_encoder);
11149
Daniel Vetterefea6e82013-07-21 21:36:59 +020011150 if (!(encoder->compute_config(encoder, pipe_config))) {
11151 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011152 goto fail;
11153 }
11154 }
11155
Daniel Vetterff9a6752013-06-01 17:16:21 +020011156 /* Set default port clock if not overwritten by the encoder. Needs to be
11157 * done afterwards in case the encoder adjusts the mode. */
11158 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011159 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011160 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011161
Daniel Vettera43f6e02013-06-07 23:10:32 +020011162 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011163 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011164 DRM_DEBUG_KMS("CRTC fixup failed\n");
11165 goto fail;
11166 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011167
11168 if (ret == RETRY) {
11169 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11170 ret = -EINVAL;
11171 goto fail;
11172 }
11173
11174 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11175 retry = false;
11176 goto encoder_retry;
11177 }
11178
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011179 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
11180 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
11181 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
11182
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011183 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020011184fail:
Daniel Vettere29c22c2013-02-21 00:00:16 +010011185 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +020011186}
11187
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011188/* Computes which crtcs are affected and sets the relevant bits in the mask. For
11189 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
11190static void
11191intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
11192 unsigned *prepare_pipes, unsigned *disable_pipes)
11193{
11194 struct intel_crtc *intel_crtc;
11195 struct drm_device *dev = crtc->dev;
11196 struct intel_encoder *encoder;
11197 struct intel_connector *connector;
11198 struct drm_crtc *tmp_crtc;
11199
11200 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
11201
11202 /* Check which crtcs have changed outputs connected to them, these need
11203 * to be part of the prepare_pipes mask. We don't (yet) support global
11204 * modeset across multiple crtcs, so modeset_pipes will only have one
11205 * bit set at most. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011206 for_each_intel_connector(dev, connector) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011207 if (connector->base.encoder == &connector->new_encoder->base)
11208 continue;
11209
11210 if (connector->base.encoder) {
11211 tmp_crtc = connector->base.encoder->crtc;
11212
11213 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11214 }
11215
11216 if (connector->new_encoder)
11217 *prepare_pipes |=
11218 1 << connector->new_encoder->new_crtc->pipe;
11219 }
11220
Damien Lespiaub2784e12014-08-05 11:29:37 +010011221 for_each_intel_encoder(dev, encoder) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011222 if (encoder->base.crtc == &encoder->new_crtc->base)
11223 continue;
11224
11225 if (encoder->base.crtc) {
11226 tmp_crtc = encoder->base.crtc;
11227
11228 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11229 }
11230
11231 if (encoder->new_crtc)
11232 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
11233 }
11234
Ville Syrjälä76688512014-01-10 11:28:06 +020011235 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011236 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011237 if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011238 continue;
11239
Ville Syrjälä76688512014-01-10 11:28:06 +020011240 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011241 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +020011242 else
11243 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011244 }
11245
11246
11247 /* set_mode is also used to update properties on life display pipes. */
11248 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +020011249 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011250 *prepare_pipes |= 1 << intel_crtc->pipe;
11251
Daniel Vetterb6c51642013-04-12 18:48:43 +020011252 /*
11253 * For simplicity do a full modeset on any pipe where the output routing
11254 * changed. We could be more clever, but that would require us to be
11255 * more careful with calling the relevant encoder->mode_set functions.
11256 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011257 if (*prepare_pipes)
11258 *modeset_pipes = *prepare_pipes;
11259
11260 /* ... and mask these out. */
11261 *modeset_pipes &= ~(*disable_pipes);
11262 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020011263
11264 /*
11265 * HACK: We don't (yet) fully support global modesets. intel_set_config
11266 * obies this rule, but the modeset restore mode of
11267 * intel_modeset_setup_hw_state does not.
11268 */
11269 *modeset_pipes &= 1 << intel_crtc->pipe;
11270 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020011271
11272 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
11273 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011274}
11275
Daniel Vetterea9d7582012-07-10 10:42:52 +020011276static bool intel_crtc_in_use(struct drm_crtc *crtc)
11277{
11278 struct drm_encoder *encoder;
11279 struct drm_device *dev = crtc->dev;
11280
11281 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11282 if (encoder->crtc == crtc)
11283 return true;
11284
11285 return false;
11286}
11287
11288static void
11289intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
11290{
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011291 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011292 struct intel_encoder *intel_encoder;
11293 struct intel_crtc *intel_crtc;
11294 struct drm_connector *connector;
11295
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011296 intel_shared_dpll_commit(dev_priv);
11297
Damien Lespiaub2784e12014-08-05 11:29:37 +010011298 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020011299 if (!intel_encoder->base.crtc)
11300 continue;
11301
11302 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
11303
11304 if (prepare_pipes & (1 << intel_crtc->pipe))
11305 intel_encoder->connectors_active = false;
11306 }
11307
11308 intel_modeset_commit_output_state(dev);
11309
Ville Syrjälä76688512014-01-10 11:28:06 +020011310 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011311 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011312 WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
Daniel Vetterea9d7582012-07-10 10:42:52 +020011313 }
11314
11315 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11316 if (!connector->encoder || !connector->encoder->crtc)
11317 continue;
11318
11319 intel_crtc = to_intel_crtc(connector->encoder->crtc);
11320
11321 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020011322 struct drm_property *dpms_property =
11323 dev->mode_config.dpms_property;
11324
Daniel Vetterea9d7582012-07-10 10:42:52 +020011325 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050011326 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020011327 dpms_property,
11328 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011329
11330 intel_encoder = to_intel_encoder(connector->encoder);
11331 intel_encoder->connectors_active = true;
11332 }
11333 }
11334
11335}
11336
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011337static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011338{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011339 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011340
11341 if (clock1 == clock2)
11342 return true;
11343
11344 if (!clock1 || !clock2)
11345 return false;
11346
11347 diff = abs(clock1 - clock2);
11348
11349 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11350 return true;
11351
11352 return false;
11353}
11354
Daniel Vetter25c5b262012-07-08 22:08:04 +020011355#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11356 list_for_each_entry((intel_crtc), \
11357 &(dev)->mode_config.crtc_list, \
11358 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020011359 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020011360
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011361static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011362intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011363 struct intel_crtc_state *current_config,
11364 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011365{
Daniel Vetter66e985c2013-06-05 13:34:20 +020011366#define PIPE_CONF_CHECK_X(name) \
11367 if (current_config->name != pipe_config->name) { \
11368 DRM_ERROR("mismatch in " #name " " \
11369 "(expected 0x%08x, found 0x%08x)\n", \
11370 current_config->name, \
11371 pipe_config->name); \
11372 return false; \
11373 }
11374
Daniel Vetter08a24032013-04-19 11:25:34 +020011375#define PIPE_CONF_CHECK_I(name) \
11376 if (current_config->name != pipe_config->name) { \
11377 DRM_ERROR("mismatch in " #name " " \
11378 "(expected %i, found %i)\n", \
11379 current_config->name, \
11380 pipe_config->name); \
11381 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011382 }
11383
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011384/* This is required for BDW+ where there is only one set of registers for
11385 * switching between high and low RR.
11386 * This macro can be used whenever a comparison has to be made between one
11387 * hw state and multiple sw state variables.
11388 */
11389#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11390 if ((current_config->name != pipe_config->name) && \
11391 (current_config->alt_name != pipe_config->name)) { \
11392 DRM_ERROR("mismatch in " #name " " \
11393 "(expected %i or %i, found %i)\n", \
11394 current_config->name, \
11395 current_config->alt_name, \
11396 pipe_config->name); \
11397 return false; \
11398 }
11399
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011400#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11401 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070011402 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011403 "(expected %i, found %i)\n", \
11404 current_config->name & (mask), \
11405 pipe_config->name & (mask)); \
11406 return false; \
11407 }
11408
Ville Syrjälä5e550652013-09-06 23:29:07 +030011409#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11410 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11411 DRM_ERROR("mismatch in " #name " " \
11412 "(expected %i, found %i)\n", \
11413 current_config->name, \
11414 pipe_config->name); \
11415 return false; \
11416 }
11417
Daniel Vetterbb760062013-06-06 14:55:52 +020011418#define PIPE_CONF_QUIRK(quirk) \
11419 ((current_config->quirks | pipe_config->quirks) & (quirk))
11420
Daniel Vettereccb1402013-05-22 00:50:22 +020011421 PIPE_CONF_CHECK_I(cpu_transcoder);
11422
Daniel Vetter08a24032013-04-19 11:25:34 +020011423 PIPE_CONF_CHECK_I(has_pch_encoder);
11424 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020011425 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11426 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11427 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11428 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11429 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020011430
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011431 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011432
11433 if (INTEL_INFO(dev)->gen < 8) {
11434 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11435 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11436 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11437 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11438 PIPE_CONF_CHECK_I(dp_m_n.tu);
11439
11440 if (current_config->has_drrs) {
11441 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11442 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11443 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11444 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11445 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11446 }
11447 } else {
11448 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11449 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11450 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11451 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11452 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11453 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011454
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011455 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11456 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11457 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11458 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11459 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11460 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011461
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011462 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11463 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11464 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11465 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11466 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11467 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011468
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011469 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020011470 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020011471 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11472 IS_VALLEYVIEW(dev))
11473 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080011474 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011475
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011476 PIPE_CONF_CHECK_I(has_audio);
11477
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011478 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011479 DRM_MODE_FLAG_INTERLACE);
11480
Daniel Vetterbb760062013-06-06 14:55:52 +020011481 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011482 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011483 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011484 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011485 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011486 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011487 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011488 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011489 DRM_MODE_FLAG_NVSYNC);
11490 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011491
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011492 PIPE_CONF_CHECK_I(pipe_src_w);
11493 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011494
Daniel Vetter99535992014-04-13 12:00:33 +020011495 /*
11496 * FIXME: BIOS likes to set up a cloned config with lvds+external
11497 * screen. Since we don't yet re-compute the pipe config when moving
11498 * just the lvds port away to another pipe the sw tracking won't match.
11499 *
11500 * Proper atomic modesets with recomputed global state will fix this.
11501 * Until then just don't check gmch state for inherited modes.
11502 */
11503 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11504 PIPE_CONF_CHECK_I(gmch_pfit.control);
11505 /* pfit ratios are autocomputed by the hw on gen4+ */
11506 if (INTEL_INFO(dev)->gen < 4)
11507 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11508 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11509 }
11510
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011511 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11512 if (current_config->pch_pfit.enabled) {
11513 PIPE_CONF_CHECK_I(pch_pfit.pos);
11514 PIPE_CONF_CHECK_I(pch_pfit.size);
11515 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011516
Chandra Kondurua1b22782015-04-07 15:28:45 -070011517 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
11518
Jesse Barnese59150d2014-01-07 13:30:45 -080011519 /* BDW+ don't expose a synchronous way to read the state */
11520 if (IS_HASWELL(dev))
11521 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011522
Ville Syrjälä282740f2013-09-04 18:30:03 +030011523 PIPE_CONF_CHECK_I(double_wide);
11524
Daniel Vetter26804af2014-06-25 22:01:55 +030011525 PIPE_CONF_CHECK_X(ddi_pll_sel);
11526
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011527 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011528 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011529 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011530 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11531 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011532 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011533 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11534 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11535 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011536
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011537 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11538 PIPE_CONF_CHECK_I(pipe_bpp);
11539
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011540 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011541 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011542
Daniel Vetter66e985c2013-06-05 13:34:20 +020011543#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011544#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011545#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011546#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011547#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011548#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011549
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011550 return true;
11551}
11552
Damien Lespiau08db6652014-11-04 17:06:52 +000011553static void check_wm_state(struct drm_device *dev)
11554{
11555 struct drm_i915_private *dev_priv = dev->dev_private;
11556 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11557 struct intel_crtc *intel_crtc;
11558 int plane;
11559
11560 if (INTEL_INFO(dev)->gen < 9)
11561 return;
11562
11563 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11564 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11565
11566 for_each_intel_crtc(dev, intel_crtc) {
11567 struct skl_ddb_entry *hw_entry, *sw_entry;
11568 const enum pipe pipe = intel_crtc->pipe;
11569
11570 if (!intel_crtc->active)
11571 continue;
11572
11573 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000011574 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000011575 hw_entry = &hw_ddb.plane[pipe][plane];
11576 sw_entry = &sw_ddb->plane[pipe][plane];
11577
11578 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11579 continue;
11580
11581 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11582 "(expected (%u,%u), found (%u,%u))\n",
11583 pipe_name(pipe), plane + 1,
11584 sw_entry->start, sw_entry->end,
11585 hw_entry->start, hw_entry->end);
11586 }
11587
11588 /* cursor */
11589 hw_entry = &hw_ddb.cursor[pipe];
11590 sw_entry = &sw_ddb->cursor[pipe];
11591
11592 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11593 continue;
11594
11595 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11596 "(expected (%u,%u), found (%u,%u))\n",
11597 pipe_name(pipe),
11598 sw_entry->start, sw_entry->end,
11599 hw_entry->start, hw_entry->end);
11600 }
11601}
11602
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011603static void
11604check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011605{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011606 struct intel_connector *connector;
11607
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011608 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011609 /* This also checks the encoder/connector hw state with the
11610 * ->get_hw_state callbacks. */
11611 intel_connector_check_state(connector);
11612
Rob Clarke2c719b2014-12-15 13:56:32 -050011613 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011614 "connector's staged encoder doesn't match current encoder\n");
11615 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011616}
11617
11618static void
11619check_encoder_state(struct drm_device *dev)
11620{
11621 struct intel_encoder *encoder;
11622 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011623
Damien Lespiaub2784e12014-08-05 11:29:37 +010011624 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011625 bool enabled = false;
11626 bool active = false;
11627 enum pipe pipe, tracked_pipe;
11628
11629 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11630 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011631 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011632
Rob Clarke2c719b2014-12-15 13:56:32 -050011633 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011634 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011635 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011636 "encoder's active_connectors set, but no crtc\n");
11637
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011638 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011639 if (connector->base.encoder != &encoder->base)
11640 continue;
11641 enabled = true;
11642 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11643 active = true;
11644 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011645 /*
11646 * for MST connectors if we unplug the connector is gone
11647 * away but the encoder is still connected to a crtc
11648 * until a modeset happens in response to the hotplug.
11649 */
11650 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
11651 continue;
11652
Rob Clarke2c719b2014-12-15 13:56:32 -050011653 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011654 "encoder's enabled state mismatch "
11655 "(expected %i, found %i)\n",
11656 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050011657 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011658 "active encoder with no crtc\n");
11659
Rob Clarke2c719b2014-12-15 13:56:32 -050011660 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011661 "encoder's computed active state doesn't match tracked active state "
11662 "(expected %i, found %i)\n", active, encoder->connectors_active);
11663
11664 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050011665 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011666 "encoder's hw state doesn't match sw tracking "
11667 "(expected %i, found %i)\n",
11668 encoder->connectors_active, active);
11669
11670 if (!encoder->base.crtc)
11671 continue;
11672
11673 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050011674 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011675 "active encoder's pipe doesn't match"
11676 "(expected %i, found %i)\n",
11677 tracked_pipe, pipe);
11678
11679 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011680}
11681
11682static void
11683check_crtc_state(struct drm_device *dev)
11684{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011685 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011686 struct intel_crtc *crtc;
11687 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011688 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011689
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011690 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011691 bool enabled = false;
11692 bool active = false;
11693
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011694 memset(&pipe_config, 0, sizeof(pipe_config));
11695
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011696 DRM_DEBUG_KMS("[CRTC:%d]\n",
11697 crtc->base.base.id);
11698
Matt Roper83d65732015-02-25 13:12:16 -080011699 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011700 "active crtc, but not enabled in sw tracking\n");
11701
Damien Lespiaub2784e12014-08-05 11:29:37 +010011702 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011703 if (encoder->base.crtc != &crtc->base)
11704 continue;
11705 enabled = true;
11706 if (encoder->connectors_active)
11707 active = true;
11708 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020011709
Rob Clarke2c719b2014-12-15 13:56:32 -050011710 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011711 "crtc's computed active state doesn't match tracked active state "
11712 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080011713 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011714 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080011715 "(expected %i, found %i)\n", enabled,
11716 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011717
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011718 active = dev_priv->display.get_pipe_config(crtc,
11719 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020011720
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030011721 /* hw state is inconsistent with the pipe quirk */
11722 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
11723 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020011724 active = crtc->active;
11725
Damien Lespiaub2784e12014-08-05 11:29:37 +010011726 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030011727 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020011728 if (encoder->base.crtc != &crtc->base)
11729 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010011730 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020011731 encoder->get_config(encoder, &pipe_config);
11732 }
11733
Rob Clarke2c719b2014-12-15 13:56:32 -050011734 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011735 "crtc active state doesn't match with hw state "
11736 "(expected %i, found %i)\n", crtc->active, active);
11737
Daniel Vetterc0b03412013-05-28 12:05:54 +020011738 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011739 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050011740 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020011741 intel_dump_pipe_config(crtc, &pipe_config,
11742 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011743 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011744 "[sw state]");
11745 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011746 }
11747}
11748
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011749static void
11750check_shared_dpll_state(struct drm_device *dev)
11751{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011752 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011753 struct intel_crtc *crtc;
11754 struct intel_dpll_hw_state dpll_hw_state;
11755 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020011756
11757 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11758 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11759 int enabled_crtcs = 0, active_crtcs = 0;
11760 bool active;
11761
11762 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
11763
11764 DRM_DEBUG_KMS("%s\n", pll->name);
11765
11766 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
11767
Rob Clarke2c719b2014-12-15 13:56:32 -050011768 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020011769 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011770 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050011771 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020011772 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011773 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020011774 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011775 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020011776 "pll on state mismatch (expected %i, found %i)\n",
11777 pll->on, active);
11778
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011779 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011780 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020011781 enabled_crtcs++;
11782 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11783 active_crtcs++;
11784 }
Rob Clarke2c719b2014-12-15 13:56:32 -050011785 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020011786 "pll active crtcs mismatch (expected %i, found %i)\n",
11787 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050011788 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020011789 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011790 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011791
Rob Clarke2c719b2014-12-15 13:56:32 -050011792 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020011793 sizeof(dpll_hw_state)),
11794 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020011795 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011796}
11797
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011798void
11799intel_modeset_check_state(struct drm_device *dev)
11800{
Damien Lespiau08db6652014-11-04 17:06:52 +000011801 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011802 check_connector_state(dev);
11803 check_encoder_state(dev);
11804 check_crtc_state(dev);
11805 check_shared_dpll_state(dev);
11806}
11807
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011808void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030011809 int dotclock)
11810{
11811 /*
11812 * FDI already provided one idea for the dotclock.
11813 * Yell if the encoder disagrees.
11814 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011815 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030011816 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011817 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030011818}
11819
Ville Syrjälä80715b22014-05-15 20:23:23 +030011820static void update_scanline_offset(struct intel_crtc *crtc)
11821{
11822 struct drm_device *dev = crtc->base.dev;
11823
11824 /*
11825 * The scanline counter increments at the leading edge of hsync.
11826 *
11827 * On most platforms it starts counting from vtotal-1 on the
11828 * first active line. That means the scanline counter value is
11829 * always one less than what we would expect. Ie. just after
11830 * start of vblank, which also occurs at start of hsync (on the
11831 * last active line), the scanline counter will read vblank_start-1.
11832 *
11833 * On gen2 the scanline counter starts counting from 1 instead
11834 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11835 * to keep the value positive), instead of adding one.
11836 *
11837 * On HSW+ the behaviour of the scanline counter depends on the output
11838 * type. For DP ports it behaves like most other platforms, but on HDMI
11839 * there's an extra 1 line difference. So we need to add two instead of
11840 * one to the value.
11841 */
11842 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011843 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030011844 int vtotal;
11845
11846 vtotal = mode->crtc_vtotal;
11847 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
11848 vtotal /= 2;
11849
11850 crtc->scanline_offset = vtotal - 1;
11851 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030011852 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030011853 crtc->scanline_offset = 2;
11854 } else
11855 crtc->scanline_offset = 1;
11856}
11857
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011858static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011859intel_modeset_compute_config(struct drm_crtc *crtc,
11860 struct drm_display_mode *mode,
11861 struct drm_framebuffer *fb,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011862 struct drm_atomic_state *state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011863 unsigned *modeset_pipes,
11864 unsigned *prepare_pipes,
11865 unsigned *disable_pipes)
11866{
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011867 struct drm_device *dev = crtc->dev;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011868 struct intel_crtc_state *pipe_config = NULL;
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011869 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011870 int ret = 0;
11871
11872 ret = drm_atomic_add_affected_connectors(state, crtc);
11873 if (ret)
11874 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011875
11876 intel_modeset_affected_pipes(crtc, modeset_pipes,
11877 prepare_pipes, disable_pipes);
11878
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011879 for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
11880 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
11881 if (IS_ERR(pipe_config))
11882 return pipe_config;
11883
11884 pipe_config->base.enable = false;
11885 }
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011886
11887 /*
11888 * Note this needs changes when we start tracking multiple modes
11889 * and crtcs. At that point we'll need to compute the whole config
11890 * (i.e. one pipe_config for each crtc) rather than just the one
11891 * for this crtc.
11892 */
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011893 for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
11894 /* FIXME: For now we still expect modeset_pipes has at most
11895 * one bit set. */
11896 if (WARN_ON(&intel_crtc->base != crtc))
11897 continue;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011898
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011899 pipe_config = intel_modeset_pipe_config(crtc, fb, mode, state);
11900 if (IS_ERR(pipe_config))
11901 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011902
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030011903 pipe_config->base.enable = true;
11904
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011905 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
11906 "[modeset]");
11907 }
11908
11909 return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011910}
11911
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030011912static int __intel_set_mode_setup_plls(struct drm_atomic_state *state,
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011913 unsigned modeset_pipes,
11914 unsigned disable_pipes)
11915{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030011916 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011917 struct drm_i915_private *dev_priv = to_i915(dev);
11918 unsigned clear_pipes = modeset_pipes | disable_pipes;
11919 struct intel_crtc *intel_crtc;
11920 int ret = 0;
11921
11922 if (!dev_priv->display.crtc_compute_clock)
11923 return 0;
11924
11925 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
11926 if (ret)
11927 goto done;
11928
11929 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030011930 struct intel_crtc_state *crtc_state =
11931 intel_atomic_get_crtc_state(state, intel_crtc);
11932
11933 /* Modeset pipes should have a new state by now */
11934 if (WARN_ON(IS_ERR(crtc_state)))
11935 continue;
11936
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011937 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030011938 crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011939 if (ret) {
11940 intel_shared_dpll_abort_config(dev_priv);
11941 goto done;
11942 }
11943 }
11944
11945done:
11946 return ret;
11947}
11948
Daniel Vetterf30da182013-04-11 20:22:50 +020011949static int __intel_set_mode(struct drm_crtc *crtc,
11950 struct drm_display_mode *mode,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011951 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011952 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011953 unsigned modeset_pipes,
11954 unsigned prepare_pipes,
11955 unsigned disable_pipes)
Daniel Vettera6778b32012-07-02 09:56:42 +020011956{
11957 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030011958 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011959 struct drm_display_mode *saved_mode;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030011960 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011961 struct intel_crtc_state *crtc_state_copy = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020011962 struct intel_crtc *intel_crtc;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011963 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020011964
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011965 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011966 if (!saved_mode)
11967 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020011968
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011969 crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
11970 if (!crtc_state_copy) {
11971 ret = -ENOMEM;
11972 goto done;
11973 }
11974
Tim Gardner3ac18232012-12-07 07:54:26 -070011975 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020011976
Jesse Barnes30a970c2013-11-04 13:48:12 -080011977 /*
11978 * See if the config requires any additional preparation, e.g.
11979 * to adjust global state with pipes off. We need to do this
11980 * here so we can get the modeset_pipe updated config for the new
11981 * mode set on this crtc. For other crtcs we need to use the
11982 * adjusted_mode bits in the crtc directly.
11983 */
Ville Syrjäläc164f832013-11-05 22:34:12 +020011984 if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030011985 ret = valleyview_modeset_global_pipes(state, &prepare_pipes);
11986 if (ret)
11987 goto done;
Jesse Barnes30a970c2013-11-04 13:48:12 -080011988
Ville Syrjäläc164f832013-11-05 22:34:12 +020011989 /* may have added more to prepare_pipes than we should */
11990 prepare_pipes &= ~disable_pipes;
11991 }
11992
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030011993 ret = __intel_set_mode_setup_plls(state, modeset_pipes, disable_pipes);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011994 if (ret)
11995 goto done;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +020011996
Daniel Vetter460da9162013-03-27 00:44:51 +010011997 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
11998 intel_crtc_disable(&intel_crtc->base);
11999
Daniel Vetterea9d7582012-07-10 10:42:52 +020012000 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012001 if (intel_crtc->base.state->enable)
Daniel Vetterea9d7582012-07-10 10:42:52 +020012002 dev_priv->display.crtc_disable(&intel_crtc->base);
12003 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012004
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012005 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12006 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012007 *
12008 * Note we'll need to fix this up when we start tracking multiple
12009 * pipes; here we assume a single modeset_pipe and only track the
12010 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012011 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012012 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020012013 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012014 /* mode_set/enable/disable functions rely on a correct pipe
12015 * config. */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012016 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020012017
12018 /*
12019 * Calculate and store various constants which
12020 * are later needed by vblank and swap-completion
12021 * timestamping. They are derived from true hwmode.
12022 */
12023 drm_calc_timestamping_constants(crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012024 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012025 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012026
Daniel Vetterea9d7582012-07-10 10:42:52 +020012027 /* Only after disabling all output pipelines that will be changed can we
12028 * update the the output configuration. */
12029 intel_modeset_update_state(dev, prepare_pipes);
12030
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012031 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020012032
Daniel Vetter25c5b262012-07-08 22:08:04 +020012033 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Gustavo Padovan455a6802014-12-01 15:40:11 -080012034 struct drm_plane *primary = intel_crtc->base.primary;
12035 int vdisplay, hdisplay;
Daniel Vetter4c107942014-04-24 23:55:05 +020012036
Gustavo Padovan455a6802014-12-01 15:40:11 -080012037 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070012038 ret = drm_plane_helper_update(primary, &intel_crtc->base,
12039 fb, 0, 0,
12040 hdisplay, vdisplay,
12041 x << 16, y << 16,
12042 hdisplay << 16, vdisplay << 16);
Daniel Vettera6778b32012-07-02 09:56:42 +020012043 }
12044
12045 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030012046 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
12047 update_scanline_offset(intel_crtc);
12048
Daniel Vetter25c5b262012-07-08 22:08:04 +020012049 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012050 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012051
Daniel Vettera6778b32012-07-02 09:56:42 +020012052 /* FIXME: add subpixel order */
12053done:
Matt Roper83d65732015-02-25 13:12:16 -080012054 if (ret && crtc->state->enable)
Tim Gardner3ac18232012-12-07 07:54:26 -070012055 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020012056
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012057 if (ret == 0 && pipe_config) {
12058 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12059
12060 /* The pipe_config will be freed with the atomic state, so
12061 * make a copy. */
12062 memcpy(crtc_state_copy, intel_crtc->config,
12063 sizeof *crtc_state_copy);
12064 intel_crtc->config = crtc_state_copy;
12065 intel_crtc->base.state = &crtc_state_copy->base;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012066 } else {
12067 kfree(crtc_state_copy);
12068 }
12069
Tim Gardner3ac18232012-12-07 07:54:26 -070012070 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020012071 return ret;
12072}
12073
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012074static int intel_set_mode_pipes(struct drm_crtc *crtc,
12075 struct drm_display_mode *mode,
12076 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012077 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012078 unsigned modeset_pipes,
12079 unsigned prepare_pipes,
12080 unsigned disable_pipes)
12081{
12082 int ret;
12083
12084 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
12085 prepare_pipes, disable_pipes);
12086
12087 if (ret == 0)
12088 intel_modeset_check_state(crtc->dev);
12089
12090 return ret;
12091}
12092
Damien Lespiaue7457a92013-08-08 22:28:59 +010012093static int intel_set_mode(struct drm_crtc *crtc,
12094 struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012095 int x, int y, struct drm_framebuffer *fb,
12096 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020012097{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012098 struct intel_crtc_state *pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012099 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012100 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020012101
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012102 pipe_config = intel_modeset_compute_config(crtc, mode, fb, state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012103 &modeset_pipes,
12104 &prepare_pipes,
12105 &disable_pipes);
Daniel Vetterf30da182013-04-11 20:22:50 +020012106
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012107 if (IS_ERR(pipe_config)) {
12108 ret = PTR_ERR(pipe_config);
12109 goto out;
12110 }
Daniel Vetterf30da182013-04-11 20:22:50 +020012111
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012112 ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
12113 modeset_pipes, prepare_pipes,
12114 disable_pipes);
12115 if (ret)
12116 goto out;
12117
12118out:
12119 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020012120}
12121
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012122void intel_crtc_restore_mode(struct drm_crtc *crtc)
12123{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012124 struct drm_device *dev = crtc->dev;
12125 struct drm_atomic_state *state;
12126 struct intel_encoder *encoder;
12127 struct intel_connector *connector;
12128 struct drm_connector_state *connector_state;
12129
12130 state = drm_atomic_state_alloc(dev);
12131 if (!state) {
12132 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12133 crtc->base.id);
12134 return;
12135 }
12136
12137 state->acquire_ctx = dev->mode_config.acquire_ctx;
12138
12139 /* The force restore path in the HW readout code relies on the staged
12140 * config still keeping the user requested config while the actual
12141 * state has been overwritten by the configuration read from HW. We
12142 * need to copy the staged config to the atomic state, otherwise the
12143 * mode set will just reapply the state the HW is already in. */
12144 for_each_intel_encoder(dev, encoder) {
12145 if (&encoder->new_crtc->base != crtc)
12146 continue;
12147
12148 for_each_intel_connector(dev, connector) {
12149 if (connector->new_encoder != encoder)
12150 continue;
12151
12152 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12153 if (IS_ERR(connector_state)) {
12154 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12155 connector->base.base.id,
12156 connector->base.name,
12157 PTR_ERR(connector_state));
12158 continue;
12159 }
12160
12161 connector_state->crtc = crtc;
12162 connector_state->best_encoder = &encoder->base;
12163 }
12164 }
12165
12166 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
12167 state);
12168
12169 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012170}
12171
Daniel Vetter25c5b262012-07-08 22:08:04 +020012172#undef for_each_intel_crtc_masked
12173
Daniel Vetterd9e55602012-07-04 22:16:09 +020012174static void intel_set_config_free(struct intel_set_config *config)
12175{
12176 if (!config)
12177 return;
12178
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012179 kfree(config->save_connector_encoders);
12180 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020012181 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020012182 kfree(config);
12183}
12184
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012185static int intel_set_config_save_state(struct drm_device *dev,
12186 struct intel_set_config *config)
12187{
Ville Syrjälä76688512014-01-10 11:28:06 +020012188 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012189 struct drm_encoder *encoder;
12190 struct drm_connector *connector;
12191 int count;
12192
Ville Syrjälä76688512014-01-10 11:28:06 +020012193 config->save_crtc_enabled =
12194 kcalloc(dev->mode_config.num_crtc,
12195 sizeof(bool), GFP_KERNEL);
12196 if (!config->save_crtc_enabled)
12197 return -ENOMEM;
12198
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012199 config->save_encoder_crtcs =
12200 kcalloc(dev->mode_config.num_encoder,
12201 sizeof(struct drm_crtc *), GFP_KERNEL);
12202 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012203 return -ENOMEM;
12204
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012205 config->save_connector_encoders =
12206 kcalloc(dev->mode_config.num_connector,
12207 sizeof(struct drm_encoder *), GFP_KERNEL);
12208 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012209 return -ENOMEM;
12210
12211 /* Copy data. Note that driver private data is not affected.
12212 * Should anything bad happen only the expected state is
12213 * restored, not the drivers personal bookkeeping.
12214 */
12215 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010012216 for_each_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012217 config->save_crtc_enabled[count++] = crtc->state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020012218 }
12219
12220 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012221 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012222 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012223 }
12224
12225 count = 0;
12226 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012227 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012228 }
12229
12230 return 0;
12231}
12232
12233static void intel_set_config_restore_state(struct drm_device *dev,
12234 struct intel_set_config *config)
12235{
Ville Syrjälä76688512014-01-10 11:28:06 +020012236 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020012237 struct intel_encoder *encoder;
12238 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012239 int count;
12240
12241 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012242 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012243 crtc->new_enabled = config->save_crtc_enabled[count++];
12244 }
12245
12246 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010012247 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012248 encoder->new_crtc =
12249 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012250 }
12251
12252 count = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012253 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012254 connector->new_encoder =
12255 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012256 }
12257}
12258
Imre Deake3de42b2013-05-03 19:44:07 +020012259static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010012260is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020012261{
12262 int i;
12263
Chris Wilson2e57f472013-07-17 12:14:40 +010012264 if (set->num_connectors == 0)
12265 return false;
12266
12267 if (WARN_ON(set->connectors == NULL))
12268 return false;
12269
12270 for (i = 0; i < set->num_connectors; i++)
12271 if (set->connectors[i]->encoder &&
12272 set->connectors[i]->encoder->crtc == set->crtc &&
12273 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020012274 return true;
12275
12276 return false;
12277}
12278
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012279static void
12280intel_set_config_compute_mode_changes(struct drm_mode_set *set,
12281 struct intel_set_config *config)
12282{
12283
12284 /* We should be able to check here if the fb has the same properties
12285 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010012286 if (is_crtc_connector_off(set)) {
12287 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070012288 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070012289 /*
12290 * If we have no fb, we can only flip as long as the crtc is
12291 * active, otherwise we need a full mode set. The crtc may
12292 * be active if we've only disabled the primary plane, or
12293 * in fastboot situations.
12294 */
Matt Roperf4510a22014-04-01 15:22:40 -070012295 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012296 struct intel_crtc *intel_crtc =
12297 to_intel_crtc(set->crtc);
12298
Matt Roper3b150f02014-05-29 08:06:53 -070012299 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012300 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
12301 config->fb_changed = true;
12302 } else {
12303 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
12304 config->mode_changed = true;
12305 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012306 } else if (set->fb == NULL) {
12307 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010012308 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070012309 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012310 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012311 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012312 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012313 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012314 }
12315
Daniel Vetter835c5872012-07-10 18:11:08 +020012316 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012317 config->fb_changed = true;
12318
12319 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
12320 DRM_DEBUG_KMS("modes are different, full mode set\n");
12321 drm_mode_debug_printmodeline(&set->crtc->mode);
12322 drm_mode_debug_printmodeline(set->mode);
12323 config->mode_changed = true;
12324 }
Chris Wilsona1d95702013-08-13 18:48:47 +010012325
12326 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
12327 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012328}
12329
Daniel Vetter2e431052012-07-04 22:42:15 +020012330static int
Daniel Vetter9a935852012-07-05 22:34:27 +020012331intel_modeset_stage_output_state(struct drm_device *dev,
12332 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012333 struct intel_set_config *config,
12334 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020012335{
Daniel Vetter9a935852012-07-05 22:34:27 +020012336 struct intel_connector *connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012337 struct drm_connector_state *connector_state;
Daniel Vetter9a935852012-07-05 22:34:27 +020012338 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020012339 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030012340 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020012341
Damien Lespiau9abdda72013-02-13 13:29:23 +000012342 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020012343 * of connectors. For paranoia, double-check this. */
12344 WARN_ON(!set->fb && (set->num_connectors != 0));
12345 WARN_ON(set->fb && (set->num_connectors == 0));
12346
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012347 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012348 /* Otherwise traverse passed in connector list and get encoders
12349 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020012350 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012351 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012352 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020012353 break;
12354 }
12355 }
12356
Daniel Vetter9a935852012-07-05 22:34:27 +020012357 /* If we disable the crtc, disable all its connectors. Also, if
12358 * the connector is on the changing crtc but not on the new
12359 * connector list, disable it. */
12360 if ((!set->fb || ro == set->num_connectors) &&
12361 connector->base.encoder &&
12362 connector->base.encoder->crtc == set->crtc) {
12363 connector->new_encoder = NULL;
12364
12365 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12366 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012367 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020012368 }
12369
12370
12371 if (&connector->new_encoder->base != connector->base.encoder) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012372 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
12373 connector->base.base.id,
12374 connector->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012375 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020012376 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012377 }
12378 /* connector->new_encoder is now updated for all connectors. */
12379
12380 /* Update crtc of enabled connectors. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012381 for_each_intel_connector(dev, connector) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012382 struct drm_crtc *new_crtc;
12383
Daniel Vetter9a935852012-07-05 22:34:27 +020012384 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020012385 continue;
12386
Daniel Vetter9a935852012-07-05 22:34:27 +020012387 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020012388
12389 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012390 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020012391 new_crtc = set->crtc;
12392 }
12393
12394 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010012395 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
12396 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012397 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012398 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012399 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020012400
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012401 connector_state =
12402 drm_atomic_get_connector_state(state, &connector->base);
12403 if (IS_ERR(connector_state))
12404 return PTR_ERR(connector_state);
12405
12406 connector_state->crtc = new_crtc;
12407 connector_state->best_encoder = &connector->new_encoder->base;
12408
Daniel Vetter9a935852012-07-05 22:34:27 +020012409 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12410 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012411 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020012412 new_crtc->base.id);
12413 }
12414
12415 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010012416 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012417 int num_connectors = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012418 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012419 if (connector->new_encoder == encoder) {
12420 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012421 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020012422 }
12423 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012424
12425 if (num_connectors == 0)
12426 encoder->new_crtc = NULL;
12427 else if (num_connectors > 1)
12428 return -EINVAL;
12429
Daniel Vetter9a935852012-07-05 22:34:27 +020012430 /* Only now check for crtc changes so we don't miss encoders
12431 * that will be disabled. */
12432 if (&encoder->new_crtc->base != encoder->base.crtc) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012433 DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
12434 encoder->base.base.id,
12435 encoder->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012436 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020012437 }
12438 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012439 /* Now we've also updated encoder->new_crtc for all encoders. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012440 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012441 connector_state =
12442 drm_atomic_get_connector_state(state, &connector->base);
Ander Conselvan de Oliveira9d918c12015-03-27 15:33:51 +020012443 if (IS_ERR(connector_state))
12444 return PTR_ERR(connector_state);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012445
12446 if (connector->new_encoder) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012447 if (connector->new_encoder != connector->encoder)
12448 connector->encoder = connector->new_encoder;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012449 } else {
12450 connector_state->crtc = NULL;
Ander Conselvan de Oliveiraf61cccf2015-03-31 11:35:00 +030012451 connector_state->best_encoder = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012452 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012453 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012454 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012455 crtc->new_enabled = false;
12456
Damien Lespiaub2784e12014-08-05 11:29:37 +010012457 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012458 if (encoder->new_crtc == crtc) {
12459 crtc->new_enabled = true;
12460 break;
12461 }
12462 }
12463
Matt Roper83d65732015-02-25 13:12:16 -080012464 if (crtc->new_enabled != crtc->base.state->enable) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012465 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
12466 crtc->base.base.id,
Ville Syrjälä76688512014-01-10 11:28:06 +020012467 crtc->new_enabled ? "en" : "dis");
12468 config->mode_changed = true;
12469 }
12470 }
12471
Daniel Vetter2e431052012-07-04 22:42:15 +020012472 return 0;
12473}
12474
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012475static void disable_crtc_nofb(struct intel_crtc *crtc)
12476{
12477 struct drm_device *dev = crtc->base.dev;
12478 struct intel_encoder *encoder;
12479 struct intel_connector *connector;
12480
12481 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
12482 pipe_name(crtc->pipe));
12483
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012484 for_each_intel_connector(dev, connector) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012485 if (connector->new_encoder &&
12486 connector->new_encoder->new_crtc == crtc)
12487 connector->new_encoder = NULL;
12488 }
12489
Damien Lespiaub2784e12014-08-05 11:29:37 +010012490 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012491 if (encoder->new_crtc == crtc)
12492 encoder->new_crtc = NULL;
12493 }
12494
12495 crtc->new_enabled = false;
12496}
12497
Daniel Vetter2e431052012-07-04 22:42:15 +020012498static int intel_crtc_set_config(struct drm_mode_set *set)
12499{
12500 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020012501 struct drm_mode_set save_set;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012502 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020012503 struct intel_set_config *config;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012504 struct intel_crtc_state *pipe_config;
Jesse Barnes50f52752014-11-07 13:11:00 -080012505 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Daniel Vetter2e431052012-07-04 22:42:15 +020012506 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020012507
Daniel Vetter8d3e3752012-07-05 16:09:09 +020012508 BUG_ON(!set);
12509 BUG_ON(!set->crtc);
12510 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020012511
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010012512 /* Enforce sane interface api - has been abused by the fb helper. */
12513 BUG_ON(!set->mode && set->fb);
12514 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020012515
Daniel Vetter2e431052012-07-04 22:42:15 +020012516 if (set->fb) {
12517 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12518 set->crtc->base.id, set->fb->base.id,
12519 (int)set->num_connectors, set->x, set->y);
12520 } else {
12521 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020012522 }
12523
12524 dev = set->crtc->dev;
12525
12526 ret = -ENOMEM;
12527 config = kzalloc(sizeof(*config), GFP_KERNEL);
12528 if (!config)
12529 goto out_config;
12530
12531 ret = intel_set_config_save_state(dev, config);
12532 if (ret)
12533 goto out_config;
12534
12535 save_set.crtc = set->crtc;
12536 save_set.mode = &set->crtc->mode;
12537 save_set.x = set->crtc->x;
12538 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070012539 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020012540
12541 /* Compute whether we need a full modeset, only an fb base update or no
12542 * change at all. In the future we might also check whether only the
12543 * mode changed, e.g. for LVDS where we only change the panel fitter in
12544 * such cases. */
12545 intel_set_config_compute_mode_changes(set, config);
12546
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012547 state = drm_atomic_state_alloc(dev);
12548 if (!state) {
12549 ret = -ENOMEM;
12550 goto out_config;
12551 }
12552
12553 state->acquire_ctx = dev->mode_config.acquire_ctx;
12554
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012555 ret = intel_modeset_stage_output_state(dev, set, config, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020012556 if (ret)
12557 goto fail;
12558
Jesse Barnes50f52752014-11-07 13:11:00 -080012559 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012560 set->fb, state,
Jesse Barnes50f52752014-11-07 13:11:00 -080012561 &modeset_pipes,
12562 &prepare_pipes,
12563 &disable_pipes);
Jesse Barnes20664592014-11-05 14:26:09 -080012564 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080012565 ret = PTR_ERR(pipe_config);
Jesse Barnes50f52752014-11-07 13:11:00 -080012566 goto fail;
Jesse Barnes20664592014-11-05 14:26:09 -080012567 } else if (pipe_config) {
Ville Syrjäläb9950a12014-11-21 21:00:36 +020012568 if (pipe_config->has_audio !=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012569 to_intel_crtc(set->crtc)->config->has_audio)
Jesse Barnes20664592014-11-05 14:26:09 -080012570 config->mode_changed = true;
12571
Jesse Barnesaf15d2c2014-12-01 09:54:28 -080012572 /*
12573 * Note we have an issue here with infoframes: current code
12574 * only updates them on the full mode set path per hw
12575 * requirements. So here we should be checking for any
12576 * required changes and forcing a mode set.
12577 */
Jesse Barnes20664592014-11-05 14:26:09 -080012578 }
Jesse Barnes50f52752014-11-07 13:11:00 -080012579
Jesse Barnes1f9954d2014-11-05 14:26:10 -080012580 intel_update_pipe_size(to_intel_crtc(set->crtc));
12581
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012582 if (config->mode_changed) {
Jesse Barnes50f52752014-11-07 13:11:00 -080012583 ret = intel_set_mode_pipes(set->crtc, set->mode,
12584 set->x, set->y, set->fb, pipe_config,
12585 modeset_pipes, prepare_pipes,
12586 disable_pipes);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012587 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070012588 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080012589 struct drm_plane *primary = set->crtc->primary;
12590 int vdisplay, hdisplay;
Matt Roper3b150f02014-05-29 08:06:53 -070012591
Gustavo Padovan455a6802014-12-01 15:40:11 -080012592 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070012593 ret = drm_plane_helper_update(primary, set->crtc, set->fb,
12594 0, 0, hdisplay, vdisplay,
12595 set->x << 16, set->y << 16,
12596 hdisplay << 16, vdisplay << 16);
Matt Roper3b150f02014-05-29 08:06:53 -070012597
12598 /*
12599 * We need to make sure the primary plane is re-enabled if it
12600 * has previously been turned off.
12601 */
12602 if (!intel_crtc->primary_enabled && ret == 0) {
12603 WARN_ON(!intel_crtc->active);
Ville Syrjäläfdd508a62014-08-08 21:51:11 +030012604 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012605 }
12606
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012607 /*
12608 * In the fastboot case this may be our only check of the
12609 * state after boot. It would be better to only do it on
12610 * the first update, but we don't have a nice way of doing that
12611 * (and really, set_config isn't used much for high freq page
12612 * flipping, so increasing its cost here shouldn't be a big
12613 * deal).
12614 */
Jani Nikulad330a952014-01-21 11:24:25 +020012615 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012616 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020012617 }
12618
Chris Wilson2d05eae2013-05-03 17:36:25 +010012619 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020012620 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12621 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020012622fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010012623 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012624
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012625 drm_atomic_state_clear(state);
12626
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012627 /*
12628 * HACK: if the pipe was on, but we didn't have a framebuffer,
12629 * force the pipe off to avoid oopsing in the modeset code
12630 * due to fb==NULL. This should only happen during boot since
12631 * we don't yet reconstruct the FB from the hardware state.
12632 */
12633 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
12634 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
12635
Chris Wilson2d05eae2013-05-03 17:36:25 +010012636 /* Try to restore the config */
12637 if (config->mode_changed &&
12638 intel_set_mode(save_set.crtc, save_set.mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012639 save_set.x, save_set.y, save_set.fb,
12640 state))
Chris Wilson2d05eae2013-05-03 17:36:25 +010012641 DRM_ERROR("failed to restore config after modeset failure\n");
12642 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012643
Daniel Vetterd9e55602012-07-04 22:16:09 +020012644out_config:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012645 if (state)
12646 drm_atomic_state_free(state);
12647
Daniel Vetterd9e55602012-07-04 22:16:09 +020012648 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012649 return ret;
12650}
12651
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012652static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012653 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020012654 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012655 .destroy = intel_crtc_destroy,
12656 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080012657 .atomic_duplicate_state = intel_crtc_duplicate_state,
12658 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012659};
12660
Daniel Vetter53589012013-06-05 13:34:16 +020012661static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
12662 struct intel_shared_dpll *pll,
12663 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012664{
Daniel Vetter53589012013-06-05 13:34:16 +020012665 uint32_t val;
12666
Daniel Vetterf458ebb2014-09-30 10:56:39 +020012667 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030012668 return false;
12669
Daniel Vetter53589012013-06-05 13:34:16 +020012670 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020012671 hw_state->dpll = val;
12672 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
12673 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020012674
12675 return val & DPLL_VCO_ENABLE;
12676}
12677
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012678static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
12679 struct intel_shared_dpll *pll)
12680{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012681 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
12682 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012683}
12684
Daniel Vettere7b903d2013-06-05 13:34:14 +020012685static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
12686 struct intel_shared_dpll *pll)
12687{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012688 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020012689 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020012690
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012691 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012692
12693 /* Wait for the clocks to stabilize. */
12694 POSTING_READ(PCH_DPLL(pll->id));
12695 udelay(150);
12696
12697 /* The pixel multiplier can only be updated once the
12698 * DPLL is enabled and the clocks are stable.
12699 *
12700 * So write it again.
12701 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012702 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012703 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012704 udelay(200);
12705}
12706
12707static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
12708 struct intel_shared_dpll *pll)
12709{
12710 struct drm_device *dev = dev_priv->dev;
12711 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020012712
12713 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012714 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020012715 if (intel_crtc_to_shared_dpll(crtc) == pll)
12716 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
12717 }
12718
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012719 I915_WRITE(PCH_DPLL(pll->id), 0);
12720 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012721 udelay(200);
12722}
12723
Daniel Vetter46edb022013-06-05 13:34:12 +020012724static char *ibx_pch_dpll_names[] = {
12725 "PCH DPLL A",
12726 "PCH DPLL B",
12727};
12728
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012729static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012730{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012731 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012732 int i;
12733
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012734 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012735
Daniel Vettere72f9fb2013-06-05 13:34:06 +020012736 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020012737 dev_priv->shared_dplls[i].id = i;
12738 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012739 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020012740 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
12741 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020012742 dev_priv->shared_dplls[i].get_hw_state =
12743 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012744 }
12745}
12746
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012747static void intel_shared_dpll_init(struct drm_device *dev)
12748{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012749 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012750
Daniel Vetter9cd86932014-06-25 22:01:57 +030012751 if (HAS_DDI(dev))
12752 intel_ddi_pll_init(dev);
12753 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012754 ibx_pch_dpll_init(dev);
12755 else
12756 dev_priv->num_shared_dpll = 0;
12757
12758 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012759}
12760
Matt Roper6beb8c232014-12-01 15:40:14 -080012761/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000012762 * intel_wm_need_update - Check whether watermarks need updating
12763 * @plane: drm plane
12764 * @state: new plane state
12765 *
12766 * Check current plane state versus the new one to determine whether
12767 * watermarks need to be recalculated.
12768 *
12769 * Returns true or false.
12770 */
12771bool intel_wm_need_update(struct drm_plane *plane,
12772 struct drm_plane_state *state)
12773{
12774 /* Update watermarks on tiling changes. */
12775 if (!plane->state->fb || !state->fb ||
12776 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
12777 plane->state->rotation != state->rotation)
12778 return true;
12779
12780 return false;
12781}
12782
12783/**
Matt Roper6beb8c232014-12-01 15:40:14 -080012784 * intel_prepare_plane_fb - Prepare fb for usage on plane
12785 * @plane: drm plane to prepare for
12786 * @fb: framebuffer to prepare for presentation
12787 *
12788 * Prepares a framebuffer for usage on a display plane. Generally this
12789 * involves pinning the underlying object and updating the frontbuffer tracking
12790 * bits. Some older platforms need special physical address handling for
12791 * cursor planes.
12792 *
12793 * Returns 0 on success, negative error code on failure.
12794 */
12795int
12796intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000012797 struct drm_framebuffer *fb,
12798 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070012799{
12800 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080012801 struct intel_plane *intel_plane = to_intel_plane(plane);
12802 enum pipe pipe = intel_plane->pipe;
12803 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12804 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
12805 unsigned frontbuffer_bits = 0;
12806 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070012807
Matt Roperea2c67b2014-12-23 10:41:52 -080012808 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070012809 return 0;
12810
Matt Roper6beb8c232014-12-01 15:40:14 -080012811 switch (plane->type) {
12812 case DRM_PLANE_TYPE_PRIMARY:
12813 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
12814 break;
12815 case DRM_PLANE_TYPE_CURSOR:
12816 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
12817 break;
12818 case DRM_PLANE_TYPE_OVERLAY:
12819 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
12820 break;
12821 }
Matt Roper465c1202014-05-29 08:06:54 -070012822
Matt Roper4c345742014-07-09 16:22:10 -070012823 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070012824
Matt Roper6beb8c232014-12-01 15:40:14 -080012825 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
12826 INTEL_INFO(dev)->cursor_needs_physical) {
12827 int align = IS_I830(dev) ? 16 * 1024 : 256;
12828 ret = i915_gem_object_attach_phys(obj, align);
12829 if (ret)
12830 DRM_DEBUG_KMS("failed to attach phys object\n");
12831 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000012832 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080012833 }
12834
12835 if (ret == 0)
12836 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
12837
12838 mutex_unlock(&dev->struct_mutex);
12839
12840 return ret;
12841}
12842
Matt Roper38f3ce32014-12-02 07:45:25 -080012843/**
12844 * intel_cleanup_plane_fb - Cleans up an fb after plane use
12845 * @plane: drm plane to clean up for
12846 * @fb: old framebuffer that was on plane
12847 *
12848 * Cleans up a framebuffer that has just been removed from a plane.
12849 */
12850void
12851intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000012852 struct drm_framebuffer *fb,
12853 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080012854{
12855 struct drm_device *dev = plane->dev;
12856 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12857
12858 if (WARN_ON(!obj))
12859 return;
12860
12861 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
12862 !INTEL_INFO(dev)->cursor_needs_physical) {
12863 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000012864 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080012865 mutex_unlock(&dev->struct_mutex);
12866 }
Matt Roper465c1202014-05-29 08:06:54 -070012867}
12868
12869static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012870intel_check_primary_plane(struct drm_plane *plane,
12871 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070012872{
Matt Roper32b7eee2014-12-24 07:59:06 -080012873 struct drm_device *dev = plane->dev;
12874 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080012875 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080012876 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080012877 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012878 struct drm_rect *dest = &state->dst;
12879 struct drm_rect *src = &state->src;
12880 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053012881 bool can_position = false;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012882 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012883
Matt Roperea2c67b2014-12-23 10:41:52 -080012884 crtc = crtc ? crtc : plane->crtc;
12885 intel_crtc = to_intel_crtc(crtc);
12886
Sonika Jindald8106362015-04-10 14:37:28 +053012887 if (INTEL_INFO(dev)->gen >= 9)
12888 can_position = true;
12889
Matt Roperc59cb172014-12-01 15:40:16 -080012890 ret = drm_plane_helper_check_update(plane, crtc, fb,
12891 src, dest, clip,
12892 DRM_PLANE_HELPER_NO_SCALING,
12893 DRM_PLANE_HELPER_NO_SCALING,
Sonika Jindald8106362015-04-10 14:37:28 +053012894 can_position, true,
12895 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080012896 if (ret)
12897 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012898
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012899 if (intel_crtc->active) {
Matt Roper32b7eee2014-12-24 07:59:06 -080012900 intel_crtc->atomic.wait_for_flips = true;
12901
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012902 /*
12903 * FBC does not work on some platforms for rotated
12904 * planes, so disable it when rotation is not 0 and
12905 * update it when rotation is set back to 0.
12906 *
12907 * FIXME: This is redundant with the fbc update done in
12908 * the primary plane enable function except that that
12909 * one is done too late. We eventually need to unify
12910 * this.
12911 */
12912 if (intel_crtc->primary_enabled &&
12913 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020012914 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080012915 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080012916 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012917 }
12918
12919 if (state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080012920 /*
12921 * BDW signals flip done immediately if the plane
12922 * is disabled, even if the plane enable is already
12923 * armed to occur at the next vblank :(
12924 */
12925 if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
12926 intel_crtc->atomic.wait_vblank = true;
12927 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012928
Matt Roper32b7eee2014-12-24 07:59:06 -080012929 intel_crtc->atomic.fb_bits |=
12930 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
12931
12932 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000012933
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000012934 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000012935 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080012936 }
12937
12938 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070012939}
12940
Sonika Jindal48404c12014-08-22 14:06:04 +053012941static void
12942intel_commit_primary_plane(struct drm_plane *plane,
12943 struct intel_plane_state *state)
12944{
Matt Roper2b875c22014-12-01 15:40:13 -080012945 struct drm_crtc *crtc = state->base.crtc;
12946 struct drm_framebuffer *fb = state->base.fb;
12947 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053012948 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080012949 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053012950 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080012951
Matt Roperea2c67b2014-12-23 10:41:52 -080012952 crtc = crtc ? crtc : plane->crtc;
12953 intel_crtc = to_intel_crtc(crtc);
12954
Matt Ropercf4c7c12014-12-04 10:27:42 -080012955 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053012956 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070012957 crtc->y = src->y1 >> 16;
12958
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012959 if (intel_crtc->active) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012960 if (state->visible) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012961 /* FIXME: kill this fastboot hack */
12962 intel_update_pipe_size(intel_crtc);
12963
12964 intel_crtc->primary_enabled = true;
12965
12966 dev_priv->display.update_primary_plane(crtc, plane->fb,
12967 crtc->x, crtc->y);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012968 } else {
12969 /*
12970 * If clipping results in a non-visible primary plane,
12971 * we'll disable the primary plane. Note that this is
12972 * a bit different than what happens if userspace
12973 * explicitly disables the plane by passing fb=0
12974 * because plane->fb still gets set and pinned.
12975 */
12976 intel_disable_primary_hw_plane(plane, crtc);
12977 }
Matt Roper32b7eee2014-12-24 07:59:06 -080012978 }
12979}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012980
Matt Roper32b7eee2014-12-24 07:59:06 -080012981static void intel_begin_crtc_commit(struct drm_crtc *crtc)
12982{
12983 struct drm_device *dev = crtc->dev;
12984 struct drm_i915_private *dev_priv = dev->dev_private;
12985 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080012986 struct intel_plane *intel_plane;
12987 struct drm_plane *p;
12988 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012989
Matt Roperea2c67b2014-12-23 10:41:52 -080012990 /* Track fb's for any planes being disabled */
12991 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
12992 intel_plane = to_intel_plane(p);
12993
12994 if (intel_crtc->atomic.disabled_planes &
12995 (1 << drm_plane_index(p))) {
12996 switch (p->type) {
12997 case DRM_PLANE_TYPE_PRIMARY:
12998 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
12999 break;
13000 case DRM_PLANE_TYPE_CURSOR:
13001 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13002 break;
13003 case DRM_PLANE_TYPE_OVERLAY:
13004 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13005 break;
13006 }
13007
13008 mutex_lock(&dev->struct_mutex);
13009 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13010 mutex_unlock(&dev->struct_mutex);
13011 }
13012 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013013
Matt Roper32b7eee2014-12-24 07:59:06 -080013014 if (intel_crtc->atomic.wait_for_flips)
13015 intel_crtc_wait_for_pending_flips(crtc);
13016
13017 if (intel_crtc->atomic.disable_fbc)
13018 intel_fbc_disable(dev);
13019
13020 if (intel_crtc->atomic.pre_disable_primary)
13021 intel_pre_disable_primary(crtc);
13022
13023 if (intel_crtc->atomic.update_wm)
13024 intel_update_watermarks(crtc);
13025
13026 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013027
13028 /* Perform vblank evasion around commit operation */
13029 if (intel_crtc->active)
13030 intel_crtc->atomic.evade =
13031 intel_pipe_update_start(intel_crtc,
13032 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013033}
13034
13035static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13036{
13037 struct drm_device *dev = crtc->dev;
13038 struct drm_i915_private *dev_priv = dev->dev_private;
13039 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13040 struct drm_plane *p;
13041
Matt Roperc34c9ee2014-12-23 10:41:50 -080013042 if (intel_crtc->atomic.evade)
13043 intel_pipe_update_end(intel_crtc,
13044 intel_crtc->atomic.start_vbl_count);
13045
Matt Roper32b7eee2014-12-24 07:59:06 -080013046 intel_runtime_pm_put(dev_priv);
13047
13048 if (intel_crtc->atomic.wait_vblank)
13049 intel_wait_for_vblank(dev, intel_crtc->pipe);
13050
13051 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13052
13053 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013054 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013055 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013056 mutex_unlock(&dev->struct_mutex);
13057 }
Matt Roper465c1202014-05-29 08:06:54 -070013058
Matt Roper32b7eee2014-12-24 07:59:06 -080013059 if (intel_crtc->atomic.post_enable_primary)
13060 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013061
Matt Roper32b7eee2014-12-24 07:59:06 -080013062 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13063 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13064 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13065 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013066
Matt Roper32b7eee2014-12-24 07:59:06 -080013067 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013068}
13069
Matt Ropercf4c7c12014-12-04 10:27:42 -080013070/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013071 * intel_plane_destroy - destroy a plane
13072 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013073 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013074 * Common destruction function for all types of planes (primary, cursor,
13075 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013076 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013077void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013078{
13079 struct intel_plane *intel_plane = to_intel_plane(plane);
13080 drm_plane_cleanup(plane);
13081 kfree(intel_plane);
13082}
13083
Matt Roper65a3fea2015-01-21 16:35:42 -080013084const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013085 .update_plane = drm_atomic_helper_update_plane,
13086 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013087 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013088 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013089 .atomic_get_property = intel_plane_atomic_get_property,
13090 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013091 .atomic_duplicate_state = intel_plane_duplicate_state,
13092 .atomic_destroy_state = intel_plane_destroy_state,
13093
Matt Roper465c1202014-05-29 08:06:54 -070013094};
13095
13096static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13097 int pipe)
13098{
13099 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013100 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013101 const uint32_t *intel_primary_formats;
13102 int num_formats;
13103
13104 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13105 if (primary == NULL)
13106 return NULL;
13107
Matt Roper8e7d6882015-01-21 16:35:41 -080013108 state = intel_create_plane_state(&primary->base);
13109 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013110 kfree(primary);
13111 return NULL;
13112 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013113 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013114
Matt Roper465c1202014-05-29 08:06:54 -070013115 primary->can_scale = false;
13116 primary->max_downscale = 1;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013117 state->scaler_id = -1;
Matt Roper465c1202014-05-29 08:06:54 -070013118 primary->pipe = pipe;
13119 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013120 primary->check_plane = intel_check_primary_plane;
13121 primary->commit_plane = intel_commit_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013122 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013123 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13124 primary->plane = !pipe;
13125
13126 if (INTEL_INFO(dev)->gen <= 3) {
13127 intel_primary_formats = intel_primary_formats_gen2;
13128 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
13129 } else {
13130 intel_primary_formats = intel_primary_formats_gen4;
13131 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
13132 }
13133
13134 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013135 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013136 intel_primary_formats, num_formats,
13137 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013138
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013139 if (INTEL_INFO(dev)->gen >= 4)
13140 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013141
Matt Roperea2c67b2014-12-23 10:41:52 -080013142 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13143
Matt Roper465c1202014-05-29 08:06:54 -070013144 return &primary->base;
13145}
13146
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013147void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13148{
13149 if (!dev->mode_config.rotation_property) {
13150 unsigned long flags = BIT(DRM_ROTATE_0) |
13151 BIT(DRM_ROTATE_180);
13152
13153 if (INTEL_INFO(dev)->gen >= 9)
13154 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13155
13156 dev->mode_config.rotation_property =
13157 drm_mode_create_rotation_property(dev, flags);
13158 }
13159 if (dev->mode_config.rotation_property)
13160 drm_object_attach_property(&plane->base.base,
13161 dev->mode_config.rotation_property,
13162 plane->base.state->rotation);
13163}
13164
Matt Roper3d7d6512014-06-10 08:28:13 -070013165static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013166intel_check_cursor_plane(struct drm_plane *plane,
13167 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013168{
Matt Roper2b875c22014-12-01 15:40:13 -080013169 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013170 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013171 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013172 struct drm_rect *dest = &state->dst;
13173 struct drm_rect *src = &state->src;
13174 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013175 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013176 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013177 unsigned stride;
13178 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013179
Matt Roperea2c67b2014-12-23 10:41:52 -080013180 crtc = crtc ? crtc : plane->crtc;
13181 intel_crtc = to_intel_crtc(crtc);
13182
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013183 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013184 src, dest, clip,
13185 DRM_PLANE_HELPER_NO_SCALING,
13186 DRM_PLANE_HELPER_NO_SCALING,
13187 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013188 if (ret)
13189 return ret;
13190
13191
13192 /* if we want to turn off the cursor ignore width and height */
13193 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013194 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013195
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013196 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013197 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13198 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13199 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013200 return -EINVAL;
13201 }
13202
Matt Roperea2c67b2014-12-23 10:41:52 -080013203 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13204 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013205 DRM_DEBUG_KMS("buffer is too small\n");
13206 return -ENOMEM;
13207 }
13208
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013209 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013210 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13211 ret = -EINVAL;
13212 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013213
Matt Roper32b7eee2014-12-24 07:59:06 -080013214finish:
13215 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013216 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013217 intel_crtc->atomic.update_wm = true;
13218
13219 intel_crtc->atomic.fb_bits |=
13220 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13221 }
13222
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013223 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013224}
13225
Matt Roperf4a2cf22014-12-01 15:40:12 -080013226static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013227intel_commit_cursor_plane(struct drm_plane *plane,
13228 struct intel_plane_state *state)
13229{
Matt Roper2b875c22014-12-01 15:40:13 -080013230 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013231 struct drm_device *dev = plane->dev;
13232 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013233 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013234 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013235
Matt Roperea2c67b2014-12-23 10:41:52 -080013236 crtc = crtc ? crtc : plane->crtc;
13237 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013238
Matt Roperea2c67b2014-12-23 10:41:52 -080013239 plane->fb = state->base.fb;
13240 crtc->cursor_x = state->base.crtc_x;
13241 crtc->cursor_y = state->base.crtc_y;
13242
Gustavo Padovana912f122014-12-01 15:40:10 -080013243 if (intel_crtc->cursor_bo == obj)
13244 goto update;
13245
Matt Roperf4a2cf22014-12-01 15:40:12 -080013246 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013247 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013248 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013249 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013250 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013251 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013252
Gustavo Padovana912f122014-12-01 15:40:10 -080013253 intel_crtc->cursor_addr = addr;
13254 intel_crtc->cursor_bo = obj;
13255update:
Gustavo Padovana912f122014-12-01 15:40:10 -080013256
Matt Roper32b7eee2014-12-24 07:59:06 -080013257 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013258 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013259}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013260
Matt Roper3d7d6512014-06-10 08:28:13 -070013261static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13262 int pipe)
13263{
13264 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013265 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013266
13267 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13268 if (cursor == NULL)
13269 return NULL;
13270
Matt Roper8e7d6882015-01-21 16:35:41 -080013271 state = intel_create_plane_state(&cursor->base);
13272 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013273 kfree(cursor);
13274 return NULL;
13275 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013276 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013277
Matt Roper3d7d6512014-06-10 08:28:13 -070013278 cursor->can_scale = false;
13279 cursor->max_downscale = 1;
13280 cursor->pipe = pipe;
13281 cursor->plane = pipe;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013282 state->scaler_id = -1;
Matt Roperc59cb172014-12-01 15:40:16 -080013283 cursor->check_plane = intel_check_cursor_plane;
13284 cursor->commit_plane = intel_commit_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013285
13286 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013287 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013288 intel_cursor_formats,
13289 ARRAY_SIZE(intel_cursor_formats),
13290 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013291
13292 if (INTEL_INFO(dev)->gen >= 4) {
13293 if (!dev->mode_config.rotation_property)
13294 dev->mode_config.rotation_property =
13295 drm_mode_create_rotation_property(dev,
13296 BIT(DRM_ROTATE_0) |
13297 BIT(DRM_ROTATE_180));
13298 if (dev->mode_config.rotation_property)
13299 drm_object_attach_property(&cursor->base.base,
13300 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013301 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013302 }
13303
Matt Roperea2c67b2014-12-23 10:41:52 -080013304 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13305
Matt Roper3d7d6512014-06-10 08:28:13 -070013306 return &cursor->base;
13307}
13308
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013309static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13310 struct intel_crtc_state *crtc_state)
13311{
13312 int i;
13313 struct intel_scaler *intel_scaler;
13314 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13315
13316 for (i = 0; i < intel_crtc->num_scalers; i++) {
13317 intel_scaler = &scaler_state->scalers[i];
13318 intel_scaler->in_use = 0;
13319 intel_scaler->id = i;
13320
13321 intel_scaler->mode = PS_SCALER_MODE_DYN;
13322 }
13323
13324 scaler_state->scaler_id = -1;
13325}
13326
Hannes Ederb358d0a2008-12-18 21:18:47 +010013327static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013328{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013329 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013330 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013331 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013332 struct drm_plane *primary = NULL;
13333 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013334 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013335
Daniel Vetter955382f2013-09-19 14:05:45 +020013336 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013337 if (intel_crtc == NULL)
13338 return;
13339
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013340 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13341 if (!crtc_state)
13342 goto fail;
13343 intel_crtc_set_state(intel_crtc, crtc_state);
Matt Roper07878242015-02-25 11:43:26 -080013344 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013345
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013346 /* initialize shared scalers */
13347 if (INTEL_INFO(dev)->gen >= 9) {
13348 if (pipe == PIPE_C)
13349 intel_crtc->num_scalers = 1;
13350 else
13351 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13352
13353 skl_init_scalers(dev, intel_crtc, crtc_state);
13354 }
13355
Matt Roper465c1202014-05-29 08:06:54 -070013356 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013357 if (!primary)
13358 goto fail;
13359
13360 cursor = intel_cursor_plane_create(dev, pipe);
13361 if (!cursor)
13362 goto fail;
13363
Matt Roper465c1202014-05-29 08:06:54 -070013364 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013365 cursor, &intel_crtc_funcs);
13366 if (ret)
13367 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013368
13369 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013370 for (i = 0; i < 256; i++) {
13371 intel_crtc->lut_r[i] = i;
13372 intel_crtc->lut_g[i] = i;
13373 intel_crtc->lut_b[i] = i;
13374 }
13375
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013376 /*
13377 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013378 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013379 */
Jesse Barnes80824002009-09-10 15:28:06 -070013380 intel_crtc->pipe = pipe;
13381 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013382 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013383 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013384 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013385 }
13386
Chris Wilson4b0e3332014-05-30 16:35:26 +030013387 intel_crtc->cursor_base = ~0;
13388 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013389 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013390
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013391 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13392 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13393 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13394 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13395
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020013396 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
13397
Jesse Barnes79e53942008-11-07 14:24:08 -080013398 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013399
13400 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013401 return;
13402
13403fail:
13404 if (primary)
13405 drm_plane_cleanup(primary);
13406 if (cursor)
13407 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013408 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013409 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013410}
13411
Jesse Barnes752aa882013-10-31 18:55:49 +020013412enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13413{
13414 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013415 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013416
Rob Clark51fd3712013-11-19 12:10:12 -050013417 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013418
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013419 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013420 return INVALID_PIPE;
13421
13422 return to_intel_crtc(encoder->crtc)->pipe;
13423}
13424
Carl Worth08d7b3d2009-04-29 14:43:54 -070013425int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013426 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013427{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013428 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013429 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013430 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013431
Rob Clark7707e652014-07-17 23:30:04 -040013432 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013433
Rob Clark7707e652014-07-17 23:30:04 -040013434 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013435 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013436 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013437 }
13438
Rob Clark7707e652014-07-17 23:30:04 -040013439 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013440 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013441
Daniel Vetterc05422d2009-08-11 16:05:30 +020013442 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013443}
13444
Daniel Vetter66a92782012-07-12 20:08:18 +020013445static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013446{
Daniel Vetter66a92782012-07-12 20:08:18 +020013447 struct drm_device *dev = encoder->base.dev;
13448 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013449 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013450 int entry = 0;
13451
Damien Lespiaub2784e12014-08-05 11:29:37 +010013452 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013453 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013454 index_mask |= (1 << entry);
13455
Jesse Barnes79e53942008-11-07 14:24:08 -080013456 entry++;
13457 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013458
Jesse Barnes79e53942008-11-07 14:24:08 -080013459 return index_mask;
13460}
13461
Chris Wilson4d302442010-12-14 19:21:29 +000013462static bool has_edp_a(struct drm_device *dev)
13463{
13464 struct drm_i915_private *dev_priv = dev->dev_private;
13465
13466 if (!IS_MOBILE(dev))
13467 return false;
13468
13469 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13470 return false;
13471
Damien Lespiaue3589902014-02-07 19:12:50 +000013472 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013473 return false;
13474
13475 return true;
13476}
13477
Jesse Barnes84b4e042014-06-25 08:24:29 -070013478static bool intel_crt_present(struct drm_device *dev)
13479{
13480 struct drm_i915_private *dev_priv = dev->dev_private;
13481
Damien Lespiau884497e2013-12-03 13:56:23 +000013482 if (INTEL_INFO(dev)->gen >= 9)
13483 return false;
13484
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013485 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013486 return false;
13487
13488 if (IS_CHERRYVIEW(dev))
13489 return false;
13490
13491 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13492 return false;
13493
13494 return true;
13495}
13496
Jesse Barnes79e53942008-11-07 14:24:08 -080013497static void intel_setup_outputs(struct drm_device *dev)
13498{
Eric Anholt725e30a2009-01-22 13:01:02 -080013499 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013500 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013501 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013502
Daniel Vetterc9093352013-06-06 22:22:47 +020013503 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013504
Jesse Barnes84b4e042014-06-25 08:24:29 -070013505 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013506 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013507
Vandana Kannanc776eb22014-08-19 12:05:01 +053013508 if (IS_BROXTON(dev)) {
13509 /*
13510 * FIXME: Broxton doesn't support port detection via the
13511 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13512 * detect the ports.
13513 */
13514 intel_ddi_init(dev, PORT_A);
13515 intel_ddi_init(dev, PORT_B);
13516 intel_ddi_init(dev, PORT_C);
13517 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013518 int found;
13519
Jesse Barnesde31fac2015-03-06 15:53:32 -080013520 /*
13521 * Haswell uses DDI functions to detect digital outputs.
13522 * On SKL pre-D0 the strap isn't connected, so we assume
13523 * it's there.
13524 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013525 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013526 /* WaIgnoreDDIAStrap: skl */
13527 if (found ||
13528 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013529 intel_ddi_init(dev, PORT_A);
13530
13531 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13532 * register */
13533 found = I915_READ(SFUSE_STRAP);
13534
13535 if (found & SFUSE_STRAP_DDIB_DETECTED)
13536 intel_ddi_init(dev, PORT_B);
13537 if (found & SFUSE_STRAP_DDIC_DETECTED)
13538 intel_ddi_init(dev, PORT_C);
13539 if (found & SFUSE_STRAP_DDID_DETECTED)
13540 intel_ddi_init(dev, PORT_D);
13541 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013542 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013543 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013544
13545 if (has_edp_a(dev))
13546 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013547
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013548 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013549 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013550 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013551 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013552 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013553 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013554 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013555 }
13556
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013557 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013558 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013559
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013560 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013561 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013562
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013563 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013564 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013565
Daniel Vetter270b3042012-10-27 15:52:05 +020013566 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013567 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013568 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013569 /*
13570 * The DP_DETECTED bit is the latched state of the DDC
13571 * SDA pin at boot. However since eDP doesn't require DDC
13572 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13573 * eDP ports may have been muxed to an alternate function.
13574 * Thus we can't rely on the DP_DETECTED bit alone to detect
13575 * eDP ports. Consult the VBT as well as DP_DETECTED to
13576 * detect eDP ports.
13577 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013578 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13579 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013580 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13581 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013582 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13583 intel_dp_is_edp(dev, PORT_B))
13584 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013585
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013586 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13587 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070013588 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13589 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013590 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13591 intel_dp_is_edp(dev, PORT_C))
13592 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053013593
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013594 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013595 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013596 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
13597 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013598 /* eDP not supported on port D, so don't check VBT */
13599 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
13600 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013601 }
13602
Jani Nikula3cfca972013-08-27 15:12:26 +030013603 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080013604 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013605 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080013606
Paulo Zanonie2debe92013-02-18 19:00:27 -030013607 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013608 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013609 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013610 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
13611 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013612 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013613 }
Ma Ling27185ae2009-08-24 13:50:23 +080013614
Imre Deake7281ea2013-05-08 13:14:08 +030013615 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013616 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080013617 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013618
13619 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013620
Paulo Zanonie2debe92013-02-18 19:00:27 -030013621 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013622 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013623 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013624 }
Ma Ling27185ae2009-08-24 13:50:23 +080013625
Paulo Zanonie2debe92013-02-18 19:00:27 -030013626 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013627
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013628 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
13629 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013630 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013631 }
Imre Deake7281ea2013-05-08 13:14:08 +030013632 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013633 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080013634 }
Ma Ling27185ae2009-08-24 13:50:23 +080013635
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013636 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030013637 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013638 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070013639 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013640 intel_dvo_init(dev);
13641
Zhenyu Wang103a1962009-11-27 11:44:36 +080013642 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013643 intel_tv_init(dev);
13644
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080013645 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070013646
Damien Lespiaub2784e12014-08-05 11:29:37 +010013647 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010013648 encoder->base.possible_crtcs = encoder->crtc_mask;
13649 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020013650 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080013651 }
Chris Wilson47356eb2011-01-11 17:06:04 +000013652
Paulo Zanonidde86e22012-12-01 12:04:25 -020013653 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020013654
13655 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013656}
13657
13658static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
13659{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013660 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080013661 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080013662
Daniel Vetteref2d6332014-02-10 18:00:38 +010013663 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013664 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010013665 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013666 drm_gem_object_unreference(&intel_fb->obj->base);
13667 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013668 kfree(intel_fb);
13669}
13670
13671static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000013672 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080013673 unsigned int *handle)
13674{
13675 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000013676 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080013677
Chris Wilson05394f32010-11-08 19:18:58 +000013678 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080013679}
13680
13681static const struct drm_framebuffer_funcs intel_fb_funcs = {
13682 .destroy = intel_user_framebuffer_destroy,
13683 .create_handle = intel_user_framebuffer_create_handle,
13684};
13685
Damien Lespiaub3218032015-02-27 11:15:18 +000013686static
13687u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
13688 uint32_t pixel_format)
13689{
13690 u32 gen = INTEL_INFO(dev)->gen;
13691
13692 if (gen >= 9) {
13693 /* "The stride in bytes must not exceed the of the size of 8K
13694 * pixels and 32K bytes."
13695 */
13696 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
13697 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
13698 return 32*1024;
13699 } else if (gen >= 4) {
13700 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13701 return 16*1024;
13702 else
13703 return 32*1024;
13704 } else if (gen >= 3) {
13705 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13706 return 8*1024;
13707 else
13708 return 16*1024;
13709 } else {
13710 /* XXX DSPC is limited to 4k tiled */
13711 return 8*1024;
13712 }
13713}
13714
Daniel Vetterb5ea6422014-03-02 21:18:00 +010013715static int intel_framebuffer_init(struct drm_device *dev,
13716 struct intel_framebuffer *intel_fb,
13717 struct drm_mode_fb_cmd2 *mode_cmd,
13718 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080013719{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000013720 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080013721 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000013722 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080013723
Daniel Vetterdd4916c2013-10-09 21:23:51 +020013724 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
13725
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013726 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
13727 /* Enforce that fb modifier and tiling mode match, but only for
13728 * X-tiled. This is needed for FBC. */
13729 if (!!(obj->tiling_mode == I915_TILING_X) !=
13730 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
13731 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
13732 return -EINVAL;
13733 }
13734 } else {
13735 if (obj->tiling_mode == I915_TILING_X)
13736 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
13737 else if (obj->tiling_mode == I915_TILING_Y) {
13738 DRM_DEBUG("No Y tiling for legacy addfb\n");
13739 return -EINVAL;
13740 }
13741 }
13742
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000013743 /* Passed in modifier sanity checking. */
13744 switch (mode_cmd->modifier[0]) {
13745 case I915_FORMAT_MOD_Y_TILED:
13746 case I915_FORMAT_MOD_Yf_TILED:
13747 if (INTEL_INFO(dev)->gen < 9) {
13748 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
13749 mode_cmd->modifier[0]);
13750 return -EINVAL;
13751 }
13752 case DRM_FORMAT_MOD_NONE:
13753 case I915_FORMAT_MOD_X_TILED:
13754 break;
13755 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070013756 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
13757 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010013758 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013759 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013760
Damien Lespiaub3218032015-02-27 11:15:18 +000013761 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
13762 mode_cmd->pixel_format);
13763 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
13764 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
13765 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010013766 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013767 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013768
Damien Lespiaub3218032015-02-27 11:15:18 +000013769 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
13770 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010013771 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000013772 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
13773 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013774 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010013775 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013776 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013777 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013778
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013779 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013780 mode_cmd->pitches[0] != obj->stride) {
13781 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
13782 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013783 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013784 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013785
Ville Syrjälä57779d02012-10-31 17:50:14 +020013786 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013787 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020013788 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020013789 case DRM_FORMAT_RGB565:
13790 case DRM_FORMAT_XRGB8888:
13791 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020013792 break;
13793 case DRM_FORMAT_XRGB1555:
13794 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013795 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013796 DRM_DEBUG("unsupported pixel format: %s\n",
13797 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013798 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013799 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020013800 break;
13801 case DRM_FORMAT_XBGR8888:
13802 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020013803 case DRM_FORMAT_XRGB2101010:
13804 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020013805 case DRM_FORMAT_XBGR2101010:
13806 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013807 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013808 DRM_DEBUG("unsupported pixel format: %s\n",
13809 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013810 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013811 }
Jesse Barnesb5626742011-06-24 12:19:27 -070013812 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020013813 case DRM_FORMAT_YUYV:
13814 case DRM_FORMAT_UYVY:
13815 case DRM_FORMAT_YVYU:
13816 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013817 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013818 DRM_DEBUG("unsupported pixel format: %s\n",
13819 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013820 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013821 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013822 break;
13823 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013824 DRM_DEBUG("unsupported pixel format: %s\n",
13825 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010013826 return -EINVAL;
13827 }
13828
Ville Syrjälä90f9a332012-10-31 17:50:19 +020013829 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
13830 if (mode_cmd->offsets[0] != 0)
13831 return -EINVAL;
13832
Damien Lespiauec2c9812015-01-20 12:51:45 +000013833 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000013834 mode_cmd->pixel_format,
13835 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020013836 /* FIXME drm helper for size checks (especially planar formats)? */
13837 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
13838 return -EINVAL;
13839
Daniel Vetterc7d73f62012-12-13 23:38:38 +010013840 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
13841 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020013842 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010013843
Jesse Barnes79e53942008-11-07 14:24:08 -080013844 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
13845 if (ret) {
13846 DRM_ERROR("framebuffer init failed %d\n", ret);
13847 return ret;
13848 }
13849
Jesse Barnes79e53942008-11-07 14:24:08 -080013850 return 0;
13851}
13852
Jesse Barnes79e53942008-11-07 14:24:08 -080013853static struct drm_framebuffer *
13854intel_user_framebuffer_create(struct drm_device *dev,
13855 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013856 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080013857{
Chris Wilson05394f32010-11-08 19:18:58 +000013858 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080013859
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013860 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
13861 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000013862 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010013863 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080013864
Chris Wilsond2dff872011-04-19 08:36:26 +010013865 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080013866}
13867
Daniel Vetter4520f532013-10-09 09:18:51 +020013868#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020013869static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020013870{
13871}
13872#endif
13873
Jesse Barnes79e53942008-11-07 14:24:08 -080013874static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080013875 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020013876 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080013877 .atomic_check = intel_atomic_check,
13878 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080013879};
13880
Jesse Barnese70236a2009-09-21 10:42:27 -070013881/* Set up chip specific display functions */
13882static void intel_init_display(struct drm_device *dev)
13883{
13884 struct drm_i915_private *dev_priv = dev->dev_private;
13885
Daniel Vetteree9300b2013-06-03 22:40:22 +020013886 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
13887 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030013888 else if (IS_CHERRYVIEW(dev))
13889 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020013890 else if (IS_VALLEYVIEW(dev))
13891 dev_priv->display.find_dpll = vlv_find_best_dpll;
13892 else if (IS_PINEVIEW(dev))
13893 dev_priv->display.find_dpll = pnv_find_best_dpll;
13894 else
13895 dev_priv->display.find_dpll = i9xx_find_best_dpll;
13896
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000013897 if (INTEL_INFO(dev)->gen >= 9) {
13898 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013899 dev_priv->display.get_initial_plane_config =
13900 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000013901 dev_priv->display.crtc_compute_clock =
13902 haswell_crtc_compute_clock;
13903 dev_priv->display.crtc_enable = haswell_crtc_enable;
13904 dev_priv->display.crtc_disable = haswell_crtc_disable;
13905 dev_priv->display.off = ironlake_crtc_off;
13906 dev_priv->display.update_primary_plane =
13907 skylake_update_primary_plane;
13908 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013909 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013910 dev_priv->display.get_initial_plane_config =
13911 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020013912 dev_priv->display.crtc_compute_clock =
13913 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020013914 dev_priv->display.crtc_enable = haswell_crtc_enable;
13915 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030013916 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000013917 dev_priv->display.update_primary_plane =
13918 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030013919 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013920 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013921 dev_priv->display.get_initial_plane_config =
13922 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020013923 dev_priv->display.crtc_compute_clock =
13924 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020013925 dev_priv->display.crtc_enable = ironlake_crtc_enable;
13926 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013927 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070013928 dev_priv->display.update_primary_plane =
13929 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070013930 } else if (IS_VALLEYVIEW(dev)) {
13931 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013932 dev_priv->display.get_initial_plane_config =
13933 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020013934 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070013935 dev_priv->display.crtc_enable = valleyview_crtc_enable;
13936 dev_priv->display.crtc_disable = i9xx_crtc_disable;
13937 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070013938 dev_priv->display.update_primary_plane =
13939 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070013940 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013941 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013942 dev_priv->display.get_initial_plane_config =
13943 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020013944 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020013945 dev_priv->display.crtc_enable = i9xx_crtc_enable;
13946 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013947 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070013948 dev_priv->display.update_primary_plane =
13949 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070013950 }
Jesse Barnese70236a2009-09-21 10:42:27 -070013951
Jesse Barnese70236a2009-09-21 10:42:27 -070013952 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030013953 if (IS_SKYLAKE(dev))
13954 dev_priv->display.get_display_clock_speed =
13955 skylake_get_display_clock_speed;
13956 else if (IS_BROADWELL(dev))
13957 dev_priv->display.get_display_clock_speed =
13958 broadwell_get_display_clock_speed;
13959 else if (IS_HASWELL(dev))
13960 dev_priv->display.get_display_clock_speed =
13961 haswell_get_display_clock_speed;
13962 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070013963 dev_priv->display.get_display_clock_speed =
13964 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030013965 else if (IS_GEN5(dev))
13966 dev_priv->display.get_display_clock_speed =
13967 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030013968 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
13969 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070013970 dev_priv->display.get_display_clock_speed =
13971 i945_get_display_clock_speed;
13972 else if (IS_I915G(dev))
13973 dev_priv->display.get_display_clock_speed =
13974 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020013975 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070013976 dev_priv->display.get_display_clock_speed =
13977 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020013978 else if (IS_PINEVIEW(dev))
13979 dev_priv->display.get_display_clock_speed =
13980 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070013981 else if (IS_I915GM(dev))
13982 dev_priv->display.get_display_clock_speed =
13983 i915gm_get_display_clock_speed;
13984 else if (IS_I865G(dev))
13985 dev_priv->display.get_display_clock_speed =
13986 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020013987 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070013988 dev_priv->display.get_display_clock_speed =
13989 i855_get_display_clock_speed;
13990 else /* 852, 830 */
13991 dev_priv->display.get_display_clock_speed =
13992 i830_get_display_clock_speed;
13993
Jani Nikula7c10a2b2014-10-27 16:26:43 +020013994 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013995 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013996 } else if (IS_GEN6(dev)) {
13997 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013998 } else if (IS_IVYBRIDGE(dev)) {
13999 /* FIXME: detect B0+ stepping and use auto training */
14000 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014001 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014002 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014003 } else if (IS_VALLEYVIEW(dev)) {
14004 dev_priv->display.modeset_global_resources =
14005 valleyview_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014006 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014007
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014008 switch (INTEL_INFO(dev)->gen) {
14009 case 2:
14010 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14011 break;
14012
14013 case 3:
14014 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14015 break;
14016
14017 case 4:
14018 case 5:
14019 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14020 break;
14021
14022 case 6:
14023 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14024 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014025 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014026 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014027 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14028 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014029 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014030 /* Drop through - unsupported since execlist only. */
14031 default:
14032 /* Default just returns -ENODEV to indicate unsupported */
14033 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014034 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014035
14036 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014037
14038 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014039}
14040
Jesse Barnesb690e962010-07-19 13:53:12 -070014041/*
14042 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14043 * resume, or other times. This quirk makes sure that's the case for
14044 * affected systems.
14045 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014046static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014047{
14048 struct drm_i915_private *dev_priv = dev->dev_private;
14049
14050 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014051 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014052}
14053
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014054static void quirk_pipeb_force(struct drm_device *dev)
14055{
14056 struct drm_i915_private *dev_priv = dev->dev_private;
14057
14058 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14059 DRM_INFO("applying pipe b force quirk\n");
14060}
14061
Keith Packard435793d2011-07-12 14:56:22 -070014062/*
14063 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14064 */
14065static void quirk_ssc_force_disable(struct drm_device *dev)
14066{
14067 struct drm_i915_private *dev_priv = dev->dev_private;
14068 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014069 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014070}
14071
Carsten Emde4dca20e2012-03-15 15:56:26 +010014072/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014073 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14074 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014075 */
14076static void quirk_invert_brightness(struct drm_device *dev)
14077{
14078 struct drm_i915_private *dev_priv = dev->dev_private;
14079 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014080 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014081}
14082
Scot Doyle9c72cc62014-07-03 23:27:50 +000014083/* Some VBT's incorrectly indicate no backlight is present */
14084static void quirk_backlight_present(struct drm_device *dev)
14085{
14086 struct drm_i915_private *dev_priv = dev->dev_private;
14087 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14088 DRM_INFO("applying backlight present quirk\n");
14089}
14090
Jesse Barnesb690e962010-07-19 13:53:12 -070014091struct intel_quirk {
14092 int device;
14093 int subsystem_vendor;
14094 int subsystem_device;
14095 void (*hook)(struct drm_device *dev);
14096};
14097
Egbert Eich5f85f172012-10-14 15:46:38 +020014098/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14099struct intel_dmi_quirk {
14100 void (*hook)(struct drm_device *dev);
14101 const struct dmi_system_id (*dmi_id_list)[];
14102};
14103
14104static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14105{
14106 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14107 return 1;
14108}
14109
14110static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14111 {
14112 .dmi_id_list = &(const struct dmi_system_id[]) {
14113 {
14114 .callback = intel_dmi_reverse_brightness,
14115 .ident = "NCR Corporation",
14116 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14117 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14118 },
14119 },
14120 { } /* terminating entry */
14121 },
14122 .hook = quirk_invert_brightness,
14123 },
14124};
14125
Ben Widawskyc43b5632012-04-16 14:07:40 -070014126static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014127 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040014128 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070014129
Jesse Barnesb690e962010-07-19 13:53:12 -070014130 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14131 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14132
Jesse Barnesb690e962010-07-19 13:53:12 -070014133 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14134 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14135
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014136 /* 830 needs to leave pipe A & dpll A up */
14137 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14138
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014139 /* 830 needs to leave pipe B & dpll B up */
14140 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14141
Keith Packard435793d2011-07-12 14:56:22 -070014142 /* Lenovo U160 cannot use SSC on LVDS */
14143 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014144
14145 /* Sony Vaio Y cannot use SSC on LVDS */
14146 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014147
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014148 /* Acer Aspire 5734Z must invert backlight brightness */
14149 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14150
14151 /* Acer/eMachines G725 */
14152 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14153
14154 /* Acer/eMachines e725 */
14155 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14156
14157 /* Acer/Packard Bell NCL20 */
14158 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14159
14160 /* Acer Aspire 4736Z */
14161 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014162
14163 /* Acer Aspire 5336 */
14164 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014165
14166 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14167 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014168
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014169 /* Acer C720 Chromebook (Core i3 4005U) */
14170 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14171
jens steinb2a96012014-10-28 20:25:53 +010014172 /* Apple Macbook 2,1 (Core 2 T7400) */
14173 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14174
Scot Doyled4967d82014-07-03 23:27:52 +000014175 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14176 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014177
14178 /* HP Chromebook 14 (Celeron 2955U) */
14179 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014180
14181 /* Dell Chromebook 11 */
14182 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014183};
14184
14185static void intel_init_quirks(struct drm_device *dev)
14186{
14187 struct pci_dev *d = dev->pdev;
14188 int i;
14189
14190 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14191 struct intel_quirk *q = &intel_quirks[i];
14192
14193 if (d->device == q->device &&
14194 (d->subsystem_vendor == q->subsystem_vendor ||
14195 q->subsystem_vendor == PCI_ANY_ID) &&
14196 (d->subsystem_device == q->subsystem_device ||
14197 q->subsystem_device == PCI_ANY_ID))
14198 q->hook(dev);
14199 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014200 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14201 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14202 intel_dmi_quirks[i].hook(dev);
14203 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014204}
14205
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014206/* Disable the VGA plane that we never use */
14207static void i915_disable_vga(struct drm_device *dev)
14208{
14209 struct drm_i915_private *dev_priv = dev->dev_private;
14210 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014211 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014212
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014213 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014214 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014215 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014216 sr1 = inb(VGA_SR_DATA);
14217 outb(sr1 | 1<<5, VGA_SR_DATA);
14218 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14219 udelay(300);
14220
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014221 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014222 POSTING_READ(vga_reg);
14223}
14224
Daniel Vetterf8175862012-04-10 15:50:11 +020014225void intel_modeset_init_hw(struct drm_device *dev)
14226{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014227 intel_prepare_ddi(dev);
14228
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030014229 if (IS_VALLEYVIEW(dev))
14230 vlv_update_cdclk(dev);
14231
Daniel Vetterf8175862012-04-10 15:50:11 +020014232 intel_init_clock_gating(dev);
14233
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014234 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014235}
14236
Jesse Barnes79e53942008-11-07 14:24:08 -080014237void intel_modeset_init(struct drm_device *dev)
14238{
Jesse Barnes652c3932009-08-17 13:31:43 -070014239 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014240 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014241 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014242 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014243
14244 drm_mode_config_init(dev);
14245
14246 dev->mode_config.min_width = 0;
14247 dev->mode_config.min_height = 0;
14248
Dave Airlie019d96c2011-09-29 16:20:42 +010014249 dev->mode_config.preferred_depth = 24;
14250 dev->mode_config.prefer_shadow = 1;
14251
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014252 dev->mode_config.allow_fb_modifiers = true;
14253
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014254 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014255
Jesse Barnesb690e962010-07-19 13:53:12 -070014256 intel_init_quirks(dev);
14257
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014258 intel_init_pm(dev);
14259
Ben Widawskye3c74752013-04-05 13:12:39 -070014260 if (INTEL_INFO(dev)->num_pipes == 0)
14261 return;
14262
Jesse Barnese70236a2009-09-21 10:42:27 -070014263 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014264 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014265
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014266 if (IS_GEN2(dev)) {
14267 dev->mode_config.max_width = 2048;
14268 dev->mode_config.max_height = 2048;
14269 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014270 dev->mode_config.max_width = 4096;
14271 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014272 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014273 dev->mode_config.max_width = 8192;
14274 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014275 }
Damien Lespiau068be562014-03-28 14:17:49 +000014276
Ville Syrjälädc41c152014-08-13 11:57:05 +030014277 if (IS_845G(dev) || IS_I865G(dev)) {
14278 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14279 dev->mode_config.cursor_height = 1023;
14280 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014281 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14282 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14283 } else {
14284 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14285 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14286 }
14287
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014288 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014289
Zhao Yakui28c97732009-10-09 11:39:41 +080014290 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014291 INTEL_INFO(dev)->num_pipes,
14292 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014293
Damien Lespiau055e3932014-08-18 13:49:10 +010014294 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014295 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014296 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014297 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014298 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014299 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014300 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014301 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014302 }
14303
Jesse Barnesf42bb702013-12-16 16:34:23 -080014304 intel_init_dpio(dev);
14305
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014306 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014307
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014308 /* Just disable it once at startup */
14309 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014310 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014311
14312 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014313 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014314
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014315 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014316 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014317 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014318
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014319 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080014320 if (!crtc->active)
14321 continue;
14322
Jesse Barnes46f297f2014-03-07 08:57:48 -080014323 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014324 * Note that reserving the BIOS fb up front prevents us
14325 * from stuffing other stolen allocations like the ring
14326 * on top. This prevents some ugliness at boot time, and
14327 * can even allow for smooth boot transitions if the BIOS
14328 * fb is large enough for the active pipe configuration.
14329 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014330 if (dev_priv->display.get_initial_plane_config) {
14331 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080014332 &crtc->plane_config);
14333 /*
14334 * If the fb is shared between multiple heads, we'll
14335 * just get the first one.
14336 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010014337 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014338 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080014339 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014340}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014341
Daniel Vetter7fad7982012-07-04 17:51:47 +020014342static void intel_enable_pipe_a(struct drm_device *dev)
14343{
14344 struct intel_connector *connector;
14345 struct drm_connector *crt = NULL;
14346 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014347 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014348
14349 /* We can't just switch on the pipe A, we need to set things up with a
14350 * proper mode and output configuration. As a gross hack, enable pipe A
14351 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014352 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014353 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14354 crt = &connector->base;
14355 break;
14356 }
14357 }
14358
14359 if (!crt)
14360 return;
14361
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014362 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014363 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014364}
14365
Daniel Vetterfa555832012-10-10 23:14:00 +020014366static bool
14367intel_check_plane_mapping(struct intel_crtc *crtc)
14368{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014369 struct drm_device *dev = crtc->base.dev;
14370 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014371 u32 reg, val;
14372
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014373 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014374 return true;
14375
14376 reg = DSPCNTR(!crtc->plane);
14377 val = I915_READ(reg);
14378
14379 if ((val & DISPLAY_PLANE_ENABLE) &&
14380 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14381 return false;
14382
14383 return true;
14384}
14385
Daniel Vetter24929352012-07-02 20:28:59 +020014386static void intel_sanitize_crtc(struct intel_crtc *crtc)
14387{
14388 struct drm_device *dev = crtc->base.dev;
14389 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014390 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014391
Daniel Vetter24929352012-07-02 20:28:59 +020014392 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014393 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014394 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14395
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014396 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014397 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014398 if (crtc->active) {
14399 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014400 drm_crtc_vblank_on(&crtc->base);
14401 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014402
Daniel Vetter24929352012-07-02 20:28:59 +020014403 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014404 * disable the crtc (and hence change the state) if it is wrong. Note
14405 * that gen4+ has a fixed plane -> pipe mapping. */
14406 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014407 struct intel_connector *connector;
14408 bool plane;
14409
Daniel Vetter24929352012-07-02 20:28:59 +020014410 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14411 crtc->base.base.id);
14412
14413 /* Pipe has the wrong plane attached and the plane is active.
14414 * Temporarily change the plane mapping and disable everything
14415 * ... */
14416 plane = crtc->plane;
14417 crtc->plane = !plane;
Daniel Vetter9c8958b2014-07-14 19:35:31 +020014418 crtc->primary_enabled = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014419 dev_priv->display.crtc_disable(&crtc->base);
14420 crtc->plane = plane;
14421
14422 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014423 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014424 if (connector->encoder->base.crtc != &crtc->base)
14425 continue;
14426
Egbert Eich7f1950f2014-04-25 10:56:22 +020014427 connector->base.dpms = DRM_MODE_DPMS_OFF;
14428 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014429 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014430 /* multiple connectors may have the same encoder:
14431 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014432 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020014433 if (connector->encoder->base.crtc == &crtc->base) {
14434 connector->encoder->base.crtc = NULL;
14435 connector->encoder->connectors_active = false;
14436 }
Daniel Vetter24929352012-07-02 20:28:59 +020014437
14438 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080014439 crtc->base.state->enable = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014440 crtc->base.enabled = false;
14441 }
Daniel Vetter24929352012-07-02 20:28:59 +020014442
Daniel Vetter7fad7982012-07-04 17:51:47 +020014443 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14444 crtc->pipe == PIPE_A && !crtc->active) {
14445 /* BIOS forgot to enable pipe A, this mostly happens after
14446 * resume. Force-enable the pipe to fix this, the update_dpms
14447 * call below we restore the pipe to the right state, but leave
14448 * the required bits on. */
14449 intel_enable_pipe_a(dev);
14450 }
14451
Daniel Vetter24929352012-07-02 20:28:59 +020014452 /* Adjust the state of the output pipe according to whether we
14453 * have active connectors/encoders. */
14454 intel_crtc_update_dpms(&crtc->base);
14455
Matt Roper83d65732015-02-25 13:12:16 -080014456 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020014457 struct intel_encoder *encoder;
14458
14459 /* This can happen either due to bugs in the get_hw_state
14460 * functions or because the pipe is force-enabled due to the
14461 * pipe A quirk. */
14462 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14463 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014464 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014465 crtc->active ? "enabled" : "disabled");
14466
Matt Roper83d65732015-02-25 13:12:16 -080014467 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014468 crtc->base.enabled = crtc->active;
14469
14470 /* Because we only establish the connector -> encoder ->
14471 * crtc links if something is active, this means the
14472 * crtc is now deactivated. Break the links. connector
14473 * -> encoder links are only establish when things are
14474 * actually up, hence no need to break them. */
14475 WARN_ON(crtc->active);
14476
14477 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14478 WARN_ON(encoder->connectors_active);
14479 encoder->base.crtc = NULL;
14480 }
14481 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014482
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014483 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014484 /*
14485 * We start out with underrun reporting disabled to avoid races.
14486 * For correct bookkeeping mark this on active crtcs.
14487 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014488 * Also on gmch platforms we dont have any hardware bits to
14489 * disable the underrun reporting. Which means we need to start
14490 * out with underrun reporting disabled also on inactive pipes,
14491 * since otherwise we'll complain about the garbage we read when
14492 * e.g. coming up after runtime pm.
14493 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014494 * No protection against concurrent access is required - at
14495 * worst a fifo underrun happens which also sets this to false.
14496 */
14497 crtc->cpu_fifo_underrun_disabled = true;
14498 crtc->pch_fifo_underrun_disabled = true;
14499 }
Daniel Vetter24929352012-07-02 20:28:59 +020014500}
14501
14502static void intel_sanitize_encoder(struct intel_encoder *encoder)
14503{
14504 struct intel_connector *connector;
14505 struct drm_device *dev = encoder->base.dev;
14506
14507 /* We need to check both for a crtc link (meaning that the
14508 * encoder is active and trying to read from a pipe) and the
14509 * pipe itself being active. */
14510 bool has_active_crtc = encoder->base.crtc &&
14511 to_intel_crtc(encoder->base.crtc)->active;
14512
14513 if (encoder->connectors_active && !has_active_crtc) {
14514 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14515 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014516 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014517
14518 /* Connector is active, but has no active pipe. This is
14519 * fallout from our resume register restoring. Disable
14520 * the encoder manually again. */
14521 if (encoder->base.crtc) {
14522 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14523 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014524 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014525 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014526 if (encoder->post_disable)
14527 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014528 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014529 encoder->base.crtc = NULL;
14530 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014531
14532 /* Inconsistent output/port/pipe state happens presumably due to
14533 * a bug in one of the get_hw_state functions. Or someplace else
14534 * in our code, like the register restore mess on resume. Clamp
14535 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014536 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014537 if (connector->encoder != encoder)
14538 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014539 connector->base.dpms = DRM_MODE_DPMS_OFF;
14540 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014541 }
14542 }
14543 /* Enabled encoders without active connectors will be fixed in
14544 * the crtc fixup. */
14545}
14546
Imre Deak04098752014-02-18 00:02:16 +020014547void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014548{
14549 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014550 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014551
Imre Deak04098752014-02-18 00:02:16 +020014552 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14553 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14554 i915_disable_vga(dev);
14555 }
14556}
14557
14558void i915_redisable_vga(struct drm_device *dev)
14559{
14560 struct drm_i915_private *dev_priv = dev->dev_private;
14561
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014562 /* This function can be called both from intel_modeset_setup_hw_state or
14563 * at a very early point in our resume sequence, where the power well
14564 * structures are not yet restored. Since this function is at a very
14565 * paranoid "someone might have enabled VGA while we were not looking"
14566 * level, just check if the power well is enabled instead of trying to
14567 * follow the "don't touch the power well if we don't need it" policy
14568 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014569 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014570 return;
14571
Imre Deak04098752014-02-18 00:02:16 +020014572 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014573}
14574
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014575static bool primary_get_hw_state(struct intel_crtc *crtc)
14576{
14577 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14578
14579 if (!crtc->active)
14580 return false;
14581
14582 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
14583}
14584
Daniel Vetter30e984d2013-06-05 13:34:17 +020014585static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020014586{
14587 struct drm_i915_private *dev_priv = dev->dev_private;
14588 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020014589 struct intel_crtc *crtc;
14590 struct intel_encoder *encoder;
14591 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020014592 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020014593
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014594 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014595 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020014596
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014597 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020014598
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014599 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014600 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014601
Matt Roper83d65732015-02-25 13:12:16 -080014602 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014603 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014604 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020014605
14606 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
14607 crtc->base.base.id,
14608 crtc->active ? "enabled" : "disabled");
14609 }
14610
Daniel Vetter53589012013-06-05 13:34:16 +020014611 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14612 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14613
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014614 pll->on = pll->get_hw_state(dev_priv, pll,
14615 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020014616 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014617 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014618 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014619 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020014620 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014621 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014622 }
Daniel Vetter53589012013-06-05 13:34:16 +020014623 }
Daniel Vetter53589012013-06-05 13:34:16 +020014624
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014625 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014626 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014627
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014628 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014629 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020014630 }
14631
Damien Lespiaub2784e12014-08-05 11:29:37 +010014632 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014633 pipe = 0;
14634
14635 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070014636 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14637 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014638 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014639 } else {
14640 encoder->base.crtc = NULL;
14641 }
14642
14643 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014644 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020014645 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014646 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014647 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014648 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020014649 }
14650
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014651 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014652 if (connector->get_hw_state(connector)) {
14653 connector->base.dpms = DRM_MODE_DPMS_ON;
14654 connector->encoder->connectors_active = true;
14655 connector->base.encoder = &connector->encoder->base;
14656 } else {
14657 connector->base.dpms = DRM_MODE_DPMS_OFF;
14658 connector->base.encoder = NULL;
14659 }
14660 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
14661 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030014662 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014663 connector->base.encoder ? "enabled" : "disabled");
14664 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020014665}
14666
14667/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
14668 * and i915 state tracking structures. */
14669void intel_modeset_setup_hw_state(struct drm_device *dev,
14670 bool force_restore)
14671{
14672 struct drm_i915_private *dev_priv = dev->dev_private;
14673 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014674 struct intel_crtc *crtc;
14675 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020014676 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014677
14678 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020014679
Jesse Barnesbabea612013-06-26 18:57:38 +030014680 /*
14681 * Now that we have the config, copy it to each CRTC struct
14682 * Note that this could go away if we move to using crtc_config
14683 * checking everywhere.
14684 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014685 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020014686 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014687 intel_mode_from_pipe_config(&crtc->base.mode,
14688 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030014689 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
14690 crtc->base.base.id);
14691 drm_mode_debug_printmodeline(&crtc->base.mode);
14692 }
14693 }
14694
Daniel Vetter24929352012-07-02 20:28:59 +020014695 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010014696 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014697 intel_sanitize_encoder(encoder);
14698 }
14699
Damien Lespiau055e3932014-08-18 13:49:10 +010014700 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020014701 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14702 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014703 intel_dump_pipe_config(crtc, crtc->config,
14704 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020014705 }
Daniel Vetter9a935852012-07-05 22:34:27 +020014706
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020014707 intel_modeset_update_connector_atomic_state(dev);
14708
Daniel Vetter35c95372013-07-17 06:55:04 +020014709 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14710 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14711
14712 if (!pll->on || pll->active)
14713 continue;
14714
14715 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
14716
14717 pll->disable(dev_priv, pll);
14718 pll->on = false;
14719 }
14720
Pradeep Bhat30789992014-11-04 17:06:45 +000014721 if (IS_GEN9(dev))
14722 skl_wm_get_hw_state(dev);
14723 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030014724 ilk_wm_get_hw_state(dev);
14725
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010014726 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030014727 i915_redisable_vga(dev);
14728
Daniel Vetterf30da182013-04-11 20:22:50 +020014729 /*
14730 * We need to use raw interfaces for restoring state to avoid
14731 * checking (bogus) intermediate states.
14732 */
Damien Lespiau055e3932014-08-18 13:49:10 +010014733 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070014734 struct drm_crtc *crtc =
14735 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020014736
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014737 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010014738 }
14739 } else {
14740 intel_modeset_update_staged_output_state(dev);
14741 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020014742
14743 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010014744}
14745
14746void intel_modeset_gem_init(struct drm_device *dev)
14747{
Jesse Barnes92122782014-10-09 12:57:42 -070014748 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080014749 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070014750 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -080014751
Imre Deakae484342014-03-31 15:10:44 +030014752 mutex_lock(&dev->struct_mutex);
14753 intel_init_gt_powersave(dev);
14754 mutex_unlock(&dev->struct_mutex);
14755
Jesse Barnes92122782014-10-09 12:57:42 -070014756 /*
14757 * There may be no VBT; and if the BIOS enabled SSC we can
14758 * just keep using it to avoid unnecessary flicker. Whereas if the
14759 * BIOS isn't using it, don't assume it will work even if the VBT
14760 * indicates as much.
14761 */
14762 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
14763 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14764 DREF_SSC1_ENABLE);
14765
Chris Wilson1833b132012-05-09 11:56:28 +010014766 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020014767
14768 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080014769
14770 /*
14771 * Make sure any fbs we allocated at startup are properly
14772 * pinned & fenced. When we do the allocation it's too early
14773 * for this.
14774 */
14775 mutex_lock(&dev->struct_mutex);
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010014776 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070014777 obj = intel_fb_obj(c->primary->fb);
14778 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080014779 continue;
14780
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +000014781 if (intel_pin_and_fence_fb_obj(c->primary,
14782 c->primary->fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000014783 c->primary->state,
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +000014784 NULL)) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080014785 DRM_ERROR("failed to pin boot fb on pipe %d\n",
14786 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100014787 drm_framebuffer_unreference(c->primary->fb);
14788 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080014789 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080014790 }
14791 }
14792 mutex_unlock(&dev->struct_mutex);
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020014793
14794 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014795}
14796
Imre Deak4932e2c2014-02-11 17:12:48 +020014797void intel_connector_unregister(struct intel_connector *intel_connector)
14798{
14799 struct drm_connector *connector = &intel_connector->base;
14800
14801 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010014802 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020014803}
14804
Jesse Barnes79e53942008-11-07 14:24:08 -080014805void intel_modeset_cleanup(struct drm_device *dev)
14806{
Jesse Barnes652c3932009-08-17 13:31:43 -070014807 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030014808 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070014809
Imre Deak2eb52522014-11-19 15:30:05 +020014810 intel_disable_gt_powersave(dev);
14811
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020014812 intel_backlight_unregister(dev);
14813
Daniel Vetterfd0c0642013-04-24 11:13:35 +020014814 /*
14815 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020014816 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020014817 * experience fancy races otherwise.
14818 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020014819 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070014820
Daniel Vetterfd0c0642013-04-24 11:13:35 +020014821 /*
14822 * Due to the hpd irq storm handling the hotplug work can re-arm the
14823 * poll handlers. Hence disable polling after hpd handling is shut down.
14824 */
Keith Packardf87ea762010-10-03 19:36:26 -070014825 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020014826
Jesse Barnes652c3932009-08-17 13:31:43 -070014827 mutex_lock(&dev->struct_mutex);
14828
Jesse Barnes723bfd72010-10-07 16:01:13 -070014829 intel_unregister_dsm_handler();
14830
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014831 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014832
Kristian Høgsberg69341a52009-11-11 12:19:17 -050014833 mutex_unlock(&dev->struct_mutex);
14834
Chris Wilson1630fe72011-07-08 12:22:42 +010014835 /* flush any delayed tasks or pending work */
14836 flush_scheduled_work();
14837
Jani Nikuladb31af1d2013-11-08 16:48:53 +020014838 /* destroy the backlight and sysfs files before encoders/connectors */
14839 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020014840 struct intel_connector *intel_connector;
14841
14842 intel_connector = to_intel_connector(connector);
14843 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020014844 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030014845
Jesse Barnes79e53942008-11-07 14:24:08 -080014846 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010014847
14848 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030014849
14850 mutex_lock(&dev->struct_mutex);
14851 intel_cleanup_gt_powersave(dev);
14852 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014853}
14854
Dave Airlie28d52042009-09-21 14:33:58 +100014855/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080014856 * Return which encoder is currently attached for connector.
14857 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010014858struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080014859{
Chris Wilsondf0e9242010-09-09 16:20:55 +010014860 return &intel_attached_encoder(connector)->base;
14861}
Jesse Barnes79e53942008-11-07 14:24:08 -080014862
Chris Wilsondf0e9242010-09-09 16:20:55 +010014863void intel_connector_attach_encoder(struct intel_connector *connector,
14864 struct intel_encoder *encoder)
14865{
14866 connector->encoder = encoder;
14867 drm_mode_connector_attach_encoder(&connector->base,
14868 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080014869}
Dave Airlie28d52042009-09-21 14:33:58 +100014870
14871/*
14872 * set vga decode state - true == enable VGA decode
14873 */
14874int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
14875{
14876 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000014877 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100014878 u16 gmch_ctrl;
14879
Chris Wilson75fa0412014-02-07 18:37:02 -020014880 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
14881 DRM_ERROR("failed to read control word\n");
14882 return -EIO;
14883 }
14884
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020014885 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
14886 return 0;
14887
Dave Airlie28d52042009-09-21 14:33:58 +100014888 if (state)
14889 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
14890 else
14891 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020014892
14893 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
14894 DRM_ERROR("failed to write control word\n");
14895 return -EIO;
14896 }
14897
Dave Airlie28d52042009-09-21 14:33:58 +100014898 return 0;
14899}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014900
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014901struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030014902
14903 u32 power_well_driver;
14904
Chris Wilson63b66e52013-08-08 15:12:06 +020014905 int num_transcoders;
14906
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014907 struct intel_cursor_error_state {
14908 u32 control;
14909 u32 position;
14910 u32 base;
14911 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010014912 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014913
14914 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020014915 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014916 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030014917 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010014918 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014919
14920 struct intel_plane_error_state {
14921 u32 control;
14922 u32 stride;
14923 u32 size;
14924 u32 pos;
14925 u32 addr;
14926 u32 surface;
14927 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010014928 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020014929
14930 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020014931 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020014932 enum transcoder cpu_transcoder;
14933
14934 u32 conf;
14935
14936 u32 htotal;
14937 u32 hblank;
14938 u32 hsync;
14939 u32 vtotal;
14940 u32 vblank;
14941 u32 vsync;
14942 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014943};
14944
14945struct intel_display_error_state *
14946intel_display_capture_error_state(struct drm_device *dev)
14947{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014948 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014949 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020014950 int transcoders[] = {
14951 TRANSCODER_A,
14952 TRANSCODER_B,
14953 TRANSCODER_C,
14954 TRANSCODER_EDP,
14955 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014956 int i;
14957
Chris Wilson63b66e52013-08-08 15:12:06 +020014958 if (INTEL_INFO(dev)->num_pipes == 0)
14959 return NULL;
14960
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020014961 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014962 if (error == NULL)
14963 return NULL;
14964
Imre Deak190be112013-11-25 17:15:31 +020014965 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030014966 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
14967
Damien Lespiau055e3932014-08-18 13:49:10 +010014968 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020014969 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014970 __intel_display_power_is_enabled(dev_priv,
14971 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020014972 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020014973 continue;
14974
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030014975 error->cursor[i].control = I915_READ(CURCNTR(i));
14976 error->cursor[i].position = I915_READ(CURPOS(i));
14977 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014978
14979 error->plane[i].control = I915_READ(DSPCNTR(i));
14980 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014981 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030014982 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014983 error->plane[i].pos = I915_READ(DSPPOS(i));
14984 }
Paulo Zanonica291362013-03-06 20:03:14 -030014985 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
14986 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014987 if (INTEL_INFO(dev)->gen >= 4) {
14988 error->plane[i].surface = I915_READ(DSPSURF(i));
14989 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
14990 }
14991
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014992 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030014993
Sonika Jindal3abfce72014-07-21 15:23:43 +053014994 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030014995 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020014996 }
14997
14998 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
14999 if (HAS_DDI(dev_priv->dev))
15000 error->num_transcoders++; /* Account for eDP. */
15001
15002 for (i = 0; i < error->num_transcoders; i++) {
15003 enum transcoder cpu_transcoder = transcoders[i];
15004
Imre Deakddf9c532013-11-27 22:02:02 +020015005 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015006 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015007 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015008 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015009 continue;
15010
Chris Wilson63b66e52013-08-08 15:12:06 +020015011 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15012
15013 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15014 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15015 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15016 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15017 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15018 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15019 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015020 }
15021
15022 return error;
15023}
15024
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015025#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15026
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015027void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015028intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015029 struct drm_device *dev,
15030 struct intel_display_error_state *error)
15031{
Damien Lespiau055e3932014-08-18 13:49:10 +010015032 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015033 int i;
15034
Chris Wilson63b66e52013-08-08 15:12:06 +020015035 if (!error)
15036 return;
15037
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015038 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015039 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015040 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015041 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015042 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015043 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015044 err_printf(m, " Power: %s\n",
15045 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015046 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015047 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015048
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015049 err_printf(m, "Plane [%d]:\n", i);
15050 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15051 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015052 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015053 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15054 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015055 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015056 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015057 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015058 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015059 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15060 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015061 }
15062
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015063 err_printf(m, "Cursor [%d]:\n", i);
15064 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15065 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15066 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015067 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015068
15069 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015070 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015071 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015072 err_printf(m, " Power: %s\n",
15073 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015074 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15075 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15076 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15077 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15078 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15079 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15080 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15081 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015082}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015083
15084void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15085{
15086 struct intel_crtc *crtc;
15087
15088 for_each_intel_crtc(dev, crtc) {
15089 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015090
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015091 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015092
15093 work = crtc->unpin_work;
15094
15095 if (work && work->event &&
15096 work->event->base.file_priv == file) {
15097 kfree(work->event);
15098 work->event = NULL;
15099 }
15100
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015101 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015102 }
15103}