blob: 48129fa9360d19b456ee6c79e39af7e94afa1c43 [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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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
Damien Lespiau70d21f02013-07-03 21:06:04 +01002934static void skylake_update_primary_plane(struct drm_crtc *crtc,
2935 struct drm_framebuffer *fb,
2936 int x, int y)
2937{
2938 struct drm_device *dev = crtc->dev;
2939 struct drm_i915_private *dev_priv = dev->dev_private;
2940 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002941 struct drm_i915_gem_object *obj;
2942 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302943 u32 plane_ctl, stride_div, stride;
2944 u32 tile_height, plane_offset, plane_size;
2945 unsigned int rotation;
2946 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002947 unsigned long surf_addr;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302948 struct drm_plane *plane;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002949
2950 if (!intel_crtc->primary_enabled) {
2951 I915_WRITE(PLANE_CTL(pipe, 0), 0);
2952 I915_WRITE(PLANE_SURF(pipe, 0), 0);
2953 POSTING_READ(PLANE_CTL(pipe, 0));
2954 return;
2955 }
2956
2957 plane_ctl = PLANE_CTL_ENABLE |
2958 PLANE_CTL_PIPE_GAMMA_ENABLE |
2959 PLANE_CTL_PIPE_CSC_ENABLE;
2960
2961 switch (fb->pixel_format) {
2962 case DRM_FORMAT_RGB565:
2963 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
2964 break;
2965 case DRM_FORMAT_XRGB8888:
2966 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2967 break;
Jani Nikulaf75fb422015-02-10 13:15:49 +02002968 case DRM_FORMAT_ARGB8888:
2969 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2970 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2971 break;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002972 case DRM_FORMAT_XBGR8888:
2973 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2974 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2975 break;
Jani Nikulaf75fb422015-02-10 13:15:49 +02002976 case DRM_FORMAT_ABGR8888:
2977 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2978 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2979 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2980 break;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002981 case DRM_FORMAT_XRGB2101010:
2982 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2983 break;
2984 case DRM_FORMAT_XBGR2101010:
2985 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2986 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2987 break;
2988 default:
2989 BUG();
2990 }
2991
Daniel Vetter30af77c2015-02-10 17:16:11 +00002992 switch (fb->modifier[0]) {
2993 case DRM_FORMAT_MOD_NONE:
Damien Lespiau70d21f02013-07-03 21:06:04 +01002994 break;
Daniel Vetter30af77c2015-02-10 17:16:11 +00002995 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau70d21f02013-07-03 21:06:04 +01002996 plane_ctl |= PLANE_CTL_TILED_X;
Damien Lespiaub3218032015-02-27 11:15:18 +00002997 break;
2998 case I915_FORMAT_MOD_Y_TILED:
2999 plane_ctl |= PLANE_CTL_TILED_Y;
3000 break;
3001 case I915_FORMAT_MOD_Yf_TILED:
3002 plane_ctl |= PLANE_CTL_TILED_YF;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003003 break;
3004 default:
Damien Lespiaub3218032015-02-27 11:15:18 +00003005 MISSING_CASE(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003006 }
3007
3008 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303009
3010 plane = crtc->primary;
3011 rotation = plane->state->rotation;
3012 switch (rotation) {
3013 case BIT(DRM_ROTATE_90):
3014 plane_ctl |= PLANE_CTL_ROTATE_90;
3015 break;
3016
3017 case BIT(DRM_ROTATE_180):
Sonika Jindal1447dde2014-10-04 10:53:31 +01003018 plane_ctl |= PLANE_CTL_ROTATE_180;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303019 break;
3020
3021 case BIT(DRM_ROTATE_270):
3022 plane_ctl |= PLANE_CTL_ROTATE_270;
3023 break;
3024 }
Damien Lespiau70d21f02013-07-03 21:06:04 +01003025
Damien Lespiaub3218032015-02-27 11:15:18 +00003026 obj = intel_fb_obj(fb);
3027 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3028 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303029 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3030
3031 if (intel_rotation_90_or_270(rotation)) {
3032 /* stride = Surface height in tiles */
3033 tile_height = intel_tile_height(dev, fb->bits_per_pixel,
3034 fb->modifier[0]);
3035 stride = DIV_ROUND_UP(fb->height, tile_height);
3036 x_offset = stride * tile_height - y - (plane->state->src_h >> 16);
3037 y_offset = x;
3038 plane_size = ((plane->state->src_w >> 16) - 1) << 16 |
3039 ((plane->state->src_h >> 16) - 1);
3040 } else {
3041 stride = fb->pitches[0] / stride_div;
3042 x_offset = x;
3043 y_offset = y;
3044 plane_size = ((plane->state->src_h >> 16) - 1) << 16 |
3045 ((plane->state->src_w >> 16) - 1);
3046 }
3047 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003048
Damien Lespiau70d21f02013-07-03 21:06:04 +01003049 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003050 I915_WRITE(PLANE_POS(pipe, 0), 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303051 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3052 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3053 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003054 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003055
3056 POSTING_READ(PLANE_SURF(pipe, 0));
3057}
3058
Jesse Barnes17638cd2011-06-24 12:19:23 -07003059/* Assume fb object is pinned & idle & fenced and just update base pointers */
3060static int
3061intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3062 int x, int y, enum mode_set_atomic state)
3063{
3064 struct drm_device *dev = crtc->dev;
3065 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003066
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003067 if (dev_priv->display.disable_fbc)
3068 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003069
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003070 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3071
3072 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003073}
3074
Ville Syrjälä75147472014-11-24 18:28:11 +02003075static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003076{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003077 struct drm_crtc *crtc;
3078
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003079 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003080 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3081 enum plane plane = intel_crtc->plane;
3082
3083 intel_prepare_page_flip(dev, plane);
3084 intel_finish_page_flip_plane(dev, plane);
3085 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003086}
3087
3088static void intel_update_primary_planes(struct drm_device *dev)
3089{
3090 struct drm_i915_private *dev_priv = dev->dev_private;
3091 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003092
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003093 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003094 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3095
Rob Clark51fd3712013-11-19 12:10:12 -05003096 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003097 /*
3098 * FIXME: Once we have proper support for primary planes (and
3099 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003100 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003101 */
Matt Roperf4510a22014-04-01 15:22:40 -07003102 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003103 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003104 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003105 crtc->x,
3106 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003107 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003108 }
3109}
3110
Ville Syrjälä75147472014-11-24 18:28:11 +02003111void intel_prepare_reset(struct drm_device *dev)
3112{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003113 struct drm_i915_private *dev_priv = to_i915(dev);
3114 struct intel_crtc *crtc;
3115
Ville Syrjälä75147472014-11-24 18:28:11 +02003116 /* no reset support for gen2 */
3117 if (IS_GEN2(dev))
3118 return;
3119
3120 /* reset doesn't touch the display */
3121 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3122 return;
3123
3124 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003125
3126 /*
3127 * Disabling the crtcs gracefully seems nicer. Also the
3128 * g33 docs say we should at least disable all the planes.
3129 */
3130 for_each_intel_crtc(dev, crtc) {
3131 if (crtc->active)
3132 dev_priv->display.crtc_disable(&crtc->base);
3133 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003134}
3135
3136void intel_finish_reset(struct drm_device *dev)
3137{
3138 struct drm_i915_private *dev_priv = to_i915(dev);
3139
3140 /*
3141 * Flips in the rings will be nuked by the reset,
3142 * so complete all pending flips so that user space
3143 * will get its events and not get stuck.
3144 */
3145 intel_complete_page_flips(dev);
3146
3147 /* no reset support for gen2 */
3148 if (IS_GEN2(dev))
3149 return;
3150
3151 /* reset doesn't touch the display */
3152 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3153 /*
3154 * Flips in the rings have been nuked by the reset,
3155 * so update the base address of all primary
3156 * planes to the the last fb to make sure we're
3157 * showing the correct fb after a reset.
3158 */
3159 intel_update_primary_planes(dev);
3160 return;
3161 }
3162
3163 /*
3164 * The display has been reset as well,
3165 * so need a full re-initialization.
3166 */
3167 intel_runtime_pm_disable_interrupts(dev_priv);
3168 intel_runtime_pm_enable_interrupts(dev_priv);
3169
3170 intel_modeset_init_hw(dev);
3171
3172 spin_lock_irq(&dev_priv->irq_lock);
3173 if (dev_priv->display.hpd_irq_setup)
3174 dev_priv->display.hpd_irq_setup(dev);
3175 spin_unlock_irq(&dev_priv->irq_lock);
3176
3177 intel_modeset_setup_hw_state(dev, true);
3178
3179 intel_hpd_init(dev_priv);
3180
3181 drm_modeset_unlock_all(dev);
3182}
3183
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003184static int
Chris Wilson14667a42012-04-03 17:58:35 +01003185intel_finish_fb(struct drm_framebuffer *old_fb)
3186{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003187 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01003188 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3189 bool was_interruptible = dev_priv->mm.interruptible;
3190 int ret;
3191
Chris Wilson14667a42012-04-03 17:58:35 +01003192 /* Big Hammer, we also need to ensure that any pending
3193 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3194 * current scanout is retired before unpinning the old
3195 * framebuffer.
3196 *
3197 * This should only fail upon a hung GPU, in which case we
3198 * can safely continue.
3199 */
3200 dev_priv->mm.interruptible = false;
3201 ret = i915_gem_object_finish_gpu(obj);
3202 dev_priv->mm.interruptible = was_interruptible;
3203
3204 return ret;
3205}
3206
Chris Wilson7d5e3792014-03-04 13:15:08 +00003207static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3208{
3209 struct drm_device *dev = crtc->dev;
3210 struct drm_i915_private *dev_priv = dev->dev_private;
3211 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003212 bool pending;
3213
3214 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3215 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3216 return false;
3217
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003218 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003219 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003220 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003221
3222 return pending;
3223}
3224
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003225static void intel_update_pipe_size(struct intel_crtc *crtc)
3226{
3227 struct drm_device *dev = crtc->base.dev;
3228 struct drm_i915_private *dev_priv = dev->dev_private;
3229 const struct drm_display_mode *adjusted_mode;
3230
3231 if (!i915.fastboot)
3232 return;
3233
3234 /*
3235 * Update pipe size and adjust fitter if needed: the reason for this is
3236 * that in compute_mode_changes we check the native mode (not the pfit
3237 * mode) to see if we can flip rather than do a full mode set. In the
3238 * fastboot case, we'll flip, but if we don't update the pipesrc and
3239 * pfit state, we'll end up with a big fb scanned out into the wrong
3240 * sized surface.
3241 *
3242 * To fix this properly, we need to hoist the checks up into
3243 * compute_mode_changes (or above), check the actual pfit state and
3244 * whether the platform allows pfit disable with pipe active, and only
3245 * then update the pipesrc and pfit state, even on the flip path.
3246 */
3247
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003248 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003249
3250 I915_WRITE(PIPESRC(crtc->pipe),
3251 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3252 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003253 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003254 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3255 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003256 I915_WRITE(PF_CTL(crtc->pipe), 0);
3257 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3258 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3259 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003260 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3261 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003262}
3263
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003264static void intel_fdi_normal_train(struct drm_crtc *crtc)
3265{
3266 struct drm_device *dev = crtc->dev;
3267 struct drm_i915_private *dev_priv = dev->dev_private;
3268 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3269 int pipe = intel_crtc->pipe;
3270 u32 reg, temp;
3271
3272 /* enable normal train */
3273 reg = FDI_TX_CTL(pipe);
3274 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003275 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003276 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3277 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003278 } else {
3279 temp &= ~FDI_LINK_TRAIN_NONE;
3280 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003281 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003282 I915_WRITE(reg, temp);
3283
3284 reg = FDI_RX_CTL(pipe);
3285 temp = I915_READ(reg);
3286 if (HAS_PCH_CPT(dev)) {
3287 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3288 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3289 } else {
3290 temp &= ~FDI_LINK_TRAIN_NONE;
3291 temp |= FDI_LINK_TRAIN_NONE;
3292 }
3293 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3294
3295 /* wait one idle pattern time */
3296 POSTING_READ(reg);
3297 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003298
3299 /* IVB wants error correction enabled */
3300 if (IS_IVYBRIDGE(dev))
3301 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3302 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003303}
3304
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003305/* The FDI link training functions for ILK/Ibexpeak. */
3306static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3307{
3308 struct drm_device *dev = crtc->dev;
3309 struct drm_i915_private *dev_priv = dev->dev_private;
3310 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3311 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003312 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003313
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003314 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003315 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003316
Adam Jacksone1a44742010-06-25 15:32:14 -04003317 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3318 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003319 reg = FDI_RX_IMR(pipe);
3320 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003321 temp &= ~FDI_RX_SYMBOL_LOCK;
3322 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003323 I915_WRITE(reg, temp);
3324 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003325 udelay(150);
3326
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003327 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003328 reg = FDI_TX_CTL(pipe);
3329 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003330 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003331 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003332 temp &= ~FDI_LINK_TRAIN_NONE;
3333 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003334 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003335
Chris Wilson5eddb702010-09-11 13:48:45 +01003336 reg = FDI_RX_CTL(pipe);
3337 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003338 temp &= ~FDI_LINK_TRAIN_NONE;
3339 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003340 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3341
3342 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003343 udelay(150);
3344
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003345 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003346 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3347 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3348 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003349
Chris Wilson5eddb702010-09-11 13:48:45 +01003350 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003351 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003352 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003353 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3354
3355 if ((temp & FDI_RX_BIT_LOCK)) {
3356 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003357 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003358 break;
3359 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003360 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003361 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003362 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003363
3364 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003365 reg = FDI_TX_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_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003369 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003370
Chris Wilson5eddb702010-09-11 13:48:45 +01003371 reg = FDI_RX_CTL(pipe);
3372 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003373 temp &= ~FDI_LINK_TRAIN_NONE;
3374 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003375 I915_WRITE(reg, temp);
3376
3377 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003378 udelay(150);
3379
Chris Wilson5eddb702010-09-11 13:48:45 +01003380 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003381 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003382 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003383 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3384
3385 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003386 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003387 DRM_DEBUG_KMS("FDI train 2 done.\n");
3388 break;
3389 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003390 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003391 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003392 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003393
3394 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003395
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003396}
3397
Akshay Joshi0206e352011-08-16 15:34:10 -04003398static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003399 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3400 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3401 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3402 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3403};
3404
3405/* The FDI link training functions for SNB/Cougarpoint. */
3406static void gen6_fdi_link_train(struct drm_crtc *crtc)
3407{
3408 struct drm_device *dev = crtc->dev;
3409 struct drm_i915_private *dev_priv = dev->dev_private;
3410 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3411 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003412 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003413
Adam Jacksone1a44742010-06-25 15:32:14 -04003414 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3415 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003416 reg = FDI_RX_IMR(pipe);
3417 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003418 temp &= ~FDI_RX_SYMBOL_LOCK;
3419 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003420 I915_WRITE(reg, temp);
3421
3422 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003423 udelay(150);
3424
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003425 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003426 reg = FDI_TX_CTL(pipe);
3427 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003428 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003429 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003430 temp &= ~FDI_LINK_TRAIN_NONE;
3431 temp |= FDI_LINK_TRAIN_PATTERN_1;
3432 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3433 /* SNB-B */
3434 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003435 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003436
Daniel Vetterd74cf322012-10-26 10:58:13 +02003437 I915_WRITE(FDI_RX_MISC(pipe),
3438 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3439
Chris Wilson5eddb702010-09-11 13:48:45 +01003440 reg = FDI_RX_CTL(pipe);
3441 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003442 if (HAS_PCH_CPT(dev)) {
3443 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3444 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3445 } else {
3446 temp &= ~FDI_LINK_TRAIN_NONE;
3447 temp |= FDI_LINK_TRAIN_PATTERN_1;
3448 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003449 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3450
3451 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003452 udelay(150);
3453
Akshay Joshi0206e352011-08-16 15:34:10 -04003454 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003455 reg = FDI_TX_CTL(pipe);
3456 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003457 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3458 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003459 I915_WRITE(reg, temp);
3460
3461 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003462 udelay(500);
3463
Sean Paulfa37d392012-03-02 12:53:39 -05003464 for (retry = 0; retry < 5; retry++) {
3465 reg = FDI_RX_IIR(pipe);
3466 temp = I915_READ(reg);
3467 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3468 if (temp & FDI_RX_BIT_LOCK) {
3469 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3470 DRM_DEBUG_KMS("FDI train 1 done.\n");
3471 break;
3472 }
3473 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003474 }
Sean Paulfa37d392012-03-02 12:53:39 -05003475 if (retry < 5)
3476 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003477 }
3478 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003479 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003480
3481 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003482 reg = FDI_TX_CTL(pipe);
3483 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003484 temp &= ~FDI_LINK_TRAIN_NONE;
3485 temp |= FDI_LINK_TRAIN_PATTERN_2;
3486 if (IS_GEN6(dev)) {
3487 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3488 /* SNB-B */
3489 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3490 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003491 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003492
Chris Wilson5eddb702010-09-11 13:48:45 +01003493 reg = FDI_RX_CTL(pipe);
3494 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003495 if (HAS_PCH_CPT(dev)) {
3496 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3497 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3498 } else {
3499 temp &= ~FDI_LINK_TRAIN_NONE;
3500 temp |= FDI_LINK_TRAIN_PATTERN_2;
3501 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003502 I915_WRITE(reg, temp);
3503
3504 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003505 udelay(150);
3506
Akshay Joshi0206e352011-08-16 15:34:10 -04003507 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003508 reg = FDI_TX_CTL(pipe);
3509 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003510 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3511 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003512 I915_WRITE(reg, temp);
3513
3514 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003515 udelay(500);
3516
Sean Paulfa37d392012-03-02 12:53:39 -05003517 for (retry = 0; retry < 5; retry++) {
3518 reg = FDI_RX_IIR(pipe);
3519 temp = I915_READ(reg);
3520 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3521 if (temp & FDI_RX_SYMBOL_LOCK) {
3522 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3523 DRM_DEBUG_KMS("FDI train 2 done.\n");
3524 break;
3525 }
3526 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003527 }
Sean Paulfa37d392012-03-02 12:53:39 -05003528 if (retry < 5)
3529 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003530 }
3531 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003532 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003533
3534 DRM_DEBUG_KMS("FDI train done.\n");
3535}
3536
Jesse Barnes357555c2011-04-28 15:09:55 -07003537/* Manual link training for Ivy Bridge A0 parts */
3538static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3539{
3540 struct drm_device *dev = crtc->dev;
3541 struct drm_i915_private *dev_priv = dev->dev_private;
3542 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3543 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003544 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003545
3546 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3547 for train result */
3548 reg = FDI_RX_IMR(pipe);
3549 temp = I915_READ(reg);
3550 temp &= ~FDI_RX_SYMBOL_LOCK;
3551 temp &= ~FDI_RX_BIT_LOCK;
3552 I915_WRITE(reg, temp);
3553
3554 POSTING_READ(reg);
3555 udelay(150);
3556
Daniel Vetter01a415f2012-10-27 15:58:40 +02003557 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3558 I915_READ(FDI_RX_IIR(pipe)));
3559
Jesse Barnes139ccd32013-08-19 11:04:55 -07003560 /* Try each vswing and preemphasis setting twice before moving on */
3561 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3562 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003563 reg = FDI_TX_CTL(pipe);
3564 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003565 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3566 temp &= ~FDI_TX_ENABLE;
3567 I915_WRITE(reg, temp);
3568
3569 reg = FDI_RX_CTL(pipe);
3570 temp = I915_READ(reg);
3571 temp &= ~FDI_LINK_TRAIN_AUTO;
3572 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3573 temp &= ~FDI_RX_ENABLE;
3574 I915_WRITE(reg, temp);
3575
3576 /* enable CPU FDI TX and PCH FDI RX */
3577 reg = FDI_TX_CTL(pipe);
3578 temp = I915_READ(reg);
3579 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003580 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003581 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003582 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003583 temp |= snb_b_fdi_train_param[j/2];
3584 temp |= FDI_COMPOSITE_SYNC;
3585 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3586
3587 I915_WRITE(FDI_RX_MISC(pipe),
3588 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3589
3590 reg = FDI_RX_CTL(pipe);
3591 temp = I915_READ(reg);
3592 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3593 temp |= FDI_COMPOSITE_SYNC;
3594 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3595
3596 POSTING_READ(reg);
3597 udelay(1); /* should be 0.5us */
3598
3599 for (i = 0; i < 4; i++) {
3600 reg = FDI_RX_IIR(pipe);
3601 temp = I915_READ(reg);
3602 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3603
3604 if (temp & FDI_RX_BIT_LOCK ||
3605 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3606 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3607 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3608 i);
3609 break;
3610 }
3611 udelay(1); /* should be 0.5us */
3612 }
3613 if (i == 4) {
3614 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3615 continue;
3616 }
3617
3618 /* Train 2 */
3619 reg = FDI_TX_CTL(pipe);
3620 temp = I915_READ(reg);
3621 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3622 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3623 I915_WRITE(reg, temp);
3624
3625 reg = FDI_RX_CTL(pipe);
3626 temp = I915_READ(reg);
3627 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3628 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003629 I915_WRITE(reg, temp);
3630
3631 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003632 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003633
Jesse Barnes139ccd32013-08-19 11:04:55 -07003634 for (i = 0; i < 4; i++) {
3635 reg = FDI_RX_IIR(pipe);
3636 temp = I915_READ(reg);
3637 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003638
Jesse Barnes139ccd32013-08-19 11:04:55 -07003639 if (temp & FDI_RX_SYMBOL_LOCK ||
3640 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3641 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3642 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3643 i);
3644 goto train_done;
3645 }
3646 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003647 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003648 if (i == 4)
3649 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003650 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003651
Jesse Barnes139ccd32013-08-19 11:04:55 -07003652train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003653 DRM_DEBUG_KMS("FDI train done.\n");
3654}
3655
Daniel Vetter88cefb62012-08-12 19:27:14 +02003656static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003657{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003658 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003659 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003660 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003661 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003662
Jesse Barnesc64e3112010-09-10 11:27:03 -07003663
Jesse Barnes0e23b992010-09-10 11:10:00 -07003664 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003665 reg = FDI_RX_CTL(pipe);
3666 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003667 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003668 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003669 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003670 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3671
3672 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003673 udelay(200);
3674
3675 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003676 temp = I915_READ(reg);
3677 I915_WRITE(reg, temp | FDI_PCDCLK);
3678
3679 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003680 udelay(200);
3681
Paulo Zanoni20749732012-11-23 15:30:38 -02003682 /* Enable CPU FDI TX PLL, always on for Ironlake */
3683 reg = FDI_TX_CTL(pipe);
3684 temp = I915_READ(reg);
3685 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3686 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003687
Paulo Zanoni20749732012-11-23 15:30:38 -02003688 POSTING_READ(reg);
3689 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003690 }
3691}
3692
Daniel Vetter88cefb62012-08-12 19:27:14 +02003693static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3694{
3695 struct drm_device *dev = intel_crtc->base.dev;
3696 struct drm_i915_private *dev_priv = dev->dev_private;
3697 int pipe = intel_crtc->pipe;
3698 u32 reg, temp;
3699
3700 /* Switch from PCDclk to Rawclk */
3701 reg = FDI_RX_CTL(pipe);
3702 temp = I915_READ(reg);
3703 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3704
3705 /* Disable CPU FDI TX PLL */
3706 reg = FDI_TX_CTL(pipe);
3707 temp = I915_READ(reg);
3708 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3709
3710 POSTING_READ(reg);
3711 udelay(100);
3712
3713 reg = FDI_RX_CTL(pipe);
3714 temp = I915_READ(reg);
3715 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3716
3717 /* Wait for the clocks to turn off. */
3718 POSTING_READ(reg);
3719 udelay(100);
3720}
3721
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003722static void ironlake_fdi_disable(struct drm_crtc *crtc)
3723{
3724 struct drm_device *dev = crtc->dev;
3725 struct drm_i915_private *dev_priv = dev->dev_private;
3726 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3727 int pipe = intel_crtc->pipe;
3728 u32 reg, temp;
3729
3730 /* disable CPU FDI tx and PCH FDI rx */
3731 reg = FDI_TX_CTL(pipe);
3732 temp = I915_READ(reg);
3733 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3734 POSTING_READ(reg);
3735
3736 reg = FDI_RX_CTL(pipe);
3737 temp = I915_READ(reg);
3738 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003739 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003740 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3741
3742 POSTING_READ(reg);
3743 udelay(100);
3744
3745 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003746 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003747 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003748
3749 /* still set train pattern 1 */
3750 reg = FDI_TX_CTL(pipe);
3751 temp = I915_READ(reg);
3752 temp &= ~FDI_LINK_TRAIN_NONE;
3753 temp |= FDI_LINK_TRAIN_PATTERN_1;
3754 I915_WRITE(reg, temp);
3755
3756 reg = FDI_RX_CTL(pipe);
3757 temp = I915_READ(reg);
3758 if (HAS_PCH_CPT(dev)) {
3759 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3760 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3761 } else {
3762 temp &= ~FDI_LINK_TRAIN_NONE;
3763 temp |= FDI_LINK_TRAIN_PATTERN_1;
3764 }
3765 /* BPC in FDI rx is consistent with that in PIPECONF */
3766 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003767 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003768 I915_WRITE(reg, temp);
3769
3770 POSTING_READ(reg);
3771 udelay(100);
3772}
3773
Chris Wilson5dce5b932014-01-20 10:17:36 +00003774bool intel_has_pending_fb_unpin(struct drm_device *dev)
3775{
3776 struct intel_crtc *crtc;
3777
3778 /* Note that we don't need to be called with mode_config.lock here
3779 * as our list of CRTC objects is static for the lifetime of the
3780 * device and so cannot disappear as we iterate. Similarly, we can
3781 * happily treat the predicates as racy, atomic checks as userspace
3782 * cannot claim and pin a new fb without at least acquring the
3783 * struct_mutex and so serialising with us.
3784 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003785 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003786 if (atomic_read(&crtc->unpin_work_count) == 0)
3787 continue;
3788
3789 if (crtc->unpin_work)
3790 intel_wait_for_vblank(dev, crtc->pipe);
3791
3792 return true;
3793 }
3794
3795 return false;
3796}
3797
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003798static void page_flip_completed(struct intel_crtc *intel_crtc)
3799{
3800 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3801 struct intel_unpin_work *work = intel_crtc->unpin_work;
3802
3803 /* ensure that the unpin work is consistent wrt ->pending. */
3804 smp_rmb();
3805 intel_crtc->unpin_work = NULL;
3806
3807 if (work->event)
3808 drm_send_vblank_event(intel_crtc->base.dev,
3809 intel_crtc->pipe,
3810 work->event);
3811
3812 drm_crtc_vblank_put(&intel_crtc->base);
3813
3814 wake_up_all(&dev_priv->pending_flip_queue);
3815 queue_work(dev_priv->wq, &work->work);
3816
3817 trace_i915_flip_complete(intel_crtc->plane,
3818 work->pending_flip_obj);
3819}
3820
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003821void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003822{
Chris Wilson0f911282012-04-17 10:05:38 +01003823 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003824 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003825
Daniel Vetter2c10d572012-12-20 21:24:07 +01003826 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003827 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3828 !intel_crtc_has_pending_flip(crtc),
3829 60*HZ) == 0)) {
3830 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003831
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003832 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003833 if (intel_crtc->unpin_work) {
3834 WARN_ONCE(1, "Removing stuck page flip\n");
3835 page_flip_completed(intel_crtc);
3836 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003837 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003838 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003839
Chris Wilson975d5682014-08-20 13:13:34 +01003840 if (crtc->primary->fb) {
3841 mutex_lock(&dev->struct_mutex);
3842 intel_finish_fb(crtc->primary->fb);
3843 mutex_unlock(&dev->struct_mutex);
3844 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003845}
3846
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003847/* Program iCLKIP clock to the desired frequency */
3848static void lpt_program_iclkip(struct drm_crtc *crtc)
3849{
3850 struct drm_device *dev = crtc->dev;
3851 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003852 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003853 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3854 u32 temp;
3855
Daniel Vetter09153002012-12-12 14:06:44 +01003856 mutex_lock(&dev_priv->dpio_lock);
3857
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003858 /* It is necessary to ungate the pixclk gate prior to programming
3859 * the divisors, and gate it back when it is done.
3860 */
3861 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3862
3863 /* Disable SSCCTL */
3864 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003865 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3866 SBI_SSCCTL_DISABLE,
3867 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003868
3869 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003870 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003871 auxdiv = 1;
3872 divsel = 0x41;
3873 phaseinc = 0x20;
3874 } else {
3875 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003876 * but the adjusted_mode->crtc_clock in in KHz. To get the
3877 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003878 * convert the virtual clock precision to KHz here for higher
3879 * precision.
3880 */
3881 u32 iclk_virtual_root_freq = 172800 * 1000;
3882 u32 iclk_pi_range = 64;
3883 u32 desired_divisor, msb_divisor_value, pi_value;
3884
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003885 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003886 msb_divisor_value = desired_divisor / iclk_pi_range;
3887 pi_value = desired_divisor % iclk_pi_range;
3888
3889 auxdiv = 0;
3890 divsel = msb_divisor_value - 2;
3891 phaseinc = pi_value;
3892 }
3893
3894 /* This should not happen with any sane values */
3895 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3896 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3897 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3898 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3899
3900 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 +03003901 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003902 auxdiv,
3903 divsel,
3904 phasedir,
3905 phaseinc);
3906
3907 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003908 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003909 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3910 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3911 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3912 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3913 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3914 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003915 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003916
3917 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003918 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003919 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3920 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003921 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003922
3923 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003924 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003925 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003926 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003927
3928 /* Wait for initialization time */
3929 udelay(24);
3930
3931 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003932
3933 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003934}
3935
Daniel Vetter275f01b22013-05-03 11:49:47 +02003936static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3937 enum pipe pch_transcoder)
3938{
3939 struct drm_device *dev = crtc->base.dev;
3940 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003941 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003942
3943 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3944 I915_READ(HTOTAL(cpu_transcoder)));
3945 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3946 I915_READ(HBLANK(cpu_transcoder)));
3947 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3948 I915_READ(HSYNC(cpu_transcoder)));
3949
3950 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3951 I915_READ(VTOTAL(cpu_transcoder)));
3952 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3953 I915_READ(VBLANK(cpu_transcoder)));
3954 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3955 I915_READ(VSYNC(cpu_transcoder)));
3956 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3957 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3958}
3959
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003960static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003961{
3962 struct drm_i915_private *dev_priv = dev->dev_private;
3963 uint32_t temp;
3964
3965 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003966 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003967 return;
3968
3969 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3970 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3971
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003972 temp &= ~FDI_BC_BIFURCATION_SELECT;
3973 if (enable)
3974 temp |= FDI_BC_BIFURCATION_SELECT;
3975
3976 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003977 I915_WRITE(SOUTH_CHICKEN1, temp);
3978 POSTING_READ(SOUTH_CHICKEN1);
3979}
3980
3981static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3982{
3983 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003984
3985 switch (intel_crtc->pipe) {
3986 case PIPE_A:
3987 break;
3988 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003989 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003990 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003991 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003992 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003993
3994 break;
3995 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003996 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003997
3998 break;
3999 default:
4000 BUG();
4001 }
4002}
4003
Jesse Barnesf67a5592011-01-05 10:31:48 -08004004/*
4005 * Enable PCH resources required for PCH ports:
4006 * - PCH PLLs
4007 * - FDI training & RX/TX
4008 * - update transcoder timings
4009 * - DP transcoding bits
4010 * - transcoder
4011 */
4012static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004013{
4014 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004015 struct drm_i915_private *dev_priv = dev->dev_private;
4016 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4017 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004018 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004019
Daniel Vetterab9412b2013-05-03 11:49:46 +02004020 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004021
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004022 if (IS_IVYBRIDGE(dev))
4023 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4024
Daniel Vettercd986ab2012-10-26 10:58:12 +02004025 /* Write the TU size bits before fdi link training, so that error
4026 * detection works. */
4027 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4028 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4029
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004030 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004031 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004032
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004033 /* We need to program the right clock selection before writing the pixel
4034 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004035 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004036 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004037
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004038 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004039 temp |= TRANS_DPLL_ENABLE(pipe);
4040 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004041 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004042 temp |= sel;
4043 else
4044 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004045 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004046 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004047
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004048 /* XXX: pch pll's can be enabled any time before we enable the PCH
4049 * transcoder, and we actually should do this to not upset any PCH
4050 * transcoder that already use the clock when we share it.
4051 *
4052 * Note that enable_shared_dpll tries to do the right thing, but
4053 * get_shared_dpll unconditionally resets the pll - we need that to have
4054 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004055 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004056
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004057 /* set transcoder timing, panel must allow it */
4058 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004059 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004060
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004061 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004062
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004063 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004064 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004065 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004066 reg = TRANS_DP_CTL(pipe);
4067 temp = I915_READ(reg);
4068 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004069 TRANS_DP_SYNC_MASK |
4070 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01004071 temp |= (TRANS_DP_OUTPUT_ENABLE |
4072 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004073 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004074
4075 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004076 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004077 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004078 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004079
4080 switch (intel_trans_dp_port_sel(crtc)) {
4081 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004082 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004083 break;
4084 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004085 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004086 break;
4087 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004088 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004089 break;
4090 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004091 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004092 }
4093
Chris Wilson5eddb702010-09-11 13:48:45 +01004094 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004095 }
4096
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004097 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004098}
4099
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004100static void lpt_pch_enable(struct drm_crtc *crtc)
4101{
4102 struct drm_device *dev = crtc->dev;
4103 struct drm_i915_private *dev_priv = dev->dev_private;
4104 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004105 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004106
Daniel Vetterab9412b2013-05-03 11:49:46 +02004107 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004108
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004109 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004110
Paulo Zanoni0540e482012-10-31 18:12:40 -02004111 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004112 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004113
Paulo Zanoni937bb612012-10-31 18:12:47 -02004114 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004115}
4116
Daniel Vetter716c2e52014-06-25 22:02:02 +03004117void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004118{
Daniel Vettere2b78262013-06-07 23:10:03 +02004119 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004120
4121 if (pll == NULL)
4122 return;
4123
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004124 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004125 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004126 return;
4127 }
4128
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004129 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4130 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004131 WARN_ON(pll->on);
4132 WARN_ON(pll->active);
4133 }
4134
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004135 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004136}
4137
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004138struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4139 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004140{
Daniel Vettere2b78262013-06-07 23:10:03 +02004141 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004142 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004143 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004144
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004145 if (HAS_PCH_IBX(dev_priv->dev)) {
4146 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004147 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004148 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004149
Daniel Vetter46edb022013-06-05 13:34:12 +02004150 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4151 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004152
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004153 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004154
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004155 goto found;
4156 }
4157
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004158 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4159 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004160
4161 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004162 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004163 continue;
4164
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004165 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004166 &pll->new_config->hw_state,
4167 sizeof(pll->new_config->hw_state)) == 0) {
4168 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004169 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004170 pll->new_config->crtc_mask,
4171 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004172 goto found;
4173 }
4174 }
4175
4176 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004177 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4178 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004179 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004180 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4181 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004182 goto found;
4183 }
4184 }
4185
4186 return NULL;
4187
4188found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004189 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004190 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004191
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004192 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004193 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4194 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004195
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004196 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004197
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004198 return pll;
4199}
4200
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004201/**
4202 * intel_shared_dpll_start_config - start a new PLL staged config
4203 * @dev_priv: DRM device
4204 * @clear_pipes: mask of pipes that will have their PLLs freed
4205 *
4206 * Starts a new PLL staged config, copying the current config but
4207 * releasing the references of pipes specified in clear_pipes.
4208 */
4209static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4210 unsigned clear_pipes)
4211{
4212 struct intel_shared_dpll *pll;
4213 enum intel_dpll_id i;
4214
4215 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4216 pll = &dev_priv->shared_dplls[i];
4217
4218 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4219 GFP_KERNEL);
4220 if (!pll->new_config)
4221 goto cleanup;
4222
4223 pll->new_config->crtc_mask &= ~clear_pipes;
4224 }
4225
4226 return 0;
4227
4228cleanup:
4229 while (--i >= 0) {
4230 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004231 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004232 pll->new_config = NULL;
4233 }
4234
4235 return -ENOMEM;
4236}
4237
4238static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4239{
4240 struct intel_shared_dpll *pll;
4241 enum intel_dpll_id i;
4242
4243 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4244 pll = &dev_priv->shared_dplls[i];
4245
4246 WARN_ON(pll->new_config == &pll->config);
4247
4248 pll->config = *pll->new_config;
4249 kfree(pll->new_config);
4250 pll->new_config = NULL;
4251 }
4252}
4253
4254static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4255{
4256 struct intel_shared_dpll *pll;
4257 enum intel_dpll_id i;
4258
4259 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4260 pll = &dev_priv->shared_dplls[i];
4261
4262 WARN_ON(pll->new_config == &pll->config);
4263
4264 kfree(pll->new_config);
4265 pll->new_config = NULL;
4266 }
4267}
4268
Daniel Vettera1520312013-05-03 11:49:50 +02004269static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004270{
4271 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004272 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004273 u32 temp;
4274
4275 temp = I915_READ(dslreg);
4276 udelay(500);
4277 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004278 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004279 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004280 }
4281}
4282
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004283static void skylake_pfit_enable(struct intel_crtc *crtc)
4284{
4285 struct drm_device *dev = crtc->base.dev;
4286 struct drm_i915_private *dev_priv = dev->dev_private;
4287 int pipe = crtc->pipe;
4288
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004289 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004290 I915_WRITE(PS_CTL(pipe), PS_ENABLE);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004291 I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4292 I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004293 }
4294}
4295
Jesse Barnesb074cec2013-04-25 12:55:02 -07004296static void ironlake_pfit_enable(struct intel_crtc *crtc)
4297{
4298 struct drm_device *dev = crtc->base.dev;
4299 struct drm_i915_private *dev_priv = dev->dev_private;
4300 int pipe = crtc->pipe;
4301
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004302 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004303 /* Force use of hard-coded filter coefficients
4304 * as some pre-programmed values are broken,
4305 * e.g. x201.
4306 */
4307 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4308 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4309 PF_PIPE_SEL_IVB(pipe));
4310 else
4311 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004312 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4313 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004314 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004315}
4316
Matt Roper4a3b8762014-12-23 10:41:51 -08004317static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004318{
4319 struct drm_device *dev = crtc->dev;
4320 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004321 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004322 struct intel_plane *intel_plane;
4323
Matt Roperaf2b6532014-04-01 15:22:32 -07004324 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4325 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004326 if (intel_plane->pipe == pipe)
4327 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004328 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004329}
4330
Matt Roper0d703d42015-03-04 10:49:04 -08004331/*
4332 * Disable a plane internally without actually modifying the plane's state.
4333 * This will allow us to easily restore the plane later by just reprogramming
4334 * its state.
4335 */
4336static void disable_plane_internal(struct drm_plane *plane)
4337{
4338 struct intel_plane *intel_plane = to_intel_plane(plane);
4339 struct drm_plane_state *state =
4340 plane->funcs->atomic_duplicate_state(plane);
4341 struct intel_plane_state *intel_state = to_intel_plane_state(state);
4342
4343 intel_state->visible = false;
4344 intel_plane->commit_plane(plane, intel_state);
4345
4346 intel_plane_destroy_state(plane, state);
4347}
4348
Matt Roper4a3b8762014-12-23 10:41:51 -08004349static void intel_disable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004350{
4351 struct drm_device *dev = crtc->dev;
4352 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004353 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004354 struct intel_plane *intel_plane;
4355
Matt Roperaf2b6532014-04-01 15:22:32 -07004356 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4357 intel_plane = to_intel_plane(plane);
Matt Roper0d703d42015-03-04 10:49:04 -08004358 if (plane->fb && intel_plane->pipe == pipe)
4359 disable_plane_internal(plane);
Matt Roperaf2b6532014-04-01 15:22:32 -07004360 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004361}
4362
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004363void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004364{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004365 struct drm_device *dev = crtc->base.dev;
4366 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004367
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004368 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004369 return;
4370
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004371 /* We can only enable IPS after we enable a plane and wait for a vblank */
4372 intel_wait_for_vblank(dev, crtc->pipe);
4373
Paulo Zanonid77e4532013-09-24 13:52:55 -03004374 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004375 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004376 mutex_lock(&dev_priv->rps.hw_lock);
4377 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4378 mutex_unlock(&dev_priv->rps.hw_lock);
4379 /* Quoting Art Runyan: "its not safe to expect any particular
4380 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004381 * mailbox." Moreover, the mailbox may return a bogus state,
4382 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004383 */
4384 } else {
4385 I915_WRITE(IPS_CTL, IPS_ENABLE);
4386 /* The bit only becomes 1 in the next vblank, so this wait here
4387 * is essentially intel_wait_for_vblank. If we don't have this
4388 * and don't wait for vblanks until the end of crtc_enable, then
4389 * the HW state readout code will complain that the expected
4390 * IPS_CTL value is not the one we read. */
4391 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4392 DRM_ERROR("Timed out waiting for IPS enable\n");
4393 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004394}
4395
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004396void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004397{
4398 struct drm_device *dev = crtc->base.dev;
4399 struct drm_i915_private *dev_priv = dev->dev_private;
4400
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004401 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004402 return;
4403
4404 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004405 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004406 mutex_lock(&dev_priv->rps.hw_lock);
4407 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4408 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004409 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4410 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4411 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004412 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004413 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004414 POSTING_READ(IPS_CTL);
4415 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004416
4417 /* We need to wait for a vblank before we can disable the plane. */
4418 intel_wait_for_vblank(dev, crtc->pipe);
4419}
4420
4421/** Loads the palette/gamma unit for the CRTC with the prepared values */
4422static void intel_crtc_load_lut(struct drm_crtc *crtc)
4423{
4424 struct drm_device *dev = crtc->dev;
4425 struct drm_i915_private *dev_priv = dev->dev_private;
4426 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4427 enum pipe pipe = intel_crtc->pipe;
4428 int palreg = PALETTE(pipe);
4429 int i;
4430 bool reenable_ips = false;
4431
4432 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004433 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004434 return;
4435
4436 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004437 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004438 assert_dsi_pll_enabled(dev_priv);
4439 else
4440 assert_pll_enabled(dev_priv, pipe);
4441 }
4442
4443 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304444 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004445 palreg = LGC_PALETTE(pipe);
4446
4447 /* Workaround : Do not read or write the pipe palette/gamma data while
4448 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4449 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004450 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004451 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4452 GAMMA_MODE_MODE_SPLIT)) {
4453 hsw_disable_ips(intel_crtc);
4454 reenable_ips = true;
4455 }
4456
4457 for (i = 0; i < 256; i++) {
4458 I915_WRITE(palreg + 4 * i,
4459 (intel_crtc->lut_r[i] << 16) |
4460 (intel_crtc->lut_g[i] << 8) |
4461 intel_crtc->lut_b[i]);
4462 }
4463
4464 if (reenable_ips)
4465 hsw_enable_ips(intel_crtc);
4466}
4467
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004468static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4469{
4470 if (!enable && intel_crtc->overlay) {
4471 struct drm_device *dev = intel_crtc->base.dev;
4472 struct drm_i915_private *dev_priv = dev->dev_private;
4473
4474 mutex_lock(&dev->struct_mutex);
4475 dev_priv->mm.interruptible = false;
4476 (void) intel_overlay_switch_off(intel_crtc->overlay);
4477 dev_priv->mm.interruptible = true;
4478 mutex_unlock(&dev->struct_mutex);
4479 }
4480
4481 /* Let userspace switch the overlay on again. In most cases userspace
4482 * has to recompute where to put it anyway.
4483 */
4484}
4485
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004486static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004487{
4488 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004489 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4490 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004491
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004492 intel_enable_primary_hw_plane(crtc->primary, crtc);
Matt Roper4a3b8762014-12-23 10:41:51 -08004493 intel_enable_sprite_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004494 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004495 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004496
4497 hsw_enable_ips(intel_crtc);
4498
4499 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004500 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004501 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004502
4503 /*
4504 * FIXME: Once we grow proper nuclear flip support out of this we need
4505 * to compute the mask of flip planes precisely. For the time being
4506 * consider this a flip from a NULL plane.
4507 */
4508 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004509}
4510
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004511static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004512{
4513 struct drm_device *dev = crtc->dev;
4514 struct drm_i915_private *dev_priv = dev->dev_private;
4515 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4516 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004517
4518 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004519
Paulo Zanonie35fef22015-02-09 14:46:29 -02004520 if (dev_priv->fbc.crtc == intel_crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004521 intel_fbc_disable(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004522
4523 hsw_disable_ips(intel_crtc);
4524
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004525 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004526 intel_crtc_update_cursor(crtc, false);
Matt Roper4a3b8762014-12-23 10:41:51 -08004527 intel_disable_sprite_planes(crtc);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004528 intel_disable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004529
Daniel Vetterf99d7062014-06-19 16:01:59 +02004530 /*
4531 * FIXME: Once we grow proper nuclear flip support out of this we need
4532 * to compute the mask of flip planes precisely. For the time being
4533 * consider this a flip to a NULL plane.
4534 */
4535 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004536}
4537
Jesse Barnesf67a5592011-01-05 10:31:48 -08004538static void ironlake_crtc_enable(struct drm_crtc *crtc)
4539{
4540 struct drm_device *dev = crtc->dev;
4541 struct drm_i915_private *dev_priv = dev->dev_private;
4542 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004543 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004544 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004545
Matt Roper83d65732015-02-25 13:12:16 -08004546 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004547
Jesse Barnesf67a5592011-01-05 10:31:48 -08004548 if (intel_crtc->active)
4549 return;
4550
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004551 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004552 intel_prepare_shared_dpll(intel_crtc);
4553
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004554 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304555 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004556
4557 intel_set_pipe_timings(intel_crtc);
4558
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004559 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004560 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004561 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004562 }
4563
4564 ironlake_set_pipeconf(crtc);
4565
Jesse Barnesf67a5592011-01-05 10:31:48 -08004566 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004567
Daniel Vettera72e4c92014-09-30 10:56:47 +02004568 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4569 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004570
Daniel Vetterf6736a12013-06-05 13:34:30 +02004571 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004572 if (encoder->pre_enable)
4573 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004574
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004575 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004576 /* Note: FDI PLL enabling _must_ be done before we enable the
4577 * cpu pipes, hence this is separate from all the other fdi/pch
4578 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004579 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004580 } else {
4581 assert_fdi_tx_disabled(dev_priv, pipe);
4582 assert_fdi_rx_disabled(dev_priv, pipe);
4583 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004584
Jesse Barnesb074cec2013-04-25 12:55:02 -07004585 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004586
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004587 /*
4588 * On ILK+ LUT must be loaded before the pipe is running but with
4589 * clocks enabled
4590 */
4591 intel_crtc_load_lut(crtc);
4592
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004593 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004594 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004595
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004596 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004597 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004598
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004599 assert_vblank_disabled(crtc);
4600 drm_crtc_vblank_on(crtc);
4601
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004602 for_each_encoder_on_crtc(dev, crtc, encoder)
4603 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004604
4605 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004606 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004607
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004608 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004609}
4610
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004611/* IPS only exists on ULT machines and is tied to pipe A. */
4612static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4613{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004614 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004615}
4616
Paulo Zanonie4916942013-09-20 16:21:19 -03004617/*
4618 * This implements the workaround described in the "notes" section of the mode
4619 * set sequence documentation. When going from no pipes or single pipe to
4620 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4621 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4622 */
4623static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4624{
4625 struct drm_device *dev = crtc->base.dev;
4626 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4627
4628 /* We want to get the other_active_crtc only if there's only 1 other
4629 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004630 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004631 if (!crtc_it->active || crtc_it == crtc)
4632 continue;
4633
4634 if (other_active_crtc)
4635 return;
4636
4637 other_active_crtc = crtc_it;
4638 }
4639 if (!other_active_crtc)
4640 return;
4641
4642 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4643 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4644}
4645
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004646static void haswell_crtc_enable(struct drm_crtc *crtc)
4647{
4648 struct drm_device *dev = crtc->dev;
4649 struct drm_i915_private *dev_priv = dev->dev_private;
4650 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4651 struct intel_encoder *encoder;
4652 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004653
Matt Roper83d65732015-02-25 13:12:16 -08004654 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004655
4656 if (intel_crtc->active)
4657 return;
4658
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004659 if (intel_crtc_to_shared_dpll(intel_crtc))
4660 intel_enable_shared_dpll(intel_crtc);
4661
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004662 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304663 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004664
4665 intel_set_pipe_timings(intel_crtc);
4666
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004667 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4668 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4669 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004670 }
4671
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004672 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004673 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004674 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004675 }
4676
4677 haswell_set_pipeconf(crtc);
4678
4679 intel_set_pipe_csc(crtc);
4680
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004681 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004682
Daniel Vettera72e4c92014-09-30 10:56:47 +02004683 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004684 for_each_encoder_on_crtc(dev, crtc, encoder)
4685 if (encoder->pre_enable)
4686 encoder->pre_enable(encoder);
4687
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004688 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004689 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4690 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004691 dev_priv->display.fdi_link_train(crtc);
4692 }
4693
Paulo Zanoni1f544382012-10-24 11:32:00 -02004694 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004695
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004696 if (IS_SKYLAKE(dev))
4697 skylake_pfit_enable(intel_crtc);
4698 else
4699 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004700
4701 /*
4702 * On ILK+ LUT must be loaded before the pipe is running but with
4703 * clocks enabled
4704 */
4705 intel_crtc_load_lut(crtc);
4706
Paulo Zanoni1f544382012-10-24 11:32:00 -02004707 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004708 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004709
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004710 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004711 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004712
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004713 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004714 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004715
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004716 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004717 intel_ddi_set_vc_payload_alloc(crtc, true);
4718
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004719 assert_vblank_disabled(crtc);
4720 drm_crtc_vblank_on(crtc);
4721
Jani Nikula8807e552013-08-30 19:40:32 +03004722 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004723 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004724 intel_opregion_notify_encoder(encoder, true);
4725 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004726
Paulo Zanonie4916942013-09-20 16:21:19 -03004727 /* If we change the relative order between pipe/planes enabling, we need
4728 * to change the workaround. */
4729 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004730 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004731}
4732
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004733static void skylake_pfit_disable(struct intel_crtc *crtc)
4734{
4735 struct drm_device *dev = crtc->base.dev;
4736 struct drm_i915_private *dev_priv = dev->dev_private;
4737 int pipe = crtc->pipe;
4738
4739 /* To avoid upsetting the power well on haswell only disable the pfit if
4740 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004741 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004742 I915_WRITE(PS_CTL(pipe), 0);
4743 I915_WRITE(PS_WIN_POS(pipe), 0);
4744 I915_WRITE(PS_WIN_SZ(pipe), 0);
4745 }
4746}
4747
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004748static void ironlake_pfit_disable(struct intel_crtc *crtc)
4749{
4750 struct drm_device *dev = crtc->base.dev;
4751 struct drm_i915_private *dev_priv = dev->dev_private;
4752 int pipe = crtc->pipe;
4753
4754 /* To avoid upsetting the power well on haswell only disable the pfit if
4755 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004756 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004757 I915_WRITE(PF_CTL(pipe), 0);
4758 I915_WRITE(PF_WIN_POS(pipe), 0);
4759 I915_WRITE(PF_WIN_SZ(pipe), 0);
4760 }
4761}
4762
Jesse Barnes6be4a602010-09-10 10:26:01 -07004763static void ironlake_crtc_disable(struct drm_crtc *crtc)
4764{
4765 struct drm_device *dev = crtc->dev;
4766 struct drm_i915_private *dev_priv = dev->dev_private;
4767 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004768 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004769 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004770 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004771
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004772 if (!intel_crtc->active)
4773 return;
4774
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004775 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004776
Daniel Vetterea9d7582012-07-10 10:42:52 +02004777 for_each_encoder_on_crtc(dev, crtc, encoder)
4778 encoder->disable(encoder);
4779
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004780 drm_crtc_vblank_off(crtc);
4781 assert_vblank_disabled(crtc);
4782
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004783 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004784 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02004785
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004786 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004787
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004788 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004789
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004790 for_each_encoder_on_crtc(dev, crtc, encoder)
4791 if (encoder->post_disable)
4792 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004793
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004794 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004795 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004796
Daniel Vetterd925c592013-06-05 13:34:04 +02004797 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004798
Daniel Vetterd925c592013-06-05 13:34:04 +02004799 if (HAS_PCH_CPT(dev)) {
4800 /* disable TRANS_DP_CTL */
4801 reg = TRANS_DP_CTL(pipe);
4802 temp = I915_READ(reg);
4803 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4804 TRANS_DP_PORT_SEL_MASK);
4805 temp |= TRANS_DP_PORT_SEL_NONE;
4806 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004807
Daniel Vetterd925c592013-06-05 13:34:04 +02004808 /* disable DPLL_SEL */
4809 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004810 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004811 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004812 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004813
4814 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004815 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02004816
4817 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004818 }
4819
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004820 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004821 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004822
4823 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004824 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004825 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004826}
4827
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004828static void haswell_crtc_disable(struct drm_crtc *crtc)
4829{
4830 struct drm_device *dev = crtc->dev;
4831 struct drm_i915_private *dev_priv = dev->dev_private;
4832 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4833 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004834 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004835
4836 if (!intel_crtc->active)
4837 return;
4838
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004839 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03004840
Jani Nikula8807e552013-08-30 19:40:32 +03004841 for_each_encoder_on_crtc(dev, crtc, encoder) {
4842 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004843 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004844 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004845
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004846 drm_crtc_vblank_off(crtc);
4847 assert_vblank_disabled(crtc);
4848
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004849 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004850 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4851 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004852 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004853
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004854 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03004855 intel_ddi_set_vc_payload_alloc(crtc, false);
4856
Paulo Zanoniad80a812012-10-24 16:06:19 -02004857 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004858
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004859 if (IS_SKYLAKE(dev))
4860 skylake_pfit_disable(intel_crtc);
4861 else
4862 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004863
Paulo Zanoni1f544382012-10-24 11:32:00 -02004864 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004865
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004866 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02004867 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02004868 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02004869 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004870
Imre Deak97b040a2014-06-25 22:01:50 +03004871 for_each_encoder_on_crtc(dev, crtc, encoder)
4872 if (encoder->post_disable)
4873 encoder->post_disable(encoder);
4874
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004875 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004876 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004877
4878 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004879 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004880 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004881
4882 if (intel_crtc_to_shared_dpll(intel_crtc))
4883 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004884}
4885
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004886static void ironlake_crtc_off(struct drm_crtc *crtc)
4887{
4888 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004889 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004890}
4891
Paulo Zanoni6441ab52012-10-05 12:05:58 -03004892
Jesse Barnes2dd24552013-04-25 12:55:01 -07004893static void i9xx_pfit_enable(struct intel_crtc *crtc)
4894{
4895 struct drm_device *dev = crtc->base.dev;
4896 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004897 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07004898
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02004899 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07004900 return;
4901
Daniel Vetterc0b03412013-05-28 12:05:54 +02004902 /*
4903 * The panel fitter should only be adjusted whilst the pipe is disabled,
4904 * according to register description and PRM.
4905 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07004906 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4907 assert_pipe_disabled(dev_priv, crtc->pipe);
4908
Jesse Barnesb074cec2013-04-25 12:55:02 -07004909 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4910 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02004911
4912 /* Border color in case we don't scale up to the full screen. Black by
4913 * default, change to something else for debugging. */
4914 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07004915}
4916
Dave Airlied05410f2014-06-05 13:22:59 +10004917static enum intel_display_power_domain port_to_power_domain(enum port port)
4918{
4919 switch (port) {
4920 case PORT_A:
4921 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4922 case PORT_B:
4923 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4924 case PORT_C:
4925 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4926 case PORT_D:
4927 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4928 default:
4929 WARN_ON_ONCE(1);
4930 return POWER_DOMAIN_PORT_OTHER;
4931 }
4932}
4933
Imre Deak77d22dc2014-03-05 16:20:52 +02004934#define for_each_power_domain(domain, mask) \
4935 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4936 if ((1 << (domain)) & (mask))
4937
Imre Deak319be8a2014-03-04 19:22:57 +02004938enum intel_display_power_domain
4939intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02004940{
Imre Deak319be8a2014-03-04 19:22:57 +02004941 struct drm_device *dev = intel_encoder->base.dev;
4942 struct intel_digital_port *intel_dig_port;
4943
4944 switch (intel_encoder->type) {
4945 case INTEL_OUTPUT_UNKNOWN:
4946 /* Only DDI platforms should ever use this output type */
4947 WARN_ON_ONCE(!HAS_DDI(dev));
4948 case INTEL_OUTPUT_DISPLAYPORT:
4949 case INTEL_OUTPUT_HDMI:
4950 case INTEL_OUTPUT_EDP:
4951 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10004952 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10004953 case INTEL_OUTPUT_DP_MST:
4954 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4955 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02004956 case INTEL_OUTPUT_ANALOG:
4957 return POWER_DOMAIN_PORT_CRT;
4958 case INTEL_OUTPUT_DSI:
4959 return POWER_DOMAIN_PORT_DSI;
4960 default:
4961 return POWER_DOMAIN_PORT_OTHER;
4962 }
4963}
4964
4965static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4966{
4967 struct drm_device *dev = crtc->dev;
4968 struct intel_encoder *intel_encoder;
4969 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4970 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02004971 unsigned long mask;
4972 enum transcoder transcoder;
4973
4974 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4975
4976 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4977 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004978 if (intel_crtc->config->pch_pfit.enabled ||
4979 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02004980 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4981
Imre Deak319be8a2014-03-04 19:22:57 +02004982 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4983 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4984
Imre Deak77d22dc2014-03-05 16:20:52 +02004985 return mask;
4986}
4987
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02004988static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02004989{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02004990 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02004991 struct drm_i915_private *dev_priv = dev->dev_private;
4992 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4993 struct intel_crtc *crtc;
4994
4995 /*
4996 * First get all needed power domains, then put all unneeded, to avoid
4997 * any unnecessary toggling of the power wells.
4998 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004999 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005000 enum intel_display_power_domain domain;
5001
Matt Roper83d65732015-02-25 13:12:16 -08005002 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005003 continue;
5004
Imre Deak319be8a2014-03-04 19:22:57 +02005005 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005006
5007 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5008 intel_display_power_get(dev_priv, domain);
5009 }
5010
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005011 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005012 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005013
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005014 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005015 enum intel_display_power_domain domain;
5016
5017 for_each_power_domain(domain, crtc->enabled_power_domains)
5018 intel_display_power_put(dev_priv, domain);
5019
5020 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5021 }
5022
5023 intel_display_set_init_power(dev_priv, false);
5024}
5025
Ville Syrjälädfcab172014-06-13 13:37:47 +03005026/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005027static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005028{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005029 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005030
Jesse Barnes586f49d2013-11-04 16:06:59 -08005031 /* Obtain SKU information */
5032 mutex_lock(&dev_priv->dpio_lock);
5033 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5034 CCK_FUSE_HPLL_FREQ_MASK;
5035 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005036
Ville Syrjälädfcab172014-06-13 13:37:47 +03005037 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005038}
5039
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005040static void vlv_update_cdclk(struct drm_device *dev)
5041{
5042 struct drm_i915_private *dev_priv = dev->dev_private;
5043
5044 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03005045 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005046 dev_priv->vlv_cdclk_freq);
5047
5048 /*
5049 * Program the gmbus_freq based on the cdclk frequency.
5050 * BSpec erroneously claims we should aim for 4MHz, but
5051 * in fact 1MHz is the correct frequency.
5052 */
Ville Syrjälä6be1e3d2014-10-16 20:52:31 +03005053 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005054}
5055
Jesse Barnes30a970c2013-11-04 13:48:12 -08005056/* Adjust CDclk dividers to allow high res or save power if possible */
5057static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5058{
5059 struct drm_i915_private *dev_priv = dev->dev_private;
5060 u32 val, cmd;
5061
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005062 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005063
Ville Syrjälädfcab172014-06-13 13:37:47 +03005064 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005065 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005066 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005067 cmd = 1;
5068 else
5069 cmd = 0;
5070
5071 mutex_lock(&dev_priv->rps.hw_lock);
5072 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5073 val &= ~DSPFREQGUAR_MASK;
5074 val |= (cmd << DSPFREQGUAR_SHIFT);
5075 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5076 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5077 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5078 50)) {
5079 DRM_ERROR("timed out waiting for CDclk change\n");
5080 }
5081 mutex_unlock(&dev_priv->rps.hw_lock);
5082
Ville Syrjälädfcab172014-06-13 13:37:47 +03005083 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005084 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005085
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005086 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005087
5088 mutex_lock(&dev_priv->dpio_lock);
5089 /* adjust cdclk divider */
5090 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005091 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005092 val |= divider;
5093 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005094
5095 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5096 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5097 50))
5098 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005099 mutex_unlock(&dev_priv->dpio_lock);
5100 }
5101
5102 mutex_lock(&dev_priv->dpio_lock);
5103 /* adjust self-refresh exit latency value */
5104 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5105 val &= ~0x7f;
5106
5107 /*
5108 * For high bandwidth configs, we set a higher latency in the bunit
5109 * so that the core display fetch happens in time to avoid underruns.
5110 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005111 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005112 val |= 4500 / 250; /* 4.5 usec */
5113 else
5114 val |= 3000 / 250; /* 3.0 usec */
5115 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5116 mutex_unlock(&dev_priv->dpio_lock);
5117
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005118 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005119}
5120
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005121static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5122{
5123 struct drm_i915_private *dev_priv = dev->dev_private;
5124 u32 val, cmd;
5125
5126 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
5127
5128 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005129 case 333333:
5130 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005131 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005132 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005133 break;
5134 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005135 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005136 return;
5137 }
5138
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005139 /*
5140 * Specs are full of misinformation, but testing on actual
5141 * hardware has shown that we just need to write the desired
5142 * CCK divider into the Punit register.
5143 */
5144 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5145
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005146 mutex_lock(&dev_priv->rps.hw_lock);
5147 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5148 val &= ~DSPFREQGUAR_MASK_CHV;
5149 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5150 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5151 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5152 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5153 50)) {
5154 DRM_ERROR("timed out waiting for CDclk change\n");
5155 }
5156 mutex_unlock(&dev_priv->rps.hw_lock);
5157
5158 vlv_update_cdclk(dev);
5159}
5160
Jesse Barnes30a970c2013-11-04 13:48:12 -08005161static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5162 int max_pixclk)
5163{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005164 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005165 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005166
Jesse Barnes30a970c2013-11-04 13:48:12 -08005167 /*
5168 * Really only a few cases to deal with, as only 4 CDclks are supported:
5169 * 200MHz
5170 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005171 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005172 * 400MHz (VLV only)
5173 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5174 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005175 *
5176 * We seem to get an unstable or solid color picture at 200MHz.
5177 * Not sure what's wrong. For now use 200MHz only when all pipes
5178 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005179 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005180 if (!IS_CHERRYVIEW(dev_priv) &&
5181 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005182 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005183 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005184 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005185 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005186 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005187 else
5188 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005189}
5190
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005191/* compute the max pixel clock for new configuration */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005192static int intel_mode_max_pixclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005193{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005194 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005195 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005196 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005197 int max_pixclk = 0;
5198
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005199 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005200 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5201 if (IS_ERR(crtc_state))
5202 return PTR_ERR(crtc_state);
5203
5204 if (!crtc_state->base.enable)
5205 continue;
5206
5207 max_pixclk = max(max_pixclk,
5208 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005209 }
5210
5211 return max_pixclk;
5212}
5213
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005214static int valleyview_modeset_global_pipes(struct drm_atomic_state *state,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005215 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005216{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005217 struct drm_i915_private *dev_priv = to_i915(state->dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005218 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005219 int max_pixclk = intel_mode_max_pixclk(state);
5220
5221 if (max_pixclk < 0)
5222 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005223
Imre Deakd60c4472014-03-27 17:45:10 +02005224 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
5225 dev_priv->vlv_cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005226 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005227
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005228 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005229 for_each_intel_crtc(state->dev, intel_crtc)
Matt Roper83d65732015-02-25 13:12:16 -08005230 if (intel_crtc->base.state->enable)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005231 *prepare_pipes |= (1 << intel_crtc->pipe);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005232
5233 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005234}
5235
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005236static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5237{
5238 unsigned int credits, default_credits;
5239
5240 if (IS_CHERRYVIEW(dev_priv))
5241 default_credits = PFI_CREDIT(12);
5242 else
5243 default_credits = PFI_CREDIT(8);
5244
5245 if (DIV_ROUND_CLOSEST(dev_priv->vlv_cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
5246 /* CHV suggested value is 31 or 63 */
5247 if (IS_CHERRYVIEW(dev_priv))
5248 credits = PFI_CREDIT_31;
5249 else
5250 credits = PFI_CREDIT(15);
5251 } else {
5252 credits = default_credits;
5253 }
5254
5255 /*
5256 * WA - write default credits before re-programming
5257 * FIXME: should we also set the resend bit here?
5258 */
5259 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5260 default_credits);
5261
5262 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5263 credits | PFI_CREDIT_RESEND);
5264
5265 /*
5266 * FIXME is this guaranteed to clear
5267 * immediately or should we poll for it?
5268 */
5269 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5270}
5271
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005272static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005273{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005274 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005275 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005276 int max_pixclk = intel_mode_max_pixclk(state);
5277 int req_cdclk;
5278
5279 /* The only reason this can fail is if we fail to add the crtc_state
5280 * to the atomic state. But that can't happen since the call to
5281 * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
5282 * can't have failed otherwise the mode set would be aborted) added all
5283 * the states already. */
5284 if (WARN_ON(max_pixclk < 0))
5285 return;
5286
5287 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005288
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005289 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005290 /*
5291 * FIXME: We can end up here with all power domains off, yet
5292 * with a CDCLK frequency other than the minimum. To account
5293 * for this take the PIPE-A power domain, which covers the HW
5294 * blocks needed for the following programming. This can be
5295 * removed once it's guaranteed that we get here either with
5296 * the minimum CDCLK set, or the required power domains
5297 * enabled.
5298 */
5299 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5300
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005301 if (IS_CHERRYVIEW(dev))
5302 cherryview_set_cdclk(dev, req_cdclk);
5303 else
5304 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005305
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005306 vlv_program_pfi_credits(dev_priv);
5307
Imre Deak738c05c2014-11-19 16:25:37 +02005308 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005309 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005310}
5311
Jesse Barnes89b667f2013-04-18 14:51:36 -07005312static void valleyview_crtc_enable(struct drm_crtc *crtc)
5313{
5314 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005315 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005316 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5317 struct intel_encoder *encoder;
5318 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005319 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005320
Matt Roper83d65732015-02-25 13:12:16 -08005321 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005322
5323 if (intel_crtc->active)
5324 return;
5325
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005326 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305327
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005328 if (!is_dsi) {
5329 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005330 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005331 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005332 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005333 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005334
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005335 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305336 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005337
5338 intel_set_pipe_timings(intel_crtc);
5339
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005340 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5341 struct drm_i915_private *dev_priv = dev->dev_private;
5342
5343 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5344 I915_WRITE(CHV_CANVAS(pipe), 0);
5345 }
5346
Daniel Vetter5b18e572014-04-24 23:55:06 +02005347 i9xx_set_pipeconf(intel_crtc);
5348
Jesse Barnes89b667f2013-04-18 14:51:36 -07005349 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005350
Daniel Vettera72e4c92014-09-30 10:56:47 +02005351 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005352
Jesse Barnes89b667f2013-04-18 14:51:36 -07005353 for_each_encoder_on_crtc(dev, crtc, encoder)
5354 if (encoder->pre_pll_enable)
5355 encoder->pre_pll_enable(encoder);
5356
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005357 if (!is_dsi) {
5358 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005359 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005360 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005361 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005362 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005363
5364 for_each_encoder_on_crtc(dev, crtc, encoder)
5365 if (encoder->pre_enable)
5366 encoder->pre_enable(encoder);
5367
Jesse Barnes2dd24552013-04-25 12:55:01 -07005368 i9xx_pfit_enable(intel_crtc);
5369
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005370 intel_crtc_load_lut(crtc);
5371
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005372 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005373 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005374
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005375 assert_vblank_disabled(crtc);
5376 drm_crtc_vblank_on(crtc);
5377
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005378 for_each_encoder_on_crtc(dev, crtc, encoder)
5379 encoder->enable(encoder);
5380
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005381 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005382
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005383 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005384 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005385}
5386
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005387static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5388{
5389 struct drm_device *dev = crtc->base.dev;
5390 struct drm_i915_private *dev_priv = dev->dev_private;
5391
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005392 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5393 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005394}
5395
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005396static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005397{
5398 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005399 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005400 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005401 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005402 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005403
Matt Roper83d65732015-02-25 13:12:16 -08005404 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02005405
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005406 if (intel_crtc->active)
5407 return;
5408
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005409 i9xx_set_pll_dividers(intel_crtc);
5410
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005411 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305412 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005413
5414 intel_set_pipe_timings(intel_crtc);
5415
Daniel Vetter5b18e572014-04-24 23:55:06 +02005416 i9xx_set_pipeconf(intel_crtc);
5417
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005418 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005419
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005420 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005421 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005422
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005423 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005424 if (encoder->pre_enable)
5425 encoder->pre_enable(encoder);
5426
Daniel Vetterf6736a12013-06-05 13:34:30 +02005427 i9xx_enable_pll(intel_crtc);
5428
Jesse Barnes2dd24552013-04-25 12:55:01 -07005429 i9xx_pfit_enable(intel_crtc);
5430
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005431 intel_crtc_load_lut(crtc);
5432
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005433 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005434 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005435
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005436 assert_vblank_disabled(crtc);
5437 drm_crtc_vblank_on(crtc);
5438
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005439 for_each_encoder_on_crtc(dev, crtc, encoder)
5440 encoder->enable(encoder);
5441
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005442 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005443
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005444 /*
5445 * Gen2 reports pipe underruns whenever all planes are disabled.
5446 * So don't enable underrun reporting before at least some planes
5447 * are enabled.
5448 * FIXME: Need to fix the logic to work when we turn off all planes
5449 * but leave the pipe running.
5450 */
5451 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005452 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005453
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005454 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005455 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005456}
5457
Daniel Vetter87476d62013-04-11 16:29:06 +02005458static void i9xx_pfit_disable(struct intel_crtc *crtc)
5459{
5460 struct drm_device *dev = crtc->base.dev;
5461 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02005462
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005463 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005464 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02005465
5466 assert_pipe_disabled(dev_priv, crtc->pipe);
5467
Daniel Vetter328d8e82013-05-08 10:36:31 +02005468 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5469 I915_READ(PFIT_CONTROL));
5470 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02005471}
5472
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005473static void i9xx_crtc_disable(struct drm_crtc *crtc)
5474{
5475 struct drm_device *dev = crtc->dev;
5476 struct drm_i915_private *dev_priv = dev->dev_private;
5477 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005478 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005479 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005480
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005481 if (!intel_crtc->active)
5482 return;
5483
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005484 /*
5485 * Gen2 reports pipe underruns whenever all planes are disabled.
5486 * So diasble underrun reporting before all the planes get disabled.
5487 * FIXME: Need to fix the logic to work when we turn off all planes
5488 * but leave the pipe running.
5489 */
5490 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005491 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005492
Imre Deak564ed192014-06-13 14:54:21 +03005493 /*
5494 * Vblank time updates from the shadow to live plane control register
5495 * are blocked if the memory self-refresh mode is active at that
5496 * moment. So to make sure the plane gets truly disabled, disable
5497 * first the self-refresh mode. The self-refresh enable bit in turn
5498 * will be checked/applied by the HW only at the next frame start
5499 * event which is after the vblank start event, so we need to have a
5500 * wait-for-vblank between disabling the plane and the pipe.
5501 */
5502 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005503 intel_crtc_disable_planes(crtc);
5504
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005505 /*
5506 * On gen2 planes are double buffered but the pipe isn't, so we must
5507 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03005508 * We also need to wait on all gmch platforms because of the
5509 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005510 */
Imre Deak564ed192014-06-13 14:54:21 +03005511 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005512
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005513 for_each_encoder_on_crtc(dev, crtc, encoder)
5514 encoder->disable(encoder);
5515
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005516 drm_crtc_vblank_off(crtc);
5517 assert_vblank_disabled(crtc);
5518
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005519 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005520
Daniel Vetter87476d62013-04-11 16:29:06 +02005521 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005522
Jesse Barnes89b667f2013-04-18 14:51:36 -07005523 for_each_encoder_on_crtc(dev, crtc, encoder)
5524 if (encoder->post_disable)
5525 encoder->post_disable(encoder);
5526
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005527 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005528 if (IS_CHERRYVIEW(dev))
5529 chv_disable_pll(dev_priv, pipe);
5530 else if (IS_VALLEYVIEW(dev))
5531 vlv_disable_pll(dev_priv, pipe);
5532 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03005533 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005534 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005535
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005536 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005537 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005538
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005539 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005540 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005541
Daniel Vetterefa96242014-04-24 23:55:02 +02005542 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005543 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02005544 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005545}
5546
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005547static void i9xx_crtc_off(struct drm_crtc *crtc)
5548{
5549}
5550
Borun Fub04c5bd2014-07-12 10:02:27 +05305551/* Master function to enable/disable CRTC and corresponding power wells */
5552void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01005553{
Chris Wilsoncdd59982010-09-08 16:30:16 +01005554 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005555 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005556 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005557 enum intel_display_power_domain domain;
5558 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005559
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005560 if (enable) {
5561 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005562 domains = get_crtc_power_domains(crtc);
5563 for_each_power_domain(domain, domains)
5564 intel_display_power_get(dev_priv, domain);
5565 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005566
5567 dev_priv->display.crtc_enable(crtc);
5568 }
5569 } else {
5570 if (intel_crtc->active) {
5571 dev_priv->display.crtc_disable(crtc);
5572
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005573 domains = intel_crtc->enabled_power_domains;
5574 for_each_power_domain(domain, domains)
5575 intel_display_power_put(dev_priv, domain);
5576 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005577 }
5578 }
Borun Fub04c5bd2014-07-12 10:02:27 +05305579}
5580
5581/**
5582 * Sets the power management mode of the pipe and plane.
5583 */
5584void intel_crtc_update_dpms(struct drm_crtc *crtc)
5585{
5586 struct drm_device *dev = crtc->dev;
5587 struct intel_encoder *intel_encoder;
5588 bool enable = false;
5589
5590 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5591 enable |= intel_encoder->connectors_active;
5592
5593 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005594}
5595
Daniel Vetter976f8a22012-07-08 22:34:21 +02005596static void intel_crtc_disable(struct drm_crtc *crtc)
5597{
5598 struct drm_device *dev = crtc->dev;
5599 struct drm_connector *connector;
5600 struct drm_i915_private *dev_priv = dev->dev_private;
5601
5602 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08005603 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005604
5605 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005606 dev_priv->display.off(crtc);
5607
Matt Roper70a101f2015-04-08 18:56:53 -07005608 drm_plane_helper_disable(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005609
5610 /* Update computed state. */
5611 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5612 if (!connector->encoder || !connector->encoder->crtc)
5613 continue;
5614
5615 if (connector->encoder->crtc != crtc)
5616 continue;
5617
5618 connector->dpms = DRM_MODE_DPMS_OFF;
5619 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01005620 }
5621}
5622
Chris Wilsonea5b2132010-08-04 13:50:23 +01005623void intel_encoder_destroy(struct drm_encoder *encoder)
5624{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005625 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01005626
Chris Wilsonea5b2132010-08-04 13:50:23 +01005627 drm_encoder_cleanup(encoder);
5628 kfree(intel_encoder);
5629}
5630
Damien Lespiau92373292013-08-08 22:28:57 +01005631/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005632 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5633 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01005634static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005635{
5636 if (mode == DRM_MODE_DPMS_ON) {
5637 encoder->connectors_active = true;
5638
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005639 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005640 } else {
5641 encoder->connectors_active = false;
5642
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005643 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005644 }
5645}
5646
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005647/* Cross check the actual hw state with our own modeset state tracking (and it's
5648 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02005649static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005650{
5651 if (connector->get_hw_state(connector)) {
5652 struct intel_encoder *encoder = connector->encoder;
5653 struct drm_crtc *crtc;
5654 bool encoder_enabled;
5655 enum pipe pipe;
5656
5657 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5658 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03005659 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005660
Dave Airlie0e32b392014-05-02 14:02:48 +10005661 /* there is no real hw state for MST connectors */
5662 if (connector->mst_port)
5663 return;
5664
Rob Clarke2c719b2014-12-15 13:56:32 -05005665 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005666 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05005667 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005668 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005669
Dave Airlie36cd7442014-05-02 13:44:18 +10005670 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05005671 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10005672 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005673
Dave Airlie36cd7442014-05-02 13:44:18 +10005674 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05005675 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
5676 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10005677 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005678
Dave Airlie36cd7442014-05-02 13:44:18 +10005679 crtc = encoder->base.crtc;
5680
Matt Roper83d65732015-02-25 13:12:16 -08005681 I915_STATE_WARN(!crtc->state->enable,
5682 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05005683 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5684 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10005685 "encoder active on the wrong pipe\n");
5686 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005687 }
5688}
5689
Ander Conselvan de Oliveira9bdbd0b2015-04-10 10:59:10 +03005690int intel_connector_init(struct intel_connector *connector)
5691{
5692 struct drm_connector_state *connector_state;
5693
5694 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
5695 if (!connector_state)
5696 return -ENOMEM;
5697
5698 connector->base.state = connector_state;
5699 return 0;
5700}
5701
5702struct intel_connector *intel_connector_alloc(void)
5703{
5704 struct intel_connector *connector;
5705
5706 connector = kzalloc(sizeof *connector, GFP_KERNEL);
5707 if (!connector)
5708 return NULL;
5709
5710 if (intel_connector_init(connector) < 0) {
5711 kfree(connector);
5712 return NULL;
5713 }
5714
5715 return connector;
5716}
5717
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005718/* Even simpler default implementation, if there's really no special case to
5719 * consider. */
5720void intel_connector_dpms(struct drm_connector *connector, int mode)
5721{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005722 /* All the simple cases only support two dpms states. */
5723 if (mode != DRM_MODE_DPMS_ON)
5724 mode = DRM_MODE_DPMS_OFF;
5725
5726 if (mode == connector->dpms)
5727 return;
5728
5729 connector->dpms = mode;
5730
5731 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01005732 if (connector->encoder)
5733 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005734
Daniel Vetterb9805142012-08-31 17:37:33 +02005735 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005736}
5737
Daniel Vetterf0947c32012-07-02 13:10:34 +02005738/* Simple connector->get_hw_state implementation for encoders that support only
5739 * one connector and no cloning and hence the encoder state determines the state
5740 * of the connector. */
5741bool intel_connector_get_hw_state(struct intel_connector *connector)
5742{
Daniel Vetter24929352012-07-02 20:28:59 +02005743 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02005744 struct intel_encoder *encoder = connector->encoder;
5745
5746 return encoder->get_hw_state(encoder, &pipe);
5747}
5748
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005749static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02005750{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005751 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
5752 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02005753
5754 return 0;
5755}
5756
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005757static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005758 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005759{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005760 struct drm_atomic_state *state = pipe_config->base.state;
5761 struct intel_crtc *other_crtc;
5762 struct intel_crtc_state *other_crtc_state;
5763
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005764 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5765 pipe_name(pipe), pipe_config->fdi_lanes);
5766 if (pipe_config->fdi_lanes > 4) {
5767 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5768 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005769 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005770 }
5771
Paulo Zanonibafb6552013-11-02 21:07:44 -07005772 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005773 if (pipe_config->fdi_lanes > 2) {
5774 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5775 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005776 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005777 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005778 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005779 }
5780 }
5781
5782 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005783 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005784
5785 /* Ivybridge 3 pipe is really complicated */
5786 switch (pipe) {
5787 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005788 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005789 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005790 if (pipe_config->fdi_lanes <= 2)
5791 return 0;
5792
5793 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
5794 other_crtc_state =
5795 intel_atomic_get_crtc_state(state, other_crtc);
5796 if (IS_ERR(other_crtc_state))
5797 return PTR_ERR(other_crtc_state);
5798
5799 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005800 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5801 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005802 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005803 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005804 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005805 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02005806 if (pipe_config->fdi_lanes > 2) {
5807 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
5808 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005809 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02005810 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005811
5812 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
5813 other_crtc_state =
5814 intel_atomic_get_crtc_state(state, other_crtc);
5815 if (IS_ERR(other_crtc_state))
5816 return PTR_ERR(other_crtc_state);
5817
5818 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005819 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005820 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005821 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005822 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005823 default:
5824 BUG();
5825 }
5826}
5827
Daniel Vettere29c22c2013-02-21 00:00:16 +01005828#define RETRY 1
5829static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005830 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02005831{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005832 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02005833 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005834 int lane, link_bw, fdi_dotclock, ret;
5835 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005836
Daniel Vettere29c22c2013-02-21 00:00:16 +01005837retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02005838 /* FDI is a binary signal running at ~2.7GHz, encoding
5839 * each output octet as 10 bits. The actual frequency
5840 * is stored as a divider into a 100MHz clock, and the
5841 * mode pixel clock is stored in units of 1KHz.
5842 * Hence the bw of each lane in terms of the mode signal
5843 * is:
5844 */
5845 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5846
Damien Lespiau241bfc32013-09-25 16:45:37 +01005847 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005848
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005849 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005850 pipe_config->pipe_bpp);
5851
5852 pipe_config->fdi_lanes = lane;
5853
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005854 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005855 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005856
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005857 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5858 intel_crtc->pipe, pipe_config);
5859 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01005860 pipe_config->pipe_bpp -= 2*3;
5861 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5862 pipe_config->pipe_bpp);
5863 needs_recompute = true;
5864 pipe_config->bw_constrained = true;
5865
5866 goto retry;
5867 }
5868
5869 if (needs_recompute)
5870 return RETRY;
5871
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005872 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005873}
5874
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005875static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005876 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005877{
Jani Nikulad330a952014-01-21 11:24:25 +02005878 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03005879 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07005880 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005881}
5882
Daniel Vettera43f6e02013-06-07 23:10:32 +02005883static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005884 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08005885{
Daniel Vettera43f6e02013-06-07 23:10:32 +02005886 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02005887 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02005888 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07005889 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01005890
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005891 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005892 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005893 int clock_limit =
5894 dev_priv->display.get_display_clock_speed(dev);
5895
5896 /*
5897 * Enable pixel doubling when the dot clock
5898 * is > 90% of the (display) core speed.
5899 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03005900 * GDG double wide on either pipe,
5901 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005902 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03005903 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01005904 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005905 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005906 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005907 }
5908
Damien Lespiau241bfc32013-09-25 16:45:37 +01005909 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005910 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005911 }
Chris Wilson89749352010-09-12 18:25:19 +01005912
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005913 /*
5914 * Pipe horizontal size must be even in:
5915 * - DVO ganged mode
5916 * - LVDS dual channel mode
5917 * - Double wide pipe
5918 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02005919 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005920 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5921 pipe_config->pipe_src_w &= ~1;
5922
Damien Lespiau8693a822013-05-03 18:48:11 +01005923 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5924 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03005925 */
5926 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5927 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005928 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03005929
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005930 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005931 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005932 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005933 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5934 * for lvds. */
5935 pipe_config->pipe_bpp = 8*3;
5936 }
5937
Damien Lespiauf5adf942013-06-24 18:29:34 +01005938 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005939 hsw_compute_ips_config(crtc, pipe_config);
5940
Daniel Vetter877d48d2013-04-19 11:24:43 +02005941 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02005942 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02005943
Chandra Kondurud03c93d2015-04-09 16:42:46 -07005944 /* FIXME: remove below call once atomic mode set is place and all crtc
5945 * related checks called from atomic_crtc_check function */
5946 ret = 0;
5947 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
5948 crtc, pipe_config->base.state);
5949 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
5950
5951 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005952}
5953
Ville Syrjälä1652d192015-03-31 14:12:01 +03005954static int skylake_get_display_clock_speed(struct drm_device *dev)
5955{
5956 struct drm_i915_private *dev_priv = to_i915(dev);
5957 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5958 uint32_t cdctl = I915_READ(CDCLK_CTL);
5959 uint32_t linkrate;
5960
5961 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
5962 WARN(1, "LCPLL1 not enabled\n");
5963 return 24000; /* 24MHz is the cd freq with NSSC ref */
5964 }
5965
5966 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
5967 return 540000;
5968
5969 linkrate = (I915_READ(DPLL_CTRL1) &
5970 DPLL_CRTL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
5971
5972 if (linkrate == DPLL_CRTL1_LINK_RATE_2160 ||
5973 linkrate == DPLL_CRTL1_LINK_RATE_1080) {
5974 /* vco 8640 */
5975 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
5976 case CDCLK_FREQ_450_432:
5977 return 432000;
5978 case CDCLK_FREQ_337_308:
5979 return 308570;
5980 case CDCLK_FREQ_675_617:
5981 return 617140;
5982 default:
5983 WARN(1, "Unknown cd freq selection\n");
5984 }
5985 } else {
5986 /* vco 8100 */
5987 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
5988 case CDCLK_FREQ_450_432:
5989 return 450000;
5990 case CDCLK_FREQ_337_308:
5991 return 337500;
5992 case CDCLK_FREQ_675_617:
5993 return 675000;
5994 default:
5995 WARN(1, "Unknown cd freq selection\n");
5996 }
5997 }
5998
5999 /* error case, do as if DPLL0 isn't enabled */
6000 return 24000;
6001}
6002
6003static int broadwell_get_display_clock_speed(struct drm_device *dev)
6004{
6005 struct drm_i915_private *dev_priv = dev->dev_private;
6006 uint32_t lcpll = I915_READ(LCPLL_CTL);
6007 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6008
6009 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6010 return 800000;
6011 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6012 return 450000;
6013 else if (freq == LCPLL_CLK_FREQ_450)
6014 return 450000;
6015 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6016 return 540000;
6017 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6018 return 337500;
6019 else
6020 return 675000;
6021}
6022
6023static int haswell_get_display_clock_speed(struct drm_device *dev)
6024{
6025 struct drm_i915_private *dev_priv = dev->dev_private;
6026 uint32_t lcpll = I915_READ(LCPLL_CTL);
6027 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6028
6029 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6030 return 800000;
6031 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6032 return 450000;
6033 else if (freq == LCPLL_CLK_FREQ_450)
6034 return 450000;
6035 else if (IS_HSW_ULT(dev))
6036 return 337500;
6037 else
6038 return 540000;
6039}
6040
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006041static int valleyview_get_display_clock_speed(struct drm_device *dev)
6042{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006043 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006044 u32 val;
6045 int divider;
6046
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006047 if (dev_priv->hpll_freq == 0)
6048 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6049
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006050 mutex_lock(&dev_priv->dpio_lock);
6051 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6052 mutex_unlock(&dev_priv->dpio_lock);
6053
6054 divider = val & DISPLAY_FREQUENCY_VALUES;
6055
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006056 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6057 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6058 "cdclk change in progress\n");
6059
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006060 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006061}
6062
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006063static int ilk_get_display_clock_speed(struct drm_device *dev)
6064{
6065 return 450000;
6066}
6067
Jesse Barnese70236a2009-09-21 10:42:27 -07006068static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006069{
Jesse Barnese70236a2009-09-21 10:42:27 -07006070 return 400000;
6071}
Jesse Barnes79e53942008-11-07 14:24:08 -08006072
Jesse Barnese70236a2009-09-21 10:42:27 -07006073static int i915_get_display_clock_speed(struct drm_device *dev)
6074{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006075 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006076}
Jesse Barnes79e53942008-11-07 14:24:08 -08006077
Jesse Barnese70236a2009-09-21 10:42:27 -07006078static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6079{
6080 return 200000;
6081}
Jesse Barnes79e53942008-11-07 14:24:08 -08006082
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006083static int pnv_get_display_clock_speed(struct drm_device *dev)
6084{
6085 u16 gcfgc = 0;
6086
6087 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6088
6089 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6090 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006091 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006092 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006093 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006094 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006095 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006096 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6097 return 200000;
6098 default:
6099 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6100 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006101 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006102 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006103 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006104 }
6105}
6106
Jesse Barnese70236a2009-09-21 10:42:27 -07006107static int i915gm_get_display_clock_speed(struct drm_device *dev)
6108{
6109 u16 gcfgc = 0;
6110
6111 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6112
6113 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006114 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006115 else {
6116 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6117 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006118 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006119 default:
6120 case GC_DISPLAY_CLOCK_190_200_MHZ:
6121 return 190000;
6122 }
6123 }
6124}
Jesse Barnes79e53942008-11-07 14:24:08 -08006125
Jesse Barnese70236a2009-09-21 10:42:27 -07006126static int i865_get_display_clock_speed(struct drm_device *dev)
6127{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006128 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006129}
6130
6131static int i855_get_display_clock_speed(struct drm_device *dev)
6132{
6133 u16 hpllcc = 0;
6134 /* Assume that the hardware is in the high speed state. This
6135 * should be the default.
6136 */
6137 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6138 case GC_CLOCK_133_200:
6139 case GC_CLOCK_100_200:
6140 return 200000;
6141 case GC_CLOCK_166_250:
6142 return 250000;
6143 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006144 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006145 }
6146
6147 /* Shouldn't happen */
6148 return 0;
6149}
6150
6151static int i830_get_display_clock_speed(struct drm_device *dev)
6152{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006153 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006154}
6155
Zhenyu Wang2c072452009-06-05 15:38:42 +08006156static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006157intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006158{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006159 while (*num > DATA_LINK_M_N_MASK ||
6160 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006161 *num >>= 1;
6162 *den >>= 1;
6163 }
6164}
6165
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006166static void compute_m_n(unsigned int m, unsigned int n,
6167 uint32_t *ret_m, uint32_t *ret_n)
6168{
6169 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6170 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6171 intel_reduce_m_n_ratio(ret_m, ret_n);
6172}
6173
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006174void
6175intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6176 int pixel_clock, int link_clock,
6177 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006178{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006179 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006180
6181 compute_m_n(bits_per_pixel * pixel_clock,
6182 link_clock * nlanes * 8,
6183 &m_n->gmch_m, &m_n->gmch_n);
6184
6185 compute_m_n(pixel_clock, link_clock,
6186 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006187}
6188
Chris Wilsona7615032011-01-12 17:04:08 +00006189static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6190{
Jani Nikulad330a952014-01-21 11:24:25 +02006191 if (i915.panel_use_ssc >= 0)
6192 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006193 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006194 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006195}
6196
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006197static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6198 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006199{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006200 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006201 struct drm_i915_private *dev_priv = dev->dev_private;
6202 int refclk;
6203
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006204 WARN_ON(!crtc_state->base.state);
6205
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006206 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006207 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006208 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006209 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006210 refclk = dev_priv->vbt.lvds_ssc_freq;
6211 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006212 } else if (!IS_GEN2(dev)) {
6213 refclk = 96000;
6214 } else {
6215 refclk = 48000;
6216 }
6217
6218 return refclk;
6219}
6220
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006221static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006222{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006223 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006224}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006225
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006226static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6227{
6228 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006229}
6230
Daniel Vetterf47709a2013-03-28 10:42:02 +01006231static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006232 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08006233 intel_clock_t *reduced_clock)
6234{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006235 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006236 u32 fp, fp2 = 0;
6237
6238 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006239 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006240 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006241 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006242 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006243 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006244 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006245 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006246 }
6247
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006248 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006249
Daniel Vetterf47709a2013-03-28 10:42:02 +01006250 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006251 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006252 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006253 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006254 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006255 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006256 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006257 }
6258}
6259
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006260static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6261 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006262{
6263 u32 reg_val;
6264
6265 /*
6266 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6267 * and set it to a reasonable value instead.
6268 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006269 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006270 reg_val &= 0xffffff00;
6271 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006272 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006273
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006274 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006275 reg_val &= 0x8cffffff;
6276 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006277 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006278
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006279 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006280 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006281 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006282
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006283 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006284 reg_val &= 0x00ffffff;
6285 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006286 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006287}
6288
Daniel Vetterb5518422013-05-03 11:49:48 +02006289static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6290 struct intel_link_m_n *m_n)
6291{
6292 struct drm_device *dev = crtc->base.dev;
6293 struct drm_i915_private *dev_priv = dev->dev_private;
6294 int pipe = crtc->pipe;
6295
Daniel Vettere3b95f12013-05-03 11:49:49 +02006296 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6297 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6298 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6299 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006300}
6301
6302static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006303 struct intel_link_m_n *m_n,
6304 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006305{
6306 struct drm_device *dev = crtc->base.dev;
6307 struct drm_i915_private *dev_priv = dev->dev_private;
6308 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006309 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006310
6311 if (INTEL_INFO(dev)->gen >= 5) {
6312 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6313 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6314 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6315 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006316 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6317 * for gen < 8) and if DRRS is supported (to make sure the
6318 * registers are not unnecessarily accessed).
6319 */
Durgadoss R44395bf2015-02-13 15:33:02 +05306320 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006321 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006322 I915_WRITE(PIPE_DATA_M2(transcoder),
6323 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6324 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6325 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6326 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6327 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006328 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006329 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6330 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6331 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6332 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006333 }
6334}
6335
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306336void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006337{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306338 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6339
6340 if (m_n == M1_N1) {
6341 dp_m_n = &crtc->config->dp_m_n;
6342 dp_m2_n2 = &crtc->config->dp_m2_n2;
6343 } else if (m_n == M2_N2) {
6344
6345 /*
6346 * M2_N2 registers are not supported. Hence m2_n2 divider value
6347 * needs to be programmed into M1_N1.
6348 */
6349 dp_m_n = &crtc->config->dp_m2_n2;
6350 } else {
6351 DRM_ERROR("Unsupported divider value\n");
6352 return;
6353 }
6354
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006355 if (crtc->config->has_pch_encoder)
6356 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006357 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306358 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006359}
6360
Ville Syrjäläd288f652014-10-28 13:20:22 +02006361static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006362 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006363{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006364 u32 dpll, dpll_md;
6365
6366 /*
6367 * Enable DPIO clock input. We should never disable the reference
6368 * clock for pipe B, since VGA hotplug / manual detection depends
6369 * on it.
6370 */
6371 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6372 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6373 /* We should never disable this, set it here for state tracking */
6374 if (crtc->pipe == PIPE_B)
6375 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6376 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006377 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006378
Ville Syrjäläd288f652014-10-28 13:20:22 +02006379 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006380 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006381 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006382}
6383
Ville Syrjäläd288f652014-10-28 13:20:22 +02006384static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006385 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006386{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006387 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006388 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006389 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006390 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006391 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006392 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006393
Daniel Vetter09153002012-12-12 14:06:44 +01006394 mutex_lock(&dev_priv->dpio_lock);
6395
Ville Syrjäläd288f652014-10-28 13:20:22 +02006396 bestn = pipe_config->dpll.n;
6397 bestm1 = pipe_config->dpll.m1;
6398 bestm2 = pipe_config->dpll.m2;
6399 bestp1 = pipe_config->dpll.p1;
6400 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006401
Jesse Barnes89b667f2013-04-18 14:51:36 -07006402 /* See eDP HDMI DPIO driver vbios notes doc */
6403
6404 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006405 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006406 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006407
6408 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006409 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006410
6411 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006412 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006413 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006414 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006415
6416 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006417 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006418
6419 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006420 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6421 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6422 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006423 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006424
6425 /*
6426 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6427 * but we don't support that).
6428 * Note: don't use the DAC post divider as it seems unstable.
6429 */
6430 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006431 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006432
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006433 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006434 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006435
Jesse Barnes89b667f2013-04-18 14:51:36 -07006436 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006437 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006438 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6439 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006440 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03006441 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006442 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006443 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006444 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006445
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02006446 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006447 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006448 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006449 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006450 0x0df40000);
6451 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006452 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006453 0x0df70000);
6454 } else { /* HDMI or VGA */
6455 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006456 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006457 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006458 0x0df70000);
6459 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006460 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006461 0x0df40000);
6462 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006463
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006464 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006465 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006466 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6467 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006468 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006469 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006470
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006471 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01006472 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006473}
6474
Ville Syrjäläd288f652014-10-28 13:20:22 +02006475static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006476 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006477{
Ville Syrjäläd288f652014-10-28 13:20:22 +02006478 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006479 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6480 DPLL_VCO_ENABLE;
6481 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006482 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006483
Ville Syrjäläd288f652014-10-28 13:20:22 +02006484 pipe_config->dpll_hw_state.dpll_md =
6485 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006486}
6487
Ville Syrjäläd288f652014-10-28 13:20:22 +02006488static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006489 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006490{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006491 struct drm_device *dev = crtc->base.dev;
6492 struct drm_i915_private *dev_priv = dev->dev_private;
6493 int pipe = crtc->pipe;
6494 int dpll_reg = DPLL(crtc->pipe);
6495 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306496 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006497 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306498 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306499 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006500
Ville Syrjäläd288f652014-10-28 13:20:22 +02006501 bestn = pipe_config->dpll.n;
6502 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6503 bestm1 = pipe_config->dpll.m1;
6504 bestm2 = pipe_config->dpll.m2 >> 22;
6505 bestp1 = pipe_config->dpll.p1;
6506 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306507 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306508 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306509 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006510
6511 /*
6512 * Enable Refclk and SSC
6513 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006514 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02006515 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006516
6517 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006518
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006519 /* p1 and p2 divider */
6520 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6521 5 << DPIO_CHV_S1_DIV_SHIFT |
6522 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6523 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6524 1 << DPIO_CHV_K_DIV_SHIFT);
6525
6526 /* Feedback post-divider - m2 */
6527 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6528
6529 /* Feedback refclk divider - n and m1 */
6530 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6531 DPIO_CHV_M1_DIV_BY_2 |
6532 1 << DPIO_CHV_N_DIV_SHIFT);
6533
6534 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306535 if (bestm2_frac)
6536 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006537
6538 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306539 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6540 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6541 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6542 if (bestm2_frac)
6543 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6544 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006545
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05306546 /* Program digital lock detect threshold */
6547 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6548 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6549 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6550 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6551 if (!bestm2_frac)
6552 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6553 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6554
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006555 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306556 if (vco == 5400000) {
6557 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6558 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6559 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6560 tribuf_calcntr = 0x9;
6561 } else if (vco <= 6200000) {
6562 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6563 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6564 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6565 tribuf_calcntr = 0x9;
6566 } else if (vco <= 6480000) {
6567 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6568 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6569 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6570 tribuf_calcntr = 0x8;
6571 } else {
6572 /* Not supported. Apply the same limits as in the max case */
6573 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6574 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6575 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6576 tribuf_calcntr = 0;
6577 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006578 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6579
Ville Syrjälä968040b2015-03-11 22:52:08 +02006580 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306581 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6582 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6583 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6584
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006585 /* AFC Recal */
6586 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6587 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6588 DPIO_AFC_RECAL);
6589
6590 mutex_unlock(&dev_priv->dpio_lock);
6591}
6592
Ville Syrjäläd288f652014-10-28 13:20:22 +02006593/**
6594 * vlv_force_pll_on - forcibly enable just the PLL
6595 * @dev_priv: i915 private structure
6596 * @pipe: pipe PLL to enable
6597 * @dpll: PLL configuration
6598 *
6599 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6600 * in cases where we need the PLL enabled even when @pipe is not going to
6601 * be enabled.
6602 */
6603void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
6604 const struct dpll *dpll)
6605{
6606 struct intel_crtc *crtc =
6607 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006608 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006609 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02006610 .pixel_multiplier = 1,
6611 .dpll = *dpll,
6612 };
6613
6614 if (IS_CHERRYVIEW(dev)) {
6615 chv_update_pll(crtc, &pipe_config);
6616 chv_prepare_pll(crtc, &pipe_config);
6617 chv_enable_pll(crtc, &pipe_config);
6618 } else {
6619 vlv_update_pll(crtc, &pipe_config);
6620 vlv_prepare_pll(crtc, &pipe_config);
6621 vlv_enable_pll(crtc, &pipe_config);
6622 }
6623}
6624
6625/**
6626 * vlv_force_pll_off - forcibly disable just the PLL
6627 * @dev_priv: i915 private structure
6628 * @pipe: pipe PLL to disable
6629 *
6630 * Disable the PLL for @pipe. To be used in cases where we need
6631 * the PLL enabled even when @pipe is not going to be enabled.
6632 */
6633void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
6634{
6635 if (IS_CHERRYVIEW(dev))
6636 chv_disable_pll(to_i915(dev), pipe);
6637 else
6638 vlv_disable_pll(to_i915(dev), pipe);
6639}
6640
Daniel Vetterf47709a2013-03-28 10:42:02 +01006641static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006642 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01006643 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006644 int num_connectors)
6645{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006646 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006647 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006648 u32 dpll;
6649 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006650 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006651
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006652 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306653
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006654 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
6655 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006656
6657 dpll = DPLL_VGA_MODE_DIS;
6658
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006659 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006660 dpll |= DPLLB_MODE_LVDS;
6661 else
6662 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006663
Daniel Vetteref1b4602013-06-01 17:17:04 +02006664 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006665 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02006666 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006667 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02006668
6669 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006670 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006671
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006672 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006673 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006674
6675 /* compute bitmask from p1 value */
6676 if (IS_PINEVIEW(dev))
6677 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6678 else {
6679 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6680 if (IS_G4X(dev) && reduced_clock)
6681 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6682 }
6683 switch (clock->p2) {
6684 case 5:
6685 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6686 break;
6687 case 7:
6688 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6689 break;
6690 case 10:
6691 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6692 break;
6693 case 14:
6694 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6695 break;
6696 }
6697 if (INTEL_INFO(dev)->gen >= 4)
6698 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6699
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006700 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006701 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006702 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006703 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6704 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6705 else
6706 dpll |= PLL_REF_INPUT_DREFCLK;
6707
6708 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006709 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006710
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006711 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006712 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02006713 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006714 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006715 }
6716}
6717
Daniel Vetterf47709a2013-03-28 10:42:02 +01006718static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006719 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01006720 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006721 int num_connectors)
6722{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006723 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006724 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006725 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006726 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006727
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006728 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306729
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006730 dpll = DPLL_VGA_MODE_DIS;
6731
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006732 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006733 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6734 } else {
6735 if (clock->p1 == 2)
6736 dpll |= PLL_P1_DIVIDE_BY_TWO;
6737 else
6738 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6739 if (clock->p2 == 4)
6740 dpll |= PLL_P2_DIVIDE_BY_4;
6741 }
6742
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006743 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02006744 dpll |= DPLL_DVO_2X_MODE;
6745
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006746 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006747 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6748 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6749 else
6750 dpll |= PLL_REF_INPUT_DREFCLK;
6751
6752 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006753 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006754}
6755
Daniel Vetter8a654f32013-06-01 17:16:22 +02006756static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006757{
6758 struct drm_device *dev = intel_crtc->base.dev;
6759 struct drm_i915_private *dev_priv = dev->dev_private;
6760 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006761 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02006762 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006763 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006764 uint32_t crtc_vtotal, crtc_vblank_end;
6765 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006766
6767 /* We need to be careful not to changed the adjusted mode, for otherwise
6768 * the hw state checker will get angry at the mismatch. */
6769 crtc_vtotal = adjusted_mode->crtc_vtotal;
6770 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006771
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006772 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006773 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006774 crtc_vtotal -= 1;
6775 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006776
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006777 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006778 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6779 else
6780 vsyncshift = adjusted_mode->crtc_hsync_start -
6781 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006782 if (vsyncshift < 0)
6783 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006784 }
6785
6786 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006787 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006788
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006789 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006790 (adjusted_mode->crtc_hdisplay - 1) |
6791 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006792 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006793 (adjusted_mode->crtc_hblank_start - 1) |
6794 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006795 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006796 (adjusted_mode->crtc_hsync_start - 1) |
6797 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6798
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006799 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006800 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006801 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006802 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006803 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006804 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006805 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006806 (adjusted_mode->crtc_vsync_start - 1) |
6807 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6808
Paulo Zanonib5e508d2012-10-24 11:34:43 -02006809 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6810 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6811 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6812 * bits. */
6813 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6814 (pipe == PIPE_B || pipe == PIPE_C))
6815 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6816
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006817 /* pipesrc controls the size that is scaled from, which should
6818 * always be the user's requested size.
6819 */
6820 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006821 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6822 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006823}
6824
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006825static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006826 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006827{
6828 struct drm_device *dev = crtc->base.dev;
6829 struct drm_i915_private *dev_priv = dev->dev_private;
6830 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6831 uint32_t tmp;
6832
6833 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006834 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6835 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006836 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006837 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6838 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006839 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006840 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6841 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006842
6843 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006844 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6845 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006846 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006847 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6848 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006849 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006850 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6851 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006852
6853 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006854 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6855 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
6856 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006857 }
6858
6859 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03006860 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6861 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6862
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006863 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
6864 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006865}
6866
Daniel Vetterf6a83282014-02-11 15:28:57 -08006867void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006868 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03006869{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006870 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
6871 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
6872 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
6873 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006874
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006875 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
6876 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
6877 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
6878 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006879
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006880 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006881
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006882 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
6883 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006884}
6885
Daniel Vetter84b046f2013-02-19 18:48:54 +01006886static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6887{
6888 struct drm_device *dev = intel_crtc->base.dev;
6889 struct drm_i915_private *dev_priv = dev->dev_private;
6890 uint32_t pipeconf;
6891
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006892 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006893
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03006894 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6895 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6896 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02006897
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006898 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006899 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006900
Daniel Vetterff9ce462013-04-24 14:57:17 +02006901 /* only g4x and later have fancy bpc/dither controls */
6902 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02006903 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006904 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02006905 pipeconf |= PIPECONF_DITHER_EN |
6906 PIPECONF_DITHER_TYPE_SP;
6907
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006908 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02006909 case 18:
6910 pipeconf |= PIPECONF_6BPC;
6911 break;
6912 case 24:
6913 pipeconf |= PIPECONF_8BPC;
6914 break;
6915 case 30:
6916 pipeconf |= PIPECONF_10BPC;
6917 break;
6918 default:
6919 /* Case prevented by intel_choose_pipe_bpp_dither. */
6920 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01006921 }
6922 }
6923
6924 if (HAS_PIPE_CXSR(dev)) {
6925 if (intel_crtc->lowfreq_avail) {
6926 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6927 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6928 } else {
6929 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01006930 }
6931 }
6932
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006933 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006934 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006935 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006936 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6937 else
6938 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6939 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01006940 pipeconf |= PIPECONF_PROGRESSIVE;
6941
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006942 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006943 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03006944
Daniel Vetter84b046f2013-02-19 18:48:54 +01006945 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6946 POSTING_READ(PIPECONF(intel_crtc->pipe));
6947}
6948
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006949static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
6950 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08006951{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006952 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08006953 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07006954 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07006955 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02006956 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006957 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01006958 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08006959 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02006960 struct drm_atomic_state *state = crtc_state->base.state;
6961 struct drm_connector_state *connector_state;
6962 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006963
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02006964 for (i = 0; i < state->num_connector; i++) {
6965 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006966 continue;
6967
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02006968 connector_state = state->connector_states[i];
6969 if (connector_state->crtc != &crtc->base)
6970 continue;
6971
6972 encoder = to_intel_encoder(connector_state->best_encoder);
6973
Chris Wilson5eddb702010-09-11 13:48:45 +01006974 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006975 case INTEL_OUTPUT_LVDS:
6976 is_lvds = true;
6977 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006978 case INTEL_OUTPUT_DSI:
6979 is_dsi = true;
6980 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02006981 default:
6982 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006983 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006984
Eric Anholtc751ce42010-03-25 11:48:48 -07006985 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08006986 }
6987
Jani Nikulaf2335332013-09-13 11:03:09 +03006988 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02006989 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006990
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006991 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006992 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03006993
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006994 /*
6995 * Returns a set of divisors for the desired target clock with
6996 * the given refclk, or FALSE. The returned values represent
6997 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6998 * 2) / p1 / p2.
6999 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007000 limit = intel_limit(crtc_state, refclk);
7001 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007002 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007003 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007004 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007005 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7006 return -EINVAL;
7007 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007008
Jani Nikulaf2335332013-09-13 11:03:09 +03007009 if (is_lvds && dev_priv->lvds_downclock_avail) {
7010 /*
7011 * Ensure we match the reduced clock's P to the target
7012 * clock. If the clocks don't match, we can't switch
7013 * the display clock by using the FP0/FP1. In such case
7014 * we will disable the LVDS downclock feature.
7015 */
7016 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007017 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007018 dev_priv->lvds_downclock,
7019 refclk, &clock,
7020 &reduced_clock);
7021 }
7022 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007023 crtc_state->dpll.n = clock.n;
7024 crtc_state->dpll.m1 = clock.m1;
7025 crtc_state->dpll.m2 = clock.m2;
7026 crtc_state->dpll.p1 = clock.p1;
7027 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007028 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007029
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007030 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007031 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307032 has_reduced_clock ? &reduced_clock : NULL,
7033 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007034 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007035 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007036 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007037 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007038 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007039 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007040 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007041 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007042 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007043
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007044 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007045}
7046
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007047static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007048 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007049{
7050 struct drm_device *dev = crtc->base.dev;
7051 struct drm_i915_private *dev_priv = dev->dev_private;
7052 uint32_t tmp;
7053
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007054 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7055 return;
7056
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007057 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007058 if (!(tmp & PFIT_ENABLE))
7059 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007060
Daniel Vetter06922822013-07-11 13:35:40 +02007061 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007062 if (INTEL_INFO(dev)->gen < 4) {
7063 if (crtc->pipe != PIPE_B)
7064 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007065 } else {
7066 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7067 return;
7068 }
7069
Daniel Vetter06922822013-07-11 13:35:40 +02007070 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007071 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7072 if (INTEL_INFO(dev)->gen < 5)
7073 pipe_config->gmch_pfit.lvds_border_bits =
7074 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7075}
7076
Jesse Barnesacbec812013-09-20 11:29:32 -07007077static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007078 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007079{
7080 struct drm_device *dev = crtc->base.dev;
7081 struct drm_i915_private *dev_priv = dev->dev_private;
7082 int pipe = pipe_config->cpu_transcoder;
7083 intel_clock_t clock;
7084 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007085 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007086
Shobhit Kumarf573de52014-07-30 20:32:37 +05307087 /* In case of MIPI DPLL will not even be used */
7088 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7089 return;
7090
Jesse Barnesacbec812013-09-20 11:29:32 -07007091 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007092 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07007093 mutex_unlock(&dev_priv->dpio_lock);
7094
7095 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7096 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7097 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7098 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7099 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7100
Ville Syrjäläf6466282013-10-14 14:50:31 +03007101 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007102
Ville Syrjäläf6466282013-10-14 14:50:31 +03007103 /* clock.dot is the fast clock */
7104 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007105}
7106
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007107static void
7108i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7109 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007110{
7111 struct drm_device *dev = crtc->base.dev;
7112 struct drm_i915_private *dev_priv = dev->dev_private;
7113 u32 val, base, offset;
7114 int pipe = crtc->pipe, plane = crtc->plane;
7115 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007116 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007117 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007118 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007119
Damien Lespiau42a7b082015-02-05 19:35:13 +00007120 val = I915_READ(DSPCNTR(plane));
7121 if (!(val & DISPLAY_PLANE_ENABLE))
7122 return;
7123
Damien Lespiaud9806c92015-01-21 14:07:19 +00007124 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007125 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007126 DRM_DEBUG_KMS("failed to alloc fb\n");
7127 return;
7128 }
7129
Damien Lespiau1b842c82015-01-21 13:50:54 +00007130 fb = &intel_fb->base;
7131
Daniel Vetter18c52472015-02-10 17:16:09 +00007132 if (INTEL_INFO(dev)->gen >= 4) {
7133 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007134 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007135 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7136 }
7137 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007138
7139 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007140 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007141 fb->pixel_format = fourcc;
7142 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007143
7144 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007145 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007146 offset = I915_READ(DSPTILEOFF(plane));
7147 else
7148 offset = I915_READ(DSPLINOFF(plane));
7149 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7150 } else {
7151 base = I915_READ(DSPADDR(plane));
7152 }
7153 plane_config->base = base;
7154
7155 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007156 fb->width = ((val >> 16) & 0xfff) + 1;
7157 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007158
7159 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007160 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007161
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007162 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007163 fb->pixel_format,
7164 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007165
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007166 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007167
Damien Lespiau2844a922015-01-20 12:51:48 +00007168 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7169 pipe_name(pipe), plane, fb->width, fb->height,
7170 fb->bits_per_pixel, base, fb->pitches[0],
7171 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007172
Damien Lespiau2d140302015-02-05 17:22:18 +00007173 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007174}
7175
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007176static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007177 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007178{
7179 struct drm_device *dev = crtc->base.dev;
7180 struct drm_i915_private *dev_priv = dev->dev_private;
7181 int pipe = pipe_config->cpu_transcoder;
7182 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7183 intel_clock_t clock;
7184 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7185 int refclk = 100000;
7186
7187 mutex_lock(&dev_priv->dpio_lock);
7188 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7189 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7190 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7191 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7192 mutex_unlock(&dev_priv->dpio_lock);
7193
7194 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7195 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7196 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7197 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7198 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7199
7200 chv_clock(refclk, &clock);
7201
7202 /* clock.dot is the fast clock */
7203 pipe_config->port_clock = clock.dot / 5;
7204}
7205
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007206static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007207 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007208{
7209 struct drm_device *dev = crtc->base.dev;
7210 struct drm_i915_private *dev_priv = dev->dev_private;
7211 uint32_t tmp;
7212
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007213 if (!intel_display_power_is_enabled(dev_priv,
7214 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007215 return false;
7216
Daniel Vettere143a212013-07-04 12:01:15 +02007217 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007218 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007219
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007220 tmp = I915_READ(PIPECONF(crtc->pipe));
7221 if (!(tmp & PIPECONF_ENABLE))
7222 return false;
7223
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007224 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7225 switch (tmp & PIPECONF_BPC_MASK) {
7226 case PIPECONF_6BPC:
7227 pipe_config->pipe_bpp = 18;
7228 break;
7229 case PIPECONF_8BPC:
7230 pipe_config->pipe_bpp = 24;
7231 break;
7232 case PIPECONF_10BPC:
7233 pipe_config->pipe_bpp = 30;
7234 break;
7235 default:
7236 break;
7237 }
7238 }
7239
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007240 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7241 pipe_config->limited_color_range = true;
7242
Ville Syrjälä282740f2013-09-04 18:30:03 +03007243 if (INTEL_INFO(dev)->gen < 4)
7244 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7245
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007246 intel_get_pipe_timings(crtc, pipe_config);
7247
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007248 i9xx_get_pfit_config(crtc, pipe_config);
7249
Daniel Vetter6c49f242013-06-06 12:45:25 +02007250 if (INTEL_INFO(dev)->gen >= 4) {
7251 tmp = I915_READ(DPLL_MD(crtc->pipe));
7252 pipe_config->pixel_multiplier =
7253 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7254 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007255 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02007256 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7257 tmp = I915_READ(DPLL(crtc->pipe));
7258 pipe_config->pixel_multiplier =
7259 ((tmp & SDVO_MULTIPLIER_MASK)
7260 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7261 } else {
7262 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7263 * port and will be fixed up in the encoder->get_config
7264 * function. */
7265 pipe_config->pixel_multiplier = 1;
7266 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007267 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7268 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007269 /*
7270 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7271 * on 830. Filter it out here so that we don't
7272 * report errors due to that.
7273 */
7274 if (IS_I830(dev))
7275 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7276
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007277 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7278 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007279 } else {
7280 /* Mask out read-only status bits. */
7281 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7282 DPLL_PORTC_READY_MASK |
7283 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007284 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007285
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007286 if (IS_CHERRYVIEW(dev))
7287 chv_crtc_clock_get(crtc, pipe_config);
7288 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07007289 vlv_crtc_clock_get(crtc, pipe_config);
7290 else
7291 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007292
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007293 return true;
7294}
7295
Paulo Zanonidde86e22012-12-01 12:04:25 -02007296static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007297{
7298 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007299 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007300 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007301 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007302 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007303 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007304 bool has_ck505 = false;
7305 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007306
7307 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01007308 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007309 switch (encoder->type) {
7310 case INTEL_OUTPUT_LVDS:
7311 has_panel = true;
7312 has_lvds = true;
7313 break;
7314 case INTEL_OUTPUT_EDP:
7315 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03007316 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007317 has_cpu_edp = true;
7318 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007319 default:
7320 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007321 }
7322 }
7323
Keith Packard99eb6a02011-09-26 14:29:12 -07007324 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007325 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007326 can_ssc = has_ck505;
7327 } else {
7328 has_ck505 = false;
7329 can_ssc = true;
7330 }
7331
Imre Deak2de69052013-05-08 13:14:04 +03007332 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7333 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007334
7335 /* Ironlake: try to setup display ref clock before DPLL
7336 * enabling. This is only under driver's control after
7337 * PCH B stepping, previous chipset stepping should be
7338 * ignoring this setting.
7339 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007340 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007341
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007342 /* As we must carefully and slowly disable/enable each source in turn,
7343 * compute the final state we want first and check if we need to
7344 * make any changes at all.
7345 */
7346 final = val;
7347 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007348 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007349 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007350 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007351 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7352
7353 final &= ~DREF_SSC_SOURCE_MASK;
7354 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7355 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007356
Keith Packard199e5d72011-09-22 12:01:57 -07007357 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007358 final |= DREF_SSC_SOURCE_ENABLE;
7359
7360 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7361 final |= DREF_SSC1_ENABLE;
7362
7363 if (has_cpu_edp) {
7364 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7365 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7366 else
7367 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7368 } else
7369 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7370 } else {
7371 final |= DREF_SSC_SOURCE_DISABLE;
7372 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7373 }
7374
7375 if (final == val)
7376 return;
7377
7378 /* Always enable nonspread source */
7379 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7380
7381 if (has_ck505)
7382 val |= DREF_NONSPREAD_CK505_ENABLE;
7383 else
7384 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7385
7386 if (has_panel) {
7387 val &= ~DREF_SSC_SOURCE_MASK;
7388 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007389
Keith Packard199e5d72011-09-22 12:01:57 -07007390 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007391 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007392 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007393 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007394 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007395 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007396
7397 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007398 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007399 POSTING_READ(PCH_DREF_CONTROL);
7400 udelay(200);
7401
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007402 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007403
7404 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007405 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007406 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007407 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007408 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007409 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007410 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007411 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007412 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007413
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007414 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007415 POSTING_READ(PCH_DREF_CONTROL);
7416 udelay(200);
7417 } else {
7418 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7419
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007420 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007421
7422 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007423 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007424
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007425 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007426 POSTING_READ(PCH_DREF_CONTROL);
7427 udelay(200);
7428
7429 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007430 val &= ~DREF_SSC_SOURCE_MASK;
7431 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007432
7433 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007434 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007435
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007436 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007437 POSTING_READ(PCH_DREF_CONTROL);
7438 udelay(200);
7439 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007440
7441 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007442}
7443
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007444static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007445{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007446 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007447
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007448 tmp = I915_READ(SOUTH_CHICKEN2);
7449 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7450 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007451
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007452 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7453 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7454 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007455
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007456 tmp = I915_READ(SOUTH_CHICKEN2);
7457 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7458 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007459
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007460 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7461 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7462 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007463}
7464
7465/* WaMPhyProgramming:hsw */
7466static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7467{
7468 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007469
7470 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7471 tmp &= ~(0xFF << 24);
7472 tmp |= (0x12 << 24);
7473 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7474
Paulo Zanonidde86e22012-12-01 12:04:25 -02007475 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7476 tmp |= (1 << 11);
7477 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7478
7479 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7480 tmp |= (1 << 11);
7481 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7482
Paulo Zanonidde86e22012-12-01 12:04:25 -02007483 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7484 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7485 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7486
7487 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7488 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7489 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7490
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007491 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7492 tmp &= ~(7 << 13);
7493 tmp |= (5 << 13);
7494 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007495
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007496 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7497 tmp &= ~(7 << 13);
7498 tmp |= (5 << 13);
7499 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007500
7501 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7502 tmp &= ~0xFF;
7503 tmp |= 0x1C;
7504 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7505
7506 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7507 tmp &= ~0xFF;
7508 tmp |= 0x1C;
7509 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7510
7511 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7512 tmp &= ~(0xFF << 16);
7513 tmp |= (0x1C << 16);
7514 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7515
7516 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7517 tmp &= ~(0xFF << 16);
7518 tmp |= (0x1C << 16);
7519 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7520
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007521 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7522 tmp |= (1 << 27);
7523 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007524
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007525 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7526 tmp |= (1 << 27);
7527 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007528
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007529 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7530 tmp &= ~(0xF << 28);
7531 tmp |= (4 << 28);
7532 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007533
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007534 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7535 tmp &= ~(0xF << 28);
7536 tmp |= (4 << 28);
7537 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007538}
7539
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007540/* Implements 3 different sequences from BSpec chapter "Display iCLK
7541 * Programming" based on the parameters passed:
7542 * - Sequence to enable CLKOUT_DP
7543 * - Sequence to enable CLKOUT_DP without spread
7544 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7545 */
7546static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
7547 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007548{
7549 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007550 uint32_t reg, tmp;
7551
7552 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7553 with_spread = true;
7554 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
7555 with_fdi, "LP PCH doesn't have FDI\n"))
7556 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007557
7558 mutex_lock(&dev_priv->dpio_lock);
7559
7560 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7561 tmp &= ~SBI_SSCCTL_DISABLE;
7562 tmp |= SBI_SSCCTL_PATHALT;
7563 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7564
7565 udelay(24);
7566
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007567 if (with_spread) {
7568 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7569 tmp &= ~SBI_SSCCTL_PATHALT;
7570 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007571
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007572 if (with_fdi) {
7573 lpt_reset_fdi_mphy(dev_priv);
7574 lpt_program_fdi_mphy(dev_priv);
7575 }
7576 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02007577
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007578 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7579 SBI_GEN0 : SBI_DBUFF0;
7580 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7581 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7582 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01007583
7584 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007585}
7586
Paulo Zanoni47701c32013-07-23 11:19:25 -03007587/* Sequence to disable CLKOUT_DP */
7588static void lpt_disable_clkout_dp(struct drm_device *dev)
7589{
7590 struct drm_i915_private *dev_priv = dev->dev_private;
7591 uint32_t reg, tmp;
7592
7593 mutex_lock(&dev_priv->dpio_lock);
7594
7595 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7596 SBI_GEN0 : SBI_DBUFF0;
7597 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7598 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7599 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7600
7601 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7602 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7603 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7604 tmp |= SBI_SSCCTL_PATHALT;
7605 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7606 udelay(32);
7607 }
7608 tmp |= SBI_SSCCTL_DISABLE;
7609 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7610 }
7611
7612 mutex_unlock(&dev_priv->dpio_lock);
7613}
7614
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007615static void lpt_init_pch_refclk(struct drm_device *dev)
7616{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007617 struct intel_encoder *encoder;
7618 bool has_vga = false;
7619
Damien Lespiaub2784e12014-08-05 11:29:37 +01007620 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007621 switch (encoder->type) {
7622 case INTEL_OUTPUT_ANALOG:
7623 has_vga = true;
7624 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007625 default:
7626 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007627 }
7628 }
7629
Paulo Zanoni47701c32013-07-23 11:19:25 -03007630 if (has_vga)
7631 lpt_enable_clkout_dp(dev, true, true);
7632 else
7633 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007634}
7635
Paulo Zanonidde86e22012-12-01 12:04:25 -02007636/*
7637 * Initialize reference clocks when the driver loads
7638 */
7639void intel_init_pch_refclk(struct drm_device *dev)
7640{
7641 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7642 ironlake_init_pch_refclk(dev);
7643 else if (HAS_PCH_LPT(dev))
7644 lpt_init_pch_refclk(dev);
7645}
7646
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007647static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007648{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007649 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007650 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007651 struct drm_atomic_state *state = crtc_state->base.state;
7652 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007653 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007654 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007655 bool is_lvds = false;
7656
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007657 for (i = 0; i < state->num_connector; i++) {
7658 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007659 continue;
7660
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007661 connector_state = state->connector_states[i];
7662 if (connector_state->crtc != crtc_state->base.crtc)
7663 continue;
7664
7665 encoder = to_intel_encoder(connector_state->best_encoder);
7666
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007667 switch (encoder->type) {
7668 case INTEL_OUTPUT_LVDS:
7669 is_lvds = true;
7670 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007671 default:
7672 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007673 }
7674 num_connectors++;
7675 }
7676
7677 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007678 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007679 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007680 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007681 }
7682
7683 return 120000;
7684}
7685
Daniel Vetter6ff93602013-04-19 11:24:36 +02007686static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03007687{
7688 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
7689 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7690 int pipe = intel_crtc->pipe;
7691 uint32_t val;
7692
Daniel Vetter78114072013-06-13 00:54:57 +02007693 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03007694
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007695 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03007696 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007697 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007698 break;
7699 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007700 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007701 break;
7702 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007703 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007704 break;
7705 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007706 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007707 break;
7708 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03007709 /* Case prevented by intel_choose_pipe_bpp_dither. */
7710 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03007711 }
7712
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007713 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03007714 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7715
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007716 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03007717 val |= PIPECONF_INTERLACED_ILK;
7718 else
7719 val |= PIPECONF_PROGRESSIVE;
7720
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007721 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007722 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007723
Paulo Zanonic8203562012-09-12 10:06:29 -03007724 I915_WRITE(PIPECONF(pipe), val);
7725 POSTING_READ(PIPECONF(pipe));
7726}
7727
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007728/*
7729 * Set up the pipe CSC unit.
7730 *
7731 * Currently only full range RGB to limited range RGB conversion
7732 * is supported, but eventually this should handle various
7733 * RGB<->YCbCr scenarios as well.
7734 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01007735static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007736{
7737 struct drm_device *dev = crtc->dev;
7738 struct drm_i915_private *dev_priv = dev->dev_private;
7739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7740 int pipe = intel_crtc->pipe;
7741 uint16_t coeff = 0x7800; /* 1.0 */
7742
7743 /*
7744 * TODO: Check what kind of values actually come out of the pipe
7745 * with these coeff/postoff values and adjust to get the best
7746 * accuracy. Perhaps we even need to take the bpc value into
7747 * consideration.
7748 */
7749
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007750 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007751 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
7752
7753 /*
7754 * GY/GU and RY/RU should be the other way around according
7755 * to BSpec, but reality doesn't agree. Just set them up in
7756 * a way that results in the correct picture.
7757 */
7758 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
7759 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
7760
7761 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
7762 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
7763
7764 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
7765 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
7766
7767 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
7768 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
7769 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
7770
7771 if (INTEL_INFO(dev)->gen > 6) {
7772 uint16_t postoff = 0;
7773
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007774 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02007775 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007776
7777 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
7778 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
7779 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
7780
7781 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
7782 } else {
7783 uint32_t mode = CSC_MODE_YUV_TO_RGB;
7784
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007785 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007786 mode |= CSC_BLACK_SCREEN_OFFSET;
7787
7788 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
7789 }
7790}
7791
Daniel Vetter6ff93602013-04-19 11:24:36 +02007792static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007793{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007794 struct drm_device *dev = crtc->dev;
7795 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007796 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007797 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007798 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007799 uint32_t val;
7800
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007801 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007802
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007803 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007804 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7805
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007806 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007807 val |= PIPECONF_INTERLACED_ILK;
7808 else
7809 val |= PIPECONF_PROGRESSIVE;
7810
Paulo Zanoni702e7a52012-10-23 18:29:59 -02007811 I915_WRITE(PIPECONF(cpu_transcoder), val);
7812 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007813
7814 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
7815 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007816
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05307817 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007818 val = 0;
7819
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007820 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007821 case 18:
7822 val |= PIPEMISC_DITHER_6_BPC;
7823 break;
7824 case 24:
7825 val |= PIPEMISC_DITHER_8_BPC;
7826 break;
7827 case 30:
7828 val |= PIPEMISC_DITHER_10_BPC;
7829 break;
7830 case 36:
7831 val |= PIPEMISC_DITHER_12_BPC;
7832 break;
7833 default:
7834 /* Case prevented by pipe_config_set_bpp. */
7835 BUG();
7836 }
7837
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007838 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007839 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7840
7841 I915_WRITE(PIPEMISC(pipe), val);
7842 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007843}
7844
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007845static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007846 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007847 intel_clock_t *clock,
7848 bool *has_reduced_clock,
7849 intel_clock_t *reduced_clock)
7850{
7851 struct drm_device *dev = crtc->dev;
7852 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007853 int refclk;
7854 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02007855 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007856
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007857 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007858
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007859 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007860
7861 /*
7862 * Returns a set of divisors for the desired target clock with the given
7863 * refclk, or FALSE. The returned values represent the clock equation:
7864 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7865 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007866 limit = intel_limit(crtc_state, refclk);
7867 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007868 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007869 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007870 if (!ret)
7871 return false;
7872
7873 if (is_lvds && dev_priv->lvds_downclock_avail) {
7874 /*
7875 * Ensure we match the reduced clock's P to the target clock.
7876 * If the clocks don't match, we can't switch the display clock
7877 * by using the FP0/FP1. In such case we will disable the LVDS
7878 * downclock feature.
7879 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02007880 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007881 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007882 dev_priv->lvds_downclock,
7883 refclk, clock,
7884 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007885 }
7886
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007887 return true;
7888}
7889
Paulo Zanonid4b19312012-11-29 11:29:32 -02007890int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7891{
7892 /*
7893 * Account for spread spectrum to avoid
7894 * oversubscribing the link. Max center spread
7895 * is 2.5%; use 5% for safety's sake.
7896 */
7897 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02007898 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02007899}
7900
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007901static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02007902{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007903 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03007904}
7905
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007906static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007907 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007908 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007909 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007910{
7911 struct drm_crtc *crtc = &intel_crtc->base;
7912 struct drm_device *dev = crtc->dev;
7913 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007914 struct drm_atomic_state *state = crtc_state->base.state;
7915 struct drm_connector_state *connector_state;
7916 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007917 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007918 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02007919 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007920
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007921 for (i = 0; i < state->num_connector; i++) {
7922 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007923 continue;
7924
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007925 connector_state = state->connector_states[i];
7926 if (connector_state->crtc != crtc_state->base.crtc)
7927 continue;
7928
7929 encoder = to_intel_encoder(connector_state->best_encoder);
7930
7931 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007932 case INTEL_OUTPUT_LVDS:
7933 is_lvds = true;
7934 break;
7935 case INTEL_OUTPUT_SDVO:
7936 case INTEL_OUTPUT_HDMI:
7937 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007938 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007939 default:
7940 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007941 }
7942
7943 num_connectors++;
7944 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007945
Chris Wilsonc1858122010-12-03 21:35:48 +00007946 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07007947 factor = 21;
7948 if (is_lvds) {
7949 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007950 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02007951 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07007952 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007953 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07007954 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00007955
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007956 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02007957 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00007958
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007959 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7960 *fp2 |= FP_CB_TUNE;
7961
Chris Wilson5eddb702010-09-11 13:48:45 +01007962 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08007963
Eric Anholta07d6782011-03-30 13:01:08 -07007964 if (is_lvds)
7965 dpll |= DPLLB_MODE_LVDS;
7966 else
7967 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007968
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007969 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007970 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007971
7972 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007973 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007974 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007975 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08007976
Eric Anholta07d6782011-03-30 13:01:08 -07007977 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007978 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007979 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007980 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007981
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007982 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07007983 case 5:
7984 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7985 break;
7986 case 7:
7987 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7988 break;
7989 case 10:
7990 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7991 break;
7992 case 14:
7993 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7994 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007995 }
7996
Daniel Vetterb4c09f32013-04-30 14:01:42 +02007997 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007998 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08007999 else
8000 dpll |= PLL_REF_INPUT_DREFCLK;
8001
Daniel Vetter959e16d2013-06-05 13:34:21 +02008002 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008003}
8004
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008005static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8006 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008007{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008008 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008009 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008010 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008011 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008012 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008013 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008014
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008015 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008016
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008017 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8018 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8019
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008020 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008021 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008022 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008023 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8024 return -EINVAL;
8025 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008026 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008027 if (!crtc_state->clock_set) {
8028 crtc_state->dpll.n = clock.n;
8029 crtc_state->dpll.m1 = clock.m1;
8030 crtc_state->dpll.m2 = clock.m2;
8031 crtc_state->dpll.p1 = clock.p1;
8032 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008033 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008034
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008035 /* 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 +02008036 if (crtc_state->has_pch_encoder) {
8037 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008038 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008039 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008040
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008041 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008042 &fp, &reduced_clock,
8043 has_reduced_clock ? &fp2 : NULL);
8044
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008045 crtc_state->dpll_hw_state.dpll = dpll;
8046 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008047 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008048 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008049 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008050 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008051
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008052 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008053 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008054 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008055 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008056 return -EINVAL;
8057 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008058 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008059
Rodrigo Viviab585de2015-03-24 12:40:09 -07008060 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008061 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008062 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008063 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008064
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008065 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008066}
8067
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008068static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8069 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008070{
8071 struct drm_device *dev = crtc->base.dev;
8072 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008073 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008074
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008075 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8076 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8077 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8078 & ~TU_SIZE_MASK;
8079 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8080 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8081 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8082}
8083
8084static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8085 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008086 struct intel_link_m_n *m_n,
8087 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008088{
8089 struct drm_device *dev = crtc->base.dev;
8090 struct drm_i915_private *dev_priv = dev->dev_private;
8091 enum pipe pipe = crtc->pipe;
8092
8093 if (INTEL_INFO(dev)->gen >= 5) {
8094 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8095 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8096 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8097 & ~TU_SIZE_MASK;
8098 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8099 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8100 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008101 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8102 * gen < 8) and if DRRS is supported (to make sure the
8103 * registers are not unnecessarily read).
8104 */
8105 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008106 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008107 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8108 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8109 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8110 & ~TU_SIZE_MASK;
8111 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8112 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8113 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8114 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008115 } else {
8116 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8117 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8118 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8119 & ~TU_SIZE_MASK;
8120 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8121 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8122 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8123 }
8124}
8125
8126void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008127 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008128{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008129 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008130 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8131 else
8132 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008133 &pipe_config->dp_m_n,
8134 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008135}
8136
Daniel Vetter72419202013-04-04 13:28:53 +02008137static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008138 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008139{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008140 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008141 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008142}
8143
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008144static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008145 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008146{
8147 struct drm_device *dev = crtc->base.dev;
8148 struct drm_i915_private *dev_priv = dev->dev_private;
8149 uint32_t tmp;
8150
8151 tmp = I915_READ(PS_CTL(crtc->pipe));
8152
8153 if (tmp & PS_ENABLE) {
8154 pipe_config->pch_pfit.enabled = true;
8155 pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
8156 pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
8157 }
8158}
8159
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008160static void
8161skylake_get_initial_plane_config(struct intel_crtc *crtc,
8162 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008163{
8164 struct drm_device *dev = crtc->base.dev;
8165 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008166 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008167 int pipe = crtc->pipe;
8168 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008169 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008170 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008171 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008172
Damien Lespiaud9806c92015-01-21 14:07:19 +00008173 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008174 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008175 DRM_DEBUG_KMS("failed to alloc fb\n");
8176 return;
8177 }
8178
Damien Lespiau1b842c82015-01-21 13:50:54 +00008179 fb = &intel_fb->base;
8180
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008181 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008182 if (!(val & PLANE_CTL_ENABLE))
8183 goto error;
8184
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008185 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8186 fourcc = skl_format_to_fourcc(pixel_format,
8187 val & PLANE_CTL_ORDER_RGBX,
8188 val & PLANE_CTL_ALPHA_MASK);
8189 fb->pixel_format = fourcc;
8190 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8191
Damien Lespiau40f46282015-02-27 11:15:21 +00008192 tiling = val & PLANE_CTL_TILED_MASK;
8193 switch (tiling) {
8194 case PLANE_CTL_TILED_LINEAR:
8195 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8196 break;
8197 case PLANE_CTL_TILED_X:
8198 plane_config->tiling = I915_TILING_X;
8199 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8200 break;
8201 case PLANE_CTL_TILED_Y:
8202 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8203 break;
8204 case PLANE_CTL_TILED_YF:
8205 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8206 break;
8207 default:
8208 MISSING_CASE(tiling);
8209 goto error;
8210 }
8211
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008212 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8213 plane_config->base = base;
8214
8215 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8216
8217 val = I915_READ(PLANE_SIZE(pipe, 0));
8218 fb->height = ((val >> 16) & 0xfff) + 1;
8219 fb->width = ((val >> 0) & 0x1fff) + 1;
8220
8221 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00008222 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8223 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008224 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8225
8226 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008227 fb->pixel_format,
8228 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008229
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008230 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008231
8232 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8233 pipe_name(pipe), fb->width, fb->height,
8234 fb->bits_per_pixel, base, fb->pitches[0],
8235 plane_config->size);
8236
Damien Lespiau2d140302015-02-05 17:22:18 +00008237 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008238 return;
8239
8240error:
8241 kfree(fb);
8242}
8243
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008244static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008245 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008246{
8247 struct drm_device *dev = crtc->base.dev;
8248 struct drm_i915_private *dev_priv = dev->dev_private;
8249 uint32_t tmp;
8250
8251 tmp = I915_READ(PF_CTL(crtc->pipe));
8252
8253 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008254 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008255 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8256 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008257
8258 /* We currently do not free assignements of panel fitters on
8259 * ivb/hsw (since we don't use the higher upscaling modes which
8260 * differentiates them) so just WARN about this case for now. */
8261 if (IS_GEN7(dev)) {
8262 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8263 PF_PIPE_SEL_IVB(crtc->pipe));
8264 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008265 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008266}
8267
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008268static void
8269ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8270 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008271{
8272 struct drm_device *dev = crtc->base.dev;
8273 struct drm_i915_private *dev_priv = dev->dev_private;
8274 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008275 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008276 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008277 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008278 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008279 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008280
Damien Lespiau42a7b082015-02-05 19:35:13 +00008281 val = I915_READ(DSPCNTR(pipe));
8282 if (!(val & DISPLAY_PLANE_ENABLE))
8283 return;
8284
Damien Lespiaud9806c92015-01-21 14:07:19 +00008285 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008286 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008287 DRM_DEBUG_KMS("failed to alloc fb\n");
8288 return;
8289 }
8290
Damien Lespiau1b842c82015-01-21 13:50:54 +00008291 fb = &intel_fb->base;
8292
Daniel Vetter18c52472015-02-10 17:16:09 +00008293 if (INTEL_INFO(dev)->gen >= 4) {
8294 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008295 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008296 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8297 }
8298 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008299
8300 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008301 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008302 fb->pixel_format = fourcc;
8303 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008304
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008305 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008306 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008307 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008308 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00008309 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008310 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008311 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008312 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008313 }
8314 plane_config->base = base;
8315
8316 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008317 fb->width = ((val >> 16) & 0xfff) + 1;
8318 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008319
8320 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008321 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008322
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008323 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008324 fb->pixel_format,
8325 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008326
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008327 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008328
Damien Lespiau2844a922015-01-20 12:51:48 +00008329 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8330 pipe_name(pipe), fb->width, fb->height,
8331 fb->bits_per_pixel, base, fb->pitches[0],
8332 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008333
Damien Lespiau2d140302015-02-05 17:22:18 +00008334 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008335}
8336
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008337static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008338 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008339{
8340 struct drm_device *dev = crtc->base.dev;
8341 struct drm_i915_private *dev_priv = dev->dev_private;
8342 uint32_t tmp;
8343
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008344 if (!intel_display_power_is_enabled(dev_priv,
8345 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008346 return false;
8347
Daniel Vettere143a212013-07-04 12:01:15 +02008348 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008349 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008350
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008351 tmp = I915_READ(PIPECONF(crtc->pipe));
8352 if (!(tmp & PIPECONF_ENABLE))
8353 return false;
8354
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008355 switch (tmp & PIPECONF_BPC_MASK) {
8356 case PIPECONF_6BPC:
8357 pipe_config->pipe_bpp = 18;
8358 break;
8359 case PIPECONF_8BPC:
8360 pipe_config->pipe_bpp = 24;
8361 break;
8362 case PIPECONF_10BPC:
8363 pipe_config->pipe_bpp = 30;
8364 break;
8365 case PIPECONF_12BPC:
8366 pipe_config->pipe_bpp = 36;
8367 break;
8368 default:
8369 break;
8370 }
8371
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008372 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8373 pipe_config->limited_color_range = true;
8374
Daniel Vetterab9412b2013-05-03 11:49:46 +02008375 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008376 struct intel_shared_dpll *pll;
8377
Daniel Vetter88adfff2013-03-28 10:42:01 +01008378 pipe_config->has_pch_encoder = true;
8379
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008380 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8381 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8382 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008383
8384 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008385
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008386 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02008387 pipe_config->shared_dpll =
8388 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008389 } else {
8390 tmp = I915_READ(PCH_DPLL_SEL);
8391 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8392 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8393 else
8394 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8395 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008396
8397 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8398
8399 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8400 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008401
8402 tmp = pipe_config->dpll_hw_state.dpll;
8403 pipe_config->pixel_multiplier =
8404 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8405 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008406
8407 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008408 } else {
8409 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008410 }
8411
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008412 intel_get_pipe_timings(crtc, pipe_config);
8413
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008414 ironlake_get_pfit_config(crtc, pipe_config);
8415
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008416 return true;
8417}
8418
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008419static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8420{
8421 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008422 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008423
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008424 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008425 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008426 pipe_name(crtc->pipe));
8427
Rob Clarke2c719b2014-12-15 13:56:32 -05008428 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8429 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8430 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8431 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8432 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8433 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008434 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03008435 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05008436 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008437 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008438 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008439 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008440 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008441 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008442 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008443
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008444 /*
8445 * In theory we can still leave IRQs enabled, as long as only the HPD
8446 * interrupts remain enabled. We used to check for that, but since it's
8447 * gen-specific and since we only disable LCPLL after we fully disable
8448 * the interrupts, the check below should be enough.
8449 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008450 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008451}
8452
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008453static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8454{
8455 struct drm_device *dev = dev_priv->dev;
8456
8457 if (IS_HASWELL(dev))
8458 return I915_READ(D_COMP_HSW);
8459 else
8460 return I915_READ(D_COMP_BDW);
8461}
8462
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008463static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8464{
8465 struct drm_device *dev = dev_priv->dev;
8466
8467 if (IS_HASWELL(dev)) {
8468 mutex_lock(&dev_priv->rps.hw_lock);
8469 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8470 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03008471 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008472 mutex_unlock(&dev_priv->rps.hw_lock);
8473 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008474 I915_WRITE(D_COMP_BDW, val);
8475 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008476 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008477}
8478
8479/*
8480 * This function implements pieces of two sequences from BSpec:
8481 * - Sequence for display software to disable LCPLL
8482 * - Sequence for display software to allow package C8+
8483 * The steps implemented here are just the steps that actually touch the LCPLL
8484 * register. Callers should take care of disabling all the display engine
8485 * functions, doing the mode unset, fixing interrupts, etc.
8486 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008487static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8488 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008489{
8490 uint32_t val;
8491
8492 assert_can_disable_lcpll(dev_priv);
8493
8494 val = I915_READ(LCPLL_CTL);
8495
8496 if (switch_to_fclk) {
8497 val |= LCPLL_CD_SOURCE_FCLK;
8498 I915_WRITE(LCPLL_CTL, val);
8499
8500 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
8501 LCPLL_CD_SOURCE_FCLK_DONE, 1))
8502 DRM_ERROR("Switching to FCLK failed\n");
8503
8504 val = I915_READ(LCPLL_CTL);
8505 }
8506
8507 val |= LCPLL_PLL_DISABLE;
8508 I915_WRITE(LCPLL_CTL, val);
8509 POSTING_READ(LCPLL_CTL);
8510
8511 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
8512 DRM_ERROR("LCPLL still locked\n");
8513
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008514 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008515 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008516 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008517 ndelay(100);
8518
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008519 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8520 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008521 DRM_ERROR("D_COMP RCOMP still in progress\n");
8522
8523 if (allow_power_down) {
8524 val = I915_READ(LCPLL_CTL);
8525 val |= LCPLL_POWER_DOWN_ALLOW;
8526 I915_WRITE(LCPLL_CTL, val);
8527 POSTING_READ(LCPLL_CTL);
8528 }
8529}
8530
8531/*
8532 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8533 * source.
8534 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008535static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008536{
8537 uint32_t val;
8538
8539 val = I915_READ(LCPLL_CTL);
8540
8541 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8542 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8543 return;
8544
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008545 /*
8546 * Make sure we're not on PC8 state before disabling PC8, otherwise
8547 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008548 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02008549 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03008550
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008551 if (val & LCPLL_POWER_DOWN_ALLOW) {
8552 val &= ~LCPLL_POWER_DOWN_ALLOW;
8553 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02008554 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008555 }
8556
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008557 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008558 val |= D_COMP_COMP_FORCE;
8559 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008560 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008561
8562 val = I915_READ(LCPLL_CTL);
8563 val &= ~LCPLL_PLL_DISABLE;
8564 I915_WRITE(LCPLL_CTL, val);
8565
8566 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
8567 DRM_ERROR("LCPLL not locked yet\n");
8568
8569 if (val & LCPLL_CD_SOURCE_FCLK) {
8570 val = I915_READ(LCPLL_CTL);
8571 val &= ~LCPLL_CD_SOURCE_FCLK;
8572 I915_WRITE(LCPLL_CTL, val);
8573
8574 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
8575 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
8576 DRM_ERROR("Switching back to LCPLL failed\n");
8577 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03008578
Mika Kuoppala59bad942015-01-16 11:34:40 +02008579 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008580}
8581
Paulo Zanoni765dab672014-03-07 20:08:18 -03008582/*
8583 * Package states C8 and deeper are really deep PC states that can only be
8584 * reached when all the devices on the system allow it, so even if the graphics
8585 * device allows PC8+, it doesn't mean the system will actually get to these
8586 * states. Our driver only allows PC8+ when going into runtime PM.
8587 *
8588 * The requirements for PC8+ are that all the outputs are disabled, the power
8589 * well is disabled and most interrupts are disabled, and these are also
8590 * requirements for runtime PM. When these conditions are met, we manually do
8591 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8592 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8593 * hang the machine.
8594 *
8595 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8596 * the state of some registers, so when we come back from PC8+ we need to
8597 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8598 * need to take care of the registers kept by RC6. Notice that this happens even
8599 * if we don't put the device in PCI D3 state (which is what currently happens
8600 * because of the runtime PM support).
8601 *
8602 * For more, read "Display Sequences for Package C8" on the hardware
8603 * documentation.
8604 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008605void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008606{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008607 struct drm_device *dev = dev_priv->dev;
8608 uint32_t val;
8609
Paulo Zanonic67a4702013-08-19 13:18:09 -03008610 DRM_DEBUG_KMS("Enabling package C8+\n");
8611
Paulo Zanonic67a4702013-08-19 13:18:09 -03008612 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8613 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8614 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8615 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8616 }
8617
8618 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008619 hsw_disable_lcpll(dev_priv, true, true);
8620}
8621
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008622void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008623{
8624 struct drm_device *dev = dev_priv->dev;
8625 uint32_t val;
8626
Paulo Zanonic67a4702013-08-19 13:18:09 -03008627 DRM_DEBUG_KMS("Disabling package C8+\n");
8628
8629 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008630 lpt_init_pch_refclk(dev);
8631
8632 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8633 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8634 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8635 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8636 }
8637
8638 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008639}
8640
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008641static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8642 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008643{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008644 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008645 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03008646
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008647 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02008648
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008649 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008650}
8651
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008652static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8653 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008654 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008655{
Damien Lespiau3148ade2014-11-21 16:14:56 +00008656 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008657
8658 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
8659 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
8660
8661 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00008662 case SKL_DPLL0:
8663 /*
8664 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
8665 * of the shared DPLL framework and thus needs to be read out
8666 * separately
8667 */
8668 dpll_ctl1 = I915_READ(DPLL_CTRL1);
8669 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
8670 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008671 case SKL_DPLL1:
8672 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
8673 break;
8674 case SKL_DPLL2:
8675 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
8676 break;
8677 case SKL_DPLL3:
8678 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
8679 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008680 }
8681}
8682
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008683static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8684 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008685 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008686{
8687 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8688
8689 switch (pipe_config->ddi_pll_sel) {
8690 case PORT_CLK_SEL_WRPLL1:
8691 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
8692 break;
8693 case PORT_CLK_SEL_WRPLL2:
8694 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
8695 break;
8696 }
8697}
8698
Daniel Vetter26804af2014-06-25 22:01:55 +03008699static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008700 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03008701{
8702 struct drm_device *dev = crtc->base.dev;
8703 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03008704 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03008705 enum port port;
8706 uint32_t tmp;
8707
8708 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
8709
8710 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
8711
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008712 if (IS_SKYLAKE(dev))
8713 skylake_get_ddi_pll(dev_priv, port, pipe_config);
8714 else
8715 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03008716
Daniel Vetterd452c5b2014-07-04 11:27:39 -03008717 if (pipe_config->shared_dpll >= 0) {
8718 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8719
8720 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8721 &pipe_config->dpll_hw_state));
8722 }
8723
Daniel Vetter26804af2014-06-25 22:01:55 +03008724 /*
8725 * Haswell has only FDI/PCH transcoder A. It is which is connected to
8726 * DDI E. So just check whether this pipe is wired to DDI E and whether
8727 * the PCH transcoder is on.
8728 */
Damien Lespiauca370452013-12-03 13:56:24 +00008729 if (INTEL_INFO(dev)->gen < 9 &&
8730 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03008731 pipe_config->has_pch_encoder = true;
8732
8733 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
8734 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8735 FDI_DP_PORT_WIDTH_SHIFT) + 1;
8736
8737 ironlake_get_fdi_m_n_config(crtc, pipe_config);
8738 }
8739}
8740
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008741static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008742 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008743{
8744 struct drm_device *dev = crtc->base.dev;
8745 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008746 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008747 uint32_t tmp;
8748
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008749 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02008750 POWER_DOMAIN_PIPE(crtc->pipe)))
8751 return false;
8752
Daniel Vettere143a212013-07-04 12:01:15 +02008753 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008754 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
8755
Daniel Vettereccb1402013-05-22 00:50:22 +02008756 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8757 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8758 enum pipe trans_edp_pipe;
8759 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8760 default:
8761 WARN(1, "unknown pipe linked to edp transcoder\n");
8762 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8763 case TRANS_DDI_EDP_INPUT_A_ON:
8764 trans_edp_pipe = PIPE_A;
8765 break;
8766 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8767 trans_edp_pipe = PIPE_B;
8768 break;
8769 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8770 trans_edp_pipe = PIPE_C;
8771 break;
8772 }
8773
8774 if (trans_edp_pipe == crtc->pipe)
8775 pipe_config->cpu_transcoder = TRANSCODER_EDP;
8776 }
8777
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008778 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02008779 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03008780 return false;
8781
Daniel Vettereccb1402013-05-22 00:50:22 +02008782 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008783 if (!(tmp & PIPECONF_ENABLE))
8784 return false;
8785
Daniel Vetter26804af2014-06-25 22:01:55 +03008786 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008787
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008788 intel_get_pipe_timings(crtc, pipe_config);
8789
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008790 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008791 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
8792 if (IS_SKYLAKE(dev))
8793 skylake_get_pfit_config(crtc, pipe_config);
8794 else
8795 ironlake_get_pfit_config(crtc, pipe_config);
8796 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01008797
Jesse Barnese59150d2014-01-07 13:30:45 -08008798 if (IS_HASWELL(dev))
8799 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
8800 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008801
Clint Taylorebb69c92014-09-30 10:30:22 -07008802 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
8803 pipe_config->pixel_multiplier =
8804 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
8805 } else {
8806 pipe_config->pixel_multiplier = 1;
8807 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008808
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008809 return true;
8810}
8811
Chris Wilson560b85b2010-08-07 11:01:38 +01008812static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8813{
8814 struct drm_device *dev = crtc->dev;
8815 struct drm_i915_private *dev_priv = dev->dev_private;
8816 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03008817 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01008818
Ville Syrjälädc41c152014-08-13 11:57:05 +03008819 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08008820 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
8821 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03008822 unsigned int stride = roundup_pow_of_two(width) * 4;
8823
8824 switch (stride) {
8825 default:
8826 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8827 width, stride);
8828 stride = 256;
8829 /* fallthrough */
8830 case 256:
8831 case 512:
8832 case 1024:
8833 case 2048:
8834 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008835 }
8836
Ville Syrjälädc41c152014-08-13 11:57:05 +03008837 cntl |= CURSOR_ENABLE |
8838 CURSOR_GAMMA_ENABLE |
8839 CURSOR_FORMAT_ARGB |
8840 CURSOR_STRIDE(stride);
8841
8842 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008843 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008844
Ville Syrjälädc41c152014-08-13 11:57:05 +03008845 if (intel_crtc->cursor_cntl != 0 &&
8846 (intel_crtc->cursor_base != base ||
8847 intel_crtc->cursor_size != size ||
8848 intel_crtc->cursor_cntl != cntl)) {
8849 /* On these chipsets we can only modify the base/size/stride
8850 * whilst the cursor is disabled.
8851 */
8852 I915_WRITE(_CURACNTR, 0);
8853 POSTING_READ(_CURACNTR);
8854 intel_crtc->cursor_cntl = 0;
8855 }
8856
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008857 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03008858 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008859 intel_crtc->cursor_base = base;
8860 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03008861
8862 if (intel_crtc->cursor_size != size) {
8863 I915_WRITE(CURSIZE, size);
8864 intel_crtc->cursor_size = size;
8865 }
8866
Chris Wilson4b0e3332014-05-30 16:35:26 +03008867 if (intel_crtc->cursor_cntl != cntl) {
8868 I915_WRITE(_CURACNTR, cntl);
8869 POSTING_READ(_CURACNTR);
8870 intel_crtc->cursor_cntl = cntl;
8871 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008872}
8873
8874static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
8875{
8876 struct drm_device *dev = crtc->dev;
8877 struct drm_i915_private *dev_priv = dev->dev_private;
8878 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8879 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008880 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01008881
Chris Wilson4b0e3332014-05-30 16:35:26 +03008882 cntl = 0;
8883 if (base) {
8884 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08008885 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308886 case 64:
8887 cntl |= CURSOR_MODE_64_ARGB_AX;
8888 break;
8889 case 128:
8890 cntl |= CURSOR_MODE_128_ARGB_AX;
8891 break;
8892 case 256:
8893 cntl |= CURSOR_MODE_256_ARGB_AX;
8894 break;
8895 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08008896 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308897 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01008898 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008899 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03008900
8901 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8902 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01008903 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008904
Matt Roper8e7d6882015-01-21 16:35:41 -08008905 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008906 cntl |= CURSOR_ROTATE_180;
8907
Chris Wilson4b0e3332014-05-30 16:35:26 +03008908 if (intel_crtc->cursor_cntl != cntl) {
8909 I915_WRITE(CURCNTR(pipe), cntl);
8910 POSTING_READ(CURCNTR(pipe));
8911 intel_crtc->cursor_cntl = cntl;
8912 }
8913
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008914 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008915 I915_WRITE(CURBASE(pipe), base);
8916 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008917
8918 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008919}
8920
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008921/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01008922static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8923 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008924{
8925 struct drm_device *dev = crtc->dev;
8926 struct drm_i915_private *dev_priv = dev->dev_private;
8927 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8928 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07008929 int x = crtc->cursor_x;
8930 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008931 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008932
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008933 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008934 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008935
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008936 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008937 base = 0;
8938
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008939 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008940 base = 0;
8941
8942 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08008943 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008944 base = 0;
8945
8946 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8947 x = -x;
8948 }
8949 pos |= x << CURSOR_X_SHIFT;
8950
8951 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08008952 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008953 base = 0;
8954
8955 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8956 y = -y;
8957 }
8958 pos |= y << CURSOR_Y_SHIFT;
8959
Chris Wilson4b0e3332014-05-30 16:35:26 +03008960 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008961 return;
8962
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008963 I915_WRITE(CURPOS(pipe), pos);
8964
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008965 /* ILK+ do this automagically */
8966 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08008967 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08008968 base += (intel_crtc->base.cursor->state->crtc_h *
8969 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008970 }
8971
Ville Syrjälä8ac54662014-08-12 19:39:54 +03008972 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008973 i845_update_cursor(crtc, base);
8974 else
8975 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008976}
8977
Ville Syrjälädc41c152014-08-13 11:57:05 +03008978static bool cursor_size_ok(struct drm_device *dev,
8979 uint32_t width, uint32_t height)
8980{
8981 if (width == 0 || height == 0)
8982 return false;
8983
8984 /*
8985 * 845g/865g are special in that they are only limited by
8986 * the width of their cursors, the height is arbitrary up to
8987 * the precision of the register. Everything else requires
8988 * square cursors, limited to a few power-of-two sizes.
8989 */
8990 if (IS_845G(dev) || IS_I865G(dev)) {
8991 if ((width & 63) != 0)
8992 return false;
8993
8994 if (width > (IS_845G(dev) ? 64 : 512))
8995 return false;
8996
8997 if (height > 1023)
8998 return false;
8999 } else {
9000 switch (width | height) {
9001 case 256:
9002 case 128:
9003 if (IS_GEN2(dev))
9004 return false;
9005 case 64:
9006 break;
9007 default:
9008 return false;
9009 }
9010 }
9011
9012 return true;
9013}
9014
Jesse Barnes79e53942008-11-07 14:24:08 -08009015static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01009016 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08009017{
James Simmons72034252010-08-03 01:33:19 +01009018 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08009019 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08009020
James Simmons72034252010-08-03 01:33:19 +01009021 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009022 intel_crtc->lut_r[i] = red[i] >> 8;
9023 intel_crtc->lut_g[i] = green[i] >> 8;
9024 intel_crtc->lut_b[i] = blue[i] >> 8;
9025 }
9026
9027 intel_crtc_load_lut(crtc);
9028}
9029
Jesse Barnes79e53942008-11-07 14:24:08 -08009030/* VESA 640x480x72Hz mode to set on the pipe */
9031static struct drm_display_mode load_detect_mode = {
9032 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9033 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9034};
9035
Daniel Vettera8bb6812014-02-10 18:00:39 +01009036struct drm_framebuffer *
9037__intel_framebuffer_create(struct drm_device *dev,
9038 struct drm_mode_fb_cmd2 *mode_cmd,
9039 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01009040{
9041 struct intel_framebuffer *intel_fb;
9042 int ret;
9043
9044 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9045 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009046 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01009047 return ERR_PTR(-ENOMEM);
9048 }
9049
9050 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009051 if (ret)
9052 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009053
9054 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009055err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009056 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009057 kfree(intel_fb);
9058
9059 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009060}
9061
Daniel Vetterb5ea6422014-03-02 21:18:00 +01009062static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01009063intel_framebuffer_create(struct drm_device *dev,
9064 struct drm_mode_fb_cmd2 *mode_cmd,
9065 struct drm_i915_gem_object *obj)
9066{
9067 struct drm_framebuffer *fb;
9068 int ret;
9069
9070 ret = i915_mutex_lock_interruptible(dev);
9071 if (ret)
9072 return ERR_PTR(ret);
9073 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9074 mutex_unlock(&dev->struct_mutex);
9075
9076 return fb;
9077}
9078
Chris Wilsond2dff872011-04-19 08:36:26 +01009079static u32
9080intel_framebuffer_pitch_for_width(int width, int bpp)
9081{
9082 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9083 return ALIGN(pitch, 64);
9084}
9085
9086static u32
9087intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9088{
9089 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009090 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009091}
9092
9093static struct drm_framebuffer *
9094intel_framebuffer_create_for_mode(struct drm_device *dev,
9095 struct drm_display_mode *mode,
9096 int depth, int bpp)
9097{
9098 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009099 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009100
9101 obj = i915_gem_alloc_object(dev,
9102 intel_framebuffer_size_for_mode(mode, bpp));
9103 if (obj == NULL)
9104 return ERR_PTR(-ENOMEM);
9105
9106 mode_cmd.width = mode->hdisplay;
9107 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009108 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9109 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009110 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009111
9112 return intel_framebuffer_create(dev, &mode_cmd, obj);
9113}
9114
9115static struct drm_framebuffer *
9116mode_fits_in_fbdev(struct drm_device *dev,
9117 struct drm_display_mode *mode)
9118{
Daniel Vetter4520f532013-10-09 09:18:51 +02009119#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01009120 struct drm_i915_private *dev_priv = dev->dev_private;
9121 struct drm_i915_gem_object *obj;
9122 struct drm_framebuffer *fb;
9123
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009124 if (!dev_priv->fbdev)
9125 return NULL;
9126
9127 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009128 return NULL;
9129
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009130 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009131 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009132
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009133 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009134 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9135 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01009136 return NULL;
9137
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009138 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009139 return NULL;
9140
9141 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009142#else
9143 return NULL;
9144#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009145}
9146
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009147bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01009148 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05009149 struct intel_load_detect_pipe *old,
9150 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009151{
9152 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009153 struct intel_encoder *intel_encoder =
9154 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009155 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009156 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009157 struct drm_crtc *crtc = NULL;
9158 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02009159 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009160 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009161 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009162 struct drm_connector_state *connector_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009163 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009164
Chris Wilsond2dff872011-04-19 08:36:26 +01009165 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009166 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009167 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009168
Rob Clark51fd3712013-11-19 12:10:12 -05009169retry:
9170 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9171 if (ret)
9172 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009173
Jesse Barnes79e53942008-11-07 14:24:08 -08009174 /*
9175 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009176 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009177 * - if the connector already has an assigned crtc, use it (but make
9178 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009179 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009180 * - try to find the first unused crtc that can drive this connector,
9181 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009182 */
9183
9184 /* See if we already have a CRTC for this connector */
9185 if (encoder->crtc) {
9186 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009187
Rob Clark51fd3712013-11-19 12:10:12 -05009188 ret = drm_modeset_lock(&crtc->mutex, ctx);
9189 if (ret)
9190 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009191 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9192 if (ret)
9193 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01009194
Daniel Vetter24218aa2012-08-12 19:27:11 +02009195 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009196 old->load_detect_temp = false;
9197
9198 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009199 if (connector->dpms != DRM_MODE_DPMS_ON)
9200 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01009201
Chris Wilson71731882011-04-19 23:10:58 +01009202 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08009203 }
9204
9205 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009206 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009207 i++;
9208 if (!(encoder->possible_crtcs & (1 << i)))
9209 continue;
Matt Roper83d65732015-02-25 13:12:16 -08009210 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +03009211 continue;
9212 /* This can occur when applying the pipe A quirk on resume. */
9213 if (to_intel_crtc(possible_crtc)->new_enabled)
9214 continue;
9215
9216 crtc = possible_crtc;
9217 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009218 }
9219
9220 /*
9221 * If we didn't find an unused CRTC, don't use any.
9222 */
9223 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01009224 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05009225 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08009226 }
9227
Rob Clark51fd3712013-11-19 12:10:12 -05009228 ret = drm_modeset_lock(&crtc->mutex, ctx);
9229 if (ret)
9230 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009231 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9232 if (ret)
9233 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02009234 intel_encoder->new_crtc = to_intel_crtc(crtc);
9235 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009236
9237 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009238 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +02009239 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009240 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01009241 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08009242
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009243 state = drm_atomic_state_alloc(dev);
9244 if (!state)
9245 return false;
9246
9247 state->acquire_ctx = ctx;
9248
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009249 connector_state = drm_atomic_get_connector_state(state, connector);
9250 if (IS_ERR(connector_state)) {
9251 ret = PTR_ERR(connector_state);
9252 goto fail;
9253 }
9254
9255 connector_state->crtc = crtc;
9256 connector_state->best_encoder = &intel_encoder->base;
9257
Chris Wilson64927112011-04-20 07:25:26 +01009258 if (!mode)
9259 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08009260
Chris Wilsond2dff872011-04-19 08:36:26 +01009261 /* We need a framebuffer large enough to accommodate all accesses
9262 * that the plane may generate whilst we perform load detection.
9263 * We can not rely on the fbcon either being present (we get called
9264 * during its initialisation to detect all boot displays, or it may
9265 * not even exist) or that it is large enough to satisfy the
9266 * requested mode.
9267 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02009268 fb = mode_fits_in_fbdev(dev, mode);
9269 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009270 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009271 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9272 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009273 } else
9274 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009275 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009276 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009277 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009278 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009279
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009280 if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
Chris Wilson64927112011-04-20 07:25:26 +01009281 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01009282 if (old->release_fb)
9283 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009284 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009285 }
Daniel Vetter9128b042015-03-03 17:31:21 +01009286 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +01009287
Jesse Barnes79e53942008-11-07 14:24:08 -08009288 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009289 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01009290 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009291
9292 fail:
Matt Roper83d65732015-02-25 13:12:16 -08009293 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -05009294fail_unlock:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009295 if (state) {
9296 drm_atomic_state_free(state);
9297 state = NULL;
9298 }
9299
Rob Clark51fd3712013-11-19 12:10:12 -05009300 if (ret == -EDEADLK) {
9301 drm_modeset_backoff(ctx);
9302 goto retry;
9303 }
9304
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009305 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009306}
9307
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009308void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +02009309 struct intel_load_detect_pipe *old,
9310 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009311{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009312 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009313 struct intel_encoder *intel_encoder =
9314 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01009315 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01009316 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009317 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009318 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009319 struct drm_connector_state *connector_state;
Jesse Barnes79e53942008-11-07 14:24:08 -08009320
Chris Wilsond2dff872011-04-19 08:36:26 +01009321 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009322 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009323 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009324
Chris Wilson8261b192011-04-19 23:18:09 +01009325 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009326 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009327 if (!state)
9328 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009329
9330 state->acquire_ctx = ctx;
9331
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009332 connector_state = drm_atomic_get_connector_state(state, connector);
9333 if (IS_ERR(connector_state))
9334 goto fail;
9335
Daniel Vetterfc303102012-07-09 10:40:58 +02009336 to_intel_connector(connector)->new_encoder = NULL;
9337 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009338 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009339
9340 connector_state->best_encoder = NULL;
9341 connector_state->crtc = NULL;
9342
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009343 intel_set_mode(crtc, NULL, 0, 0, NULL, state);
9344
9345 drm_atomic_state_free(state);
Chris Wilsond2dff872011-04-19 08:36:26 +01009346
Daniel Vetter36206362012-12-10 20:42:17 +01009347 if (old->release_fb) {
9348 drm_framebuffer_unregister_private(old->release_fb);
9349 drm_framebuffer_unreference(old->release_fb);
9350 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009351
Chris Wilson0622a532011-04-21 09:32:11 +01009352 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009353 }
9354
Eric Anholtc751ce42010-03-25 11:48:48 -07009355 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009356 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9357 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009358
9359 return;
9360fail:
9361 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9362 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -08009363}
9364
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009365static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009366 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009367{
9368 struct drm_i915_private *dev_priv = dev->dev_private;
9369 u32 dpll = pipe_config->dpll_hw_state.dpll;
9370
9371 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009372 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009373 else if (HAS_PCH_SPLIT(dev))
9374 return 120000;
9375 else if (!IS_GEN2(dev))
9376 return 96000;
9377 else
9378 return 48000;
9379}
9380
Jesse Barnes79e53942008-11-07 14:24:08 -08009381/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009382static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009383 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08009384{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009385 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08009386 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009387 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009388 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08009389 u32 fp;
9390 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009391 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08009392
9393 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03009394 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009395 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03009396 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009397
9398 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009399 if (IS_PINEVIEW(dev)) {
9400 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9401 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08009402 } else {
9403 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9404 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9405 }
9406
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009407 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009408 if (IS_PINEVIEW(dev))
9409 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9410 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08009411 else
9412 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08009413 DPLL_FPA01_P1_POST_DIV_SHIFT);
9414
9415 switch (dpll & DPLL_MODE_MASK) {
9416 case DPLLB_MODE_DAC_SERIAL:
9417 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9418 5 : 10;
9419 break;
9420 case DPLLB_MODE_LVDS:
9421 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9422 7 : 14;
9423 break;
9424 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08009425 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08009426 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009427 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009428 }
9429
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009430 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009431 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009432 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009433 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009434 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02009435 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009436 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08009437
9438 if (is_lvds) {
9439 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
9440 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009441
9442 if (lvds & LVDS_CLKB_POWER_UP)
9443 clock.p2 = 7;
9444 else
9445 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08009446 } else {
9447 if (dpll & PLL_P1_DIVIDE_BY_TWO)
9448 clock.p1 = 2;
9449 else {
9450 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
9451 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
9452 }
9453 if (dpll & PLL_P2_DIVIDE_BY_4)
9454 clock.p2 = 4;
9455 else
9456 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08009457 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009458
9459 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009460 }
9461
Ville Syrjälä18442d02013-09-13 16:00:08 +03009462 /*
9463 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01009464 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03009465 * encoder's get_config() function.
9466 */
9467 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009468}
9469
Ville Syrjälä6878da02013-09-13 15:59:11 +03009470int intel_dotclock_calculate(int link_freq,
9471 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009472{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009473 /*
9474 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009475 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009476 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009477 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009478 *
9479 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009480 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08009481 */
9482
Ville Syrjälä6878da02013-09-13 15:59:11 +03009483 if (!m_n->link_n)
9484 return 0;
9485
9486 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
9487}
9488
Ville Syrjälä18442d02013-09-13 16:00:08 +03009489static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009490 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03009491{
9492 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009493
9494 /* read out port_clock from the DPLL */
9495 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03009496
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009497 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03009498 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01009499 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03009500 * agree once we know their relationship in the encoder's
9501 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009502 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02009503 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03009504 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
9505 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08009506}
9507
9508/** Returns the currently programmed mode of the given pipe. */
9509struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9510 struct drm_crtc *crtc)
9511{
Jesse Barnes548f2452011-02-17 10:40:53 -08009512 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08009513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009514 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009515 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009516 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02009517 int htot = I915_READ(HTOTAL(cpu_transcoder));
9518 int hsync = I915_READ(HSYNC(cpu_transcoder));
9519 int vtot = I915_READ(VTOTAL(cpu_transcoder));
9520 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03009521 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08009522
9523 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
9524 if (!mode)
9525 return NULL;
9526
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009527 /*
9528 * Construct a pipe_config sufficient for getting the clock info
9529 * back out of crtc_clock_get.
9530 *
9531 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
9532 * to use a real value here instead.
9533 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03009534 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009535 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009536 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
9537 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
9538 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009539 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
9540
Ville Syrjälä773ae032013-09-23 17:48:20 +03009541 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08009542 mode->hdisplay = (htot & 0xffff) + 1;
9543 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
9544 mode->hsync_start = (hsync & 0xffff) + 1;
9545 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
9546 mode->vdisplay = (vtot & 0xffff) + 1;
9547 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
9548 mode->vsync_start = (vsync & 0xffff) + 1;
9549 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
9550
9551 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08009552
9553 return mode;
9554}
9555
Jesse Barnes652c3932009-08-17 13:31:43 -07009556static void intel_decrease_pllclock(struct drm_crtc *crtc)
9557{
9558 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03009559 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07009560 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009561
Sonika Jindalbaff2962014-07-22 11:16:35 +05309562 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07009563 return;
9564
9565 if (!dev_priv->lvds_downclock_avail)
9566 return;
9567
9568 /*
9569 * Since this is called by a timer, we should never get here in
9570 * the manual case.
9571 */
9572 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01009573 int pipe = intel_crtc->pipe;
9574 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02009575 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01009576
Zhao Yakui44d98a62009-10-09 11:39:40 +08009577 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009578
Sean Paul8ac5a6d2012-02-13 13:14:51 -05009579 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009580
Chris Wilson074b5e12012-05-02 12:07:06 +01009581 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07009582 dpll |= DISPLAY_RATE_SELECT_FPA1;
9583 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009584 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009585 dpll = I915_READ(dpll_reg);
9586 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08009587 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009588 }
9589
9590}
9591
Chris Wilsonf047e392012-07-21 12:31:41 +01009592void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07009593{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009594 struct drm_i915_private *dev_priv = dev->dev_private;
9595
Chris Wilsonf62a0072014-02-21 17:55:39 +00009596 if (dev_priv->mm.busy)
9597 return;
9598
Paulo Zanoni43694d62014-03-07 20:08:08 -03009599 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009600 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +00009601 if (INTEL_INFO(dev)->gen >= 6)
9602 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +00009603 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +01009604}
9605
9606void intel_mark_idle(struct drm_device *dev)
9607{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009608 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00009609 struct drm_crtc *crtc;
9610
Chris Wilsonf62a0072014-02-21 17:55:39 +00009611 if (!dev_priv->mm.busy)
9612 return;
9613
9614 dev_priv->mm.busy = false;
9615
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009616 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07009617 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +00009618 continue;
9619
9620 intel_decrease_pllclock(crtc);
9621 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009622
Damien Lespiau3d13ef22014-02-07 19:12:47 +00009623 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009624 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03009625
Paulo Zanoni43694d62014-03-07 20:08:08 -03009626 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01009627}
9628
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009629static void intel_crtc_set_state(struct intel_crtc *crtc,
9630 struct intel_crtc_state *crtc_state)
9631{
9632 kfree(crtc->config);
9633 crtc->config = crtc_state;
Ander Conselvan de Oliveira16f3f652015-01-15 14:55:27 +02009634 crtc->base.state = &crtc_state->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009635}
9636
Jesse Barnes79e53942008-11-07 14:24:08 -08009637static void intel_crtc_destroy(struct drm_crtc *crtc)
9638{
9639 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009640 struct drm_device *dev = crtc->dev;
9641 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +02009642
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009643 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009644 work = intel_crtc->unpin_work;
9645 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009646 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009647
9648 if (work) {
9649 cancel_work_sync(&work->work);
9650 kfree(work);
9651 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009652
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009653 intel_crtc_set_state(intel_crtc, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08009654 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009655
Jesse Barnes79e53942008-11-07 14:24:08 -08009656 kfree(intel_crtc);
9657}
9658
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009659static void intel_unpin_work_fn(struct work_struct *__work)
9660{
9661 struct intel_unpin_work *work =
9662 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009663 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +02009664 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009665
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009666 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00009667 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +00009668 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00009669
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02009670 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +00009671
9672 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +00009673 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009674 mutex_unlock(&dev->struct_mutex);
9675
Daniel Vetterf99d7062014-06-19 16:01:59 +02009676 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +00009677 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +02009678
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009679 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9680 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9681
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009682 kfree(work);
9683}
9684
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009685static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01009686 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009687{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009688 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9689 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009690 unsigned long flags;
9691
9692 /* Ignore early vblank irqs */
9693 if (intel_crtc == NULL)
9694 return;
9695
Daniel Vetterf3260382014-09-15 14:55:23 +02009696 /*
9697 * This is called both by irq handlers and the reset code (to complete
9698 * lost pageflips) so needs the full irqsave spinlocks.
9699 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009700 spin_lock_irqsave(&dev->event_lock, flags);
9701 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00009702
9703 /* Ensure we don't miss a work->pending update ... */
9704 smp_rmb();
9705
9706 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009707 spin_unlock_irqrestore(&dev->event_lock, flags);
9708 return;
9709 }
9710
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009711 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01009712
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009713 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009714}
9715
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009716void intel_finish_page_flip(struct drm_device *dev, int pipe)
9717{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009718 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009719 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9720
Mario Kleiner49b14a52010-12-09 07:00:07 +01009721 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009722}
9723
9724void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9725{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009726 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009727 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9728
Mario Kleiner49b14a52010-12-09 07:00:07 +01009729 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009730}
9731
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009732/* Is 'a' after or equal to 'b'? */
9733static bool g4x_flip_count_after_eq(u32 a, u32 b)
9734{
9735 return !((a - b) & 0x80000000);
9736}
9737
9738static bool page_flip_finished(struct intel_crtc *crtc)
9739{
9740 struct drm_device *dev = crtc->base.dev;
9741 struct drm_i915_private *dev_priv = dev->dev_private;
9742
Ville Syrjäläbdfa7542014-05-27 21:33:09 +03009743 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
9744 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
9745 return true;
9746
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009747 /*
9748 * The relevant registers doen't exist on pre-ctg.
9749 * As the flip done interrupt doesn't trigger for mmio
9750 * flips on gmch platforms, a flip count check isn't
9751 * really needed there. But since ctg has the registers,
9752 * include it in the check anyway.
9753 */
9754 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9755 return true;
9756
9757 /*
9758 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9759 * used the same base address. In that case the mmio flip might
9760 * have completed, but the CS hasn't even executed the flip yet.
9761 *
9762 * A flip count check isn't enough as the CS might have updated
9763 * the base address just after start of vblank, but before we
9764 * managed to process the interrupt. This means we'd complete the
9765 * CS flip too soon.
9766 *
9767 * Combining both checks should get us a good enough result. It may
9768 * still happen that the CS flip has been executed, but has not
9769 * yet actually completed. But in case the base address is the same
9770 * anyway, we don't really care.
9771 */
9772 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9773 crtc->unpin_work->gtt_offset &&
9774 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9775 crtc->unpin_work->flip_count);
9776}
9777
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009778void intel_prepare_page_flip(struct drm_device *dev, int plane)
9779{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009780 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009781 struct intel_crtc *intel_crtc =
9782 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9783 unsigned long flags;
9784
Daniel Vetterf3260382014-09-15 14:55:23 +02009785
9786 /*
9787 * This is called both by irq handlers and the reset code (to complete
9788 * lost pageflips) so needs the full irqsave spinlocks.
9789 *
9790 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +00009791 * generate a page-flip completion irq, i.e. every modeset
9792 * is also accompanied by a spurious intel_prepare_page_flip().
9793 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009794 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009795 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +00009796 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009797 spin_unlock_irqrestore(&dev->event_lock, flags);
9798}
9799
Robin Schroereba905b2014-05-18 02:24:50 +02009800static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +00009801{
9802 /* Ensure that the work item is consistent when activating it ... */
9803 smp_wmb();
9804 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9805 /* and that it is marked active as soon as the irq could fire. */
9806 smp_wmb();
9807}
9808
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009809static int intel_gen2_queue_flip(struct drm_device *dev,
9810 struct drm_crtc *crtc,
9811 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009812 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009813 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009814 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009815{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009816 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009817 u32 flip_mask;
9818 int ret;
9819
Daniel Vetter6d90c952012-04-26 23:28:05 +02009820 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009821 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009822 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009823
9824 /* Can't queue multiple flips, so wait for the previous
9825 * one to finish before executing the next.
9826 */
9827 if (intel_crtc->plane)
9828 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9829 else
9830 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009831 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9832 intel_ring_emit(ring, MI_NOOP);
9833 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9834 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9835 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009836 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009837 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00009838
9839 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009840 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009841 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009842}
9843
9844static int intel_gen3_queue_flip(struct drm_device *dev,
9845 struct drm_crtc *crtc,
9846 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009847 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009848 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009849 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009850{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009851 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009852 u32 flip_mask;
9853 int ret;
9854
Daniel Vetter6d90c952012-04-26 23:28:05 +02009855 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009856 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009857 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009858
9859 if (intel_crtc->plane)
9860 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9861 else
9862 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009863 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9864 intel_ring_emit(ring, MI_NOOP);
9865 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9866 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9867 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009868 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009869 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009870
Chris Wilsone7d841c2012-12-03 11:36:30 +00009871 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009872 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009873 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009874}
9875
9876static int intel_gen4_queue_flip(struct drm_device *dev,
9877 struct drm_crtc *crtc,
9878 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009879 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009880 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009881 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009882{
9883 struct drm_i915_private *dev_priv = dev->dev_private;
9884 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9885 uint32_t pf, pipesrc;
9886 int ret;
9887
Daniel Vetter6d90c952012-04-26 23:28:05 +02009888 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009889 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009890 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009891
9892 /* i965+ uses the linear or tiled offsets from the
9893 * Display Registers (which do not change across a page-flip)
9894 * so we need only reprogram the base address.
9895 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02009896 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9897 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9898 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009899 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +02009900 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009901
9902 /* XXX Enabling the panel-fitter across page-flip is so far
9903 * untested on non-native modes, so ignore it for now.
9904 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9905 */
9906 pf = 0;
9907 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009908 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009909
9910 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009911 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009912 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009913}
9914
9915static int intel_gen6_queue_flip(struct drm_device *dev,
9916 struct drm_crtc *crtc,
9917 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009918 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009919 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009920 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009921{
9922 struct drm_i915_private *dev_priv = dev->dev_private;
9923 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9924 uint32_t pf, pipesrc;
9925 int ret;
9926
Daniel Vetter6d90c952012-04-26 23:28:05 +02009927 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009928 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009929 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009930
Daniel Vetter6d90c952012-04-26 23:28:05 +02009931 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9932 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9933 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009934 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009935
Chris Wilson99d9acd2012-04-17 20:37:00 +01009936 /* Contrary to the suggestions in the documentation,
9937 * "Enable Panel Fitter" does not seem to be required when page
9938 * flipping with a non-native mode, and worse causes a normal
9939 * modeset to fail.
9940 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9941 */
9942 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009943 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009944 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009945
9946 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009947 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009948 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009949}
9950
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009951static int intel_gen7_queue_flip(struct drm_device *dev,
9952 struct drm_crtc *crtc,
9953 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009954 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009955 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009956 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009957{
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009958 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009959 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01009960 int len, ret;
9961
Robin Schroereba905b2014-05-18 02:24:50 +02009962 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009963 case PLANE_A:
9964 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9965 break;
9966 case PLANE_B:
9967 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9968 break;
9969 case PLANE_C:
9970 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9971 break;
9972 default:
9973 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009974 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009975 }
9976
Chris Wilsonffe74d72013-08-26 20:58:12 +01009977 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +01009978 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +01009979 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +01009980 /*
9981 * On Gen 8, SRM is now taking an extra dword to accommodate
9982 * 48bits addresses, and we need a NOOP for the batch size to
9983 * stay even.
9984 */
9985 if (IS_GEN8(dev))
9986 len += 2;
9987 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009988
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009989 /*
9990 * BSpec MI_DISPLAY_FLIP for IVB:
9991 * "The full packet must be contained within the same cache line."
9992 *
9993 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9994 * cacheline, if we ever start emitting more commands before
9995 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9996 * then do the cacheline alignment, and finally emit the
9997 * MI_DISPLAY_FLIP.
9998 */
9999 ret = intel_ring_cacheline_align(ring);
10000 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010001 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010002
Chris Wilsonffe74d72013-08-26 20:58:12 +010010003 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010004 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010005 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010006
Chris Wilsonffe74d72013-08-26 20:58:12 +010010007 /* Unmask the flip-done completion message. Note that the bspec says that
10008 * we should do this for both the BCS and RCS, and that we must not unmask
10009 * more than one flip event at any time (or ensure that one flip message
10010 * can be sent by waiting for flip-done prior to queueing new flips).
10011 * Experimentation says that BCS works despite DERRMR masking all
10012 * flip-done completion events and that unmasking all planes at once
10013 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10014 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10015 */
10016 if (ring->id == RCS) {
10017 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10018 intel_ring_emit(ring, DERRMR);
10019 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10020 DERRMR_PIPEB_PRI_FLIP_DONE |
10021 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010010022 if (IS_GEN8(dev))
10023 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10024 MI_SRM_LRM_GLOBAL_GTT);
10025 else
10026 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10027 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010010028 intel_ring_emit(ring, DERRMR);
10029 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010010030 if (IS_GEN8(dev)) {
10031 intel_ring_emit(ring, 0);
10032 intel_ring_emit(ring, MI_NOOP);
10033 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010034 }
10035
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010036 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010037 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010038 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010039 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000010040
10041 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010042 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010043 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010044}
10045
Sourab Gupta84c33a62014-06-02 16:47:17 +053010046static bool use_mmio_flip(struct intel_engine_cs *ring,
10047 struct drm_i915_gem_object *obj)
10048{
10049 /*
10050 * This is not being used for older platforms, because
10051 * non-availability of flip done interrupt forces us to use
10052 * CS flips. Older platforms derive flip done using some clever
10053 * tricks involving the flip_pending status bits and vblank irqs.
10054 * So using MMIO flips there would disrupt this mechanism.
10055 */
10056
Chris Wilson8e09bf82014-07-08 10:40:30 +010010057 if (ring == NULL)
10058 return true;
10059
Sourab Gupta84c33a62014-06-02 16:47:17 +053010060 if (INTEL_INFO(ring->dev)->gen < 5)
10061 return false;
10062
10063 if (i915.use_mmio_flip < 0)
10064 return false;
10065 else if (i915.use_mmio_flip > 0)
10066 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010010067 else if (i915.enable_execlists)
10068 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010069 else
John Harrison41c52412014-11-24 18:49:43 +000010070 return ring != i915_gem_request_get_ring(obj->last_read_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010071}
10072
Damien Lespiauff944562014-11-20 14:58:16 +000010073static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10074{
10075 struct drm_device *dev = intel_crtc->base.dev;
10076 struct drm_i915_private *dev_priv = dev->dev_private;
10077 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
10078 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
10079 struct drm_i915_gem_object *obj = intel_fb->obj;
10080 const enum pipe pipe = intel_crtc->pipe;
10081 u32 ctl, stride;
10082
10083 ctl = I915_READ(PLANE_CTL(pipe, 0));
10084 ctl &= ~PLANE_CTL_TILED_MASK;
10085 if (obj->tiling_mode == I915_TILING_X)
10086 ctl |= PLANE_CTL_TILED_X;
10087
10088 /*
10089 * The stride is either expressed as a multiple of 64 bytes chunks for
10090 * linear buffers or in number of tiles for tiled buffers.
10091 */
10092 stride = fb->pitches[0] >> 6;
10093 if (obj->tiling_mode == I915_TILING_X)
10094 stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
10095
10096 /*
10097 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10098 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10099 */
10100 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10101 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10102
10103 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10104 POSTING_READ(PLANE_SURF(pipe, 0));
10105}
10106
10107static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010108{
10109 struct drm_device *dev = intel_crtc->base.dev;
10110 struct drm_i915_private *dev_priv = dev->dev_private;
10111 struct intel_framebuffer *intel_fb =
10112 to_intel_framebuffer(intel_crtc->base.primary->fb);
10113 struct drm_i915_gem_object *obj = intel_fb->obj;
10114 u32 dspcntr;
10115 u32 reg;
10116
Sourab Gupta84c33a62014-06-02 16:47:17 +053010117 reg = DSPCNTR(intel_crtc->plane);
10118 dspcntr = I915_READ(reg);
10119
Damien Lespiauc5d97472014-10-25 00:11:11 +010010120 if (obj->tiling_mode != I915_TILING_NONE)
10121 dspcntr |= DISPPLANE_TILED;
10122 else
10123 dspcntr &= ~DISPPLANE_TILED;
10124
Sourab Gupta84c33a62014-06-02 16:47:17 +053010125 I915_WRITE(reg, dspcntr);
10126
10127 I915_WRITE(DSPSURF(intel_crtc->plane),
10128 intel_crtc->unpin_work->gtt_offset);
10129 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010130
Damien Lespiauff944562014-11-20 14:58:16 +000010131}
10132
10133/*
10134 * XXX: This is the temporary way to update the plane registers until we get
10135 * around to using the usual plane update functions for MMIO flips
10136 */
10137static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10138{
10139 struct drm_device *dev = intel_crtc->base.dev;
10140 bool atomic_update;
10141 u32 start_vbl_count;
10142
10143 intel_mark_page_flip_active(intel_crtc);
10144
10145 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10146
10147 if (INTEL_INFO(dev)->gen >= 9)
10148 skl_do_mmio_flip(intel_crtc);
10149 else
10150 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10151 ilk_do_mmio_flip(intel_crtc);
10152
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010153 if (atomic_update)
10154 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010155}
10156
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010157static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010158{
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010159 struct intel_crtc *crtc =
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010160 container_of(work, struct intel_crtc, mmio_flip.work);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010161 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010162
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010163 mmio_flip = &crtc->mmio_flip;
10164 if (mmio_flip->req)
John Harrison9c654812014-11-24 18:49:35 +000010165 WARN_ON(__i915_wait_request(mmio_flip->req,
10166 crtc->reset_counter,
10167 false, NULL, NULL) != 0);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010168
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010169 intel_do_mmio_flip(crtc);
10170 if (mmio_flip->req) {
10171 mutex_lock(&crtc->base.dev->struct_mutex);
John Harrison146d84f2014-12-05 13:49:33 +000010172 i915_gem_request_assign(&mmio_flip->req, NULL);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010173 mutex_unlock(&crtc->base.dev->struct_mutex);
10174 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053010175}
10176
10177static int intel_queue_mmio_flip(struct drm_device *dev,
10178 struct drm_crtc *crtc,
10179 struct drm_framebuffer *fb,
10180 struct drm_i915_gem_object *obj,
10181 struct intel_engine_cs *ring,
10182 uint32_t flags)
10183{
Sourab Gupta84c33a62014-06-02 16:47:17 +053010184 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010185
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010186 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
10187 obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010188
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020010189 schedule_work(&intel_crtc->mmio_flip.work);
10190
Sourab Gupta84c33a62014-06-02 16:47:17 +053010191 return 0;
10192}
10193
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010194static int intel_default_queue_flip(struct drm_device *dev,
10195 struct drm_crtc *crtc,
10196 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010197 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010198 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010199 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010200{
10201 return -ENODEV;
10202}
10203
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010204static bool __intel_pageflip_stall_check(struct drm_device *dev,
10205 struct drm_crtc *crtc)
10206{
10207 struct drm_i915_private *dev_priv = dev->dev_private;
10208 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10209 struct intel_unpin_work *work = intel_crtc->unpin_work;
10210 u32 addr;
10211
10212 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10213 return true;
10214
10215 if (!work->enable_stall_check)
10216 return false;
10217
10218 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010010219 if (work->flip_queued_req &&
10220 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010221 return false;
10222
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010223 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010224 }
10225
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010226 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010227 return false;
10228
10229 /* Potential stall - if we see that the flip has happened,
10230 * assume a missed interrupt. */
10231 if (INTEL_INFO(dev)->gen >= 4)
10232 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10233 else
10234 addr = I915_READ(DSPADDR(intel_crtc->plane));
10235
10236 /* There is a potential issue here with a false positive after a flip
10237 * to the same address. We could address this by checking for a
10238 * non-incrementing frame counter.
10239 */
10240 return addr == work->gtt_offset;
10241}
10242
10243void intel_check_page_flip(struct drm_device *dev, int pipe)
10244{
10245 struct drm_i915_private *dev_priv = dev->dev_private;
10246 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10247 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010248 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020010249
Dave Gordon6c51d462015-03-06 15:34:26 +000010250 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010251
10252 if (crtc == NULL)
10253 return;
10254
Daniel Vetterf3260382014-09-15 14:55:23 +020010255 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010256 work = intel_crtc->unpin_work;
10257 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010258 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010010259 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010260 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010261 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010262 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010010263 if (work != NULL &&
10264 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10265 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020010266 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010267}
10268
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010269static int intel_crtc_page_flip(struct drm_crtc *crtc,
10270 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010271 struct drm_pending_vblank_event *event,
10272 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010273{
10274 struct drm_device *dev = crtc->dev;
10275 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070010276 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070010277 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010278 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080010279 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020010280 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010281 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010282 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010283 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010010284 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010285
Matt Roper2ff8fde2014-07-08 07:50:07 -070010286 /*
10287 * drm_mode_page_flip_ioctl() should already catch this, but double
10288 * check to be safe. In the future we may enable pageflipping from
10289 * a disabled primary plane.
10290 */
10291 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10292 return -EBUSY;
10293
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010294 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070010295 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010296 return -EINVAL;
10297
10298 /*
10299 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10300 * Note that pitch changes could also affect these register.
10301 */
10302 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070010303 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10304 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010305 return -EINVAL;
10306
Chris Wilsonf900db42014-02-20 09:26:13 +000010307 if (i915_terminally_wedged(&dev_priv->gpu_error))
10308 goto out_hang;
10309
Daniel Vetterb14c5672013-09-19 12:18:32 +020010310 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010311 if (work == NULL)
10312 return -ENOMEM;
10313
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010314 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010315 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010316 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010317 INIT_WORK(&work->work, intel_unpin_work_fn);
10318
Daniel Vetter87b6b102014-05-15 15:33:46 +020010319 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010320 if (ret)
10321 goto free_work;
10322
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010323 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010324 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010325 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010326 /* Before declaring the flip queue wedged, check if
10327 * the hardware completed the operation behind our backs.
10328 */
10329 if (__intel_pageflip_stall_check(dev, crtc)) {
10330 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10331 page_flip_completed(intel_crtc);
10332 } else {
10333 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010334 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010010335
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010336 drm_crtc_vblank_put(crtc);
10337 kfree(work);
10338 return -EBUSY;
10339 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010340 }
10341 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010342 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010343
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010344 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10345 flush_workqueue(dev_priv->wq);
10346
Jesse Barnes75dfca82010-02-10 15:09:44 -080010347 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010348 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010349 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010350
Matt Roperf4510a22014-04-01 15:22:40 -070010351 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010352 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080010353
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010354 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010355
Chris Wilson89ed88b2015-02-16 14:31:49 +000010356 ret = i915_mutex_lock_interruptible(dev);
10357 if (ret)
10358 goto cleanup;
10359
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010360 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020010361 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010362
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010363 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020010364 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010365
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010366 if (IS_VALLEYVIEW(dev)) {
10367 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010368 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010010369 /* vlv: DISPLAY_FLIP fails to change tiling */
10370 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000010371 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010010372 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010373 } else if (INTEL_INFO(dev)->gen >= 7) {
John Harrison41c52412014-11-24 18:49:43 +000010374 ring = i915_gem_request_get_ring(obj->last_read_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010375 if (ring == NULL || ring->id != RCS)
10376 ring = &dev_priv->ring[BCS];
10377 } else {
10378 ring = &dev_priv->ring[RCS];
10379 }
10380
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010381 mmio_flip = use_mmio_flip(ring, obj);
10382
10383 /* When using CS flips, we want to emit semaphores between rings.
10384 * However, when using mmio flips we will create a task to do the
10385 * synchronisation, so all we want here is to pin the framebuffer
10386 * into the display plane and skip any waits.
10387 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010388 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010389 crtc->primary->state,
10390 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010391 if (ret)
10392 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010393
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000010394 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
10395 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010396
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010397 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010398 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10399 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010400 if (ret)
10401 goto cleanup_unpin;
10402
John Harrisonf06cc1b2014-11-24 18:49:37 +000010403 i915_gem_request_assign(&work->flip_queued_req,
10404 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010405 } else {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010406 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010407 page_flip_flags);
10408 if (ret)
10409 goto cleanup_unpin;
10410
John Harrisonf06cc1b2014-11-24 18:49:37 +000010411 i915_gem_request_assign(&work->flip_queued_req,
10412 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010413 }
10414
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010415 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010416 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010417
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010418 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020010419 INTEL_FRONTBUFFER_PRIMARY(pipe));
10420
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010421 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010422 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010423 mutex_unlock(&dev->struct_mutex);
10424
Jesse Barnese5510fa2010-07-01 16:48:37 -070010425 trace_i915_flip_request(intel_crtc->plane, obj);
10426
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010427 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010010428
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010429cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010430 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010431cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010432 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010433 mutex_unlock(&dev->struct_mutex);
10434cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070010435 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010436 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010010437
Chris Wilson89ed88b2015-02-16 14:31:49 +000010438 drm_gem_object_unreference_unlocked(&obj->base);
10439 drm_framebuffer_unreference(work->old_fb);
10440
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010441 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010442 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010443 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010444
Daniel Vetter87b6b102014-05-15 15:33:46 +020010445 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010446free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010010447 kfree(work);
10448
Chris Wilsonf900db42014-02-20 09:26:13 +000010449 if (ret == -EIO) {
10450out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080010451 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010452 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010453 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020010454 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010455 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010456 }
Chris Wilsonf900db42014-02-20 09:26:13 +000010457 }
Chris Wilson96b099f2010-06-07 14:03:04 +010010458 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010459}
10460
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010461static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010462 .mode_set_base_atomic = intel_pipe_set_base_atomic,
10463 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080010464 .atomic_begin = intel_begin_crtc_commit,
10465 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010466};
10467
Daniel Vetter9a935852012-07-05 22:34:27 +020010468/**
10469 * intel_modeset_update_staged_output_state
10470 *
10471 * Updates the staged output configuration state, e.g. after we've read out the
10472 * current hw state.
10473 */
10474static void intel_modeset_update_staged_output_state(struct drm_device *dev)
10475{
Ville Syrjälä76688512014-01-10 11:28:06 +020010476 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010477 struct intel_encoder *encoder;
10478 struct intel_connector *connector;
10479
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010480 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010481 connector->new_encoder =
10482 to_intel_encoder(connector->base.encoder);
10483 }
10484
Damien Lespiaub2784e12014-08-05 11:29:37 +010010485 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010486 encoder->new_crtc =
10487 to_intel_crtc(encoder->base.crtc);
10488 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010489
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010490 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010491 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020010492 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010493}
10494
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010495/* Transitional helper to copy current connector/encoder state to
10496 * connector->state. This is needed so that code that is partially
10497 * converted to atomic does the right thing.
10498 */
10499static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
10500{
10501 struct intel_connector *connector;
10502
10503 for_each_intel_connector(dev, connector) {
10504 if (connector->base.encoder) {
10505 connector->base.state->best_encoder =
10506 connector->base.encoder;
10507 connector->base.state->crtc =
10508 connector->base.encoder->crtc;
10509 } else {
10510 connector->base.state->best_encoder = NULL;
10511 connector->base.state->crtc = NULL;
10512 }
10513 }
10514}
10515
Daniel Vetter9a935852012-07-05 22:34:27 +020010516/**
10517 * intel_modeset_commit_output_state
10518 *
10519 * This function copies the stage display pipe configuration to the real one.
10520 */
10521static void intel_modeset_commit_output_state(struct drm_device *dev)
10522{
Ville Syrjälä76688512014-01-10 11:28:06 +020010523 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010524 struct intel_encoder *encoder;
10525 struct intel_connector *connector;
10526
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010527 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010528 connector->base.encoder = &connector->new_encoder->base;
10529 }
10530
Damien Lespiaub2784e12014-08-05 11:29:37 +010010531 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010532 encoder->base.crtc = &encoder->new_crtc->base;
10533 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010534
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010535 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010536 crtc->base.state->enable = crtc->new_enabled;
Ville Syrjälä76688512014-01-10 11:28:06 +020010537 crtc->base.enabled = crtc->new_enabled;
10538 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010539
10540 intel_modeset_update_connector_atomic_state(dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020010541}
10542
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010543static void
Robin Schroereba905b2014-05-18 02:24:50 +020010544connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010545 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010546{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010547 int bpp = pipe_config->pipe_bpp;
10548
10549 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10550 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010551 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010552
10553 /* Don't use an invalid EDID bpc value */
10554 if (connector->base.display_info.bpc &&
10555 connector->base.display_info.bpc * 3 < bpp) {
10556 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10557 bpp, connector->base.display_info.bpc*3);
10558 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10559 }
10560
10561 /* Clamp bpp to 8 on screens without EDID 1.4 */
10562 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10563 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10564 bpp);
10565 pipe_config->pipe_bpp = 24;
10566 }
10567}
10568
10569static int
10570compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10571 struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010572 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010573{
10574 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010575 struct drm_atomic_state *state;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010576 struct intel_connector *connector;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010577 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010578
Daniel Vetterd42264b2013-03-28 16:38:08 +010010579 switch (fb->pixel_format) {
10580 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010581 bpp = 8*3; /* since we go through a colormap */
10582 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010583 case DRM_FORMAT_XRGB1555:
10584 case DRM_FORMAT_ARGB1555:
10585 /* checked in intel_framebuffer_init already */
10586 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10587 return -EINVAL;
10588 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010589 bpp = 6*3; /* min is 18bpp */
10590 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010591 case DRM_FORMAT_XBGR8888:
10592 case DRM_FORMAT_ABGR8888:
10593 /* checked in intel_framebuffer_init already */
10594 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10595 return -EINVAL;
10596 case DRM_FORMAT_XRGB8888:
10597 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010598 bpp = 8*3;
10599 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010600 case DRM_FORMAT_XRGB2101010:
10601 case DRM_FORMAT_ARGB2101010:
10602 case DRM_FORMAT_XBGR2101010:
10603 case DRM_FORMAT_ABGR2101010:
10604 /* checked in intel_framebuffer_init already */
10605 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +010010606 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010607 bpp = 10*3;
10608 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +010010609 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010610 default:
10611 DRM_DEBUG_KMS("unsupported depth\n");
10612 return -EINVAL;
10613 }
10614
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010615 pipe_config->pipe_bpp = bpp;
10616
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010617 state = pipe_config->base.state;
10618
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010619 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010620 for (i = 0; i < state->num_connector; i++) {
10621 if (!state->connectors[i])
10622 continue;
10623
10624 connector = to_intel_connector(state->connectors[i]);
10625 if (state->connector_states[i]->crtc != &crtc->base)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010626 continue;
10627
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010628 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010629 }
10630
10631 return bpp;
10632}
10633
Daniel Vetter644db712013-09-19 14:53:58 +020010634static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10635{
10636 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10637 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010010638 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020010639 mode->crtc_hdisplay, mode->crtc_hsync_start,
10640 mode->crtc_hsync_end, mode->crtc_htotal,
10641 mode->crtc_vdisplay, mode->crtc_vsync_start,
10642 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10643}
10644
Daniel Vetterc0b03412013-05-28 12:05:54 +020010645static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010646 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020010647 const char *context)
10648{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070010649 struct drm_device *dev = crtc->base.dev;
10650 struct drm_plane *plane;
10651 struct intel_plane *intel_plane;
10652 struct intel_plane_state *state;
10653 struct drm_framebuffer *fb;
10654
10655 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
10656 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020010657
10658 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10659 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10660 pipe_config->pipe_bpp, pipe_config->dither);
10661 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10662 pipe_config->has_pch_encoder,
10663 pipe_config->fdi_lanes,
10664 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10665 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10666 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010667 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10668 pipe_config->has_dp_encoder,
10669 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10670 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10671 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010672
10673 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10674 pipe_config->has_dp_encoder,
10675 pipe_config->dp_m2_n2.gmch_m,
10676 pipe_config->dp_m2_n2.gmch_n,
10677 pipe_config->dp_m2_n2.link_m,
10678 pipe_config->dp_m2_n2.link_n,
10679 pipe_config->dp_m2_n2.tu);
10680
Daniel Vetter55072d12014-11-20 16:10:28 +010010681 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
10682 pipe_config->has_audio,
10683 pipe_config->has_infoframe);
10684
Daniel Vetterc0b03412013-05-28 12:05:54 +020010685 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010686 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010687 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010688 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
10689 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030010690 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010691 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10692 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070010693 DRM_DEBUG_KMS("num_scalers: %d\n", crtc->num_scalers);
10694 DRM_DEBUG_KMS("scaler_users: 0x%x\n", pipe_config->scaler_state.scaler_users);
10695 DRM_DEBUG_KMS("scaler id: %d\n", pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010696 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10697 pipe_config->gmch_pfit.control,
10698 pipe_config->gmch_pfit.pgm_ratios,
10699 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010700 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020010701 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010702 pipe_config->pch_pfit.size,
10703 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010704 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030010705 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070010706
10707 DRM_DEBUG_KMS("planes on this crtc\n");
10708 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
10709 intel_plane = to_intel_plane(plane);
10710 if (intel_plane->pipe != crtc->pipe)
10711 continue;
10712
10713 state = to_intel_plane_state(plane->state);
10714 fb = state->base.fb;
10715 if (!fb) {
10716 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
10717 "disabled, scaler_id = %d\n",
10718 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
10719 plane->base.id, intel_plane->pipe,
10720 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
10721 drm_plane_index(plane), state->scaler_id);
10722 continue;
10723 }
10724
10725 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
10726 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
10727 plane->base.id, intel_plane->pipe,
10728 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
10729 drm_plane_index(plane));
10730 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
10731 fb->base.id, fb->width, fb->height, fb->pixel_format);
10732 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
10733 state->scaler_id,
10734 state->src.x1 >> 16, state->src.y1 >> 16,
10735 drm_rect_width(&state->src) >> 16,
10736 drm_rect_height(&state->src) >> 16,
10737 state->dst.x1, state->dst.y1,
10738 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
10739 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020010740}
10741
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010742static bool encoders_cloneable(const struct intel_encoder *a,
10743 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010744{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010745 /* masks could be asymmetric, so check both ways */
10746 return a == b || (a->cloneable & (1 << b->type) &&
10747 b->cloneable & (1 << a->type));
10748}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010749
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010750static bool check_single_encoder_cloning(struct drm_atomic_state *state,
10751 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010752 struct intel_encoder *encoder)
10753{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010754 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010755 struct drm_connector_state *connector_state;
10756 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010757
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010758 for (i = 0; i < state->num_connector; i++) {
10759 if (!state->connectors[i])
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010760 continue;
10761
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010762 connector_state = state->connector_states[i];
10763 if (connector_state->crtc != &crtc->base)
10764 continue;
10765
10766 source_encoder =
10767 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010768 if (!encoders_cloneable(encoder, source_encoder))
10769 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010770 }
10771
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010772 return true;
10773}
10774
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010775static bool check_encoder_cloning(struct drm_atomic_state *state,
10776 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010777{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010778 struct intel_encoder *encoder;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010779 struct drm_connector_state *connector_state;
10780 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010781
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010782 for (i = 0; i < state->num_connector; i++) {
10783 if (!state->connectors[i])
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010784 continue;
10785
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010786 connector_state = state->connector_states[i];
10787 if (connector_state->crtc != &crtc->base)
10788 continue;
10789
10790 encoder = to_intel_encoder(connector_state->best_encoder);
10791 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010792 return false;
10793 }
10794
10795 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010796}
10797
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010798static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010799{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010800 struct drm_device *dev = state->dev;
10801 struct intel_encoder *encoder;
10802 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010803 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010804 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010805
10806 /*
10807 * Walk the connector list instead of the encoder
10808 * list to detect the problem on ddi platforms
10809 * where there's just one encoder per digital port.
10810 */
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010811 for (i = 0; i < state->num_connector; i++) {
10812 if (!state->connectors[i])
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010813 continue;
10814
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010815 connector_state = state->connector_states[i];
10816 if (!connector_state->best_encoder)
10817 continue;
10818
10819 encoder = to_intel_encoder(connector_state->best_encoder);
10820
10821 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010822
10823 switch (encoder->type) {
10824 unsigned int port_mask;
10825 case INTEL_OUTPUT_UNKNOWN:
10826 if (WARN_ON(!HAS_DDI(dev)))
10827 break;
10828 case INTEL_OUTPUT_DISPLAYPORT:
10829 case INTEL_OUTPUT_HDMI:
10830 case INTEL_OUTPUT_EDP:
10831 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
10832
10833 /* the same port mustn't appear more than once */
10834 if (used_ports & port_mask)
10835 return false;
10836
10837 used_ports |= port_mask;
10838 default:
10839 break;
10840 }
10841 }
10842
10843 return true;
10844}
10845
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010846static void
10847clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
10848{
10849 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070010850 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010851
Chandra Konduru663a3642015-04-07 15:28:41 -070010852 /* Clear only the intel specific part of the crtc state excluding scalers */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010853 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070010854 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010855 memset(crtc_state, 0, sizeof *crtc_state);
10856 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070010857 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010858}
10859
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010860static struct intel_crtc_state *
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010861intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010862 struct drm_framebuffer *fb,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010863 struct drm_display_mode *mode,
10864 struct drm_atomic_state *state)
Daniel Vetter7758a112012-07-08 19:40:39 +020010865{
Daniel Vetter7758a112012-07-08 19:40:39 +020010866 struct intel_encoder *encoder;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020010867 struct intel_connector *connector;
10868 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010869 struct intel_crtc_state *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +010010870 int plane_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020010871 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010010872 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020010873
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010874 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010875 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10876 return ERR_PTR(-EINVAL);
10877 }
10878
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010879 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010880 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
10881 return ERR_PTR(-EINVAL);
10882 }
10883
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010884 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
10885 if (IS_ERR(pipe_config))
10886 return pipe_config;
10887
10888 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020010889
Matt Roper07878242015-02-25 11:43:26 -080010890 pipe_config->base.crtc = crtc;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010891 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
10892 drm_mode_copy(&pipe_config->base.mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010893
Daniel Vettere143a212013-07-04 12:01:15 +020010894 pipe_config->cpu_transcoder =
10895 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010896 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010897
Imre Deak2960bc92013-07-30 13:36:32 +030010898 /*
10899 * Sanitize sync polarity flags based on requested ones. If neither
10900 * positive or negative polarity is requested, treat this as meaning
10901 * negative polarity.
10902 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010903 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030010904 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010905 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030010906
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010907 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030010908 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010909 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030010910
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010911 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10912 * plane pixel format and any sink constraints into account. Returns the
10913 * source plane bpp so that dithering can be selected on mismatches
10914 * after encoders and crtc also have had their say. */
10915 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10916 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010917 if (plane_bpp < 0)
10918 goto fail;
10919
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030010920 /*
10921 * Determine the real pipe dimensions. Note that stereo modes can
10922 * increase the actual pipe size due to the frame doubling and
10923 * insertion of additional space for blanks between the frame. This
10924 * is stored in the crtc timings. We use the requested mode to do this
10925 * computation to clearly distinguish it from the adjusted mode, which
10926 * can be changed by the connectors in the below retry loop.
10927 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010928 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080010929 &pipe_config->pipe_src_w,
10930 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030010931
Daniel Vettere29c22c2013-02-21 00:00:16 +010010932encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020010933 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020010934 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020010935 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010936
Daniel Vetter135c81b2013-07-21 21:37:09 +020010937 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010938 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
10939 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020010940
Daniel Vetter7758a112012-07-08 19:40:39 +020010941 /* Pass our mode to the connectors and the CRTC to give them a chance to
10942 * adjust it according to limitations or connector properties, and also
10943 * a chance to reject the mode entirely.
10944 */
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020010945 for (i = 0; i < state->num_connector; i++) {
10946 connector = to_intel_connector(state->connectors[i]);
10947 if (!connector)
Daniel Vetter7758a112012-07-08 19:40:39 +020010948 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +010010949
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020010950 connector_state = state->connector_states[i];
10951 if (connector_state->crtc != crtc)
10952 continue;
10953
10954 encoder = to_intel_encoder(connector_state->best_encoder);
10955
Daniel Vetterefea6e82013-07-21 21:36:59 +020010956 if (!(encoder->compute_config(encoder, pipe_config))) {
10957 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020010958 goto fail;
10959 }
10960 }
10961
Daniel Vetterff9a6752013-06-01 17:16:21 +020010962 /* Set default port clock if not overwritten by the encoder. Needs to be
10963 * done afterwards in case the encoder adjusts the mode. */
10964 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010965 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010010966 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010967
Daniel Vettera43f6e02013-06-07 23:10:32 +020010968 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010010969 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020010970 DRM_DEBUG_KMS("CRTC fixup failed\n");
10971 goto fail;
10972 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010010973
10974 if (ret == RETRY) {
10975 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10976 ret = -EINVAL;
10977 goto fail;
10978 }
10979
10980 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10981 retry = false;
10982 goto encoder_retry;
10983 }
10984
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010985 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10986 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10987 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10988
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010989 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020010990fail:
Daniel Vettere29c22c2013-02-21 00:00:16 +010010991 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +020010992}
10993
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010994/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10995 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10996static void
10997intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10998 unsigned *prepare_pipes, unsigned *disable_pipes)
10999{
11000 struct intel_crtc *intel_crtc;
11001 struct drm_device *dev = crtc->dev;
11002 struct intel_encoder *encoder;
11003 struct intel_connector *connector;
11004 struct drm_crtc *tmp_crtc;
11005
11006 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
11007
11008 /* Check which crtcs have changed outputs connected to them, these need
11009 * to be part of the prepare_pipes mask. We don't (yet) support global
11010 * modeset across multiple crtcs, so modeset_pipes will only have one
11011 * bit set at most. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011012 for_each_intel_connector(dev, connector) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011013 if (connector->base.encoder == &connector->new_encoder->base)
11014 continue;
11015
11016 if (connector->base.encoder) {
11017 tmp_crtc = connector->base.encoder->crtc;
11018
11019 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11020 }
11021
11022 if (connector->new_encoder)
11023 *prepare_pipes |=
11024 1 << connector->new_encoder->new_crtc->pipe;
11025 }
11026
Damien Lespiaub2784e12014-08-05 11:29:37 +010011027 for_each_intel_encoder(dev, encoder) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011028 if (encoder->base.crtc == &encoder->new_crtc->base)
11029 continue;
11030
11031 if (encoder->base.crtc) {
11032 tmp_crtc = encoder->base.crtc;
11033
11034 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11035 }
11036
11037 if (encoder->new_crtc)
11038 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
11039 }
11040
Ville Syrjälä76688512014-01-10 11:28:06 +020011041 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011042 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011043 if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011044 continue;
11045
Ville Syrjälä76688512014-01-10 11:28:06 +020011046 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011047 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +020011048 else
11049 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011050 }
11051
11052
11053 /* set_mode is also used to update properties on life display pipes. */
11054 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +020011055 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011056 *prepare_pipes |= 1 << intel_crtc->pipe;
11057
Daniel Vetterb6c51642013-04-12 18:48:43 +020011058 /*
11059 * For simplicity do a full modeset on any pipe where the output routing
11060 * changed. We could be more clever, but that would require us to be
11061 * more careful with calling the relevant encoder->mode_set functions.
11062 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011063 if (*prepare_pipes)
11064 *modeset_pipes = *prepare_pipes;
11065
11066 /* ... and mask these out. */
11067 *modeset_pipes &= ~(*disable_pipes);
11068 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020011069
11070 /*
11071 * HACK: We don't (yet) fully support global modesets. intel_set_config
11072 * obies this rule, but the modeset restore mode of
11073 * intel_modeset_setup_hw_state does not.
11074 */
11075 *modeset_pipes &= 1 << intel_crtc->pipe;
11076 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020011077
11078 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
11079 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011080}
11081
Daniel Vetterea9d7582012-07-10 10:42:52 +020011082static bool intel_crtc_in_use(struct drm_crtc *crtc)
11083{
11084 struct drm_encoder *encoder;
11085 struct drm_device *dev = crtc->dev;
11086
11087 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11088 if (encoder->crtc == crtc)
11089 return true;
11090
11091 return false;
11092}
11093
11094static void
11095intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
11096{
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011097 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011098 struct intel_encoder *intel_encoder;
11099 struct intel_crtc *intel_crtc;
11100 struct drm_connector *connector;
11101
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011102 intel_shared_dpll_commit(dev_priv);
11103
Damien Lespiaub2784e12014-08-05 11:29:37 +010011104 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020011105 if (!intel_encoder->base.crtc)
11106 continue;
11107
11108 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
11109
11110 if (prepare_pipes & (1 << intel_crtc->pipe))
11111 intel_encoder->connectors_active = false;
11112 }
11113
11114 intel_modeset_commit_output_state(dev);
11115
Ville Syrjälä76688512014-01-10 11:28:06 +020011116 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011117 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011118 WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
Daniel Vetterea9d7582012-07-10 10:42:52 +020011119 }
11120
11121 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11122 if (!connector->encoder || !connector->encoder->crtc)
11123 continue;
11124
11125 intel_crtc = to_intel_crtc(connector->encoder->crtc);
11126
11127 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020011128 struct drm_property *dpms_property =
11129 dev->mode_config.dpms_property;
11130
Daniel Vetterea9d7582012-07-10 10:42:52 +020011131 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050011132 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020011133 dpms_property,
11134 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011135
11136 intel_encoder = to_intel_encoder(connector->encoder);
11137 intel_encoder->connectors_active = true;
11138 }
11139 }
11140
11141}
11142
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011143static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011144{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011145 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011146
11147 if (clock1 == clock2)
11148 return true;
11149
11150 if (!clock1 || !clock2)
11151 return false;
11152
11153 diff = abs(clock1 - clock2);
11154
11155 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11156 return true;
11157
11158 return false;
11159}
11160
Daniel Vetter25c5b262012-07-08 22:08:04 +020011161#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11162 list_for_each_entry((intel_crtc), \
11163 &(dev)->mode_config.crtc_list, \
11164 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020011165 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020011166
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011167static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011168intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011169 struct intel_crtc_state *current_config,
11170 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011171{
Daniel Vetter66e985c2013-06-05 13:34:20 +020011172#define PIPE_CONF_CHECK_X(name) \
11173 if (current_config->name != pipe_config->name) { \
11174 DRM_ERROR("mismatch in " #name " " \
11175 "(expected 0x%08x, found 0x%08x)\n", \
11176 current_config->name, \
11177 pipe_config->name); \
11178 return false; \
11179 }
11180
Daniel Vetter08a24032013-04-19 11:25:34 +020011181#define PIPE_CONF_CHECK_I(name) \
11182 if (current_config->name != pipe_config->name) { \
11183 DRM_ERROR("mismatch in " #name " " \
11184 "(expected %i, found %i)\n", \
11185 current_config->name, \
11186 pipe_config->name); \
11187 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011188 }
11189
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011190/* This is required for BDW+ where there is only one set of registers for
11191 * switching between high and low RR.
11192 * This macro can be used whenever a comparison has to be made between one
11193 * hw state and multiple sw state variables.
11194 */
11195#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11196 if ((current_config->name != pipe_config->name) && \
11197 (current_config->alt_name != pipe_config->name)) { \
11198 DRM_ERROR("mismatch in " #name " " \
11199 "(expected %i or %i, found %i)\n", \
11200 current_config->name, \
11201 current_config->alt_name, \
11202 pipe_config->name); \
11203 return false; \
11204 }
11205
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011206#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11207 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070011208 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011209 "(expected %i, found %i)\n", \
11210 current_config->name & (mask), \
11211 pipe_config->name & (mask)); \
11212 return false; \
11213 }
11214
Ville Syrjälä5e550652013-09-06 23:29:07 +030011215#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11216 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11217 DRM_ERROR("mismatch in " #name " " \
11218 "(expected %i, found %i)\n", \
11219 current_config->name, \
11220 pipe_config->name); \
11221 return false; \
11222 }
11223
Daniel Vetterbb760062013-06-06 14:55:52 +020011224#define PIPE_CONF_QUIRK(quirk) \
11225 ((current_config->quirks | pipe_config->quirks) & (quirk))
11226
Daniel Vettereccb1402013-05-22 00:50:22 +020011227 PIPE_CONF_CHECK_I(cpu_transcoder);
11228
Daniel Vetter08a24032013-04-19 11:25:34 +020011229 PIPE_CONF_CHECK_I(has_pch_encoder);
11230 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020011231 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11232 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11233 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11234 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11235 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020011236
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011237 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011238
11239 if (INTEL_INFO(dev)->gen < 8) {
11240 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11241 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11242 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11243 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11244 PIPE_CONF_CHECK_I(dp_m_n.tu);
11245
11246 if (current_config->has_drrs) {
11247 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11248 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11249 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11250 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11251 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11252 }
11253 } else {
11254 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11255 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11256 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11257 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11258 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11259 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011260
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011261 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11262 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11263 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11264 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11265 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11266 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011267
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011268 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11269 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11270 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11271 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11272 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11273 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011274
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011275 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020011276 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020011277 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11278 IS_VALLEYVIEW(dev))
11279 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080011280 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011281
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011282 PIPE_CONF_CHECK_I(has_audio);
11283
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011284 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011285 DRM_MODE_FLAG_INTERLACE);
11286
Daniel Vetterbb760062013-06-06 14:55:52 +020011287 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011288 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011289 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011290 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011291 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011292 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011293 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011294 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011295 DRM_MODE_FLAG_NVSYNC);
11296 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011297
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011298 PIPE_CONF_CHECK_I(pipe_src_w);
11299 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011300
Daniel Vetter99535992014-04-13 12:00:33 +020011301 /*
11302 * FIXME: BIOS likes to set up a cloned config with lvds+external
11303 * screen. Since we don't yet re-compute the pipe config when moving
11304 * just the lvds port away to another pipe the sw tracking won't match.
11305 *
11306 * Proper atomic modesets with recomputed global state will fix this.
11307 * Until then just don't check gmch state for inherited modes.
11308 */
11309 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11310 PIPE_CONF_CHECK_I(gmch_pfit.control);
11311 /* pfit ratios are autocomputed by the hw on gen4+ */
11312 if (INTEL_INFO(dev)->gen < 4)
11313 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11314 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11315 }
11316
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011317 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11318 if (current_config->pch_pfit.enabled) {
11319 PIPE_CONF_CHECK_I(pch_pfit.pos);
11320 PIPE_CONF_CHECK_I(pch_pfit.size);
11321 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011322
Jesse Barnese59150d2014-01-07 13:30:45 -080011323 /* BDW+ don't expose a synchronous way to read the state */
11324 if (IS_HASWELL(dev))
11325 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011326
Ville Syrjälä282740f2013-09-04 18:30:03 +030011327 PIPE_CONF_CHECK_I(double_wide);
11328
Daniel Vetter26804af2014-06-25 22:01:55 +030011329 PIPE_CONF_CHECK_X(ddi_pll_sel);
11330
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011331 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011332 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011333 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011334 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11335 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011336 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011337 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11338 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11339 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011340
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011341 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11342 PIPE_CONF_CHECK_I(pipe_bpp);
11343
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011344 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011345 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011346
Daniel Vetter66e985c2013-06-05 13:34:20 +020011347#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011348#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011349#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011350#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011351#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011352#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011353
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011354 return true;
11355}
11356
Damien Lespiau08db6652014-11-04 17:06:52 +000011357static void check_wm_state(struct drm_device *dev)
11358{
11359 struct drm_i915_private *dev_priv = dev->dev_private;
11360 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11361 struct intel_crtc *intel_crtc;
11362 int plane;
11363
11364 if (INTEL_INFO(dev)->gen < 9)
11365 return;
11366
11367 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11368 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11369
11370 for_each_intel_crtc(dev, intel_crtc) {
11371 struct skl_ddb_entry *hw_entry, *sw_entry;
11372 const enum pipe pipe = intel_crtc->pipe;
11373
11374 if (!intel_crtc->active)
11375 continue;
11376
11377 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000011378 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000011379 hw_entry = &hw_ddb.plane[pipe][plane];
11380 sw_entry = &sw_ddb->plane[pipe][plane];
11381
11382 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11383 continue;
11384
11385 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11386 "(expected (%u,%u), found (%u,%u))\n",
11387 pipe_name(pipe), plane + 1,
11388 sw_entry->start, sw_entry->end,
11389 hw_entry->start, hw_entry->end);
11390 }
11391
11392 /* cursor */
11393 hw_entry = &hw_ddb.cursor[pipe];
11394 sw_entry = &sw_ddb->cursor[pipe];
11395
11396 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11397 continue;
11398
11399 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11400 "(expected (%u,%u), found (%u,%u))\n",
11401 pipe_name(pipe),
11402 sw_entry->start, sw_entry->end,
11403 hw_entry->start, hw_entry->end);
11404 }
11405}
11406
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011407static void
11408check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011409{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011410 struct intel_connector *connector;
11411
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011412 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011413 /* This also checks the encoder/connector hw state with the
11414 * ->get_hw_state callbacks. */
11415 intel_connector_check_state(connector);
11416
Rob Clarke2c719b2014-12-15 13:56:32 -050011417 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011418 "connector's staged encoder doesn't match current encoder\n");
11419 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011420}
11421
11422static void
11423check_encoder_state(struct drm_device *dev)
11424{
11425 struct intel_encoder *encoder;
11426 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011427
Damien Lespiaub2784e12014-08-05 11:29:37 +010011428 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011429 bool enabled = false;
11430 bool active = false;
11431 enum pipe pipe, tracked_pipe;
11432
11433 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11434 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011435 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011436
Rob Clarke2c719b2014-12-15 13:56:32 -050011437 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011438 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011439 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011440 "encoder's active_connectors set, but no crtc\n");
11441
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011442 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011443 if (connector->base.encoder != &encoder->base)
11444 continue;
11445 enabled = true;
11446 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11447 active = true;
11448 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011449 /*
11450 * for MST connectors if we unplug the connector is gone
11451 * away but the encoder is still connected to a crtc
11452 * until a modeset happens in response to the hotplug.
11453 */
11454 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
11455 continue;
11456
Rob Clarke2c719b2014-12-15 13:56:32 -050011457 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011458 "encoder's enabled state mismatch "
11459 "(expected %i, found %i)\n",
11460 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050011461 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011462 "active encoder with no crtc\n");
11463
Rob Clarke2c719b2014-12-15 13:56:32 -050011464 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011465 "encoder's computed active state doesn't match tracked active state "
11466 "(expected %i, found %i)\n", active, encoder->connectors_active);
11467
11468 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050011469 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011470 "encoder's hw state doesn't match sw tracking "
11471 "(expected %i, found %i)\n",
11472 encoder->connectors_active, active);
11473
11474 if (!encoder->base.crtc)
11475 continue;
11476
11477 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050011478 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011479 "active encoder's pipe doesn't match"
11480 "(expected %i, found %i)\n",
11481 tracked_pipe, pipe);
11482
11483 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011484}
11485
11486static void
11487check_crtc_state(struct drm_device *dev)
11488{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011489 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011490 struct intel_crtc *crtc;
11491 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011492 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011493
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011494 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011495 bool enabled = false;
11496 bool active = false;
11497
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011498 memset(&pipe_config, 0, sizeof(pipe_config));
11499
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011500 DRM_DEBUG_KMS("[CRTC:%d]\n",
11501 crtc->base.base.id);
11502
Matt Roper83d65732015-02-25 13:12:16 -080011503 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011504 "active crtc, but not enabled in sw tracking\n");
11505
Damien Lespiaub2784e12014-08-05 11:29:37 +010011506 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011507 if (encoder->base.crtc != &crtc->base)
11508 continue;
11509 enabled = true;
11510 if (encoder->connectors_active)
11511 active = true;
11512 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020011513
Rob Clarke2c719b2014-12-15 13:56:32 -050011514 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011515 "crtc's computed active state doesn't match tracked active state "
11516 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080011517 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011518 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080011519 "(expected %i, found %i)\n", enabled,
11520 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011521
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011522 active = dev_priv->display.get_pipe_config(crtc,
11523 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020011524
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030011525 /* hw state is inconsistent with the pipe quirk */
11526 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
11527 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020011528 active = crtc->active;
11529
Damien Lespiaub2784e12014-08-05 11:29:37 +010011530 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030011531 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020011532 if (encoder->base.crtc != &crtc->base)
11533 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010011534 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020011535 encoder->get_config(encoder, &pipe_config);
11536 }
11537
Rob Clarke2c719b2014-12-15 13:56:32 -050011538 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011539 "crtc active state doesn't match with hw state "
11540 "(expected %i, found %i)\n", crtc->active, active);
11541
Daniel Vetterc0b03412013-05-28 12:05:54 +020011542 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011543 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050011544 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020011545 intel_dump_pipe_config(crtc, &pipe_config,
11546 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011547 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011548 "[sw state]");
11549 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011550 }
11551}
11552
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011553static void
11554check_shared_dpll_state(struct drm_device *dev)
11555{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011556 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011557 struct intel_crtc *crtc;
11558 struct intel_dpll_hw_state dpll_hw_state;
11559 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020011560
11561 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11562 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11563 int enabled_crtcs = 0, active_crtcs = 0;
11564 bool active;
11565
11566 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
11567
11568 DRM_DEBUG_KMS("%s\n", pll->name);
11569
11570 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
11571
Rob Clarke2c719b2014-12-15 13:56:32 -050011572 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020011573 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011574 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050011575 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020011576 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011577 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020011578 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011579 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020011580 "pll on state mismatch (expected %i, found %i)\n",
11581 pll->on, active);
11582
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011583 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011584 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020011585 enabled_crtcs++;
11586 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11587 active_crtcs++;
11588 }
Rob Clarke2c719b2014-12-15 13:56:32 -050011589 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020011590 "pll active crtcs mismatch (expected %i, found %i)\n",
11591 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050011592 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020011593 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011594 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011595
Rob Clarke2c719b2014-12-15 13:56:32 -050011596 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020011597 sizeof(dpll_hw_state)),
11598 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020011599 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011600}
11601
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011602void
11603intel_modeset_check_state(struct drm_device *dev)
11604{
Damien Lespiau08db6652014-11-04 17:06:52 +000011605 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011606 check_connector_state(dev);
11607 check_encoder_state(dev);
11608 check_crtc_state(dev);
11609 check_shared_dpll_state(dev);
11610}
11611
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011612void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030011613 int dotclock)
11614{
11615 /*
11616 * FDI already provided one idea for the dotclock.
11617 * Yell if the encoder disagrees.
11618 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011619 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030011620 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011621 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030011622}
11623
Ville Syrjälä80715b22014-05-15 20:23:23 +030011624static void update_scanline_offset(struct intel_crtc *crtc)
11625{
11626 struct drm_device *dev = crtc->base.dev;
11627
11628 /*
11629 * The scanline counter increments at the leading edge of hsync.
11630 *
11631 * On most platforms it starts counting from vtotal-1 on the
11632 * first active line. That means the scanline counter value is
11633 * always one less than what we would expect. Ie. just after
11634 * start of vblank, which also occurs at start of hsync (on the
11635 * last active line), the scanline counter will read vblank_start-1.
11636 *
11637 * On gen2 the scanline counter starts counting from 1 instead
11638 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11639 * to keep the value positive), instead of adding one.
11640 *
11641 * On HSW+ the behaviour of the scanline counter depends on the output
11642 * type. For DP ports it behaves like most other platforms, but on HDMI
11643 * there's an extra 1 line difference. So we need to add two instead of
11644 * one to the value.
11645 */
11646 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011647 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030011648 int vtotal;
11649
11650 vtotal = mode->crtc_vtotal;
11651 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
11652 vtotal /= 2;
11653
11654 crtc->scanline_offset = vtotal - 1;
11655 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030011656 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030011657 crtc->scanline_offset = 2;
11658 } else
11659 crtc->scanline_offset = 1;
11660}
11661
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011662static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011663intel_modeset_compute_config(struct drm_crtc *crtc,
11664 struct drm_display_mode *mode,
11665 struct drm_framebuffer *fb,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011666 struct drm_atomic_state *state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011667 unsigned *modeset_pipes,
11668 unsigned *prepare_pipes,
11669 unsigned *disable_pipes)
11670{
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011671 struct drm_device *dev = crtc->dev;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011672 struct intel_crtc_state *pipe_config = NULL;
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011673 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011674 int ret = 0;
11675
11676 ret = drm_atomic_add_affected_connectors(state, crtc);
11677 if (ret)
11678 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011679
11680 intel_modeset_affected_pipes(crtc, modeset_pipes,
11681 prepare_pipes, disable_pipes);
11682
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011683 for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
11684 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
11685 if (IS_ERR(pipe_config))
11686 return pipe_config;
11687
11688 pipe_config->base.enable = false;
11689 }
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011690
11691 /*
11692 * Note this needs changes when we start tracking multiple modes
11693 * and crtcs. At that point we'll need to compute the whole config
11694 * (i.e. one pipe_config for each crtc) rather than just the one
11695 * for this crtc.
11696 */
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011697 for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
11698 /* FIXME: For now we still expect modeset_pipes has at most
11699 * one bit set. */
11700 if (WARN_ON(&intel_crtc->base != crtc))
11701 continue;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011702
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011703 pipe_config = intel_modeset_pipe_config(crtc, fb, mode, state);
11704 if (IS_ERR(pipe_config))
11705 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011706
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030011707 pipe_config->base.enable = true;
11708
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011709 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
11710 "[modeset]");
11711 }
11712
11713 return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011714}
11715
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030011716static int __intel_set_mode_setup_plls(struct drm_atomic_state *state,
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011717 unsigned modeset_pipes,
11718 unsigned disable_pipes)
11719{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030011720 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011721 struct drm_i915_private *dev_priv = to_i915(dev);
11722 unsigned clear_pipes = modeset_pipes | disable_pipes;
11723 struct intel_crtc *intel_crtc;
11724 int ret = 0;
11725
11726 if (!dev_priv->display.crtc_compute_clock)
11727 return 0;
11728
11729 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
11730 if (ret)
11731 goto done;
11732
11733 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030011734 struct intel_crtc_state *crtc_state =
11735 intel_atomic_get_crtc_state(state, intel_crtc);
11736
11737 /* Modeset pipes should have a new state by now */
11738 if (WARN_ON(IS_ERR(crtc_state)))
11739 continue;
11740
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011741 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030011742 crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011743 if (ret) {
11744 intel_shared_dpll_abort_config(dev_priv);
11745 goto done;
11746 }
11747 }
11748
11749done:
11750 return ret;
11751}
11752
Daniel Vetterf30da182013-04-11 20:22:50 +020011753static int __intel_set_mode(struct drm_crtc *crtc,
11754 struct drm_display_mode *mode,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011755 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011756 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011757 unsigned modeset_pipes,
11758 unsigned prepare_pipes,
11759 unsigned disable_pipes)
Daniel Vettera6778b32012-07-02 09:56:42 +020011760{
11761 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030011762 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011763 struct drm_display_mode *saved_mode;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030011764 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011765 struct intel_crtc_state *crtc_state_copy = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020011766 struct intel_crtc *intel_crtc;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011767 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020011768
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011769 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011770 if (!saved_mode)
11771 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020011772
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011773 crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
11774 if (!crtc_state_copy) {
11775 ret = -ENOMEM;
11776 goto done;
11777 }
11778
Tim Gardner3ac18232012-12-07 07:54:26 -070011779 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020011780
Jesse Barnes30a970c2013-11-04 13:48:12 -080011781 /*
11782 * See if the config requires any additional preparation, e.g.
11783 * to adjust global state with pipes off. We need to do this
11784 * here so we can get the modeset_pipe updated config for the new
11785 * mode set on this crtc. For other crtcs we need to use the
11786 * adjusted_mode bits in the crtc directly.
11787 */
Ville Syrjäläc164f832013-11-05 22:34:12 +020011788 if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030011789 ret = valleyview_modeset_global_pipes(state, &prepare_pipes);
11790 if (ret)
11791 goto done;
Jesse Barnes30a970c2013-11-04 13:48:12 -080011792
Ville Syrjäläc164f832013-11-05 22:34:12 +020011793 /* may have added more to prepare_pipes than we should */
11794 prepare_pipes &= ~disable_pipes;
11795 }
11796
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030011797 ret = __intel_set_mode_setup_plls(state, modeset_pipes, disable_pipes);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011798 if (ret)
11799 goto done;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +020011800
Daniel Vetter460da9162013-03-27 00:44:51 +010011801 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
11802 intel_crtc_disable(&intel_crtc->base);
11803
Daniel Vetterea9d7582012-07-10 10:42:52 +020011804 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011805 if (intel_crtc->base.state->enable)
Daniel Vetterea9d7582012-07-10 10:42:52 +020011806 dev_priv->display.crtc_disable(&intel_crtc->base);
11807 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011808
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020011809 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
11810 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011811 *
11812 * Note we'll need to fix this up when we start tracking multiple
11813 * pipes; here we assume a single modeset_pipe and only track the
11814 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020011815 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011816 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020011817 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011818 /* mode_set/enable/disable functions rely on a correct pipe
11819 * config. */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020011820 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020011821
11822 /*
11823 * Calculate and store various constants which
11824 * are later needed by vblank and swap-completion
11825 * timestamping. They are derived from true hwmode.
11826 */
11827 drm_calc_timestamping_constants(crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011828 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011829 }
Daniel Vetter7758a112012-07-08 19:40:39 +020011830
Daniel Vetterea9d7582012-07-10 10:42:52 +020011831 /* Only after disabling all output pipelines that will be changed can we
11832 * update the the output configuration. */
11833 intel_modeset_update_state(dev, prepare_pipes);
11834
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030011835 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020011836
Daniel Vetter25c5b262012-07-08 22:08:04 +020011837 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Gustavo Padovan455a6802014-12-01 15:40:11 -080011838 struct drm_plane *primary = intel_crtc->base.primary;
11839 int vdisplay, hdisplay;
Daniel Vetter4c107942014-04-24 23:55:05 +020011840
Gustavo Padovan455a6802014-12-01 15:40:11 -080011841 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070011842 ret = drm_plane_helper_update(primary, &intel_crtc->base,
11843 fb, 0, 0,
11844 hdisplay, vdisplay,
11845 x << 16, y << 16,
11846 hdisplay << 16, vdisplay << 16);
Daniel Vettera6778b32012-07-02 09:56:42 +020011847 }
11848
11849 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030011850 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11851 update_scanline_offset(intel_crtc);
11852
Daniel Vetter25c5b262012-07-08 22:08:04 +020011853 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030011854 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011855
Daniel Vettera6778b32012-07-02 09:56:42 +020011856 /* FIXME: add subpixel order */
11857done:
Matt Roper83d65732015-02-25 13:12:16 -080011858 if (ret && crtc->state->enable)
Tim Gardner3ac18232012-12-07 07:54:26 -070011859 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020011860
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011861 if (ret == 0 && pipe_config) {
11862 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11863
11864 /* The pipe_config will be freed with the atomic state, so
11865 * make a copy. */
11866 memcpy(crtc_state_copy, intel_crtc->config,
11867 sizeof *crtc_state_copy);
11868 intel_crtc->config = crtc_state_copy;
11869 intel_crtc->base.state = &crtc_state_copy->base;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011870 } else {
11871 kfree(crtc_state_copy);
11872 }
11873
Tim Gardner3ac18232012-12-07 07:54:26 -070011874 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020011875 return ret;
11876}
11877
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011878static int intel_set_mode_pipes(struct drm_crtc *crtc,
11879 struct drm_display_mode *mode,
11880 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011881 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011882 unsigned modeset_pipes,
11883 unsigned prepare_pipes,
11884 unsigned disable_pipes)
11885{
11886 int ret;
11887
11888 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
11889 prepare_pipes, disable_pipes);
11890
11891 if (ret == 0)
11892 intel_modeset_check_state(crtc->dev);
11893
11894 return ret;
11895}
11896
Damien Lespiaue7457a92013-08-08 22:28:59 +010011897static int intel_set_mode(struct drm_crtc *crtc,
11898 struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011899 int x, int y, struct drm_framebuffer *fb,
11900 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020011901{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011902 struct intel_crtc_state *pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011903 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011904 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020011905
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011906 pipe_config = intel_modeset_compute_config(crtc, mode, fb, state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011907 &modeset_pipes,
11908 &prepare_pipes,
11909 &disable_pipes);
Daniel Vetterf30da182013-04-11 20:22:50 +020011910
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011911 if (IS_ERR(pipe_config)) {
11912 ret = PTR_ERR(pipe_config);
11913 goto out;
11914 }
Daniel Vetterf30da182013-04-11 20:22:50 +020011915
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011916 ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
11917 modeset_pipes, prepare_pipes,
11918 disable_pipes);
11919 if (ret)
11920 goto out;
11921
11922out:
11923 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020011924}
11925
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011926void intel_crtc_restore_mode(struct drm_crtc *crtc)
11927{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011928 struct drm_device *dev = crtc->dev;
11929 struct drm_atomic_state *state;
11930 struct intel_encoder *encoder;
11931 struct intel_connector *connector;
11932 struct drm_connector_state *connector_state;
11933
11934 state = drm_atomic_state_alloc(dev);
11935 if (!state) {
11936 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
11937 crtc->base.id);
11938 return;
11939 }
11940
11941 state->acquire_ctx = dev->mode_config.acquire_ctx;
11942
11943 /* The force restore path in the HW readout code relies on the staged
11944 * config still keeping the user requested config while the actual
11945 * state has been overwritten by the configuration read from HW. We
11946 * need to copy the staged config to the atomic state, otherwise the
11947 * mode set will just reapply the state the HW is already in. */
11948 for_each_intel_encoder(dev, encoder) {
11949 if (&encoder->new_crtc->base != crtc)
11950 continue;
11951
11952 for_each_intel_connector(dev, connector) {
11953 if (connector->new_encoder != encoder)
11954 continue;
11955
11956 connector_state = drm_atomic_get_connector_state(state, &connector->base);
11957 if (IS_ERR(connector_state)) {
11958 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
11959 connector->base.base.id,
11960 connector->base.name,
11961 PTR_ERR(connector_state));
11962 continue;
11963 }
11964
11965 connector_state->crtc = crtc;
11966 connector_state->best_encoder = &encoder->base;
11967 }
11968 }
11969
11970 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
11971 state);
11972
11973 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011974}
11975
Daniel Vetter25c5b262012-07-08 22:08:04 +020011976#undef for_each_intel_crtc_masked
11977
Daniel Vetterd9e55602012-07-04 22:16:09 +020011978static void intel_set_config_free(struct intel_set_config *config)
11979{
11980 if (!config)
11981 return;
11982
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011983 kfree(config->save_connector_encoders);
11984 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020011985 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020011986 kfree(config);
11987}
11988
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011989static int intel_set_config_save_state(struct drm_device *dev,
11990 struct intel_set_config *config)
11991{
Ville Syrjälä76688512014-01-10 11:28:06 +020011992 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011993 struct drm_encoder *encoder;
11994 struct drm_connector *connector;
11995 int count;
11996
Ville Syrjälä76688512014-01-10 11:28:06 +020011997 config->save_crtc_enabled =
11998 kcalloc(dev->mode_config.num_crtc,
11999 sizeof(bool), GFP_KERNEL);
12000 if (!config->save_crtc_enabled)
12001 return -ENOMEM;
12002
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012003 config->save_encoder_crtcs =
12004 kcalloc(dev->mode_config.num_encoder,
12005 sizeof(struct drm_crtc *), GFP_KERNEL);
12006 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012007 return -ENOMEM;
12008
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012009 config->save_connector_encoders =
12010 kcalloc(dev->mode_config.num_connector,
12011 sizeof(struct drm_encoder *), GFP_KERNEL);
12012 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012013 return -ENOMEM;
12014
12015 /* Copy data. Note that driver private data is not affected.
12016 * Should anything bad happen only the expected state is
12017 * restored, not the drivers personal bookkeeping.
12018 */
12019 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010012020 for_each_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012021 config->save_crtc_enabled[count++] = crtc->state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020012022 }
12023
12024 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012025 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012026 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012027 }
12028
12029 count = 0;
12030 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012031 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012032 }
12033
12034 return 0;
12035}
12036
12037static void intel_set_config_restore_state(struct drm_device *dev,
12038 struct intel_set_config *config)
12039{
Ville Syrjälä76688512014-01-10 11:28:06 +020012040 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020012041 struct intel_encoder *encoder;
12042 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012043 int count;
12044
12045 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012046 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012047 crtc->new_enabled = config->save_crtc_enabled[count++];
12048 }
12049
12050 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010012051 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012052 encoder->new_crtc =
12053 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012054 }
12055
12056 count = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012057 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012058 connector->new_encoder =
12059 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012060 }
12061}
12062
Imre Deake3de42b2013-05-03 19:44:07 +020012063static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010012064is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020012065{
12066 int i;
12067
Chris Wilson2e57f472013-07-17 12:14:40 +010012068 if (set->num_connectors == 0)
12069 return false;
12070
12071 if (WARN_ON(set->connectors == NULL))
12072 return false;
12073
12074 for (i = 0; i < set->num_connectors; i++)
12075 if (set->connectors[i]->encoder &&
12076 set->connectors[i]->encoder->crtc == set->crtc &&
12077 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020012078 return true;
12079
12080 return false;
12081}
12082
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012083static void
12084intel_set_config_compute_mode_changes(struct drm_mode_set *set,
12085 struct intel_set_config *config)
12086{
12087
12088 /* We should be able to check here if the fb has the same properties
12089 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010012090 if (is_crtc_connector_off(set)) {
12091 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070012092 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070012093 /*
12094 * If we have no fb, we can only flip as long as the crtc is
12095 * active, otherwise we need a full mode set. The crtc may
12096 * be active if we've only disabled the primary plane, or
12097 * in fastboot situations.
12098 */
Matt Roperf4510a22014-04-01 15:22:40 -070012099 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012100 struct intel_crtc *intel_crtc =
12101 to_intel_crtc(set->crtc);
12102
Matt Roper3b150f02014-05-29 08:06:53 -070012103 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012104 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
12105 config->fb_changed = true;
12106 } else {
12107 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
12108 config->mode_changed = true;
12109 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012110 } else if (set->fb == NULL) {
12111 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010012112 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070012113 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012114 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012115 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012116 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012117 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012118 }
12119
Daniel Vetter835c5872012-07-10 18:11:08 +020012120 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012121 config->fb_changed = true;
12122
12123 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
12124 DRM_DEBUG_KMS("modes are different, full mode set\n");
12125 drm_mode_debug_printmodeline(&set->crtc->mode);
12126 drm_mode_debug_printmodeline(set->mode);
12127 config->mode_changed = true;
12128 }
Chris Wilsona1d95702013-08-13 18:48:47 +010012129
12130 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
12131 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012132}
12133
Daniel Vetter2e431052012-07-04 22:42:15 +020012134static int
Daniel Vetter9a935852012-07-05 22:34:27 +020012135intel_modeset_stage_output_state(struct drm_device *dev,
12136 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012137 struct intel_set_config *config,
12138 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020012139{
Daniel Vetter9a935852012-07-05 22:34:27 +020012140 struct intel_connector *connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012141 struct drm_connector_state *connector_state;
Daniel Vetter9a935852012-07-05 22:34:27 +020012142 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020012143 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030012144 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020012145
Damien Lespiau9abdda72013-02-13 13:29:23 +000012146 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020012147 * of connectors. For paranoia, double-check this. */
12148 WARN_ON(!set->fb && (set->num_connectors != 0));
12149 WARN_ON(set->fb && (set->num_connectors == 0));
12150
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012151 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012152 /* Otherwise traverse passed in connector list and get encoders
12153 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020012154 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012155 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012156 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020012157 break;
12158 }
12159 }
12160
Daniel Vetter9a935852012-07-05 22:34:27 +020012161 /* If we disable the crtc, disable all its connectors. Also, if
12162 * the connector is on the changing crtc but not on the new
12163 * connector list, disable it. */
12164 if ((!set->fb || ro == set->num_connectors) &&
12165 connector->base.encoder &&
12166 connector->base.encoder->crtc == set->crtc) {
12167 connector->new_encoder = NULL;
12168
12169 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12170 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012171 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020012172 }
12173
12174
12175 if (&connector->new_encoder->base != connector->base.encoder) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012176 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
12177 connector->base.base.id,
12178 connector->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012179 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020012180 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012181 }
12182 /* connector->new_encoder is now updated for all connectors. */
12183
12184 /* Update crtc of enabled connectors. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012185 for_each_intel_connector(dev, connector) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012186 struct drm_crtc *new_crtc;
12187
Daniel Vetter9a935852012-07-05 22:34:27 +020012188 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020012189 continue;
12190
Daniel Vetter9a935852012-07-05 22:34:27 +020012191 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020012192
12193 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012194 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020012195 new_crtc = set->crtc;
12196 }
12197
12198 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010012199 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
12200 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012201 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012202 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012203 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020012204
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012205 connector_state =
12206 drm_atomic_get_connector_state(state, &connector->base);
12207 if (IS_ERR(connector_state))
12208 return PTR_ERR(connector_state);
12209
12210 connector_state->crtc = new_crtc;
12211 connector_state->best_encoder = &connector->new_encoder->base;
12212
Daniel Vetter9a935852012-07-05 22:34:27 +020012213 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12214 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012215 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020012216 new_crtc->base.id);
12217 }
12218
12219 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010012220 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012221 int num_connectors = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012222 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012223 if (connector->new_encoder == encoder) {
12224 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012225 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020012226 }
12227 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012228
12229 if (num_connectors == 0)
12230 encoder->new_crtc = NULL;
12231 else if (num_connectors > 1)
12232 return -EINVAL;
12233
Daniel Vetter9a935852012-07-05 22:34:27 +020012234 /* Only now check for crtc changes so we don't miss encoders
12235 * that will be disabled. */
12236 if (&encoder->new_crtc->base != encoder->base.crtc) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012237 DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
12238 encoder->base.base.id,
12239 encoder->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012240 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020012241 }
12242 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012243 /* Now we've also updated encoder->new_crtc for all encoders. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012244 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012245 connector_state =
12246 drm_atomic_get_connector_state(state, &connector->base);
Ander Conselvan de Oliveira9d918c12015-03-27 15:33:51 +020012247 if (IS_ERR(connector_state))
12248 return PTR_ERR(connector_state);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012249
12250 if (connector->new_encoder) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012251 if (connector->new_encoder != connector->encoder)
12252 connector->encoder = connector->new_encoder;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012253 } else {
12254 connector_state->crtc = NULL;
Ander Conselvan de Oliveiraf61cccf2015-03-31 11:35:00 +030012255 connector_state->best_encoder = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012256 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012257 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012258 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012259 crtc->new_enabled = false;
12260
Damien Lespiaub2784e12014-08-05 11:29:37 +010012261 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012262 if (encoder->new_crtc == crtc) {
12263 crtc->new_enabled = true;
12264 break;
12265 }
12266 }
12267
Matt Roper83d65732015-02-25 13:12:16 -080012268 if (crtc->new_enabled != crtc->base.state->enable) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012269 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
12270 crtc->base.base.id,
Ville Syrjälä76688512014-01-10 11:28:06 +020012271 crtc->new_enabled ? "en" : "dis");
12272 config->mode_changed = true;
12273 }
12274 }
12275
Daniel Vetter2e431052012-07-04 22:42:15 +020012276 return 0;
12277}
12278
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012279static void disable_crtc_nofb(struct intel_crtc *crtc)
12280{
12281 struct drm_device *dev = crtc->base.dev;
12282 struct intel_encoder *encoder;
12283 struct intel_connector *connector;
12284
12285 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
12286 pipe_name(crtc->pipe));
12287
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012288 for_each_intel_connector(dev, connector) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012289 if (connector->new_encoder &&
12290 connector->new_encoder->new_crtc == crtc)
12291 connector->new_encoder = NULL;
12292 }
12293
Damien Lespiaub2784e12014-08-05 11:29:37 +010012294 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012295 if (encoder->new_crtc == crtc)
12296 encoder->new_crtc = NULL;
12297 }
12298
12299 crtc->new_enabled = false;
12300}
12301
Daniel Vetter2e431052012-07-04 22:42:15 +020012302static int intel_crtc_set_config(struct drm_mode_set *set)
12303{
12304 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020012305 struct drm_mode_set save_set;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012306 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020012307 struct intel_set_config *config;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012308 struct intel_crtc_state *pipe_config;
Jesse Barnes50f52752014-11-07 13:11:00 -080012309 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Daniel Vetter2e431052012-07-04 22:42:15 +020012310 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020012311
Daniel Vetter8d3e3752012-07-05 16:09:09 +020012312 BUG_ON(!set);
12313 BUG_ON(!set->crtc);
12314 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020012315
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010012316 /* Enforce sane interface api - has been abused by the fb helper. */
12317 BUG_ON(!set->mode && set->fb);
12318 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020012319
Daniel Vetter2e431052012-07-04 22:42:15 +020012320 if (set->fb) {
12321 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12322 set->crtc->base.id, set->fb->base.id,
12323 (int)set->num_connectors, set->x, set->y);
12324 } else {
12325 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020012326 }
12327
12328 dev = set->crtc->dev;
12329
12330 ret = -ENOMEM;
12331 config = kzalloc(sizeof(*config), GFP_KERNEL);
12332 if (!config)
12333 goto out_config;
12334
12335 ret = intel_set_config_save_state(dev, config);
12336 if (ret)
12337 goto out_config;
12338
12339 save_set.crtc = set->crtc;
12340 save_set.mode = &set->crtc->mode;
12341 save_set.x = set->crtc->x;
12342 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070012343 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020012344
12345 /* Compute whether we need a full modeset, only an fb base update or no
12346 * change at all. In the future we might also check whether only the
12347 * mode changed, e.g. for LVDS where we only change the panel fitter in
12348 * such cases. */
12349 intel_set_config_compute_mode_changes(set, config);
12350
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012351 state = drm_atomic_state_alloc(dev);
12352 if (!state) {
12353 ret = -ENOMEM;
12354 goto out_config;
12355 }
12356
12357 state->acquire_ctx = dev->mode_config.acquire_ctx;
12358
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012359 ret = intel_modeset_stage_output_state(dev, set, config, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020012360 if (ret)
12361 goto fail;
12362
Jesse Barnes50f52752014-11-07 13:11:00 -080012363 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012364 set->fb, state,
Jesse Barnes50f52752014-11-07 13:11:00 -080012365 &modeset_pipes,
12366 &prepare_pipes,
12367 &disable_pipes);
Jesse Barnes20664592014-11-05 14:26:09 -080012368 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080012369 ret = PTR_ERR(pipe_config);
Jesse Barnes50f52752014-11-07 13:11:00 -080012370 goto fail;
Jesse Barnes20664592014-11-05 14:26:09 -080012371 } else if (pipe_config) {
Ville Syrjäläb9950a12014-11-21 21:00:36 +020012372 if (pipe_config->has_audio !=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012373 to_intel_crtc(set->crtc)->config->has_audio)
Jesse Barnes20664592014-11-05 14:26:09 -080012374 config->mode_changed = true;
12375
Jesse Barnesaf15d2c2014-12-01 09:54:28 -080012376 /*
12377 * Note we have an issue here with infoframes: current code
12378 * only updates them on the full mode set path per hw
12379 * requirements. So here we should be checking for any
12380 * required changes and forcing a mode set.
12381 */
Jesse Barnes20664592014-11-05 14:26:09 -080012382 }
Jesse Barnes50f52752014-11-07 13:11:00 -080012383
Jesse Barnes1f9954d2014-11-05 14:26:10 -080012384 intel_update_pipe_size(to_intel_crtc(set->crtc));
12385
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012386 if (config->mode_changed) {
Jesse Barnes50f52752014-11-07 13:11:00 -080012387 ret = intel_set_mode_pipes(set->crtc, set->mode,
12388 set->x, set->y, set->fb, pipe_config,
12389 modeset_pipes, prepare_pipes,
12390 disable_pipes);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012391 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070012392 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080012393 struct drm_plane *primary = set->crtc->primary;
12394 int vdisplay, hdisplay;
Matt Roper3b150f02014-05-29 08:06:53 -070012395
Gustavo Padovan455a6802014-12-01 15:40:11 -080012396 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070012397 ret = drm_plane_helper_update(primary, set->crtc, set->fb,
12398 0, 0, hdisplay, vdisplay,
12399 set->x << 16, set->y << 16,
12400 hdisplay << 16, vdisplay << 16);
Matt Roper3b150f02014-05-29 08:06:53 -070012401
12402 /*
12403 * We need to make sure the primary plane is re-enabled if it
12404 * has previously been turned off.
12405 */
12406 if (!intel_crtc->primary_enabled && ret == 0) {
12407 WARN_ON(!intel_crtc->active);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +030012408 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012409 }
12410
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012411 /*
12412 * In the fastboot case this may be our only check of the
12413 * state after boot. It would be better to only do it on
12414 * the first update, but we don't have a nice way of doing that
12415 * (and really, set_config isn't used much for high freq page
12416 * flipping, so increasing its cost here shouldn't be a big
12417 * deal).
12418 */
Jani Nikulad330a952014-01-21 11:24:25 +020012419 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012420 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020012421 }
12422
Chris Wilson2d05eae2013-05-03 17:36:25 +010012423 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020012424 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12425 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020012426fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010012427 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012428
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012429 drm_atomic_state_clear(state);
12430
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012431 /*
12432 * HACK: if the pipe was on, but we didn't have a framebuffer,
12433 * force the pipe off to avoid oopsing in the modeset code
12434 * due to fb==NULL. This should only happen during boot since
12435 * we don't yet reconstruct the FB from the hardware state.
12436 */
12437 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
12438 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
12439
Chris Wilson2d05eae2013-05-03 17:36:25 +010012440 /* Try to restore the config */
12441 if (config->mode_changed &&
12442 intel_set_mode(save_set.crtc, save_set.mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012443 save_set.x, save_set.y, save_set.fb,
12444 state))
Chris Wilson2d05eae2013-05-03 17:36:25 +010012445 DRM_ERROR("failed to restore config after modeset failure\n");
12446 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012447
Daniel Vetterd9e55602012-07-04 22:16:09 +020012448out_config:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012449 if (state)
12450 drm_atomic_state_free(state);
12451
Daniel Vetterd9e55602012-07-04 22:16:09 +020012452 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012453 return ret;
12454}
12455
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012456static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012457 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020012458 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012459 .destroy = intel_crtc_destroy,
12460 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080012461 .atomic_duplicate_state = intel_crtc_duplicate_state,
12462 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012463};
12464
Daniel Vetter53589012013-06-05 13:34:16 +020012465static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
12466 struct intel_shared_dpll *pll,
12467 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012468{
Daniel Vetter53589012013-06-05 13:34:16 +020012469 uint32_t val;
12470
Daniel Vetterf458ebb2014-09-30 10:56:39 +020012471 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030012472 return false;
12473
Daniel Vetter53589012013-06-05 13:34:16 +020012474 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020012475 hw_state->dpll = val;
12476 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
12477 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020012478
12479 return val & DPLL_VCO_ENABLE;
12480}
12481
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012482static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
12483 struct intel_shared_dpll *pll)
12484{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012485 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
12486 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012487}
12488
Daniel Vettere7b903d2013-06-05 13:34:14 +020012489static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
12490 struct intel_shared_dpll *pll)
12491{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012492 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020012493 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020012494
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012495 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012496
12497 /* Wait for the clocks to stabilize. */
12498 POSTING_READ(PCH_DPLL(pll->id));
12499 udelay(150);
12500
12501 /* The pixel multiplier can only be updated once the
12502 * DPLL is enabled and the clocks are stable.
12503 *
12504 * So write it again.
12505 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012506 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012507 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012508 udelay(200);
12509}
12510
12511static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
12512 struct intel_shared_dpll *pll)
12513{
12514 struct drm_device *dev = dev_priv->dev;
12515 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020012516
12517 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012518 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020012519 if (intel_crtc_to_shared_dpll(crtc) == pll)
12520 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
12521 }
12522
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012523 I915_WRITE(PCH_DPLL(pll->id), 0);
12524 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012525 udelay(200);
12526}
12527
Daniel Vetter46edb022013-06-05 13:34:12 +020012528static char *ibx_pch_dpll_names[] = {
12529 "PCH DPLL A",
12530 "PCH DPLL B",
12531};
12532
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012533static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012534{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012535 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012536 int i;
12537
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012538 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012539
Daniel Vettere72f9fb2013-06-05 13:34:06 +020012540 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020012541 dev_priv->shared_dplls[i].id = i;
12542 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012543 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020012544 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
12545 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020012546 dev_priv->shared_dplls[i].get_hw_state =
12547 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012548 }
12549}
12550
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012551static void intel_shared_dpll_init(struct drm_device *dev)
12552{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012553 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012554
Daniel Vetter9cd86932014-06-25 22:01:57 +030012555 if (HAS_DDI(dev))
12556 intel_ddi_pll_init(dev);
12557 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012558 ibx_pch_dpll_init(dev);
12559 else
12560 dev_priv->num_shared_dpll = 0;
12561
12562 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012563}
12564
Matt Roper6beb8c232014-12-01 15:40:14 -080012565/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000012566 * intel_wm_need_update - Check whether watermarks need updating
12567 * @plane: drm plane
12568 * @state: new plane state
12569 *
12570 * Check current plane state versus the new one to determine whether
12571 * watermarks need to be recalculated.
12572 *
12573 * Returns true or false.
12574 */
12575bool intel_wm_need_update(struct drm_plane *plane,
12576 struct drm_plane_state *state)
12577{
12578 /* Update watermarks on tiling changes. */
12579 if (!plane->state->fb || !state->fb ||
12580 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
12581 plane->state->rotation != state->rotation)
12582 return true;
12583
12584 return false;
12585}
12586
12587/**
Matt Roper6beb8c232014-12-01 15:40:14 -080012588 * intel_prepare_plane_fb - Prepare fb for usage on plane
12589 * @plane: drm plane to prepare for
12590 * @fb: framebuffer to prepare for presentation
12591 *
12592 * Prepares a framebuffer for usage on a display plane. Generally this
12593 * involves pinning the underlying object and updating the frontbuffer tracking
12594 * bits. Some older platforms need special physical address handling for
12595 * cursor planes.
12596 *
12597 * Returns 0 on success, negative error code on failure.
12598 */
12599int
12600intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000012601 struct drm_framebuffer *fb,
12602 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070012603{
12604 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080012605 struct intel_plane *intel_plane = to_intel_plane(plane);
12606 enum pipe pipe = intel_plane->pipe;
12607 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12608 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
12609 unsigned frontbuffer_bits = 0;
12610 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070012611
Matt Roperea2c67b2014-12-23 10:41:52 -080012612 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070012613 return 0;
12614
Matt Roper6beb8c232014-12-01 15:40:14 -080012615 switch (plane->type) {
12616 case DRM_PLANE_TYPE_PRIMARY:
12617 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
12618 break;
12619 case DRM_PLANE_TYPE_CURSOR:
12620 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
12621 break;
12622 case DRM_PLANE_TYPE_OVERLAY:
12623 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
12624 break;
12625 }
Matt Roper465c1202014-05-29 08:06:54 -070012626
Matt Roper4c345742014-07-09 16:22:10 -070012627 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070012628
Matt Roper6beb8c232014-12-01 15:40:14 -080012629 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
12630 INTEL_INFO(dev)->cursor_needs_physical) {
12631 int align = IS_I830(dev) ? 16 * 1024 : 256;
12632 ret = i915_gem_object_attach_phys(obj, align);
12633 if (ret)
12634 DRM_DEBUG_KMS("failed to attach phys object\n");
12635 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000012636 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080012637 }
12638
12639 if (ret == 0)
12640 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
12641
12642 mutex_unlock(&dev->struct_mutex);
12643
12644 return ret;
12645}
12646
Matt Roper38f3ce32014-12-02 07:45:25 -080012647/**
12648 * intel_cleanup_plane_fb - Cleans up an fb after plane use
12649 * @plane: drm plane to clean up for
12650 * @fb: old framebuffer that was on plane
12651 *
12652 * Cleans up a framebuffer that has just been removed from a plane.
12653 */
12654void
12655intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000012656 struct drm_framebuffer *fb,
12657 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080012658{
12659 struct drm_device *dev = plane->dev;
12660 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12661
12662 if (WARN_ON(!obj))
12663 return;
12664
12665 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
12666 !INTEL_INFO(dev)->cursor_needs_physical) {
12667 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000012668 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080012669 mutex_unlock(&dev->struct_mutex);
12670 }
Matt Roper465c1202014-05-29 08:06:54 -070012671}
12672
12673static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012674intel_check_primary_plane(struct drm_plane *plane,
12675 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070012676{
Matt Roper32b7eee2014-12-24 07:59:06 -080012677 struct drm_device *dev = plane->dev;
12678 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080012679 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080012680 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080012681 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012682 struct drm_rect *dest = &state->dst;
12683 struct drm_rect *src = &state->src;
12684 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053012685 bool can_position = false;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012686 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012687
Matt Roperea2c67b2014-12-23 10:41:52 -080012688 crtc = crtc ? crtc : plane->crtc;
12689 intel_crtc = to_intel_crtc(crtc);
12690
Sonika Jindald8106362015-04-10 14:37:28 +053012691 if (INTEL_INFO(dev)->gen >= 9)
12692 can_position = true;
12693
Matt Roperc59cb172014-12-01 15:40:16 -080012694 ret = drm_plane_helper_check_update(plane, crtc, fb,
12695 src, dest, clip,
12696 DRM_PLANE_HELPER_NO_SCALING,
12697 DRM_PLANE_HELPER_NO_SCALING,
Sonika Jindald8106362015-04-10 14:37:28 +053012698 can_position, true,
12699 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080012700 if (ret)
12701 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012702
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012703 if (intel_crtc->active) {
Matt Roper32b7eee2014-12-24 07:59:06 -080012704 intel_crtc->atomic.wait_for_flips = true;
12705
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012706 /*
12707 * FBC does not work on some platforms for rotated
12708 * planes, so disable it when rotation is not 0 and
12709 * update it when rotation is set back to 0.
12710 *
12711 * FIXME: This is redundant with the fbc update done in
12712 * the primary plane enable function except that that
12713 * one is done too late. We eventually need to unify
12714 * this.
12715 */
12716 if (intel_crtc->primary_enabled &&
12717 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020012718 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080012719 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080012720 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012721 }
12722
12723 if (state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080012724 /*
12725 * BDW signals flip done immediately if the plane
12726 * is disabled, even if the plane enable is already
12727 * armed to occur at the next vblank :(
12728 */
12729 if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
12730 intel_crtc->atomic.wait_vblank = true;
12731 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012732
Matt Roper32b7eee2014-12-24 07:59:06 -080012733 intel_crtc->atomic.fb_bits |=
12734 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
12735
12736 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000012737
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000012738 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000012739 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080012740 }
12741
12742 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070012743}
12744
Sonika Jindal48404c12014-08-22 14:06:04 +053012745static void
12746intel_commit_primary_plane(struct drm_plane *plane,
12747 struct intel_plane_state *state)
12748{
Matt Roper2b875c22014-12-01 15:40:13 -080012749 struct drm_crtc *crtc = state->base.crtc;
12750 struct drm_framebuffer *fb = state->base.fb;
12751 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053012752 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080012753 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053012754 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080012755
Matt Roperea2c67b2014-12-23 10:41:52 -080012756 crtc = crtc ? crtc : plane->crtc;
12757 intel_crtc = to_intel_crtc(crtc);
12758
Matt Ropercf4c7c12014-12-04 10:27:42 -080012759 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053012760 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070012761 crtc->y = src->y1 >> 16;
12762
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012763 if (intel_crtc->active) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012764 if (state->visible) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012765 /* FIXME: kill this fastboot hack */
12766 intel_update_pipe_size(intel_crtc);
12767
12768 intel_crtc->primary_enabled = true;
12769
12770 dev_priv->display.update_primary_plane(crtc, plane->fb,
12771 crtc->x, crtc->y);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012772 } else {
12773 /*
12774 * If clipping results in a non-visible primary plane,
12775 * we'll disable the primary plane. Note that this is
12776 * a bit different than what happens if userspace
12777 * explicitly disables the plane by passing fb=0
12778 * because plane->fb still gets set and pinned.
12779 */
12780 intel_disable_primary_hw_plane(plane, crtc);
12781 }
Matt Roper32b7eee2014-12-24 07:59:06 -080012782 }
12783}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012784
Matt Roper32b7eee2014-12-24 07:59:06 -080012785static void intel_begin_crtc_commit(struct drm_crtc *crtc)
12786{
12787 struct drm_device *dev = crtc->dev;
12788 struct drm_i915_private *dev_priv = dev->dev_private;
12789 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080012790 struct intel_plane *intel_plane;
12791 struct drm_plane *p;
12792 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012793
Matt Roperea2c67b2014-12-23 10:41:52 -080012794 /* Track fb's for any planes being disabled */
12795 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
12796 intel_plane = to_intel_plane(p);
12797
12798 if (intel_crtc->atomic.disabled_planes &
12799 (1 << drm_plane_index(p))) {
12800 switch (p->type) {
12801 case DRM_PLANE_TYPE_PRIMARY:
12802 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
12803 break;
12804 case DRM_PLANE_TYPE_CURSOR:
12805 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
12806 break;
12807 case DRM_PLANE_TYPE_OVERLAY:
12808 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
12809 break;
12810 }
12811
12812 mutex_lock(&dev->struct_mutex);
12813 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
12814 mutex_unlock(&dev->struct_mutex);
12815 }
12816 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012817
Matt Roper32b7eee2014-12-24 07:59:06 -080012818 if (intel_crtc->atomic.wait_for_flips)
12819 intel_crtc_wait_for_pending_flips(crtc);
12820
12821 if (intel_crtc->atomic.disable_fbc)
12822 intel_fbc_disable(dev);
12823
12824 if (intel_crtc->atomic.pre_disable_primary)
12825 intel_pre_disable_primary(crtc);
12826
12827 if (intel_crtc->atomic.update_wm)
12828 intel_update_watermarks(crtc);
12829
12830 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080012831
12832 /* Perform vblank evasion around commit operation */
12833 if (intel_crtc->active)
12834 intel_crtc->atomic.evade =
12835 intel_pipe_update_start(intel_crtc,
12836 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080012837}
12838
12839static void intel_finish_crtc_commit(struct drm_crtc *crtc)
12840{
12841 struct drm_device *dev = crtc->dev;
12842 struct drm_i915_private *dev_priv = dev->dev_private;
12843 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12844 struct drm_plane *p;
12845
Matt Roperc34c9ee2014-12-23 10:41:50 -080012846 if (intel_crtc->atomic.evade)
12847 intel_pipe_update_end(intel_crtc,
12848 intel_crtc->atomic.start_vbl_count);
12849
Matt Roper32b7eee2014-12-24 07:59:06 -080012850 intel_runtime_pm_put(dev_priv);
12851
12852 if (intel_crtc->atomic.wait_vblank)
12853 intel_wait_for_vblank(dev, intel_crtc->pipe);
12854
12855 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
12856
12857 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012858 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020012859 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012860 mutex_unlock(&dev->struct_mutex);
12861 }
Matt Roper465c1202014-05-29 08:06:54 -070012862
Matt Roper32b7eee2014-12-24 07:59:06 -080012863 if (intel_crtc->atomic.post_enable_primary)
12864 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012865
Matt Roper32b7eee2014-12-24 07:59:06 -080012866 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
12867 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
12868 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
12869 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012870
Matt Roper32b7eee2014-12-24 07:59:06 -080012871 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012872}
12873
Matt Ropercf4c7c12014-12-04 10:27:42 -080012874/**
Matt Roper4a3b8762014-12-23 10:41:51 -080012875 * intel_plane_destroy - destroy a plane
12876 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080012877 *
Matt Roper4a3b8762014-12-23 10:41:51 -080012878 * Common destruction function for all types of planes (primary, cursor,
12879 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080012880 */
Matt Roper4a3b8762014-12-23 10:41:51 -080012881void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070012882{
12883 struct intel_plane *intel_plane = to_intel_plane(plane);
12884 drm_plane_cleanup(plane);
12885 kfree(intel_plane);
12886}
12887
Matt Roper65a3fea2015-01-21 16:35:42 -080012888const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070012889 .update_plane = drm_atomic_helper_update_plane,
12890 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070012891 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080012892 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080012893 .atomic_get_property = intel_plane_atomic_get_property,
12894 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080012895 .atomic_duplicate_state = intel_plane_duplicate_state,
12896 .atomic_destroy_state = intel_plane_destroy_state,
12897
Matt Roper465c1202014-05-29 08:06:54 -070012898};
12899
12900static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
12901 int pipe)
12902{
12903 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080012904 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070012905 const uint32_t *intel_primary_formats;
12906 int num_formats;
12907
12908 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
12909 if (primary == NULL)
12910 return NULL;
12911
Matt Roper8e7d6882015-01-21 16:35:41 -080012912 state = intel_create_plane_state(&primary->base);
12913 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080012914 kfree(primary);
12915 return NULL;
12916 }
Matt Roper8e7d6882015-01-21 16:35:41 -080012917 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080012918
Matt Roper465c1202014-05-29 08:06:54 -070012919 primary->can_scale = false;
12920 primary->max_downscale = 1;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070012921 state->scaler_id = -1;
Matt Roper465c1202014-05-29 08:06:54 -070012922 primary->pipe = pipe;
12923 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080012924 primary->check_plane = intel_check_primary_plane;
12925 primary->commit_plane = intel_commit_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070012926 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070012927 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
12928 primary->plane = !pipe;
12929
12930 if (INTEL_INFO(dev)->gen <= 3) {
12931 intel_primary_formats = intel_primary_formats_gen2;
12932 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
12933 } else {
12934 intel_primary_formats = intel_primary_formats_gen4;
12935 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
12936 }
12937
12938 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080012939 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070012940 intel_primary_formats, num_formats,
12941 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053012942
Sonika Jindal3b7a5112015-04-10 14:37:29 +053012943 if (INTEL_INFO(dev)->gen >= 4)
12944 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053012945
Matt Roperea2c67b2014-12-23 10:41:52 -080012946 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
12947
Matt Roper465c1202014-05-29 08:06:54 -070012948 return &primary->base;
12949}
12950
Sonika Jindal3b7a5112015-04-10 14:37:29 +053012951void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
12952{
12953 if (!dev->mode_config.rotation_property) {
12954 unsigned long flags = BIT(DRM_ROTATE_0) |
12955 BIT(DRM_ROTATE_180);
12956
12957 if (INTEL_INFO(dev)->gen >= 9)
12958 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
12959
12960 dev->mode_config.rotation_property =
12961 drm_mode_create_rotation_property(dev, flags);
12962 }
12963 if (dev->mode_config.rotation_property)
12964 drm_object_attach_property(&plane->base.base,
12965 dev->mode_config.rotation_property,
12966 plane->base.state->rotation);
12967}
12968
Matt Roper3d7d6512014-06-10 08:28:13 -070012969static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030012970intel_check_cursor_plane(struct drm_plane *plane,
12971 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070012972{
Matt Roper2b875c22014-12-01 15:40:13 -080012973 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080012974 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080012975 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012976 struct drm_rect *dest = &state->dst;
12977 struct drm_rect *src = &state->src;
12978 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012979 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080012980 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012981 unsigned stride;
12982 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012983
Matt Roperea2c67b2014-12-23 10:41:52 -080012984 crtc = crtc ? crtc : plane->crtc;
12985 intel_crtc = to_intel_crtc(crtc);
12986
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012987 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030012988 src, dest, clip,
12989 DRM_PLANE_HELPER_NO_SCALING,
12990 DRM_PLANE_HELPER_NO_SCALING,
12991 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012992 if (ret)
12993 return ret;
12994
12995
12996 /* if we want to turn off the cursor ignore width and height */
12997 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080012998 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012999
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013000 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013001 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13002 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13003 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013004 return -EINVAL;
13005 }
13006
Matt Roperea2c67b2014-12-23 10:41:52 -080013007 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13008 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013009 DRM_DEBUG_KMS("buffer is too small\n");
13010 return -ENOMEM;
13011 }
13012
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013013 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013014 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13015 ret = -EINVAL;
13016 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013017
Matt Roper32b7eee2014-12-24 07:59:06 -080013018finish:
13019 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013020 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013021 intel_crtc->atomic.update_wm = true;
13022
13023 intel_crtc->atomic.fb_bits |=
13024 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13025 }
13026
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013027 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013028}
13029
Matt Roperf4a2cf22014-12-01 15:40:12 -080013030static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013031intel_commit_cursor_plane(struct drm_plane *plane,
13032 struct intel_plane_state *state)
13033{
Matt Roper2b875c22014-12-01 15:40:13 -080013034 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013035 struct drm_device *dev = plane->dev;
13036 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013037 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013038 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013039
Matt Roperea2c67b2014-12-23 10:41:52 -080013040 crtc = crtc ? crtc : plane->crtc;
13041 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013042
Matt Roperea2c67b2014-12-23 10:41:52 -080013043 plane->fb = state->base.fb;
13044 crtc->cursor_x = state->base.crtc_x;
13045 crtc->cursor_y = state->base.crtc_y;
13046
Gustavo Padovana912f122014-12-01 15:40:10 -080013047 if (intel_crtc->cursor_bo == obj)
13048 goto update;
13049
Matt Roperf4a2cf22014-12-01 15:40:12 -080013050 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013051 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013052 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013053 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013054 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013055 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013056
Gustavo Padovana912f122014-12-01 15:40:10 -080013057 intel_crtc->cursor_addr = addr;
13058 intel_crtc->cursor_bo = obj;
13059update:
Gustavo Padovana912f122014-12-01 15:40:10 -080013060
Matt Roper32b7eee2014-12-24 07:59:06 -080013061 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013062 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013063}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013064
Matt Roper3d7d6512014-06-10 08:28:13 -070013065static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13066 int pipe)
13067{
13068 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013069 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013070
13071 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13072 if (cursor == NULL)
13073 return NULL;
13074
Matt Roper8e7d6882015-01-21 16:35:41 -080013075 state = intel_create_plane_state(&cursor->base);
13076 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013077 kfree(cursor);
13078 return NULL;
13079 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013080 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013081
Matt Roper3d7d6512014-06-10 08:28:13 -070013082 cursor->can_scale = false;
13083 cursor->max_downscale = 1;
13084 cursor->pipe = pipe;
13085 cursor->plane = pipe;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013086 state->scaler_id = -1;
Matt Roperc59cb172014-12-01 15:40:16 -080013087 cursor->check_plane = intel_check_cursor_plane;
13088 cursor->commit_plane = intel_commit_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013089
13090 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013091 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013092 intel_cursor_formats,
13093 ARRAY_SIZE(intel_cursor_formats),
13094 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013095
13096 if (INTEL_INFO(dev)->gen >= 4) {
13097 if (!dev->mode_config.rotation_property)
13098 dev->mode_config.rotation_property =
13099 drm_mode_create_rotation_property(dev,
13100 BIT(DRM_ROTATE_0) |
13101 BIT(DRM_ROTATE_180));
13102 if (dev->mode_config.rotation_property)
13103 drm_object_attach_property(&cursor->base.base,
13104 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013105 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013106 }
13107
Matt Roperea2c67b2014-12-23 10:41:52 -080013108 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13109
Matt Roper3d7d6512014-06-10 08:28:13 -070013110 return &cursor->base;
13111}
13112
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013113static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13114 struct intel_crtc_state *crtc_state)
13115{
13116 int i;
13117 struct intel_scaler *intel_scaler;
13118 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13119
13120 for (i = 0; i < intel_crtc->num_scalers; i++) {
13121 intel_scaler = &scaler_state->scalers[i];
13122 intel_scaler->in_use = 0;
13123 intel_scaler->id = i;
13124
13125 intel_scaler->mode = PS_SCALER_MODE_DYN;
13126 }
13127
13128 scaler_state->scaler_id = -1;
13129}
13130
Hannes Ederb358d0a2008-12-18 21:18:47 +010013131static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013132{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013133 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013134 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013135 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013136 struct drm_plane *primary = NULL;
13137 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013138 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013139
Daniel Vetter955382f2013-09-19 14:05:45 +020013140 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013141 if (intel_crtc == NULL)
13142 return;
13143
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013144 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13145 if (!crtc_state)
13146 goto fail;
13147 intel_crtc_set_state(intel_crtc, crtc_state);
Matt Roper07878242015-02-25 11:43:26 -080013148 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013149
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013150 /* initialize shared scalers */
13151 if (INTEL_INFO(dev)->gen >= 9) {
13152 if (pipe == PIPE_C)
13153 intel_crtc->num_scalers = 1;
13154 else
13155 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13156
13157 skl_init_scalers(dev, intel_crtc, crtc_state);
13158 }
13159
Matt Roper465c1202014-05-29 08:06:54 -070013160 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013161 if (!primary)
13162 goto fail;
13163
13164 cursor = intel_cursor_plane_create(dev, pipe);
13165 if (!cursor)
13166 goto fail;
13167
Matt Roper465c1202014-05-29 08:06:54 -070013168 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013169 cursor, &intel_crtc_funcs);
13170 if (ret)
13171 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013172
13173 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013174 for (i = 0; i < 256; i++) {
13175 intel_crtc->lut_r[i] = i;
13176 intel_crtc->lut_g[i] = i;
13177 intel_crtc->lut_b[i] = i;
13178 }
13179
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013180 /*
13181 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013182 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013183 */
Jesse Barnes80824002009-09-10 15:28:06 -070013184 intel_crtc->pipe = pipe;
13185 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013186 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013187 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013188 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013189 }
13190
Chris Wilson4b0e3332014-05-30 16:35:26 +030013191 intel_crtc->cursor_base = ~0;
13192 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013193 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013194
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013195 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13196 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13197 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13198 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13199
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020013200 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
13201
Jesse Barnes79e53942008-11-07 14:24:08 -080013202 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013203
13204 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013205 return;
13206
13207fail:
13208 if (primary)
13209 drm_plane_cleanup(primary);
13210 if (cursor)
13211 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013212 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013213 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013214}
13215
Jesse Barnes752aa882013-10-31 18:55:49 +020013216enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13217{
13218 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013219 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013220
Rob Clark51fd3712013-11-19 12:10:12 -050013221 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013222
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013223 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013224 return INVALID_PIPE;
13225
13226 return to_intel_crtc(encoder->crtc)->pipe;
13227}
13228
Carl Worth08d7b3d2009-04-29 14:43:54 -070013229int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013230 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013231{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013232 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013233 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013234 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013235
Rob Clark7707e652014-07-17 23:30:04 -040013236 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013237
Rob Clark7707e652014-07-17 23:30:04 -040013238 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013239 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013240 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013241 }
13242
Rob Clark7707e652014-07-17 23:30:04 -040013243 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013244 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013245
Daniel Vetterc05422d2009-08-11 16:05:30 +020013246 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013247}
13248
Daniel Vetter66a92782012-07-12 20:08:18 +020013249static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013250{
Daniel Vetter66a92782012-07-12 20:08:18 +020013251 struct drm_device *dev = encoder->base.dev;
13252 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013253 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013254 int entry = 0;
13255
Damien Lespiaub2784e12014-08-05 11:29:37 +010013256 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013257 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013258 index_mask |= (1 << entry);
13259
Jesse Barnes79e53942008-11-07 14:24:08 -080013260 entry++;
13261 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013262
Jesse Barnes79e53942008-11-07 14:24:08 -080013263 return index_mask;
13264}
13265
Chris Wilson4d302442010-12-14 19:21:29 +000013266static bool has_edp_a(struct drm_device *dev)
13267{
13268 struct drm_i915_private *dev_priv = dev->dev_private;
13269
13270 if (!IS_MOBILE(dev))
13271 return false;
13272
13273 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13274 return false;
13275
Damien Lespiaue3589902014-02-07 19:12:50 +000013276 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013277 return false;
13278
13279 return true;
13280}
13281
Jesse Barnes84b4e042014-06-25 08:24:29 -070013282static bool intel_crt_present(struct drm_device *dev)
13283{
13284 struct drm_i915_private *dev_priv = dev->dev_private;
13285
Damien Lespiau884497e2013-12-03 13:56:23 +000013286 if (INTEL_INFO(dev)->gen >= 9)
13287 return false;
13288
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013289 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013290 return false;
13291
13292 if (IS_CHERRYVIEW(dev))
13293 return false;
13294
13295 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13296 return false;
13297
13298 return true;
13299}
13300
Jesse Barnes79e53942008-11-07 14:24:08 -080013301static void intel_setup_outputs(struct drm_device *dev)
13302{
Eric Anholt725e30a2009-01-22 13:01:02 -080013303 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013304 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013305 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013306
Daniel Vetterc9093352013-06-06 22:22:47 +020013307 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013308
Jesse Barnes84b4e042014-06-25 08:24:29 -070013309 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013310 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013311
Paulo Zanoniaffa9352012-11-23 15:30:39 -020013312 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013313 int found;
13314
Jesse Barnesde31fac2015-03-06 15:53:32 -080013315 /*
13316 * Haswell uses DDI functions to detect digital outputs.
13317 * On SKL pre-D0 the strap isn't connected, so we assume
13318 * it's there.
13319 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013320 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013321 /* WaIgnoreDDIAStrap: skl */
13322 if (found ||
13323 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013324 intel_ddi_init(dev, PORT_A);
13325
13326 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13327 * register */
13328 found = I915_READ(SFUSE_STRAP);
13329
13330 if (found & SFUSE_STRAP_DDIB_DETECTED)
13331 intel_ddi_init(dev, PORT_B);
13332 if (found & SFUSE_STRAP_DDIC_DETECTED)
13333 intel_ddi_init(dev, PORT_C);
13334 if (found & SFUSE_STRAP_DDID_DETECTED)
13335 intel_ddi_init(dev, PORT_D);
13336 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013337 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013338 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013339
13340 if (has_edp_a(dev))
13341 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013342
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013343 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013344 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013345 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013346 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013347 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013348 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013349 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013350 }
13351
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013352 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013353 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013354
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013355 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013356 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013357
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013358 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013359 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013360
Daniel Vetter270b3042012-10-27 15:52:05 +020013361 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013362 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013363 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013364 /*
13365 * The DP_DETECTED bit is the latched state of the DDC
13366 * SDA pin at boot. However since eDP doesn't require DDC
13367 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13368 * eDP ports may have been muxed to an alternate function.
13369 * Thus we can't rely on the DP_DETECTED bit alone to detect
13370 * eDP ports. Consult the VBT as well as DP_DETECTED to
13371 * detect eDP ports.
13372 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013373 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13374 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013375 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13376 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013377 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13378 intel_dp_is_edp(dev, PORT_B))
13379 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013380
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013381 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13382 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070013383 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13384 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013385 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13386 intel_dp_is_edp(dev, PORT_C))
13387 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053013388
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013389 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013390 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013391 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
13392 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013393 /* eDP not supported on port D, so don't check VBT */
13394 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
13395 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013396 }
13397
Jani Nikula3cfca972013-08-27 15:12:26 +030013398 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080013399 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013400 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080013401
Paulo Zanonie2debe92013-02-18 19:00:27 -030013402 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013403 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013404 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013405 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
13406 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013407 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013408 }
Ma Ling27185ae2009-08-24 13:50:23 +080013409
Imre Deake7281ea2013-05-08 13:14:08 +030013410 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013411 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080013412 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013413
13414 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013415
Paulo Zanonie2debe92013-02-18 19:00:27 -030013416 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013417 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013418 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013419 }
Ma Ling27185ae2009-08-24 13:50:23 +080013420
Paulo Zanonie2debe92013-02-18 19:00:27 -030013421 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013422
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013423 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
13424 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013425 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013426 }
Imre Deake7281ea2013-05-08 13:14:08 +030013427 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013428 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080013429 }
Ma Ling27185ae2009-08-24 13:50:23 +080013430
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013431 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030013432 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013433 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070013434 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013435 intel_dvo_init(dev);
13436
Zhenyu Wang103a1962009-11-27 11:44:36 +080013437 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013438 intel_tv_init(dev);
13439
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080013440 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070013441
Damien Lespiaub2784e12014-08-05 11:29:37 +010013442 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010013443 encoder->base.possible_crtcs = encoder->crtc_mask;
13444 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020013445 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080013446 }
Chris Wilson47356eb2011-01-11 17:06:04 +000013447
Paulo Zanonidde86e22012-12-01 12:04:25 -020013448 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020013449
13450 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013451}
13452
13453static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
13454{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013455 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080013456 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080013457
Daniel Vetteref2d6332014-02-10 18:00:38 +010013458 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013459 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010013460 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013461 drm_gem_object_unreference(&intel_fb->obj->base);
13462 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013463 kfree(intel_fb);
13464}
13465
13466static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000013467 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080013468 unsigned int *handle)
13469{
13470 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000013471 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080013472
Chris Wilson05394f32010-11-08 19:18:58 +000013473 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080013474}
13475
13476static const struct drm_framebuffer_funcs intel_fb_funcs = {
13477 .destroy = intel_user_framebuffer_destroy,
13478 .create_handle = intel_user_framebuffer_create_handle,
13479};
13480
Damien Lespiaub3218032015-02-27 11:15:18 +000013481static
13482u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
13483 uint32_t pixel_format)
13484{
13485 u32 gen = INTEL_INFO(dev)->gen;
13486
13487 if (gen >= 9) {
13488 /* "The stride in bytes must not exceed the of the size of 8K
13489 * pixels and 32K bytes."
13490 */
13491 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
13492 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
13493 return 32*1024;
13494 } else if (gen >= 4) {
13495 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13496 return 16*1024;
13497 else
13498 return 32*1024;
13499 } else if (gen >= 3) {
13500 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13501 return 8*1024;
13502 else
13503 return 16*1024;
13504 } else {
13505 /* XXX DSPC is limited to 4k tiled */
13506 return 8*1024;
13507 }
13508}
13509
Daniel Vetterb5ea6422014-03-02 21:18:00 +010013510static int intel_framebuffer_init(struct drm_device *dev,
13511 struct intel_framebuffer *intel_fb,
13512 struct drm_mode_fb_cmd2 *mode_cmd,
13513 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080013514{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000013515 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080013516 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000013517 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080013518
Daniel Vetterdd4916c2013-10-09 21:23:51 +020013519 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
13520
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013521 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
13522 /* Enforce that fb modifier and tiling mode match, but only for
13523 * X-tiled. This is needed for FBC. */
13524 if (!!(obj->tiling_mode == I915_TILING_X) !=
13525 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
13526 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
13527 return -EINVAL;
13528 }
13529 } else {
13530 if (obj->tiling_mode == I915_TILING_X)
13531 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
13532 else if (obj->tiling_mode == I915_TILING_Y) {
13533 DRM_DEBUG("No Y tiling for legacy addfb\n");
13534 return -EINVAL;
13535 }
13536 }
13537
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000013538 /* Passed in modifier sanity checking. */
13539 switch (mode_cmd->modifier[0]) {
13540 case I915_FORMAT_MOD_Y_TILED:
13541 case I915_FORMAT_MOD_Yf_TILED:
13542 if (INTEL_INFO(dev)->gen < 9) {
13543 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
13544 mode_cmd->modifier[0]);
13545 return -EINVAL;
13546 }
13547 case DRM_FORMAT_MOD_NONE:
13548 case I915_FORMAT_MOD_X_TILED:
13549 break;
13550 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070013551 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
13552 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010013553 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013554 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013555
Damien Lespiaub3218032015-02-27 11:15:18 +000013556 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
13557 mode_cmd->pixel_format);
13558 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
13559 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
13560 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010013561 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013562 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013563
Damien Lespiaub3218032015-02-27 11:15:18 +000013564 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
13565 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010013566 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000013567 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
13568 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013569 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010013570 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013571 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013572 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013573
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013574 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013575 mode_cmd->pitches[0] != obj->stride) {
13576 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
13577 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013578 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013579 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013580
Ville Syrjälä57779d02012-10-31 17:50:14 +020013581 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013582 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020013583 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020013584 case DRM_FORMAT_RGB565:
13585 case DRM_FORMAT_XRGB8888:
13586 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020013587 break;
13588 case DRM_FORMAT_XRGB1555:
13589 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013590 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013591 DRM_DEBUG("unsupported pixel format: %s\n",
13592 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013593 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013594 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020013595 break;
13596 case DRM_FORMAT_XBGR8888:
13597 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020013598 case DRM_FORMAT_XRGB2101010:
13599 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020013600 case DRM_FORMAT_XBGR2101010:
13601 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013602 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013603 DRM_DEBUG("unsupported pixel format: %s\n",
13604 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013605 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013606 }
Jesse Barnesb5626742011-06-24 12:19:27 -070013607 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020013608 case DRM_FORMAT_YUYV:
13609 case DRM_FORMAT_UYVY:
13610 case DRM_FORMAT_YVYU:
13611 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013612 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013613 DRM_DEBUG("unsupported pixel format: %s\n",
13614 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013615 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013616 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013617 break;
13618 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013619 DRM_DEBUG("unsupported pixel format: %s\n",
13620 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010013621 return -EINVAL;
13622 }
13623
Ville Syrjälä90f9a332012-10-31 17:50:19 +020013624 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
13625 if (mode_cmd->offsets[0] != 0)
13626 return -EINVAL;
13627
Damien Lespiauec2c9812015-01-20 12:51:45 +000013628 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000013629 mode_cmd->pixel_format,
13630 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020013631 /* FIXME drm helper for size checks (especially planar formats)? */
13632 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
13633 return -EINVAL;
13634
Daniel Vetterc7d73f62012-12-13 23:38:38 +010013635 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
13636 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020013637 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010013638
Jesse Barnes79e53942008-11-07 14:24:08 -080013639 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
13640 if (ret) {
13641 DRM_ERROR("framebuffer init failed %d\n", ret);
13642 return ret;
13643 }
13644
Jesse Barnes79e53942008-11-07 14:24:08 -080013645 return 0;
13646}
13647
Jesse Barnes79e53942008-11-07 14:24:08 -080013648static struct drm_framebuffer *
13649intel_user_framebuffer_create(struct drm_device *dev,
13650 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013651 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080013652{
Chris Wilson05394f32010-11-08 19:18:58 +000013653 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080013654
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013655 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
13656 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000013657 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010013658 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080013659
Chris Wilsond2dff872011-04-19 08:36:26 +010013660 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080013661}
13662
Daniel Vetter4520f532013-10-09 09:18:51 +020013663#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020013664static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020013665{
13666}
13667#endif
13668
Jesse Barnes79e53942008-11-07 14:24:08 -080013669static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080013670 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020013671 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080013672 .atomic_check = intel_atomic_check,
13673 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080013674};
13675
Jesse Barnese70236a2009-09-21 10:42:27 -070013676/* Set up chip specific display functions */
13677static void intel_init_display(struct drm_device *dev)
13678{
13679 struct drm_i915_private *dev_priv = dev->dev_private;
13680
Daniel Vetteree9300b2013-06-03 22:40:22 +020013681 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
13682 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030013683 else if (IS_CHERRYVIEW(dev))
13684 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020013685 else if (IS_VALLEYVIEW(dev))
13686 dev_priv->display.find_dpll = vlv_find_best_dpll;
13687 else if (IS_PINEVIEW(dev))
13688 dev_priv->display.find_dpll = pnv_find_best_dpll;
13689 else
13690 dev_priv->display.find_dpll = i9xx_find_best_dpll;
13691
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000013692 if (INTEL_INFO(dev)->gen >= 9) {
13693 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013694 dev_priv->display.get_initial_plane_config =
13695 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000013696 dev_priv->display.crtc_compute_clock =
13697 haswell_crtc_compute_clock;
13698 dev_priv->display.crtc_enable = haswell_crtc_enable;
13699 dev_priv->display.crtc_disable = haswell_crtc_disable;
13700 dev_priv->display.off = ironlake_crtc_off;
13701 dev_priv->display.update_primary_plane =
13702 skylake_update_primary_plane;
13703 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013704 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013705 dev_priv->display.get_initial_plane_config =
13706 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020013707 dev_priv->display.crtc_compute_clock =
13708 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020013709 dev_priv->display.crtc_enable = haswell_crtc_enable;
13710 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030013711 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000013712 dev_priv->display.update_primary_plane =
13713 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030013714 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013715 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013716 dev_priv->display.get_initial_plane_config =
13717 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020013718 dev_priv->display.crtc_compute_clock =
13719 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020013720 dev_priv->display.crtc_enable = ironlake_crtc_enable;
13721 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013722 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070013723 dev_priv->display.update_primary_plane =
13724 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070013725 } else if (IS_VALLEYVIEW(dev)) {
13726 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013727 dev_priv->display.get_initial_plane_config =
13728 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020013729 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070013730 dev_priv->display.crtc_enable = valleyview_crtc_enable;
13731 dev_priv->display.crtc_disable = i9xx_crtc_disable;
13732 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070013733 dev_priv->display.update_primary_plane =
13734 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070013735 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013736 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013737 dev_priv->display.get_initial_plane_config =
13738 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020013739 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020013740 dev_priv->display.crtc_enable = i9xx_crtc_enable;
13741 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013742 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070013743 dev_priv->display.update_primary_plane =
13744 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070013745 }
Jesse Barnese70236a2009-09-21 10:42:27 -070013746
Jesse Barnese70236a2009-09-21 10:42:27 -070013747 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030013748 if (IS_SKYLAKE(dev))
13749 dev_priv->display.get_display_clock_speed =
13750 skylake_get_display_clock_speed;
13751 else if (IS_BROADWELL(dev))
13752 dev_priv->display.get_display_clock_speed =
13753 broadwell_get_display_clock_speed;
13754 else if (IS_HASWELL(dev))
13755 dev_priv->display.get_display_clock_speed =
13756 haswell_get_display_clock_speed;
13757 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070013758 dev_priv->display.get_display_clock_speed =
13759 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030013760 else if (IS_GEN5(dev))
13761 dev_priv->display.get_display_clock_speed =
13762 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030013763 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
13764 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070013765 dev_priv->display.get_display_clock_speed =
13766 i945_get_display_clock_speed;
13767 else if (IS_I915G(dev))
13768 dev_priv->display.get_display_clock_speed =
13769 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020013770 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070013771 dev_priv->display.get_display_clock_speed =
13772 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020013773 else if (IS_PINEVIEW(dev))
13774 dev_priv->display.get_display_clock_speed =
13775 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070013776 else if (IS_I915GM(dev))
13777 dev_priv->display.get_display_clock_speed =
13778 i915gm_get_display_clock_speed;
13779 else if (IS_I865G(dev))
13780 dev_priv->display.get_display_clock_speed =
13781 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020013782 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070013783 dev_priv->display.get_display_clock_speed =
13784 i855_get_display_clock_speed;
13785 else /* 852, 830 */
13786 dev_priv->display.get_display_clock_speed =
13787 i830_get_display_clock_speed;
13788
Jani Nikula7c10a2b2014-10-27 16:26:43 +020013789 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013790 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013791 } else if (IS_GEN6(dev)) {
13792 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013793 } else if (IS_IVYBRIDGE(dev)) {
13794 /* FIXME: detect B0+ stepping and use auto training */
13795 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030013796 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013797 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080013798 } else if (IS_VALLEYVIEW(dev)) {
13799 dev_priv->display.modeset_global_resources =
13800 valleyview_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070013801 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070013802
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070013803 switch (INTEL_INFO(dev)->gen) {
13804 case 2:
13805 dev_priv->display.queue_flip = intel_gen2_queue_flip;
13806 break;
13807
13808 case 3:
13809 dev_priv->display.queue_flip = intel_gen3_queue_flip;
13810 break;
13811
13812 case 4:
13813 case 5:
13814 dev_priv->display.queue_flip = intel_gen4_queue_flip;
13815 break;
13816
13817 case 6:
13818 dev_priv->display.queue_flip = intel_gen6_queue_flip;
13819 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070013820 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070013821 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070013822 dev_priv->display.queue_flip = intel_gen7_queue_flip;
13823 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000013824 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000013825 /* Drop through - unsupported since execlist only. */
13826 default:
13827 /* Default just returns -ENODEV to indicate unsupported */
13828 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070013829 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020013830
13831 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030013832
13833 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070013834}
13835
Jesse Barnesb690e962010-07-19 13:53:12 -070013836/*
13837 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
13838 * resume, or other times. This quirk makes sure that's the case for
13839 * affected systems.
13840 */
Akshay Joshi0206e352011-08-16 15:34:10 -040013841static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070013842{
13843 struct drm_i915_private *dev_priv = dev->dev_private;
13844
13845 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013846 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070013847}
13848
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030013849static void quirk_pipeb_force(struct drm_device *dev)
13850{
13851 struct drm_i915_private *dev_priv = dev->dev_private;
13852
13853 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
13854 DRM_INFO("applying pipe b force quirk\n");
13855}
13856
Keith Packard435793d2011-07-12 14:56:22 -070013857/*
13858 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
13859 */
13860static void quirk_ssc_force_disable(struct drm_device *dev)
13861{
13862 struct drm_i915_private *dev_priv = dev->dev_private;
13863 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013864 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070013865}
13866
Carsten Emde4dca20e2012-03-15 15:56:26 +010013867/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010013868 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
13869 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010013870 */
13871static void quirk_invert_brightness(struct drm_device *dev)
13872{
13873 struct drm_i915_private *dev_priv = dev->dev_private;
13874 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013875 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070013876}
13877
Scot Doyle9c72cc62014-07-03 23:27:50 +000013878/* Some VBT's incorrectly indicate no backlight is present */
13879static void quirk_backlight_present(struct drm_device *dev)
13880{
13881 struct drm_i915_private *dev_priv = dev->dev_private;
13882 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
13883 DRM_INFO("applying backlight present quirk\n");
13884}
13885
Jesse Barnesb690e962010-07-19 13:53:12 -070013886struct intel_quirk {
13887 int device;
13888 int subsystem_vendor;
13889 int subsystem_device;
13890 void (*hook)(struct drm_device *dev);
13891};
13892
Egbert Eich5f85f172012-10-14 15:46:38 +020013893/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
13894struct intel_dmi_quirk {
13895 void (*hook)(struct drm_device *dev);
13896 const struct dmi_system_id (*dmi_id_list)[];
13897};
13898
13899static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
13900{
13901 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
13902 return 1;
13903}
13904
13905static const struct intel_dmi_quirk intel_dmi_quirks[] = {
13906 {
13907 .dmi_id_list = &(const struct dmi_system_id[]) {
13908 {
13909 .callback = intel_dmi_reverse_brightness,
13910 .ident = "NCR Corporation",
13911 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
13912 DMI_MATCH(DMI_PRODUCT_NAME, ""),
13913 },
13914 },
13915 { } /* terminating entry */
13916 },
13917 .hook = quirk_invert_brightness,
13918 },
13919};
13920
Ben Widawskyc43b5632012-04-16 14:07:40 -070013921static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070013922 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040013923 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070013924
Jesse Barnesb690e962010-07-19 13:53:12 -070013925 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
13926 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
13927
Jesse Barnesb690e962010-07-19 13:53:12 -070013928 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
13929 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
13930
Ville Syrjälä5f080c02014-08-15 01:22:06 +030013931 /* 830 needs to leave pipe A & dpll A up */
13932 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
13933
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030013934 /* 830 needs to leave pipe B & dpll B up */
13935 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
13936
Keith Packard435793d2011-07-12 14:56:22 -070013937 /* Lenovo U160 cannot use SSC on LVDS */
13938 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020013939
13940 /* Sony Vaio Y cannot use SSC on LVDS */
13941 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010013942
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010013943 /* Acer Aspire 5734Z must invert backlight brightness */
13944 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
13945
13946 /* Acer/eMachines G725 */
13947 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
13948
13949 /* Acer/eMachines e725 */
13950 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
13951
13952 /* Acer/Packard Bell NCL20 */
13953 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
13954
13955 /* Acer Aspire 4736Z */
13956 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020013957
13958 /* Acer Aspire 5336 */
13959 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000013960
13961 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
13962 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000013963
Scot Doyledfb3d47b2014-08-21 16:08:02 +000013964 /* Acer C720 Chromebook (Core i3 4005U) */
13965 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
13966
jens steinb2a96012014-10-28 20:25:53 +010013967 /* Apple Macbook 2,1 (Core 2 T7400) */
13968 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
13969
Scot Doyled4967d82014-07-03 23:27:52 +000013970 /* Toshiba CB35 Chromebook (Celeron 2955U) */
13971 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000013972
13973 /* HP Chromebook 14 (Celeron 2955U) */
13974 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020013975
13976 /* Dell Chromebook 11 */
13977 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070013978};
13979
13980static void intel_init_quirks(struct drm_device *dev)
13981{
13982 struct pci_dev *d = dev->pdev;
13983 int i;
13984
13985 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
13986 struct intel_quirk *q = &intel_quirks[i];
13987
13988 if (d->device == q->device &&
13989 (d->subsystem_vendor == q->subsystem_vendor ||
13990 q->subsystem_vendor == PCI_ANY_ID) &&
13991 (d->subsystem_device == q->subsystem_device ||
13992 q->subsystem_device == PCI_ANY_ID))
13993 q->hook(dev);
13994 }
Egbert Eich5f85f172012-10-14 15:46:38 +020013995 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
13996 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
13997 intel_dmi_quirks[i].hook(dev);
13998 }
Jesse Barnesb690e962010-07-19 13:53:12 -070013999}
14000
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014001/* Disable the VGA plane that we never use */
14002static void i915_disable_vga(struct drm_device *dev)
14003{
14004 struct drm_i915_private *dev_priv = dev->dev_private;
14005 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014006 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014007
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014008 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014009 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014010 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014011 sr1 = inb(VGA_SR_DATA);
14012 outb(sr1 | 1<<5, VGA_SR_DATA);
14013 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14014 udelay(300);
14015
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014016 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014017 POSTING_READ(vga_reg);
14018}
14019
Daniel Vetterf8175862012-04-10 15:50:11 +020014020void intel_modeset_init_hw(struct drm_device *dev)
14021{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014022 intel_prepare_ddi(dev);
14023
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030014024 if (IS_VALLEYVIEW(dev))
14025 vlv_update_cdclk(dev);
14026
Daniel Vetterf8175862012-04-10 15:50:11 +020014027 intel_init_clock_gating(dev);
14028
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014029 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014030}
14031
Jesse Barnes79e53942008-11-07 14:24:08 -080014032void intel_modeset_init(struct drm_device *dev)
14033{
Jesse Barnes652c3932009-08-17 13:31:43 -070014034 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014035 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014036 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014037 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014038
14039 drm_mode_config_init(dev);
14040
14041 dev->mode_config.min_width = 0;
14042 dev->mode_config.min_height = 0;
14043
Dave Airlie019d96c2011-09-29 16:20:42 +010014044 dev->mode_config.preferred_depth = 24;
14045 dev->mode_config.prefer_shadow = 1;
14046
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014047 dev->mode_config.allow_fb_modifiers = true;
14048
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014049 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014050
Jesse Barnesb690e962010-07-19 13:53:12 -070014051 intel_init_quirks(dev);
14052
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014053 intel_init_pm(dev);
14054
Ben Widawskye3c74752013-04-05 13:12:39 -070014055 if (INTEL_INFO(dev)->num_pipes == 0)
14056 return;
14057
Jesse Barnese70236a2009-09-21 10:42:27 -070014058 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014059 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014060
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014061 if (IS_GEN2(dev)) {
14062 dev->mode_config.max_width = 2048;
14063 dev->mode_config.max_height = 2048;
14064 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014065 dev->mode_config.max_width = 4096;
14066 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014067 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014068 dev->mode_config.max_width = 8192;
14069 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014070 }
Damien Lespiau068be562014-03-28 14:17:49 +000014071
Ville Syrjälädc41c152014-08-13 11:57:05 +030014072 if (IS_845G(dev) || IS_I865G(dev)) {
14073 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14074 dev->mode_config.cursor_height = 1023;
14075 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014076 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14077 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14078 } else {
14079 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14080 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14081 }
14082
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014083 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014084
Zhao Yakui28c97732009-10-09 11:39:41 +080014085 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014086 INTEL_INFO(dev)->num_pipes,
14087 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014088
Damien Lespiau055e3932014-08-18 13:49:10 +010014089 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014090 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014091 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014092 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014093 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014094 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014095 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014096 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014097 }
14098
Jesse Barnesf42bb702013-12-16 16:34:23 -080014099 intel_init_dpio(dev);
14100
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014101 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014102
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014103 /* Just disable it once at startup */
14104 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014105 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014106
14107 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014108 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014109
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014110 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014111 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014112 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014113
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014114 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080014115 if (!crtc->active)
14116 continue;
14117
Jesse Barnes46f297f2014-03-07 08:57:48 -080014118 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014119 * Note that reserving the BIOS fb up front prevents us
14120 * from stuffing other stolen allocations like the ring
14121 * on top. This prevents some ugliness at boot time, and
14122 * can even allow for smooth boot transitions if the BIOS
14123 * fb is large enough for the active pipe configuration.
14124 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014125 if (dev_priv->display.get_initial_plane_config) {
14126 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080014127 &crtc->plane_config);
14128 /*
14129 * If the fb is shared between multiple heads, we'll
14130 * just get the first one.
14131 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010014132 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014133 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080014134 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014135}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014136
Daniel Vetter7fad7982012-07-04 17:51:47 +020014137static void intel_enable_pipe_a(struct drm_device *dev)
14138{
14139 struct intel_connector *connector;
14140 struct drm_connector *crt = NULL;
14141 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014142 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014143
14144 /* We can't just switch on the pipe A, we need to set things up with a
14145 * proper mode and output configuration. As a gross hack, enable pipe A
14146 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014147 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014148 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14149 crt = &connector->base;
14150 break;
14151 }
14152 }
14153
14154 if (!crt)
14155 return;
14156
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014157 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014158 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014159}
14160
Daniel Vetterfa555832012-10-10 23:14:00 +020014161static bool
14162intel_check_plane_mapping(struct intel_crtc *crtc)
14163{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014164 struct drm_device *dev = crtc->base.dev;
14165 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014166 u32 reg, val;
14167
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014168 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014169 return true;
14170
14171 reg = DSPCNTR(!crtc->plane);
14172 val = I915_READ(reg);
14173
14174 if ((val & DISPLAY_PLANE_ENABLE) &&
14175 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14176 return false;
14177
14178 return true;
14179}
14180
Daniel Vetter24929352012-07-02 20:28:59 +020014181static void intel_sanitize_crtc(struct intel_crtc *crtc)
14182{
14183 struct drm_device *dev = crtc->base.dev;
14184 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014185 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014186
Daniel Vetter24929352012-07-02 20:28:59 +020014187 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014188 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014189 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14190
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014191 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014192 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014193 if (crtc->active) {
14194 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014195 drm_crtc_vblank_on(&crtc->base);
14196 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014197
Daniel Vetter24929352012-07-02 20:28:59 +020014198 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014199 * disable the crtc (and hence change the state) if it is wrong. Note
14200 * that gen4+ has a fixed plane -> pipe mapping. */
14201 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014202 struct intel_connector *connector;
14203 bool plane;
14204
Daniel Vetter24929352012-07-02 20:28:59 +020014205 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14206 crtc->base.base.id);
14207
14208 /* Pipe has the wrong plane attached and the plane is active.
14209 * Temporarily change the plane mapping and disable everything
14210 * ... */
14211 plane = crtc->plane;
14212 crtc->plane = !plane;
Daniel Vetter9c8958b2014-07-14 19:35:31 +020014213 crtc->primary_enabled = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014214 dev_priv->display.crtc_disable(&crtc->base);
14215 crtc->plane = plane;
14216
14217 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014218 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014219 if (connector->encoder->base.crtc != &crtc->base)
14220 continue;
14221
Egbert Eich7f1950f2014-04-25 10:56:22 +020014222 connector->base.dpms = DRM_MODE_DPMS_OFF;
14223 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014224 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014225 /* multiple connectors may have the same encoder:
14226 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014227 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020014228 if (connector->encoder->base.crtc == &crtc->base) {
14229 connector->encoder->base.crtc = NULL;
14230 connector->encoder->connectors_active = false;
14231 }
Daniel Vetter24929352012-07-02 20:28:59 +020014232
14233 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080014234 crtc->base.state->enable = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014235 crtc->base.enabled = false;
14236 }
Daniel Vetter24929352012-07-02 20:28:59 +020014237
Daniel Vetter7fad7982012-07-04 17:51:47 +020014238 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14239 crtc->pipe == PIPE_A && !crtc->active) {
14240 /* BIOS forgot to enable pipe A, this mostly happens after
14241 * resume. Force-enable the pipe to fix this, the update_dpms
14242 * call below we restore the pipe to the right state, but leave
14243 * the required bits on. */
14244 intel_enable_pipe_a(dev);
14245 }
14246
Daniel Vetter24929352012-07-02 20:28:59 +020014247 /* Adjust the state of the output pipe according to whether we
14248 * have active connectors/encoders. */
14249 intel_crtc_update_dpms(&crtc->base);
14250
Matt Roper83d65732015-02-25 13:12:16 -080014251 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020014252 struct intel_encoder *encoder;
14253
14254 /* This can happen either due to bugs in the get_hw_state
14255 * functions or because the pipe is force-enabled due to the
14256 * pipe A quirk. */
14257 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14258 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014259 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014260 crtc->active ? "enabled" : "disabled");
14261
Matt Roper83d65732015-02-25 13:12:16 -080014262 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014263 crtc->base.enabled = crtc->active;
14264
14265 /* Because we only establish the connector -> encoder ->
14266 * crtc links if something is active, this means the
14267 * crtc is now deactivated. Break the links. connector
14268 * -> encoder links are only establish when things are
14269 * actually up, hence no need to break them. */
14270 WARN_ON(crtc->active);
14271
14272 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14273 WARN_ON(encoder->connectors_active);
14274 encoder->base.crtc = NULL;
14275 }
14276 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014277
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014278 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014279 /*
14280 * We start out with underrun reporting disabled to avoid races.
14281 * For correct bookkeeping mark this on active crtcs.
14282 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014283 * Also on gmch platforms we dont have any hardware bits to
14284 * disable the underrun reporting. Which means we need to start
14285 * out with underrun reporting disabled also on inactive pipes,
14286 * since otherwise we'll complain about the garbage we read when
14287 * e.g. coming up after runtime pm.
14288 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014289 * No protection against concurrent access is required - at
14290 * worst a fifo underrun happens which also sets this to false.
14291 */
14292 crtc->cpu_fifo_underrun_disabled = true;
14293 crtc->pch_fifo_underrun_disabled = true;
14294 }
Daniel Vetter24929352012-07-02 20:28:59 +020014295}
14296
14297static void intel_sanitize_encoder(struct intel_encoder *encoder)
14298{
14299 struct intel_connector *connector;
14300 struct drm_device *dev = encoder->base.dev;
14301
14302 /* We need to check both for a crtc link (meaning that the
14303 * encoder is active and trying to read from a pipe) and the
14304 * pipe itself being active. */
14305 bool has_active_crtc = encoder->base.crtc &&
14306 to_intel_crtc(encoder->base.crtc)->active;
14307
14308 if (encoder->connectors_active && !has_active_crtc) {
14309 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14310 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014311 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014312
14313 /* Connector is active, but has no active pipe. This is
14314 * fallout from our resume register restoring. Disable
14315 * the encoder manually again. */
14316 if (encoder->base.crtc) {
14317 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14318 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014319 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014320 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014321 if (encoder->post_disable)
14322 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014323 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014324 encoder->base.crtc = NULL;
14325 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014326
14327 /* Inconsistent output/port/pipe state happens presumably due to
14328 * a bug in one of the get_hw_state functions. Or someplace else
14329 * in our code, like the register restore mess on resume. Clamp
14330 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014331 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014332 if (connector->encoder != encoder)
14333 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014334 connector->base.dpms = DRM_MODE_DPMS_OFF;
14335 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014336 }
14337 }
14338 /* Enabled encoders without active connectors will be fixed in
14339 * the crtc fixup. */
14340}
14341
Imre Deak04098752014-02-18 00:02:16 +020014342void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014343{
14344 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014345 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014346
Imre Deak04098752014-02-18 00:02:16 +020014347 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14348 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14349 i915_disable_vga(dev);
14350 }
14351}
14352
14353void i915_redisable_vga(struct drm_device *dev)
14354{
14355 struct drm_i915_private *dev_priv = dev->dev_private;
14356
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014357 /* This function can be called both from intel_modeset_setup_hw_state or
14358 * at a very early point in our resume sequence, where the power well
14359 * structures are not yet restored. Since this function is at a very
14360 * paranoid "someone might have enabled VGA while we were not looking"
14361 * level, just check if the power well is enabled instead of trying to
14362 * follow the "don't touch the power well if we don't need it" policy
14363 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014364 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014365 return;
14366
Imre Deak04098752014-02-18 00:02:16 +020014367 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014368}
14369
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014370static bool primary_get_hw_state(struct intel_crtc *crtc)
14371{
14372 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14373
14374 if (!crtc->active)
14375 return false;
14376
14377 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
14378}
14379
Daniel Vetter30e984d2013-06-05 13:34:17 +020014380static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020014381{
14382 struct drm_i915_private *dev_priv = dev->dev_private;
14383 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020014384 struct intel_crtc *crtc;
14385 struct intel_encoder *encoder;
14386 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020014387 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020014388
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014389 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014390 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020014391
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014392 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020014393
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014394 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014395 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014396
Matt Roper83d65732015-02-25 13:12:16 -080014397 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014398 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014399 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020014400
14401 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
14402 crtc->base.base.id,
14403 crtc->active ? "enabled" : "disabled");
14404 }
14405
Daniel Vetter53589012013-06-05 13:34:16 +020014406 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14407 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14408
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014409 pll->on = pll->get_hw_state(dev_priv, pll,
14410 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020014411 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014412 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014413 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014414 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020014415 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014416 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014417 }
Daniel Vetter53589012013-06-05 13:34:16 +020014418 }
Daniel Vetter53589012013-06-05 13:34:16 +020014419
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014420 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014421 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014422
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014423 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014424 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020014425 }
14426
Damien Lespiaub2784e12014-08-05 11:29:37 +010014427 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014428 pipe = 0;
14429
14430 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070014431 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14432 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014433 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014434 } else {
14435 encoder->base.crtc = NULL;
14436 }
14437
14438 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014439 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020014440 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014441 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014442 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014443 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020014444 }
14445
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014446 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014447 if (connector->get_hw_state(connector)) {
14448 connector->base.dpms = DRM_MODE_DPMS_ON;
14449 connector->encoder->connectors_active = true;
14450 connector->base.encoder = &connector->encoder->base;
14451 } else {
14452 connector->base.dpms = DRM_MODE_DPMS_OFF;
14453 connector->base.encoder = NULL;
14454 }
14455 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
14456 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030014457 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014458 connector->base.encoder ? "enabled" : "disabled");
14459 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020014460}
14461
14462/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
14463 * and i915 state tracking structures. */
14464void intel_modeset_setup_hw_state(struct drm_device *dev,
14465 bool force_restore)
14466{
14467 struct drm_i915_private *dev_priv = dev->dev_private;
14468 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014469 struct intel_crtc *crtc;
14470 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020014471 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014472
14473 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020014474
Jesse Barnesbabea612013-06-26 18:57:38 +030014475 /*
14476 * Now that we have the config, copy it to each CRTC struct
14477 * Note that this could go away if we move to using crtc_config
14478 * checking everywhere.
14479 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014480 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020014481 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014482 intel_mode_from_pipe_config(&crtc->base.mode,
14483 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030014484 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
14485 crtc->base.base.id);
14486 drm_mode_debug_printmodeline(&crtc->base.mode);
14487 }
14488 }
14489
Daniel Vetter24929352012-07-02 20:28:59 +020014490 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010014491 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014492 intel_sanitize_encoder(encoder);
14493 }
14494
Damien Lespiau055e3932014-08-18 13:49:10 +010014495 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020014496 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14497 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014498 intel_dump_pipe_config(crtc, crtc->config,
14499 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020014500 }
Daniel Vetter9a935852012-07-05 22:34:27 +020014501
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020014502 intel_modeset_update_connector_atomic_state(dev);
14503
Daniel Vetter35c95372013-07-17 06:55:04 +020014504 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14505 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14506
14507 if (!pll->on || pll->active)
14508 continue;
14509
14510 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
14511
14512 pll->disable(dev_priv, pll);
14513 pll->on = false;
14514 }
14515
Pradeep Bhat30789992014-11-04 17:06:45 +000014516 if (IS_GEN9(dev))
14517 skl_wm_get_hw_state(dev);
14518 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030014519 ilk_wm_get_hw_state(dev);
14520
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010014521 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030014522 i915_redisable_vga(dev);
14523
Daniel Vetterf30da182013-04-11 20:22:50 +020014524 /*
14525 * We need to use raw interfaces for restoring state to avoid
14526 * checking (bogus) intermediate states.
14527 */
Damien Lespiau055e3932014-08-18 13:49:10 +010014528 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070014529 struct drm_crtc *crtc =
14530 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020014531
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014532 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010014533 }
14534 } else {
14535 intel_modeset_update_staged_output_state(dev);
14536 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020014537
14538 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010014539}
14540
14541void intel_modeset_gem_init(struct drm_device *dev)
14542{
Jesse Barnes92122782014-10-09 12:57:42 -070014543 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080014544 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070014545 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -080014546
Imre Deakae484342014-03-31 15:10:44 +030014547 mutex_lock(&dev->struct_mutex);
14548 intel_init_gt_powersave(dev);
14549 mutex_unlock(&dev->struct_mutex);
14550
Jesse Barnes92122782014-10-09 12:57:42 -070014551 /*
14552 * There may be no VBT; and if the BIOS enabled SSC we can
14553 * just keep using it to avoid unnecessary flicker. Whereas if the
14554 * BIOS isn't using it, don't assume it will work even if the VBT
14555 * indicates as much.
14556 */
14557 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
14558 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14559 DREF_SSC1_ENABLE);
14560
Chris Wilson1833b132012-05-09 11:56:28 +010014561 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020014562
14563 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080014564
14565 /*
14566 * Make sure any fbs we allocated at startup are properly
14567 * pinned & fenced. When we do the allocation it's too early
14568 * for this.
14569 */
14570 mutex_lock(&dev->struct_mutex);
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010014571 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070014572 obj = intel_fb_obj(c->primary->fb);
14573 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080014574 continue;
14575
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +000014576 if (intel_pin_and_fence_fb_obj(c->primary,
14577 c->primary->fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000014578 c->primary->state,
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +000014579 NULL)) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080014580 DRM_ERROR("failed to pin boot fb on pipe %d\n",
14581 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100014582 drm_framebuffer_unreference(c->primary->fb);
14583 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080014584 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080014585 }
14586 }
14587 mutex_unlock(&dev->struct_mutex);
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020014588
14589 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014590}
14591
Imre Deak4932e2c2014-02-11 17:12:48 +020014592void intel_connector_unregister(struct intel_connector *intel_connector)
14593{
14594 struct drm_connector *connector = &intel_connector->base;
14595
14596 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010014597 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020014598}
14599
Jesse Barnes79e53942008-11-07 14:24:08 -080014600void intel_modeset_cleanup(struct drm_device *dev)
14601{
Jesse Barnes652c3932009-08-17 13:31:43 -070014602 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030014603 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070014604
Imre Deak2eb52522014-11-19 15:30:05 +020014605 intel_disable_gt_powersave(dev);
14606
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020014607 intel_backlight_unregister(dev);
14608
Daniel Vetterfd0c0642013-04-24 11:13:35 +020014609 /*
14610 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020014611 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020014612 * experience fancy races otherwise.
14613 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020014614 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070014615
Daniel Vetterfd0c0642013-04-24 11:13:35 +020014616 /*
14617 * Due to the hpd irq storm handling the hotplug work can re-arm the
14618 * poll handlers. Hence disable polling after hpd handling is shut down.
14619 */
Keith Packardf87ea762010-10-03 19:36:26 -070014620 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020014621
Jesse Barnes652c3932009-08-17 13:31:43 -070014622 mutex_lock(&dev->struct_mutex);
14623
Jesse Barnes723bfd72010-10-07 16:01:13 -070014624 intel_unregister_dsm_handler();
14625
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014626 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014627
Kristian Høgsberg69341a52009-11-11 12:19:17 -050014628 mutex_unlock(&dev->struct_mutex);
14629
Chris Wilson1630fe72011-07-08 12:22:42 +010014630 /* flush any delayed tasks or pending work */
14631 flush_scheduled_work();
14632
Jani Nikuladb31af1d2013-11-08 16:48:53 +020014633 /* destroy the backlight and sysfs files before encoders/connectors */
14634 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020014635 struct intel_connector *intel_connector;
14636
14637 intel_connector = to_intel_connector(connector);
14638 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020014639 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030014640
Jesse Barnes79e53942008-11-07 14:24:08 -080014641 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010014642
14643 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030014644
14645 mutex_lock(&dev->struct_mutex);
14646 intel_cleanup_gt_powersave(dev);
14647 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014648}
14649
Dave Airlie28d52042009-09-21 14:33:58 +100014650/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080014651 * Return which encoder is currently attached for connector.
14652 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010014653struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080014654{
Chris Wilsondf0e9242010-09-09 16:20:55 +010014655 return &intel_attached_encoder(connector)->base;
14656}
Jesse Barnes79e53942008-11-07 14:24:08 -080014657
Chris Wilsondf0e9242010-09-09 16:20:55 +010014658void intel_connector_attach_encoder(struct intel_connector *connector,
14659 struct intel_encoder *encoder)
14660{
14661 connector->encoder = encoder;
14662 drm_mode_connector_attach_encoder(&connector->base,
14663 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080014664}
Dave Airlie28d52042009-09-21 14:33:58 +100014665
14666/*
14667 * set vga decode state - true == enable VGA decode
14668 */
14669int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
14670{
14671 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000014672 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100014673 u16 gmch_ctrl;
14674
Chris Wilson75fa0412014-02-07 18:37:02 -020014675 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
14676 DRM_ERROR("failed to read control word\n");
14677 return -EIO;
14678 }
14679
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020014680 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
14681 return 0;
14682
Dave Airlie28d52042009-09-21 14:33:58 +100014683 if (state)
14684 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
14685 else
14686 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020014687
14688 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
14689 DRM_ERROR("failed to write control word\n");
14690 return -EIO;
14691 }
14692
Dave Airlie28d52042009-09-21 14:33:58 +100014693 return 0;
14694}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014695
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014696struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030014697
14698 u32 power_well_driver;
14699
Chris Wilson63b66e52013-08-08 15:12:06 +020014700 int num_transcoders;
14701
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014702 struct intel_cursor_error_state {
14703 u32 control;
14704 u32 position;
14705 u32 base;
14706 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010014707 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014708
14709 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020014710 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014711 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030014712 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010014713 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014714
14715 struct intel_plane_error_state {
14716 u32 control;
14717 u32 stride;
14718 u32 size;
14719 u32 pos;
14720 u32 addr;
14721 u32 surface;
14722 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010014723 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020014724
14725 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020014726 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020014727 enum transcoder cpu_transcoder;
14728
14729 u32 conf;
14730
14731 u32 htotal;
14732 u32 hblank;
14733 u32 hsync;
14734 u32 vtotal;
14735 u32 vblank;
14736 u32 vsync;
14737 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014738};
14739
14740struct intel_display_error_state *
14741intel_display_capture_error_state(struct drm_device *dev)
14742{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014743 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014744 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020014745 int transcoders[] = {
14746 TRANSCODER_A,
14747 TRANSCODER_B,
14748 TRANSCODER_C,
14749 TRANSCODER_EDP,
14750 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014751 int i;
14752
Chris Wilson63b66e52013-08-08 15:12:06 +020014753 if (INTEL_INFO(dev)->num_pipes == 0)
14754 return NULL;
14755
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020014756 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014757 if (error == NULL)
14758 return NULL;
14759
Imre Deak190be112013-11-25 17:15:31 +020014760 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030014761 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
14762
Damien Lespiau055e3932014-08-18 13:49:10 +010014763 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020014764 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014765 __intel_display_power_is_enabled(dev_priv,
14766 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020014767 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020014768 continue;
14769
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030014770 error->cursor[i].control = I915_READ(CURCNTR(i));
14771 error->cursor[i].position = I915_READ(CURPOS(i));
14772 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014773
14774 error->plane[i].control = I915_READ(DSPCNTR(i));
14775 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014776 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030014777 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014778 error->plane[i].pos = I915_READ(DSPPOS(i));
14779 }
Paulo Zanonica291362013-03-06 20:03:14 -030014780 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
14781 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014782 if (INTEL_INFO(dev)->gen >= 4) {
14783 error->plane[i].surface = I915_READ(DSPSURF(i));
14784 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
14785 }
14786
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014787 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030014788
Sonika Jindal3abfce72014-07-21 15:23:43 +053014789 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030014790 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020014791 }
14792
14793 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
14794 if (HAS_DDI(dev_priv->dev))
14795 error->num_transcoders++; /* Account for eDP. */
14796
14797 for (i = 0; i < error->num_transcoders; i++) {
14798 enum transcoder cpu_transcoder = transcoders[i];
14799
Imre Deakddf9c532013-11-27 22:02:02 +020014800 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014801 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020014802 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020014803 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020014804 continue;
14805
Chris Wilson63b66e52013-08-08 15:12:06 +020014806 error->transcoder[i].cpu_transcoder = cpu_transcoder;
14807
14808 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
14809 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
14810 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
14811 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
14812 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
14813 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
14814 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014815 }
14816
14817 return error;
14818}
14819
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014820#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
14821
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014822void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014823intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014824 struct drm_device *dev,
14825 struct intel_display_error_state *error)
14826{
Damien Lespiau055e3932014-08-18 13:49:10 +010014827 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014828 int i;
14829
Chris Wilson63b66e52013-08-08 15:12:06 +020014830 if (!error)
14831 return;
14832
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014833 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020014834 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014835 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030014836 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010014837 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014838 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020014839 err_printf(m, " Power: %s\n",
14840 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014841 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030014842 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014843
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014844 err_printf(m, "Plane [%d]:\n", i);
14845 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
14846 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014847 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014848 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
14849 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014850 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030014851 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014852 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014853 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014854 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
14855 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014856 }
14857
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014858 err_printf(m, "Cursor [%d]:\n", i);
14859 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
14860 err_printf(m, " POS: %08x\n", error->cursor[i].position);
14861 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014862 }
Chris Wilson63b66e52013-08-08 15:12:06 +020014863
14864 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010014865 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020014866 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020014867 err_printf(m, " Power: %s\n",
14868 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020014869 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
14870 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
14871 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
14872 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
14873 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
14874 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
14875 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
14876 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014877}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014878
14879void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
14880{
14881 struct intel_crtc *crtc;
14882
14883 for_each_intel_crtc(dev, crtc) {
14884 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014885
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020014886 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014887
14888 work = crtc->unpin_work;
14889
14890 if (work && work->event &&
14891 work->event->base.file_priv == file) {
14892 kfree(work->event);
14893 work->event = NULL;
14894 }
14895
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020014896 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014897 }
14898}