blob: d1f737a8ad9eb1617d533938de359d02853d08c4 [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 struct drm_atomic_state *state);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080087static int intel_framebuffer_init(struct drm_device *dev,
88 struct intel_framebuffer *ifb,
89 struct drm_mode_fb_cmd2 *mode_cmd,
90 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020091static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
92static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020093static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070094 struct intel_link_m_n *m_n,
95 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +020096static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +020097static void haswell_set_pipeconf(struct drm_crtc *crtc);
98static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +020099static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200100 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200101static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200102 const struct intel_crtc_state *pipe_config);
Matt Roperea2c67b2014-12-23 10:41:52 -0800103static void intel_begin_crtc_commit(struct drm_crtc *crtc);
104static void intel_finish_crtc_commit(struct drm_crtc *crtc);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700105static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
106 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200107static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
108 int num_connectors);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +0300109static void intel_crtc_enable_planes(struct drm_crtc *crtc);
110static void intel_crtc_disable_planes(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100111
Dave Airlie0e32b392014-05-02 14:02:48 +1000112static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
113{
114 if (!connector->mst_port)
115 return connector->encoder;
116 else
117 return &connector->mst_port->mst_encoders[pipe]->base;
118}
119
Jesse Barnes79e53942008-11-07 14:24:08 -0800120typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400121 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800122} intel_range_t;
123
124typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400125 int dot_limit;
126 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800127} intel_p2_t;
128
Ma Lingd4906092009-03-18 20:13:27 +0800129typedef struct intel_limit intel_limit_t;
130struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400131 intel_range_t dot, vco, n, m, m1, m2, p, p1;
132 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800133};
Jesse Barnes79e53942008-11-07 14:24:08 -0800134
Daniel Vetterd2acd212012-10-20 20:57:43 +0200135int
136intel_pch_rawclk(struct drm_device *dev)
137{
138 struct drm_i915_private *dev_priv = dev->dev_private;
139
140 WARN_ON(!HAS_PCH_SPLIT(dev));
141
142 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
143}
144
Chris Wilson021357a2010-09-07 20:54:59 +0100145static inline u32 /* units of 100MHz */
146intel_fdi_link_freq(struct drm_device *dev)
147{
Chris Wilson8b99e682010-10-13 09:59:17 +0100148 if (IS_GEN5(dev)) {
149 struct drm_i915_private *dev_priv = dev->dev_private;
150 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
151 } else
152 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100153}
154
Daniel Vetter5d536e22013-07-06 12:52:06 +0200155static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400156 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200157 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200158 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400159 .m = { .min = 96, .max = 140 },
160 .m1 = { .min = 18, .max = 26 },
161 .m2 = { .min = 6, .max = 16 },
162 .p = { .min = 4, .max = 128 },
163 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700164 .p2 = { .dot_limit = 165000,
165 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700166};
167
Daniel Vetter5d536e22013-07-06 12:52:06 +0200168static const intel_limit_t intel_limits_i8xx_dvo = {
169 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200170 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200171 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200172 .m = { .min = 96, .max = 140 },
173 .m1 = { .min = 18, .max = 26 },
174 .m2 = { .min = 6, .max = 16 },
175 .p = { .min = 4, .max = 128 },
176 .p1 = { .min = 2, .max = 33 },
177 .p2 = { .dot_limit = 165000,
178 .p2_slow = 4, .p2_fast = 4 },
179};
180
Keith Packarde4b36692009-06-05 19:22:17 -0700181static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400182 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200183 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200184 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400185 .m = { .min = 96, .max = 140 },
186 .m1 = { .min = 18, .max = 26 },
187 .m2 = { .min = 6, .max = 16 },
188 .p = { .min = 4, .max = 128 },
189 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700190 .p2 = { .dot_limit = 165000,
191 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700192};
Eric Anholt273e27c2011-03-30 13:01:10 -0700193
Keith Packarde4b36692009-06-05 19:22:17 -0700194static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400195 .dot = { .min = 20000, .max = 400000 },
196 .vco = { .min = 1400000, .max = 2800000 },
197 .n = { .min = 1, .max = 6 },
198 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100199 .m1 = { .min = 8, .max = 18 },
200 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400201 .p = { .min = 5, .max = 80 },
202 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700203 .p2 = { .dot_limit = 200000,
204 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700205};
206
207static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400208 .dot = { .min = 20000, .max = 400000 },
209 .vco = { .min = 1400000, .max = 2800000 },
210 .n = { .min = 1, .max = 6 },
211 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100212 .m1 = { .min = 8, .max = 18 },
213 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400214 .p = { .min = 7, .max = 98 },
215 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700216 .p2 = { .dot_limit = 112000,
217 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700218};
219
Eric Anholt273e27c2011-03-30 13:01:10 -0700220
Keith Packarde4b36692009-06-05 19:22:17 -0700221static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700222 .dot = { .min = 25000, .max = 270000 },
223 .vco = { .min = 1750000, .max = 3500000},
224 .n = { .min = 1, .max = 4 },
225 .m = { .min = 104, .max = 138 },
226 .m1 = { .min = 17, .max = 23 },
227 .m2 = { .min = 5, .max = 11 },
228 .p = { .min = 10, .max = 30 },
229 .p1 = { .min = 1, .max = 3},
230 .p2 = { .dot_limit = 270000,
231 .p2_slow = 10,
232 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800233 },
Keith Packarde4b36692009-06-05 19:22:17 -0700234};
235
236static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700237 .dot = { .min = 22000, .max = 400000 },
238 .vco = { .min = 1750000, .max = 3500000},
239 .n = { .min = 1, .max = 4 },
240 .m = { .min = 104, .max = 138 },
241 .m1 = { .min = 16, .max = 23 },
242 .m2 = { .min = 5, .max = 11 },
243 .p = { .min = 5, .max = 80 },
244 .p1 = { .min = 1, .max = 8},
245 .p2 = { .dot_limit = 165000,
246 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700247};
248
249static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700250 .dot = { .min = 20000, .max = 115000 },
251 .vco = { .min = 1750000, .max = 3500000 },
252 .n = { .min = 1, .max = 3 },
253 .m = { .min = 104, .max = 138 },
254 .m1 = { .min = 17, .max = 23 },
255 .m2 = { .min = 5, .max = 11 },
256 .p = { .min = 28, .max = 112 },
257 .p1 = { .min = 2, .max = 8 },
258 .p2 = { .dot_limit = 0,
259 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800260 },
Keith Packarde4b36692009-06-05 19:22:17 -0700261};
262
263static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700264 .dot = { .min = 80000, .max = 224000 },
265 .vco = { .min = 1750000, .max = 3500000 },
266 .n = { .min = 1, .max = 3 },
267 .m = { .min = 104, .max = 138 },
268 .m1 = { .min = 17, .max = 23 },
269 .m2 = { .min = 5, .max = 11 },
270 .p = { .min = 14, .max = 42 },
271 .p1 = { .min = 2, .max = 6 },
272 .p2 = { .dot_limit = 0,
273 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800274 },
Keith Packarde4b36692009-06-05 19:22:17 -0700275};
276
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500277static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400278 .dot = { .min = 20000, .max = 400000},
279 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700280 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400281 .n = { .min = 3, .max = 6 },
282 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700283 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400284 .m1 = { .min = 0, .max = 0 },
285 .m2 = { .min = 0, .max = 254 },
286 .p = { .min = 5, .max = 80 },
287 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700288 .p2 = { .dot_limit = 200000,
289 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700290};
291
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500292static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400293 .dot = { .min = 20000, .max = 400000 },
294 .vco = { .min = 1700000, .max = 3500000 },
295 .n = { .min = 3, .max = 6 },
296 .m = { .min = 2, .max = 256 },
297 .m1 = { .min = 0, .max = 0 },
298 .m2 = { .min = 0, .max = 254 },
299 .p = { .min = 7, .max = 112 },
300 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700301 .p2 = { .dot_limit = 112000,
302 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700303};
304
Eric Anholt273e27c2011-03-30 13:01:10 -0700305/* Ironlake / Sandybridge
306 *
307 * We calculate clock using (register_value + 2) for N/M1/M2, so here
308 * the range value for them is (actual_value - 2).
309 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800310static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700311 .dot = { .min = 25000, .max = 350000 },
312 .vco = { .min = 1760000, .max = 3510000 },
313 .n = { .min = 1, .max = 5 },
314 .m = { .min = 79, .max = 127 },
315 .m1 = { .min = 12, .max = 22 },
316 .m2 = { .min = 5, .max = 9 },
317 .p = { .min = 5, .max = 80 },
318 .p1 = { .min = 1, .max = 8 },
319 .p2 = { .dot_limit = 225000,
320 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700321};
322
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800323static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700324 .dot = { .min = 25000, .max = 350000 },
325 .vco = { .min = 1760000, .max = 3510000 },
326 .n = { .min = 1, .max = 3 },
327 .m = { .min = 79, .max = 118 },
328 .m1 = { .min = 12, .max = 22 },
329 .m2 = { .min = 5, .max = 9 },
330 .p = { .min = 28, .max = 112 },
331 .p1 = { .min = 2, .max = 8 },
332 .p2 = { .dot_limit = 225000,
333 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800334};
335
336static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700337 .dot = { .min = 25000, .max = 350000 },
338 .vco = { .min = 1760000, .max = 3510000 },
339 .n = { .min = 1, .max = 3 },
340 .m = { .min = 79, .max = 127 },
341 .m1 = { .min = 12, .max = 22 },
342 .m2 = { .min = 5, .max = 9 },
343 .p = { .min = 14, .max = 56 },
344 .p1 = { .min = 2, .max = 8 },
345 .p2 = { .dot_limit = 225000,
346 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800347};
348
Eric Anholt273e27c2011-03-30 13:01:10 -0700349/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800350static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700351 .dot = { .min = 25000, .max = 350000 },
352 .vco = { .min = 1760000, .max = 3510000 },
353 .n = { .min = 1, .max = 2 },
354 .m = { .min = 79, .max = 126 },
355 .m1 = { .min = 12, .max = 22 },
356 .m2 = { .min = 5, .max = 9 },
357 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400358 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700359 .p2 = { .dot_limit = 225000,
360 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800361};
362
363static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700364 .dot = { .min = 25000, .max = 350000 },
365 .vco = { .min = 1760000, .max = 3510000 },
366 .n = { .min = 1, .max = 3 },
367 .m = { .min = 79, .max = 126 },
368 .m1 = { .min = 12, .max = 22 },
369 .m2 = { .min = 5, .max = 9 },
370 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400371 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700372 .p2 = { .dot_limit = 225000,
373 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800374};
375
Ville Syrjälädc730512013-09-24 21:26:30 +0300376static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300377 /*
378 * These are the data rate limits (measured in fast clocks)
379 * since those are the strictest limits we have. The fast
380 * clock and actual rate limits are more relaxed, so checking
381 * them would make no difference.
382 */
383 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200384 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700385 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700386 .m1 = { .min = 2, .max = 3 },
387 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300388 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300389 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700390};
391
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300392static const intel_limit_t intel_limits_chv = {
393 /*
394 * These are the data rate limits (measured in fast clocks)
395 * since those are the strictest limits we have. The fast
396 * clock and actual rate limits are more relaxed, so checking
397 * them would make no difference.
398 */
399 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200400 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300401 .n = { .min = 1, .max = 1 },
402 .m1 = { .min = 2, .max = 2 },
403 .m2 = { .min = 24 << 22, .max = 175 << 22 },
404 .p1 = { .min = 2, .max = 4 },
405 .p2 = { .p2_slow = 1, .p2_fast = 14 },
406};
407
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200408static const intel_limit_t intel_limits_bxt = {
409 /* FIXME: find real dot limits */
410 .dot = { .min = 0, .max = INT_MAX },
411 .vco = { .min = 4800000, .max = 6480000 },
412 .n = { .min = 1, .max = 1 },
413 .m1 = { .min = 2, .max = 2 },
414 /* FIXME: find real m2 limits */
415 .m2 = { .min = 2 << 22, .max = 255 << 22 },
416 .p1 = { .min = 2, .max = 4 },
417 .p2 = { .p2_slow = 1, .p2_fast = 20 },
418};
419
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300420static void vlv_clock(int refclk, intel_clock_t *clock)
421{
422 clock->m = clock->m1 * clock->m2;
423 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200424 if (WARN_ON(clock->n == 0 || clock->p == 0))
425 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300426 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
427 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300428}
429
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300430/**
431 * Returns whether any output on the specified pipe is of the specified type
432 */
Damien Lespiau40935612014-10-29 11:16:59 +0000433bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300434{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300435 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300436 struct intel_encoder *encoder;
437
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300438 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300439 if (encoder->type == type)
440 return true;
441
442 return false;
443}
444
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200445/**
446 * Returns whether any output on the specified pipe will have the specified
447 * type after a staged modeset is complete, i.e., the same as
448 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
449 * encoder->crtc.
450 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200451static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
452 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200453{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200454 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300455 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200456 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200457 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200458 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200459
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300460 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200461 if (connector_state->crtc != crtc_state->base.crtc)
462 continue;
463
464 num_connectors++;
465
466 encoder = to_intel_encoder(connector_state->best_encoder);
467 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200468 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200469 }
470
471 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200472
473 return false;
474}
475
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200476static const intel_limit_t *
477intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800478{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200479 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800480 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800481
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200482 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100483 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000484 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800485 limit = &intel_limits_ironlake_dual_lvds_100m;
486 else
487 limit = &intel_limits_ironlake_dual_lvds;
488 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000489 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800490 limit = &intel_limits_ironlake_single_lvds_100m;
491 else
492 limit = &intel_limits_ironlake_single_lvds;
493 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200494 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800495 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800496
497 return limit;
498}
499
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200500static const intel_limit_t *
501intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800502{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200503 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800504 const intel_limit_t *limit;
505
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200506 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100507 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700508 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800509 else
Keith Packarde4b36692009-06-05 19:22:17 -0700510 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200511 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
512 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700513 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200514 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700515 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800516 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700517 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800518
519 return limit;
520}
521
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200522static const intel_limit_t *
523intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800524{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200525 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800526 const intel_limit_t *limit;
527
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200528 if (IS_BROXTON(dev))
529 limit = &intel_limits_bxt;
530 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200531 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800532 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200533 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500534 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200535 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500536 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800537 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500538 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300539 } else if (IS_CHERRYVIEW(dev)) {
540 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700541 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300542 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100543 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200544 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100545 limit = &intel_limits_i9xx_lvds;
546 else
547 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800548 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200549 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700550 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200551 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700552 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200553 else
554 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800555 }
556 return limit;
557}
558
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500559/* m1 is reserved as 0 in Pineview, n is a ring counter */
560static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800561{
Shaohua Li21778322009-02-23 15:19:16 +0800562 clock->m = clock->m2 + 2;
563 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200564 if (WARN_ON(clock->n == 0 || clock->p == 0))
565 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300566 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
567 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800568}
569
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200570static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
571{
572 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
573}
574
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200575static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800576{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200577 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800578 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200579 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
580 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300581 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
582 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800583}
584
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300585static void chv_clock(int refclk, intel_clock_t *clock)
586{
587 clock->m = clock->m1 * clock->m2;
588 clock->p = clock->p1 * clock->p2;
589 if (WARN_ON(clock->n == 0 || clock->p == 0))
590 return;
591 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
592 clock->n << 22);
593 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
594}
595
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800596#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800597/**
598 * Returns whether the given set of divisors are valid for a given refclk with
599 * the given connectors.
600 */
601
Chris Wilson1b894b52010-12-14 20:04:54 +0000602static bool intel_PLL_is_valid(struct drm_device *dev,
603 const intel_limit_t *limit,
604 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800605{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300606 if (clock->n < limit->n.min || limit->n.max < clock->n)
607 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800608 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400609 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800610 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400611 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400613 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300614
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200615 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300616 if (clock->m1 <= clock->m2)
617 INTELPllInvalid("m1 <= m2\n");
618
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200619 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300620 if (clock->p < limit->p.min || limit->p.max < clock->p)
621 INTELPllInvalid("p out of range\n");
622 if (clock->m < limit->m.min || limit->m.max < clock->m)
623 INTELPllInvalid("m out of range\n");
624 }
625
Jesse Barnes79e53942008-11-07 14:24:08 -0800626 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400627 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800628 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
629 * connector, etc., rather than just a single range.
630 */
631 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400632 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800633
634 return true;
635}
636
Ma Lingd4906092009-03-18 20:13:27 +0800637static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200638i9xx_find_best_dpll(const intel_limit_t *limit,
639 struct intel_crtc_state *crtc_state,
Sean Paulcec2f352012-01-10 15:09:36 -0800640 int target, int refclk, intel_clock_t *match_clock,
641 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800642{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200643 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300644 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800645 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800646 int err = target;
647
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200648 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800649 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100650 * For LVDS just rely on its current settings for dual-channel.
651 * We haven't figured out how to reliably set up different
652 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800653 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100654 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800655 clock.p2 = limit->p2.p2_fast;
656 else
657 clock.p2 = limit->p2.p2_slow;
658 } else {
659 if (target < limit->p2.dot_limit)
660 clock.p2 = limit->p2.p2_slow;
661 else
662 clock.p2 = limit->p2.p2_fast;
663 }
664
Akshay Joshi0206e352011-08-16 15:34:10 -0400665 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800666
Zhao Yakui42158662009-11-20 11:24:18 +0800667 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
668 clock.m1++) {
669 for (clock.m2 = limit->m2.min;
670 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200671 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800672 break;
673 for (clock.n = limit->n.min;
674 clock.n <= limit->n.max; clock.n++) {
675 for (clock.p1 = limit->p1.min;
676 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800677 int this_err;
678
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200679 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000680 if (!intel_PLL_is_valid(dev, limit,
681 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800682 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800683 if (match_clock &&
684 clock.p != match_clock->p)
685 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800686
687 this_err = abs(clock.dot - target);
688 if (this_err < err) {
689 *best_clock = clock;
690 err = this_err;
691 }
692 }
693 }
694 }
695 }
696
697 return (err != target);
698}
699
Ma Lingd4906092009-03-18 20:13:27 +0800700static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200701pnv_find_best_dpll(const intel_limit_t *limit,
702 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200703 int target, int refclk, intel_clock_t *match_clock,
704 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200705{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200706 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300707 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200708 intel_clock_t clock;
709 int err = target;
710
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200711 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200712 /*
713 * For LVDS just rely on its current settings for dual-channel.
714 * We haven't figured out how to reliably set up different
715 * single/dual channel state, if we even can.
716 */
717 if (intel_is_dual_link_lvds(dev))
718 clock.p2 = limit->p2.p2_fast;
719 else
720 clock.p2 = limit->p2.p2_slow;
721 } else {
722 if (target < limit->p2.dot_limit)
723 clock.p2 = limit->p2.p2_slow;
724 else
725 clock.p2 = limit->p2.p2_fast;
726 }
727
728 memset(best_clock, 0, sizeof(*best_clock));
729
730 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
731 clock.m1++) {
732 for (clock.m2 = limit->m2.min;
733 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200734 for (clock.n = limit->n.min;
735 clock.n <= limit->n.max; clock.n++) {
736 for (clock.p1 = limit->p1.min;
737 clock.p1 <= limit->p1.max; clock.p1++) {
738 int this_err;
739
740 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800741 if (!intel_PLL_is_valid(dev, limit,
742 &clock))
743 continue;
744 if (match_clock &&
745 clock.p != match_clock->p)
746 continue;
747
748 this_err = abs(clock.dot - target);
749 if (this_err < err) {
750 *best_clock = clock;
751 err = this_err;
752 }
753 }
754 }
755 }
756 }
757
758 return (err != target);
759}
760
Ma Lingd4906092009-03-18 20:13:27 +0800761static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200762g4x_find_best_dpll(const intel_limit_t *limit,
763 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200764 int target, int refclk, intel_clock_t *match_clock,
765 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800766{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200767 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300768 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800769 intel_clock_t clock;
770 int max_n;
771 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400772 /* approximately equals target * 0.00585 */
773 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800774 found = false;
775
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200776 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100777 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800778 clock.p2 = limit->p2.p2_fast;
779 else
780 clock.p2 = limit->p2.p2_slow;
781 } else {
782 if (target < limit->p2.dot_limit)
783 clock.p2 = limit->p2.p2_slow;
784 else
785 clock.p2 = limit->p2.p2_fast;
786 }
787
788 memset(best_clock, 0, sizeof(*best_clock));
789 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200790 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800791 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200792 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800793 for (clock.m1 = limit->m1.max;
794 clock.m1 >= limit->m1.min; clock.m1--) {
795 for (clock.m2 = limit->m2.max;
796 clock.m2 >= limit->m2.min; clock.m2--) {
797 for (clock.p1 = limit->p1.max;
798 clock.p1 >= limit->p1.min; clock.p1--) {
799 int this_err;
800
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200801 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000802 if (!intel_PLL_is_valid(dev, limit,
803 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800804 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000805
806 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800807 if (this_err < err_most) {
808 *best_clock = clock;
809 err_most = this_err;
810 max_n = clock.n;
811 found = true;
812 }
813 }
814 }
815 }
816 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800817 return found;
818}
Ma Lingd4906092009-03-18 20:13:27 +0800819
Imre Deakd5dd62b2015-03-17 11:40:03 +0200820/*
821 * Check if the calculated PLL configuration is more optimal compared to the
822 * best configuration and error found so far. Return the calculated error.
823 */
824static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
825 const intel_clock_t *calculated_clock,
826 const intel_clock_t *best_clock,
827 unsigned int best_error_ppm,
828 unsigned int *error_ppm)
829{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200830 /*
831 * For CHV ignore the error and consider only the P value.
832 * Prefer a bigger P value based on HW requirements.
833 */
834 if (IS_CHERRYVIEW(dev)) {
835 *error_ppm = 0;
836
837 return calculated_clock->p > best_clock->p;
838 }
839
Imre Deak24be4e42015-03-17 11:40:04 +0200840 if (WARN_ON_ONCE(!target_freq))
841 return false;
842
Imre Deakd5dd62b2015-03-17 11:40:03 +0200843 *error_ppm = div_u64(1000000ULL *
844 abs(target_freq - calculated_clock->dot),
845 target_freq);
846 /*
847 * Prefer a better P value over a better (smaller) error if the error
848 * is small. Ensure this preference for future configurations too by
849 * setting the error to 0.
850 */
851 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
852 *error_ppm = 0;
853
854 return true;
855 }
856
857 return *error_ppm + 10 < best_error_ppm;
858}
859
Zhenyu Wang2c072452009-06-05 15:38:42 +0800860static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200861vlv_find_best_dpll(const intel_limit_t *limit,
862 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200863 int target, int refclk, intel_clock_t *match_clock,
864 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700865{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200866 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300867 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300868 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300869 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300870 /* min update 19.2 MHz */
871 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300872 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700873
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300874 target *= 5; /* fast clock */
875
876 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700877
878 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300879 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300880 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300881 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300882 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300883 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700884 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300885 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200886 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300887
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300888 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
889 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300890
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300891 vlv_clock(refclk, &clock);
892
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300893 if (!intel_PLL_is_valid(dev, limit,
894 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300895 continue;
896
Imre Deakd5dd62b2015-03-17 11:40:03 +0200897 if (!vlv_PLL_is_optimal(dev, target,
898 &clock,
899 best_clock,
900 bestppm, &ppm))
901 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300902
Imre Deakd5dd62b2015-03-17 11:40:03 +0200903 *best_clock = clock;
904 bestppm = ppm;
905 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700906 }
907 }
908 }
909 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700910
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300911 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700912}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700913
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300914static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200915chv_find_best_dpll(const intel_limit_t *limit,
916 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300917 int target, int refclk, intel_clock_t *match_clock,
918 intel_clock_t *best_clock)
919{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200920 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300921 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200922 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300923 intel_clock_t clock;
924 uint64_t m2;
925 int found = false;
926
927 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200928 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300929
930 /*
931 * Based on hardware doc, the n always set to 1, and m1 always
932 * set to 2. If requires to support 200Mhz refclk, we need to
933 * revisit this because n may not 1 anymore.
934 */
935 clock.n = 1, clock.m1 = 2;
936 target *= 5; /* fast clock */
937
938 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
939 for (clock.p2 = limit->p2.p2_fast;
940 clock.p2 >= limit->p2.p2_slow;
941 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200942 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300943
944 clock.p = clock.p1 * clock.p2;
945
946 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
947 clock.n) << 22, refclk * clock.m1);
948
949 if (m2 > INT_MAX/clock.m1)
950 continue;
951
952 clock.m2 = m2;
953
954 chv_clock(refclk, &clock);
955
956 if (!intel_PLL_is_valid(dev, limit, &clock))
957 continue;
958
Imre Deak9ca3ba02015-03-17 11:40:05 +0200959 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
960 best_error_ppm, &error_ppm))
961 continue;
962
963 *best_clock = clock;
964 best_error_ppm = error_ppm;
965 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300966 }
967 }
968
969 return found;
970}
971
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200972bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
973 intel_clock_t *best_clock)
974{
975 int refclk = i9xx_get_refclk(crtc_state, 0);
976
977 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
978 target_clock, refclk, NULL, best_clock);
979}
980
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300981bool intel_crtc_active(struct drm_crtc *crtc)
982{
983 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
984
985 /* Be paranoid as we can arrive here with only partial
986 * state retrieved from the hardware during setup.
987 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100988 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300989 * as Haswell has gained clock readout/fastboot support.
990 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000991 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300992 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700993 *
994 * FIXME: The intel_crtc->active here should be switched to
995 * crtc->state->active once we have proper CRTC states wired up
996 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300997 */
Matt Roperc3d1f432015-03-09 10:19:23 -0700998 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200999 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001000}
1001
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001002enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1003 enum pipe pipe)
1004{
1005 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1006 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1007
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001008 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001009}
1010
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001011static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1012{
1013 struct drm_i915_private *dev_priv = dev->dev_private;
1014 u32 reg = PIPEDSL(pipe);
1015 u32 line1, line2;
1016 u32 line_mask;
1017
1018 if (IS_GEN2(dev))
1019 line_mask = DSL_LINEMASK_GEN2;
1020 else
1021 line_mask = DSL_LINEMASK_GEN3;
1022
1023 line1 = I915_READ(reg) & line_mask;
1024 mdelay(5);
1025 line2 = I915_READ(reg) & line_mask;
1026
1027 return line1 == line2;
1028}
1029
Keith Packardab7ad7f2010-10-03 00:33:06 -07001030/*
1031 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001032 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001033 *
1034 * After disabling a pipe, we can't wait for vblank in the usual way,
1035 * spinning on the vblank interrupt status bit, since we won't actually
1036 * see an interrupt when the pipe is disabled.
1037 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001038 * On Gen4 and above:
1039 * wait for the pipe register state bit to turn off
1040 *
1041 * Otherwise:
1042 * wait for the display line value to settle (it usually
1043 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001044 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001045 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001046static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001047{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001048 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001049 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001050 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001051 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001052
Keith Packardab7ad7f2010-10-03 00:33:06 -07001053 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001054 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001055
Keith Packardab7ad7f2010-10-03 00:33:06 -07001056 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001057 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1058 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001059 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001060 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001061 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001062 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001063 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001064 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001065}
1066
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001067/*
1068 * ibx_digital_port_connected - is the specified port connected?
1069 * @dev_priv: i915 private structure
1070 * @port: the port to test
1071 *
1072 * Returns true if @port is connected, false otherwise.
1073 */
1074bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1075 struct intel_digital_port *port)
1076{
1077 u32 bit;
1078
Damien Lespiauc36346e2012-12-13 16:09:03 +00001079 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001080 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001081 case PORT_B:
1082 bit = SDE_PORTB_HOTPLUG;
1083 break;
1084 case PORT_C:
1085 bit = SDE_PORTC_HOTPLUG;
1086 break;
1087 case PORT_D:
1088 bit = SDE_PORTD_HOTPLUG;
1089 break;
1090 default:
1091 return true;
1092 }
1093 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001094 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001095 case PORT_B:
1096 bit = SDE_PORTB_HOTPLUG_CPT;
1097 break;
1098 case PORT_C:
1099 bit = SDE_PORTC_HOTPLUG_CPT;
1100 break;
1101 case PORT_D:
1102 bit = SDE_PORTD_HOTPLUG_CPT;
1103 break;
1104 default:
1105 return true;
1106 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001107 }
1108
1109 return I915_READ(SDEISR) & bit;
1110}
1111
Jesse Barnesb24e7172011-01-04 15:09:30 -08001112static const char *state_string(bool enabled)
1113{
1114 return enabled ? "on" : "off";
1115}
1116
1117/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001118void assert_pll(struct drm_i915_private *dev_priv,
1119 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001120{
1121 int reg;
1122 u32 val;
1123 bool cur_state;
1124
1125 reg = DPLL(pipe);
1126 val = I915_READ(reg);
1127 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001128 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001129 "PLL state assertion failure (expected %s, current %s)\n",
1130 state_string(state), state_string(cur_state));
1131}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001132
Jani Nikula23538ef2013-08-27 15:12:22 +03001133/* XXX: the dsi pll is shared between MIPI DSI ports */
1134static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1135{
1136 u32 val;
1137 bool cur_state;
1138
1139 mutex_lock(&dev_priv->dpio_lock);
1140 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1141 mutex_unlock(&dev_priv->dpio_lock);
1142
1143 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001144 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001145 "DSI PLL state assertion failure (expected %s, current %s)\n",
1146 state_string(state), state_string(cur_state));
1147}
1148#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1149#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1150
Daniel Vetter55607e82013-06-16 21:42:39 +02001151struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001152intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001153{
Daniel Vettere2b78262013-06-07 23:10:03 +02001154 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1155
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001156 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001157 return NULL;
1158
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001159 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001160}
1161
Jesse Barnesb24e7172011-01-04 15:09:30 -08001162/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001163void assert_shared_dpll(struct drm_i915_private *dev_priv,
1164 struct intel_shared_dpll *pll,
1165 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001166{
Jesse Barnes040484a2011-01-03 12:14:26 -08001167 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001168 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001169
Chris Wilson92b27b02012-05-20 18:10:50 +01001170 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001171 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001172 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001173
Daniel Vetter53589012013-06-05 13:34:16 +02001174 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001175 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001176 "%s assertion failure (expected %s, current %s)\n",
1177 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001178}
Jesse Barnes040484a2011-01-03 12:14:26 -08001179
1180static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1181 enum pipe pipe, bool state)
1182{
1183 int reg;
1184 u32 val;
1185 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001186 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1187 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001188
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001189 if (HAS_DDI(dev_priv->dev)) {
1190 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001191 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001192 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001193 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001194 } else {
1195 reg = FDI_TX_CTL(pipe);
1196 val = I915_READ(reg);
1197 cur_state = !!(val & FDI_TX_ENABLE);
1198 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001199 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001200 "FDI TX state assertion failure (expected %s, current %s)\n",
1201 state_string(state), state_string(cur_state));
1202}
1203#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1204#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1205
1206static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1207 enum pipe pipe, bool state)
1208{
1209 int reg;
1210 u32 val;
1211 bool cur_state;
1212
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001213 reg = FDI_RX_CTL(pipe);
1214 val = I915_READ(reg);
1215 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001216 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001217 "FDI RX state assertion failure (expected %s, current %s)\n",
1218 state_string(state), state_string(cur_state));
1219}
1220#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1221#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1222
1223static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1224 enum pipe pipe)
1225{
1226 int reg;
1227 u32 val;
1228
1229 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001230 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001231 return;
1232
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001233 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001234 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001235 return;
1236
Jesse Barnes040484a2011-01-03 12:14:26 -08001237 reg = FDI_TX_CTL(pipe);
1238 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001239 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 -08001240}
1241
Daniel Vetter55607e82013-06-16 21:42:39 +02001242void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1243 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001244{
1245 int reg;
1246 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001247 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001248
1249 reg = FDI_RX_CTL(pipe);
1250 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001251 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001252 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001253 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1254 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001255}
1256
Daniel Vetterb680c372014-09-19 18:27:27 +02001257void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1258 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001259{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001260 struct drm_device *dev = dev_priv->dev;
1261 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001262 u32 val;
1263 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001264 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001265
Jani Nikulabedd4db2014-08-22 15:04:13 +03001266 if (WARN_ON(HAS_DDI(dev)))
1267 return;
1268
1269 if (HAS_PCH_SPLIT(dev)) {
1270 u32 port_sel;
1271
Jesse Barnesea0760c2011-01-04 15:09:32 -08001272 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001273 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1274
1275 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1276 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1277 panel_pipe = PIPE_B;
1278 /* XXX: else fix for eDP */
1279 } else if (IS_VALLEYVIEW(dev)) {
1280 /* presumably write lock depends on pipe, not port select */
1281 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1282 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001283 } else {
1284 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001285 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1286 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001287 }
1288
1289 val = I915_READ(pp_reg);
1290 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001291 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001292 locked = false;
1293
Rob Clarke2c719b2014-12-15 13:56:32 -05001294 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001295 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001296 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001297}
1298
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001299static void assert_cursor(struct drm_i915_private *dev_priv,
1300 enum pipe pipe, bool state)
1301{
1302 struct drm_device *dev = dev_priv->dev;
1303 bool cur_state;
1304
Paulo Zanonid9d82082014-02-27 16:30:56 -03001305 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001306 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001307 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001308 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001309
Rob Clarke2c719b2014-12-15 13:56:32 -05001310 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001311 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1312 pipe_name(pipe), state_string(state), state_string(cur_state));
1313}
1314#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1315#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1316
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001317void assert_pipe(struct drm_i915_private *dev_priv,
1318 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001319{
1320 int reg;
1321 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001322 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001323 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1324 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001325
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001326 /* if we need the pipe quirk it must be always on */
1327 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1328 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001329 state = true;
1330
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001331 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001332 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001333 cur_state = false;
1334 } else {
1335 reg = PIPECONF(cpu_transcoder);
1336 val = I915_READ(reg);
1337 cur_state = !!(val & PIPECONF_ENABLE);
1338 }
1339
Rob Clarke2c719b2014-12-15 13:56:32 -05001340 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001341 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001342 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001343}
1344
Chris Wilson931872f2012-01-16 23:01:13 +00001345static void assert_plane(struct drm_i915_private *dev_priv,
1346 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001347{
1348 int reg;
1349 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001350 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001351
1352 reg = DSPCNTR(plane);
1353 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001354 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001355 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001356 "plane %c assertion failure (expected %s, current %s)\n",
1357 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001358}
1359
Chris Wilson931872f2012-01-16 23:01:13 +00001360#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1361#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1362
Jesse Barnesb24e7172011-01-04 15:09:30 -08001363static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1364 enum pipe pipe)
1365{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001366 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001367 int reg, i;
1368 u32 val;
1369 int cur_pipe;
1370
Ville Syrjälä653e1022013-06-04 13:49:05 +03001371 /* Primary planes are fixed to pipes on gen4+ */
1372 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001373 reg = DSPCNTR(pipe);
1374 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001375 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001376 "plane %c assertion failure, should be disabled but not\n",
1377 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001378 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001379 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001380
Jesse Barnesb24e7172011-01-04 15:09:30 -08001381 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001382 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001383 reg = DSPCNTR(i);
1384 val = I915_READ(reg);
1385 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1386 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001387 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001388 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1389 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001390 }
1391}
1392
Jesse Barnes19332d72013-03-28 09:55:38 -07001393static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1394 enum pipe pipe)
1395{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001396 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001397 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001398 u32 val;
1399
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001400 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001401 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001402 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001403 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001404 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1405 sprite, pipe_name(pipe));
1406 }
1407 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001408 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001409 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001410 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001411 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001412 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001413 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001414 }
1415 } else if (INTEL_INFO(dev)->gen >= 7) {
1416 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001417 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001418 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001419 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001420 plane_name(pipe), pipe_name(pipe));
1421 } else if (INTEL_INFO(dev)->gen >= 5) {
1422 reg = DVSCNTR(pipe);
1423 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001424 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001425 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1426 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001427 }
1428}
1429
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001430static void assert_vblank_disabled(struct drm_crtc *crtc)
1431{
Rob Clarke2c719b2014-12-15 13:56:32 -05001432 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001433 drm_crtc_vblank_put(crtc);
1434}
1435
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001436static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001437{
1438 u32 val;
1439 bool enabled;
1440
Rob Clarke2c719b2014-12-15 13:56:32 -05001441 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001442
Jesse Barnes92f25842011-01-04 15:09:34 -08001443 val = I915_READ(PCH_DREF_CONTROL);
1444 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1445 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001446 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001447}
1448
Daniel Vetterab9412b2013-05-03 11:49:46 +02001449static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1450 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001451{
1452 int reg;
1453 u32 val;
1454 bool enabled;
1455
Daniel Vetterab9412b2013-05-03 11:49:46 +02001456 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001457 val = I915_READ(reg);
1458 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001459 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001460 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1461 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001462}
1463
Keith Packard4e634382011-08-06 10:39:45 -07001464static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1465 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001466{
1467 if ((val & DP_PORT_EN) == 0)
1468 return false;
1469
1470 if (HAS_PCH_CPT(dev_priv->dev)) {
1471 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1472 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1473 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1474 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001475 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1476 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1477 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001478 } else {
1479 if ((val & DP_PIPE_MASK) != (pipe << 30))
1480 return false;
1481 }
1482 return true;
1483}
1484
Keith Packard1519b992011-08-06 10:35:34 -07001485static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1486 enum pipe pipe, u32 val)
1487{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001488 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001489 return false;
1490
1491 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001492 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001493 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001494 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1495 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1496 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001497 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001498 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001499 return false;
1500 }
1501 return true;
1502}
1503
1504static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1505 enum pipe pipe, u32 val)
1506{
1507 if ((val & LVDS_PORT_EN) == 0)
1508 return false;
1509
1510 if (HAS_PCH_CPT(dev_priv->dev)) {
1511 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1512 return false;
1513 } else {
1514 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1515 return false;
1516 }
1517 return true;
1518}
1519
1520static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1521 enum pipe pipe, u32 val)
1522{
1523 if ((val & ADPA_DAC_ENABLE) == 0)
1524 return false;
1525 if (HAS_PCH_CPT(dev_priv->dev)) {
1526 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1527 return false;
1528 } else {
1529 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1530 return false;
1531 }
1532 return true;
1533}
1534
Jesse Barnes291906f2011-02-02 12:28:03 -08001535static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001536 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001537{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001538 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001539 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001540 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001541 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001542
Rob Clarke2c719b2014-12-15 13:56:32 -05001543 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001544 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001545 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001546}
1547
1548static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1549 enum pipe pipe, int reg)
1550{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001551 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001552 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001553 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001554 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001555
Rob Clarke2c719b2014-12-15 13:56:32 -05001556 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001557 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001558 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001559}
1560
1561static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1562 enum pipe pipe)
1563{
1564 int reg;
1565 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001566
Keith Packardf0575e92011-07-25 22:12:43 -07001567 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1568 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1569 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001570
1571 reg = PCH_ADPA;
1572 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001573 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001574 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001575 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001576
1577 reg = PCH_LVDS;
1578 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001579 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001580 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001581 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001582
Paulo Zanonie2debe92013-02-18 19:00:27 -03001583 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1584 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1585 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001586}
1587
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001588static void intel_init_dpio(struct drm_device *dev)
1589{
1590 struct drm_i915_private *dev_priv = dev->dev_private;
1591
1592 if (!IS_VALLEYVIEW(dev))
1593 return;
1594
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001595 /*
1596 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1597 * CHV x1 PHY (DP/HDMI D)
1598 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1599 */
1600 if (IS_CHERRYVIEW(dev)) {
1601 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1602 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1603 } else {
1604 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1605 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001606}
1607
Ville Syrjäläd288f652014-10-28 13:20:22 +02001608static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001609 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001610{
Daniel Vetter426115c2013-07-11 22:13:42 +02001611 struct drm_device *dev = crtc->base.dev;
1612 struct drm_i915_private *dev_priv = dev->dev_private;
1613 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001614 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001615
Daniel Vetter426115c2013-07-11 22:13:42 +02001616 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001617
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001618 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001619 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1620
1621 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001622 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001623 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001624
Daniel Vetter426115c2013-07-11 22:13:42 +02001625 I915_WRITE(reg, dpll);
1626 POSTING_READ(reg);
1627 udelay(150);
1628
1629 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1630 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1631
Ville Syrjäläd288f652014-10-28 13:20:22 +02001632 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001633 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001634
1635 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001636 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001637 POSTING_READ(reg);
1638 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001639 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001640 POSTING_READ(reg);
1641 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001642 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001643 POSTING_READ(reg);
1644 udelay(150); /* wait for warmup */
1645}
1646
Ville Syrjäläd288f652014-10-28 13:20:22 +02001647static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001648 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001649{
1650 struct drm_device *dev = crtc->base.dev;
1651 struct drm_i915_private *dev_priv = dev->dev_private;
1652 int pipe = crtc->pipe;
1653 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001654 u32 tmp;
1655
1656 assert_pipe_disabled(dev_priv, crtc->pipe);
1657
1658 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1659
1660 mutex_lock(&dev_priv->dpio_lock);
1661
1662 /* Enable back the 10bit clock to display controller */
1663 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1664 tmp |= DPIO_DCLKP_EN;
1665 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1666
1667 /*
1668 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1669 */
1670 udelay(1);
1671
1672 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001673 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001674
1675 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001676 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001677 DRM_ERROR("PLL %d failed to lock\n", pipe);
1678
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001679 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001680 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001681 POSTING_READ(DPLL_MD(pipe));
1682
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001683 mutex_unlock(&dev_priv->dpio_lock);
1684}
1685
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001686static int intel_num_dvo_pipes(struct drm_device *dev)
1687{
1688 struct intel_crtc *crtc;
1689 int count = 0;
1690
1691 for_each_intel_crtc(dev, crtc)
1692 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001693 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001694
1695 return count;
1696}
1697
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001698static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001699{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001700 struct drm_device *dev = crtc->base.dev;
1701 struct drm_i915_private *dev_priv = dev->dev_private;
1702 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001703 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001704
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001705 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001706
1707 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001708 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001709
1710 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001711 if (IS_MOBILE(dev) && !IS_I830(dev))
1712 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001713
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001714 /* Enable DVO 2x clock on both PLLs if necessary */
1715 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1716 /*
1717 * It appears to be important that we don't enable this
1718 * for the current pipe before otherwise configuring the
1719 * PLL. No idea how this should be handled if multiple
1720 * DVO outputs are enabled simultaneosly.
1721 */
1722 dpll |= DPLL_DVO_2X_MODE;
1723 I915_WRITE(DPLL(!crtc->pipe),
1724 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1725 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001726
1727 /* Wait for the clocks to stabilize. */
1728 POSTING_READ(reg);
1729 udelay(150);
1730
1731 if (INTEL_INFO(dev)->gen >= 4) {
1732 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001733 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001734 } else {
1735 /* The pixel multiplier can only be updated once the
1736 * DPLL is enabled and the clocks are stable.
1737 *
1738 * So write it again.
1739 */
1740 I915_WRITE(reg, dpll);
1741 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001742
1743 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001744 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001745 POSTING_READ(reg);
1746 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001747 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001748 POSTING_READ(reg);
1749 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001750 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001751 POSTING_READ(reg);
1752 udelay(150); /* wait for warmup */
1753}
1754
1755/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001756 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001757 * @dev_priv: i915 private structure
1758 * @pipe: pipe PLL to disable
1759 *
1760 * Disable the PLL for @pipe, making sure the pipe is off first.
1761 *
1762 * Note! This is for pre-ILK only.
1763 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001764static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001765{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001766 struct drm_device *dev = crtc->base.dev;
1767 struct drm_i915_private *dev_priv = dev->dev_private;
1768 enum pipe pipe = crtc->pipe;
1769
1770 /* Disable DVO 2x clock on both PLLs if necessary */
1771 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001772 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001773 intel_num_dvo_pipes(dev) == 1) {
1774 I915_WRITE(DPLL(PIPE_B),
1775 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1776 I915_WRITE(DPLL(PIPE_A),
1777 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1778 }
1779
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001780 /* Don't disable pipe or pipe PLLs if needed */
1781 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1782 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001783 return;
1784
1785 /* Make sure the pipe isn't still relying on us */
1786 assert_pipe_disabled(dev_priv, pipe);
1787
Daniel Vetter50b44a42013-06-05 13:34:33 +02001788 I915_WRITE(DPLL(pipe), 0);
1789 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001790}
1791
Jesse Barnesf6071162013-10-01 10:41:38 -07001792static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1793{
1794 u32 val = 0;
1795
1796 /* Make sure the pipe isn't still relying on us */
1797 assert_pipe_disabled(dev_priv, pipe);
1798
Imre Deake5cbfbf2014-01-09 17:08:16 +02001799 /*
1800 * Leave integrated clock source and reference clock enabled for pipe B.
1801 * The latter is needed for VGA hotplug / manual detection.
1802 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001803 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001804 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001805 I915_WRITE(DPLL(pipe), val);
1806 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001807
1808}
1809
1810static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1811{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001812 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001813 u32 val;
1814
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001815 /* Make sure the pipe isn't still relying on us */
1816 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001817
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001818 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001819 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001820 if (pipe != PIPE_A)
1821 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1822 I915_WRITE(DPLL(pipe), val);
1823 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001824
1825 mutex_lock(&dev_priv->dpio_lock);
1826
1827 /* Disable 10bit clock to display controller */
1828 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1829 val &= ~DPIO_DCLKP_EN;
1830 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1831
Ville Syrjälä61407f62014-05-27 16:32:55 +03001832 /* disable left/right clock distribution */
1833 if (pipe != PIPE_B) {
1834 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1835 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1836 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1837 } else {
1838 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1839 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1840 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1841 }
1842
Ville Syrjäläd7520482014-04-09 13:28:59 +03001843 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001844}
1845
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001846void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1847 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001848{
1849 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001850 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001851
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001852 switch (dport->port) {
1853 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001854 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001855 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001856 break;
1857 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001858 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001859 dpll_reg = DPLL(0);
1860 break;
1861 case PORT_D:
1862 port_mask = DPLL_PORTD_READY_MASK;
1863 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001864 break;
1865 default:
1866 BUG();
1867 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001868
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001869 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
Jesse Barnes89b667f2013-04-18 14:51:36 -07001870 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001871 port_name(dport->port), I915_READ(dpll_reg));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001872}
1873
Daniel Vetterb14b1052014-04-24 23:55:13 +02001874static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1875{
1876 struct drm_device *dev = crtc->base.dev;
1877 struct drm_i915_private *dev_priv = dev->dev_private;
1878 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1879
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001880 if (WARN_ON(pll == NULL))
1881 return;
1882
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001883 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001884 if (pll->active == 0) {
1885 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1886 WARN_ON(pll->on);
1887 assert_shared_dpll_disabled(dev_priv, pll);
1888
1889 pll->mode_set(dev_priv, pll);
1890 }
1891}
1892
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001893/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001894 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001895 * @dev_priv: i915 private structure
1896 * @pipe: pipe PLL to enable
1897 *
1898 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1899 * drives the transcoder clock.
1900 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001901static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001902{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001903 struct drm_device *dev = crtc->base.dev;
1904 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001905 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001906
Daniel Vetter87a875b2013-06-05 13:34:19 +02001907 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001908 return;
1909
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001910 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001911 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001912
Damien Lespiau74dd6922014-07-29 18:06:17 +01001913 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001914 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001915 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001916
Daniel Vettercdbd2312013-06-05 13:34:03 +02001917 if (pll->active++) {
1918 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001919 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001920 return;
1921 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001922 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001923
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001924 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1925
Daniel Vetter46edb022013-06-05 13:34:12 +02001926 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001927 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001928 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001929}
1930
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001931static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001932{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001933 struct drm_device *dev = crtc->base.dev;
1934 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001935 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001936
Jesse Barnes92f25842011-01-04 15:09:34 -08001937 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001938 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001939 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001940 return;
1941
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001942 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001943 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001944
Daniel Vetter46edb022013-06-05 13:34:12 +02001945 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1946 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001947 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001948
Chris Wilson48da64a2012-05-13 20:16:12 +01001949 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001950 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001951 return;
1952 }
1953
Daniel Vettere9d69442013-06-05 13:34:15 +02001954 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001955 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001956 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001957 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001958
Daniel Vetter46edb022013-06-05 13:34:12 +02001959 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001960 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001961 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001962
1963 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001964}
1965
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001966static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1967 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001968{
Daniel Vetter23670b322012-11-01 09:15:30 +01001969 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001970 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001971 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001972 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001973
1974 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001975 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001976
1977 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001978 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001979 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001980
1981 /* FDI must be feeding us bits for PCH ports */
1982 assert_fdi_tx_enabled(dev_priv, pipe);
1983 assert_fdi_rx_enabled(dev_priv, pipe);
1984
Daniel Vetter23670b322012-11-01 09:15:30 +01001985 if (HAS_PCH_CPT(dev)) {
1986 /* Workaround: Set the timing override bit before enabling the
1987 * pch transcoder. */
1988 reg = TRANS_CHICKEN2(pipe);
1989 val = I915_READ(reg);
1990 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1991 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001992 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001993
Daniel Vetterab9412b2013-05-03 11:49:46 +02001994 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001995 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001996 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001997
1998 if (HAS_PCH_IBX(dev_priv->dev)) {
1999 /*
2000 * make the BPC in transcoder be consistent with
2001 * that in pipeconf reg.
2002 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002003 val &= ~PIPECONF_BPC_MASK;
2004 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002005 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002006
2007 val &= ~TRANS_INTERLACE_MASK;
2008 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002009 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002010 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002011 val |= TRANS_LEGACY_INTERLACED_ILK;
2012 else
2013 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002014 else
2015 val |= TRANS_PROGRESSIVE;
2016
Jesse Barnes040484a2011-01-03 12:14:26 -08002017 I915_WRITE(reg, val | TRANS_ENABLE);
2018 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002019 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002020}
2021
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002022static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002023 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002024{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002025 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002026
2027 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002028 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002029
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002030 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002031 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002032 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002033
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002034 /* Workaround: set timing override bit. */
2035 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002036 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002037 I915_WRITE(_TRANSA_CHICKEN2, val);
2038
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002039 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002040 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002041
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002042 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2043 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002044 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002045 else
2046 val |= TRANS_PROGRESSIVE;
2047
Daniel Vetterab9412b2013-05-03 11:49:46 +02002048 I915_WRITE(LPT_TRANSCONF, val);
2049 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002050 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002051}
2052
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002053static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2054 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002055{
Daniel Vetter23670b322012-11-01 09:15:30 +01002056 struct drm_device *dev = dev_priv->dev;
2057 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002058
2059 /* FDI relies on the transcoder */
2060 assert_fdi_tx_disabled(dev_priv, pipe);
2061 assert_fdi_rx_disabled(dev_priv, pipe);
2062
Jesse Barnes291906f2011-02-02 12:28:03 -08002063 /* Ports must be off as well */
2064 assert_pch_ports_disabled(dev_priv, pipe);
2065
Daniel Vetterab9412b2013-05-03 11:49:46 +02002066 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002067 val = I915_READ(reg);
2068 val &= ~TRANS_ENABLE;
2069 I915_WRITE(reg, val);
2070 /* wait for PCH transcoder off, transcoder state */
2071 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002072 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002073
2074 if (!HAS_PCH_IBX(dev)) {
2075 /* Workaround: Clear the timing override chicken bit again. */
2076 reg = TRANS_CHICKEN2(pipe);
2077 val = I915_READ(reg);
2078 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2079 I915_WRITE(reg, val);
2080 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002081}
2082
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002083static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002084{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002085 u32 val;
2086
Daniel Vetterab9412b2013-05-03 11:49:46 +02002087 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002088 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002089 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002090 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002091 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002092 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002093
2094 /* Workaround: clear timing override bit. */
2095 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002096 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002097 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002098}
2099
2100/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002101 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002102 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002103 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002104 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002105 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002106 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002107static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002108{
Paulo Zanoni03722642014-01-17 13:51:09 -02002109 struct drm_device *dev = crtc->base.dev;
2110 struct drm_i915_private *dev_priv = dev->dev_private;
2111 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002112 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2113 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002114 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002115 int reg;
2116 u32 val;
2117
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002118 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002119 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002120 assert_sprites_disabled(dev_priv, pipe);
2121
Paulo Zanoni681e5812012-12-06 11:12:38 -02002122 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002123 pch_transcoder = TRANSCODER_A;
2124 else
2125 pch_transcoder = pipe;
2126
Jesse Barnesb24e7172011-01-04 15:09:30 -08002127 /*
2128 * A pipe without a PLL won't actually be able to drive bits from
2129 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2130 * need the check.
2131 */
Imre Deak50360402015-01-16 00:55:16 -08002132 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002133 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002134 assert_dsi_pll_enabled(dev_priv);
2135 else
2136 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002137 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002138 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002139 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002140 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002141 assert_fdi_tx_pll_enabled(dev_priv,
2142 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002143 }
2144 /* FIXME: assert CPU port conditions for SNB+ */
2145 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002146
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002147 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002148 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002149 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002150 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2151 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002152 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002153 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002154
2155 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002156 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002157}
2158
2159/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002160 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002161 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002162 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002163 * Disable the pipe of @crtc, making sure that various hardware
2164 * specific requirements are met, if applicable, e.g. plane
2165 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002166 *
2167 * Will wait until the pipe has shut down before returning.
2168 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002169static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002170{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002171 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002172 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002173 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002174 int reg;
2175 u32 val;
2176
2177 /*
2178 * Make sure planes won't keep trying to pump pixels to us,
2179 * or we might hang the display.
2180 */
2181 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002182 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002183 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002184
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002185 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002186 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002187 if ((val & PIPECONF_ENABLE) == 0)
2188 return;
2189
Ville Syrjälä67adc642014-08-15 01:21:57 +03002190 /*
2191 * Double wide has implications for planes
2192 * so best keep it disabled when not needed.
2193 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002194 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002195 val &= ~PIPECONF_DOUBLE_WIDE;
2196
2197 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002198 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2199 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002200 val &= ~PIPECONF_ENABLE;
2201
2202 I915_WRITE(reg, val);
2203 if ((val & PIPECONF_ENABLE) == 0)
2204 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002205}
2206
Keith Packardd74362c2011-07-28 14:47:14 -07002207/*
2208 * Plane regs are double buffered, going from enabled->disabled needs a
2209 * trigger in order to latch. The display address reg provides this.
2210 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002211void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2212 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002213{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002214 struct drm_device *dev = dev_priv->dev;
2215 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002216
2217 I915_WRITE(reg, I915_READ(reg));
2218 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002219}
2220
Jesse Barnesb24e7172011-01-04 15:09:30 -08002221/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002222 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002223 * @plane: plane to be enabled
2224 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002225 *
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002226 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002227 */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002228static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2229 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002230{
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002231 struct drm_device *dev = plane->dev;
2232 struct drm_i915_private *dev_priv = dev->dev_private;
2233 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002234
2235 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002236 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002237 to_intel_plane_state(plane->state)->visible = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002238
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002239 dev_priv->display.update_primary_plane(crtc, plane->fb,
2240 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002241}
2242
Chris Wilson693db182013-03-05 14:52:39 +00002243static bool need_vtd_wa(struct drm_device *dev)
2244{
2245#ifdef CONFIG_INTEL_IOMMU
2246 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2247 return true;
2248#endif
2249 return false;
2250}
2251
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002252unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002253intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2254 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002255{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002256 unsigned int tile_height;
2257 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002258
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002259 switch (fb_format_modifier) {
2260 case DRM_FORMAT_MOD_NONE:
2261 tile_height = 1;
2262 break;
2263 case I915_FORMAT_MOD_X_TILED:
2264 tile_height = IS_GEN2(dev) ? 16 : 8;
2265 break;
2266 case I915_FORMAT_MOD_Y_TILED:
2267 tile_height = 32;
2268 break;
2269 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002270 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2271 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002272 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002273 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002274 tile_height = 64;
2275 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002276 case 2:
2277 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002278 tile_height = 32;
2279 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002280 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002281 tile_height = 16;
2282 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002283 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002284 WARN_ONCE(1,
2285 "128-bit pixels are not supported for display!");
2286 tile_height = 16;
2287 break;
2288 }
2289 break;
2290 default:
2291 MISSING_CASE(fb_format_modifier);
2292 tile_height = 1;
2293 break;
2294 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002295
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002296 return tile_height;
2297}
2298
2299unsigned int
2300intel_fb_align_height(struct drm_device *dev, unsigned int height,
2301 uint32_t pixel_format, uint64_t fb_format_modifier)
2302{
2303 return ALIGN(height, intel_tile_height(dev, pixel_format,
2304 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002305}
2306
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002307static int
2308intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2309 const struct drm_plane_state *plane_state)
2310{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002311 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002312
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002313 *view = i915_ggtt_view_normal;
2314
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002315 if (!plane_state)
2316 return 0;
2317
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002318 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002319 return 0;
2320
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002321 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002322
2323 info->height = fb->height;
2324 info->pixel_format = fb->pixel_format;
2325 info->pitch = fb->pitches[0];
2326 info->fb_modifier = fb->modifier[0];
2327
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002328 return 0;
2329}
2330
Chris Wilson127bd2a2010-07-23 23:32:05 +01002331int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002332intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2333 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002334 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002335 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002336{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002337 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002338 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002339 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002340 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002341 u32 alignment;
2342 int ret;
2343
Matt Roperebcdd392014-07-09 16:22:11 -07002344 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2345
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002346 switch (fb->modifier[0]) {
2347 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002348 if (INTEL_INFO(dev)->gen >= 9)
2349 alignment = 256 * 1024;
2350 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002351 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002352 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002353 alignment = 4 * 1024;
2354 else
2355 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002356 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002357 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002358 if (INTEL_INFO(dev)->gen >= 9)
2359 alignment = 256 * 1024;
2360 else {
2361 /* pin() will align the object as required by fence */
2362 alignment = 0;
2363 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002364 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002365 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002366 case I915_FORMAT_MOD_Yf_TILED:
2367 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2368 "Y tiling bo slipped through, driver bug!\n"))
2369 return -EINVAL;
2370 alignment = 1 * 1024 * 1024;
2371 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002372 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002373 MISSING_CASE(fb->modifier[0]);
2374 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002375 }
2376
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002377 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2378 if (ret)
2379 return ret;
2380
Chris Wilson693db182013-03-05 14:52:39 +00002381 /* Note that the w/a also requires 64 PTE of padding following the
2382 * bo. We currently fill all unused PTE with the shadow page and so
2383 * we should always have valid PTE following the scanout preventing
2384 * the VT-d warning.
2385 */
2386 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2387 alignment = 256 * 1024;
2388
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002389 /*
2390 * Global gtt pte registers are special registers which actually forward
2391 * writes to a chunk of system memory. Which means that there is no risk
2392 * that the register values disappear as soon as we call
2393 * intel_runtime_pm_put(), so it is correct to wrap only the
2394 * pin/unpin/fence and not more.
2395 */
2396 intel_runtime_pm_get(dev_priv);
2397
Chris Wilsonce453d82011-02-21 14:43:56 +00002398 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002399 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002400 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002401 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002402 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002403
2404 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2405 * fence, whereas 965+ only requires a fence if using
2406 * framebuffer compression. For simplicity, we always install
2407 * a fence as the cost is not that onerous.
2408 */
Chris Wilson06d98132012-04-17 15:31:24 +01002409 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002410 if (ret)
2411 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002412
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002413 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002414
Chris Wilsonce453d82011-02-21 14:43:56 +00002415 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002416 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002417 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002418
2419err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002420 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002421err_interruptible:
2422 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002423 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002424 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002425}
2426
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002427static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2428 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002429{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002430 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002431 struct i915_ggtt_view view;
2432 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002433
Matt Roperebcdd392014-07-09 16:22:11 -07002434 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2435
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002436 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2437 WARN_ONCE(ret, "Couldn't get view from plane state!");
2438
Chris Wilson1690e1e2011-12-14 13:57:08 +01002439 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002440 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002441}
2442
Daniel Vetterc2c75132012-07-05 12:17:30 +02002443/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2444 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002445unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2446 unsigned int tiling_mode,
2447 unsigned int cpp,
2448 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002449{
Chris Wilsonbc752862013-02-21 20:04:31 +00002450 if (tiling_mode != I915_TILING_NONE) {
2451 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002452
Chris Wilsonbc752862013-02-21 20:04:31 +00002453 tile_rows = *y / 8;
2454 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002455
Chris Wilsonbc752862013-02-21 20:04:31 +00002456 tiles = *x / (512/cpp);
2457 *x %= 512/cpp;
2458
2459 return tile_rows * pitch * 8 + tiles * 4096;
2460 } else {
2461 unsigned int offset;
2462
2463 offset = *y * pitch + *x * cpp;
2464 *y = 0;
2465 *x = (offset & 4095) / cpp;
2466 return offset & -4096;
2467 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002468}
2469
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002470static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002471{
2472 switch (format) {
2473 case DISPPLANE_8BPP:
2474 return DRM_FORMAT_C8;
2475 case DISPPLANE_BGRX555:
2476 return DRM_FORMAT_XRGB1555;
2477 case DISPPLANE_BGRX565:
2478 return DRM_FORMAT_RGB565;
2479 default:
2480 case DISPPLANE_BGRX888:
2481 return DRM_FORMAT_XRGB8888;
2482 case DISPPLANE_RGBX888:
2483 return DRM_FORMAT_XBGR8888;
2484 case DISPPLANE_BGRX101010:
2485 return DRM_FORMAT_XRGB2101010;
2486 case DISPPLANE_RGBX101010:
2487 return DRM_FORMAT_XBGR2101010;
2488 }
2489}
2490
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002491static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2492{
2493 switch (format) {
2494 case PLANE_CTL_FORMAT_RGB_565:
2495 return DRM_FORMAT_RGB565;
2496 default:
2497 case PLANE_CTL_FORMAT_XRGB_8888:
2498 if (rgb_order) {
2499 if (alpha)
2500 return DRM_FORMAT_ABGR8888;
2501 else
2502 return DRM_FORMAT_XBGR8888;
2503 } else {
2504 if (alpha)
2505 return DRM_FORMAT_ARGB8888;
2506 else
2507 return DRM_FORMAT_XRGB8888;
2508 }
2509 case PLANE_CTL_FORMAT_XRGB_2101010:
2510 if (rgb_order)
2511 return DRM_FORMAT_XBGR2101010;
2512 else
2513 return DRM_FORMAT_XRGB2101010;
2514 }
2515}
2516
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002517static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002518intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2519 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002520{
2521 struct drm_device *dev = crtc->base.dev;
2522 struct drm_i915_gem_object *obj = NULL;
2523 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002524 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002525 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2526 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2527 PAGE_SIZE);
2528
2529 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002530
Chris Wilsonff2652e2014-03-10 08:07:02 +00002531 if (plane_config->size == 0)
2532 return false;
2533
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002534 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2535 base_aligned,
2536 base_aligned,
2537 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002538 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002539 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002540
Damien Lespiau49af4492015-01-20 12:51:44 +00002541 obj->tiling_mode = plane_config->tiling;
2542 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002543 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002544
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002545 mode_cmd.pixel_format = fb->pixel_format;
2546 mode_cmd.width = fb->width;
2547 mode_cmd.height = fb->height;
2548 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002549 mode_cmd.modifier[0] = fb->modifier[0];
2550 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002551
2552 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002553 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002554 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002555 DRM_DEBUG_KMS("intel fb init failed\n");
2556 goto out_unref_obj;
2557 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002558 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002559
Daniel Vetterf6936e22015-03-26 12:17:05 +01002560 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002561 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002562
2563out_unref_obj:
2564 drm_gem_object_unreference(&obj->base);
2565 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002566 return false;
2567}
2568
Matt Roperafd65eb2015-02-03 13:10:04 -08002569/* Update plane->state->fb to match plane->fb after driver-internal updates */
2570static void
2571update_state_fb(struct drm_plane *plane)
2572{
2573 if (plane->fb == plane->state->fb)
2574 return;
2575
2576 if (plane->state->fb)
2577 drm_framebuffer_unreference(plane->state->fb);
2578 plane->state->fb = plane->fb;
2579 if (plane->state->fb)
2580 drm_framebuffer_reference(plane->state->fb);
2581}
2582
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002583static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002584intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2585 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002586{
2587 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002588 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002589 struct drm_crtc *c;
2590 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002591 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002592 struct drm_plane *primary = intel_crtc->base.primary;
2593 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002594
Damien Lespiau2d140302015-02-05 17:22:18 +00002595 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002596 return;
2597
Daniel Vetterf6936e22015-03-26 12:17:05 +01002598 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002599 fb = &plane_config->fb->base;
2600 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002601 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002602
Damien Lespiau2d140302015-02-05 17:22:18 +00002603 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002604
2605 /*
2606 * Failed to alloc the obj, check to see if we should share
2607 * an fb with another CRTC instead
2608 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002609 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002610 i = to_intel_crtc(c);
2611
2612 if (c == &intel_crtc->base)
2613 continue;
2614
Matt Roper2ff8fde2014-07-08 07:50:07 -07002615 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002616 continue;
2617
Daniel Vetter88595ac2015-03-26 12:42:24 +01002618 fb = c->primary->fb;
2619 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002620 continue;
2621
Daniel Vetter88595ac2015-03-26 12:42:24 +01002622 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002623 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002624 drm_framebuffer_reference(fb);
2625 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002626 }
2627 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002628
2629 return;
2630
2631valid_fb:
2632 obj = intel_fb_obj(fb);
2633 if (obj->tiling_mode != I915_TILING_NONE)
2634 dev_priv->preserve_bios_swizzle = true;
2635
2636 primary->fb = fb;
2637 primary->state->crtc = &intel_crtc->base;
2638 primary->crtc = &intel_crtc->base;
2639 update_state_fb(primary);
2640 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002641}
2642
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002643static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2644 struct drm_framebuffer *fb,
2645 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002646{
2647 struct drm_device *dev = crtc->dev;
2648 struct drm_i915_private *dev_priv = dev->dev_private;
2649 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002650 struct drm_plane *primary = crtc->primary;
2651 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002652 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002653 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002654 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002655 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002656 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302657 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002658
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002659 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002660 I915_WRITE(reg, 0);
2661 if (INTEL_INFO(dev)->gen >= 4)
2662 I915_WRITE(DSPSURF(plane), 0);
2663 else
2664 I915_WRITE(DSPADDR(plane), 0);
2665 POSTING_READ(reg);
2666 return;
2667 }
2668
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002669 obj = intel_fb_obj(fb);
2670 if (WARN_ON(obj == NULL))
2671 return;
2672
2673 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2674
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002675 dspcntr = DISPPLANE_GAMMA_ENABLE;
2676
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002677 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002678
2679 if (INTEL_INFO(dev)->gen < 4) {
2680 if (intel_crtc->pipe == PIPE_B)
2681 dspcntr |= DISPPLANE_SEL_PIPE_B;
2682
2683 /* pipesrc and dspsize control the size that is scaled from,
2684 * which should always be the user's requested size.
2685 */
2686 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002687 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2688 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002689 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002690 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2691 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002692 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2693 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002694 I915_WRITE(PRIMPOS(plane), 0);
2695 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002696 }
2697
Ville Syrjälä57779d02012-10-31 17:50:14 +02002698 switch (fb->pixel_format) {
2699 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002700 dspcntr |= DISPPLANE_8BPP;
2701 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002702 case DRM_FORMAT_XRGB1555:
2703 case DRM_FORMAT_ARGB1555:
2704 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002705 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002706 case DRM_FORMAT_RGB565:
2707 dspcntr |= DISPPLANE_BGRX565;
2708 break;
2709 case DRM_FORMAT_XRGB8888:
2710 case DRM_FORMAT_ARGB8888:
2711 dspcntr |= DISPPLANE_BGRX888;
2712 break;
2713 case DRM_FORMAT_XBGR8888:
2714 case DRM_FORMAT_ABGR8888:
2715 dspcntr |= DISPPLANE_RGBX888;
2716 break;
2717 case DRM_FORMAT_XRGB2101010:
2718 case DRM_FORMAT_ARGB2101010:
2719 dspcntr |= DISPPLANE_BGRX101010;
2720 break;
2721 case DRM_FORMAT_XBGR2101010:
2722 case DRM_FORMAT_ABGR2101010:
2723 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002724 break;
2725 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002726 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002727 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002728
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002729 if (INTEL_INFO(dev)->gen >= 4 &&
2730 obj->tiling_mode != I915_TILING_NONE)
2731 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002732
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002733 if (IS_G4X(dev))
2734 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2735
Ville Syrjäläb98971272014-08-27 16:51:22 +03002736 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002737
Daniel Vetterc2c75132012-07-05 12:17:30 +02002738 if (INTEL_INFO(dev)->gen >= 4) {
2739 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002740 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002741 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002742 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002743 linear_offset -= intel_crtc->dspaddr_offset;
2744 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002745 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002746 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002747
Matt Roper8e7d6882015-01-21 16:35:41 -08002748 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302749 dspcntr |= DISPPLANE_ROTATE_180;
2750
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002751 x += (intel_crtc->config->pipe_src_w - 1);
2752 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302753
2754 /* Finding the last pixel of the last line of the display
2755 data and adding to linear_offset*/
2756 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002757 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2758 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302759 }
2760
2761 I915_WRITE(reg, dspcntr);
2762
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002763 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002764 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002765 I915_WRITE(DSPSURF(plane),
2766 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002767 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002768 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002769 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002770 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002771 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002772}
2773
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002774static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2775 struct drm_framebuffer *fb,
2776 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002777{
2778 struct drm_device *dev = crtc->dev;
2779 struct drm_i915_private *dev_priv = dev->dev_private;
2780 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002781 struct drm_plane *primary = crtc->primary;
2782 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002783 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002784 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002785 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002786 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002787 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302788 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002789
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002790 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002791 I915_WRITE(reg, 0);
2792 I915_WRITE(DSPSURF(plane), 0);
2793 POSTING_READ(reg);
2794 return;
2795 }
2796
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002797 obj = intel_fb_obj(fb);
2798 if (WARN_ON(obj == NULL))
2799 return;
2800
2801 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2802
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002803 dspcntr = DISPPLANE_GAMMA_ENABLE;
2804
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002805 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002806
2807 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2808 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2809
Ville Syrjälä57779d02012-10-31 17:50:14 +02002810 switch (fb->pixel_format) {
2811 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002812 dspcntr |= DISPPLANE_8BPP;
2813 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002814 case DRM_FORMAT_RGB565:
2815 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002816 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002817 case DRM_FORMAT_XRGB8888:
2818 case DRM_FORMAT_ARGB8888:
2819 dspcntr |= DISPPLANE_BGRX888;
2820 break;
2821 case DRM_FORMAT_XBGR8888:
2822 case DRM_FORMAT_ABGR8888:
2823 dspcntr |= DISPPLANE_RGBX888;
2824 break;
2825 case DRM_FORMAT_XRGB2101010:
2826 case DRM_FORMAT_ARGB2101010:
2827 dspcntr |= DISPPLANE_BGRX101010;
2828 break;
2829 case DRM_FORMAT_XBGR2101010:
2830 case DRM_FORMAT_ABGR2101010:
2831 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002832 break;
2833 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002834 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002835 }
2836
2837 if (obj->tiling_mode != I915_TILING_NONE)
2838 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002839
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002840 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002841 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002842
Ville Syrjäläb98971272014-08-27 16:51:22 +03002843 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002844 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002845 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002846 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002847 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002848 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002849 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302850 dspcntr |= DISPPLANE_ROTATE_180;
2851
2852 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002853 x += (intel_crtc->config->pipe_src_w - 1);
2854 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302855
2856 /* Finding the last pixel of the last line of the display
2857 data and adding to linear_offset*/
2858 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002859 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2860 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302861 }
2862 }
2863
2864 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002865
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002866 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002867 I915_WRITE(DSPSURF(plane),
2868 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002869 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002870 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2871 } else {
2872 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2873 I915_WRITE(DSPLINOFF(plane), linear_offset);
2874 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002875 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002876}
2877
Damien Lespiaub3218032015-02-27 11:15:18 +00002878u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2879 uint32_t pixel_format)
2880{
2881 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2882
2883 /*
2884 * The stride is either expressed as a multiple of 64 bytes
2885 * chunks for linear buffers or in number of tiles for tiled
2886 * buffers.
2887 */
2888 switch (fb_modifier) {
2889 case DRM_FORMAT_MOD_NONE:
2890 return 64;
2891 case I915_FORMAT_MOD_X_TILED:
2892 if (INTEL_INFO(dev)->gen == 2)
2893 return 128;
2894 return 512;
2895 case I915_FORMAT_MOD_Y_TILED:
2896 /* No need to check for old gens and Y tiling since this is
2897 * about the display engine and those will be blocked before
2898 * we get here.
2899 */
2900 return 128;
2901 case I915_FORMAT_MOD_Yf_TILED:
2902 if (bits_per_pixel == 8)
2903 return 64;
2904 else
2905 return 128;
2906 default:
2907 MISSING_CASE(fb_modifier);
2908 return 64;
2909 }
2910}
2911
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002912unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2913 struct drm_i915_gem_object *obj)
2914{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002915 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002916
2917 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002918 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002919
2920 return i915_gem_obj_ggtt_offset_view(obj, view);
2921}
2922
Chandra Kondurua1b22782015-04-07 15:28:45 -07002923/*
2924 * This function detaches (aka. unbinds) unused scalers in hardware
2925 */
2926void skl_detach_scalers(struct intel_crtc *intel_crtc)
2927{
2928 struct drm_device *dev;
2929 struct drm_i915_private *dev_priv;
2930 struct intel_crtc_scaler_state *scaler_state;
2931 int i;
2932
2933 if (!intel_crtc || !intel_crtc->config)
2934 return;
2935
2936 dev = intel_crtc->base.dev;
2937 dev_priv = dev->dev_private;
2938 scaler_state = &intel_crtc->config->scaler_state;
2939
2940 /* loop through and disable scalers that aren't in use */
2941 for (i = 0; i < intel_crtc->num_scalers; i++) {
2942 if (!scaler_state->scalers[i].in_use) {
2943 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2944 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2945 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2946 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2947 intel_crtc->base.base.id, intel_crtc->pipe, i);
2948 }
2949 }
2950}
2951
Chandra Konduru6156a452015-04-27 13:48:39 -07002952u32 skl_plane_ctl_format(uint32_t pixel_format)
2953{
2954 u32 plane_ctl_format = 0;
2955 switch (pixel_format) {
2956 case DRM_FORMAT_RGB565:
2957 plane_ctl_format = PLANE_CTL_FORMAT_RGB_565;
2958 break;
2959 case DRM_FORMAT_XBGR8888:
2960 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
2961 break;
2962 case DRM_FORMAT_XRGB8888:
2963 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888;
2964 break;
2965 /*
2966 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2967 * to be already pre-multiplied. We need to add a knob (or a different
2968 * DRM_FORMAT) for user-space to configure that.
2969 */
2970 case DRM_FORMAT_ABGR8888:
2971 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
2972 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2973 break;
2974 case DRM_FORMAT_ARGB8888:
2975 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 |
2976 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2977 break;
2978 case DRM_FORMAT_XRGB2101010:
2979 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_2101010;
2980 break;
2981 case DRM_FORMAT_XBGR2101010:
2982 plane_ctl_format = PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
2983 break;
2984 case DRM_FORMAT_YUYV:
2985 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
2986 break;
2987 case DRM_FORMAT_YVYU:
2988 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
2989 break;
2990 case DRM_FORMAT_UYVY:
2991 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
2992 break;
2993 case DRM_FORMAT_VYUY:
2994 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
2995 break;
2996 default:
2997 BUG();
2998 }
2999 return plane_ctl_format;
3000}
3001
3002u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3003{
3004 u32 plane_ctl_tiling = 0;
3005 switch (fb_modifier) {
3006 case DRM_FORMAT_MOD_NONE:
3007 break;
3008 case I915_FORMAT_MOD_X_TILED:
3009 plane_ctl_tiling = PLANE_CTL_TILED_X;
3010 break;
3011 case I915_FORMAT_MOD_Y_TILED:
3012 plane_ctl_tiling = PLANE_CTL_TILED_Y;
3013 break;
3014 case I915_FORMAT_MOD_Yf_TILED:
3015 plane_ctl_tiling = PLANE_CTL_TILED_YF;
3016 break;
3017 default:
3018 MISSING_CASE(fb_modifier);
3019 }
3020 return plane_ctl_tiling;
3021}
3022
3023u32 skl_plane_ctl_rotation(unsigned int rotation)
3024{
3025 u32 plane_ctl_rotation = 0;
3026 switch (rotation) {
3027 case BIT(DRM_ROTATE_0):
3028 break;
3029 case BIT(DRM_ROTATE_90):
3030 plane_ctl_rotation = PLANE_CTL_ROTATE_90;
3031 break;
3032 case BIT(DRM_ROTATE_180):
3033 plane_ctl_rotation = PLANE_CTL_ROTATE_180;
3034 break;
3035 case BIT(DRM_ROTATE_270):
3036 plane_ctl_rotation = PLANE_CTL_ROTATE_270;
3037 break;
3038 default:
3039 MISSING_CASE(rotation);
3040 }
3041
3042 return plane_ctl_rotation;
3043}
3044
Damien Lespiau70d21f02013-07-03 21:06:04 +01003045static void skylake_update_primary_plane(struct drm_crtc *crtc,
3046 struct drm_framebuffer *fb,
3047 int x, int y)
3048{
3049 struct drm_device *dev = crtc->dev;
3050 struct drm_i915_private *dev_priv = dev->dev_private;
3051 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003052 struct drm_plane *plane = crtc->primary;
3053 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003054 struct drm_i915_gem_object *obj;
3055 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303056 u32 plane_ctl, stride_div, stride;
3057 u32 tile_height, plane_offset, plane_size;
3058 unsigned int rotation;
3059 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003060 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003061 struct intel_crtc_state *crtc_state = intel_crtc->config;
3062 struct intel_plane_state *plane_state;
3063 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3064 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3065 int scaler_id = -1;
3066
Chandra Konduru6156a452015-04-27 13:48:39 -07003067 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003068
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003069 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003070 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3071 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3072 POSTING_READ(PLANE_CTL(pipe, 0));
3073 return;
3074 }
3075
3076 plane_ctl = PLANE_CTL_ENABLE |
3077 PLANE_CTL_PIPE_GAMMA_ENABLE |
3078 PLANE_CTL_PIPE_CSC_ENABLE;
3079
Chandra Konduru6156a452015-04-27 13:48:39 -07003080 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3081 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003082 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303083
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303084 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003085 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003086
Damien Lespiaub3218032015-02-27 11:15:18 +00003087 obj = intel_fb_obj(fb);
3088 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3089 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303090 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3091
Chandra Konduru6156a452015-04-27 13:48:39 -07003092 /*
3093 * FIXME: intel_plane_state->src, dst aren't set when transitional
3094 * update_plane helpers are called from legacy paths.
3095 * Once full atomic crtc is available, below check can be avoided.
3096 */
3097 if (drm_rect_width(&plane_state->src)) {
3098 scaler_id = plane_state->scaler_id;
3099 src_x = plane_state->src.x1 >> 16;
3100 src_y = plane_state->src.y1 >> 16;
3101 src_w = drm_rect_width(&plane_state->src) >> 16;
3102 src_h = drm_rect_height(&plane_state->src) >> 16;
3103 dst_x = plane_state->dst.x1;
3104 dst_y = plane_state->dst.y1;
3105 dst_w = drm_rect_width(&plane_state->dst);
3106 dst_h = drm_rect_height(&plane_state->dst);
3107
3108 WARN_ON(x != src_x || y != src_y);
3109 } else {
3110 src_w = intel_crtc->config->pipe_src_w;
3111 src_h = intel_crtc->config->pipe_src_h;
3112 }
3113
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303114 if (intel_rotation_90_or_270(rotation)) {
3115 /* stride = Surface height in tiles */
3116 tile_height = intel_tile_height(dev, fb->bits_per_pixel,
3117 fb->modifier[0]);
3118 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003119 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303120 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003121 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303122 } else {
3123 stride = fb->pitches[0] / stride_div;
3124 x_offset = x;
3125 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003126 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303127 }
3128 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003129
Damien Lespiau70d21f02013-07-03 21:06:04 +01003130 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303131 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3132 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3133 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003134
3135 if (scaler_id >= 0) {
3136 uint32_t ps_ctrl = 0;
3137
3138 WARN_ON(!dst_w || !dst_h);
3139 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3140 crtc_state->scaler_state.scalers[scaler_id].mode;
3141 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3142 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3143 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3144 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3145 I915_WRITE(PLANE_POS(pipe, 0), 0);
3146 } else {
3147 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3148 }
3149
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003150 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003151
3152 POSTING_READ(PLANE_SURF(pipe, 0));
3153}
3154
Jesse Barnes17638cd2011-06-24 12:19:23 -07003155/* Assume fb object is pinned & idle & fenced and just update base pointers */
3156static int
3157intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3158 int x, int y, enum mode_set_atomic state)
3159{
3160 struct drm_device *dev = crtc->dev;
3161 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003162
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003163 if (dev_priv->display.disable_fbc)
3164 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003165
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003166 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3167
3168 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003169}
3170
Ville Syrjälä75147472014-11-24 18:28:11 +02003171static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003172{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003173 struct drm_crtc *crtc;
3174
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003175 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003176 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3177 enum plane plane = intel_crtc->plane;
3178
3179 intel_prepare_page_flip(dev, plane);
3180 intel_finish_page_flip_plane(dev, plane);
3181 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003182}
3183
3184static void intel_update_primary_planes(struct drm_device *dev)
3185{
3186 struct drm_i915_private *dev_priv = dev->dev_private;
3187 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003188
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003189 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003190 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3191
Rob Clark51fd3712013-11-19 12:10:12 -05003192 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003193 /*
3194 * FIXME: Once we have proper support for primary planes (and
3195 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003196 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003197 */
Matt Roperf4510a22014-04-01 15:22:40 -07003198 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003199 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003200 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003201 crtc->x,
3202 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003203 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003204 }
3205}
3206
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003207void intel_crtc_reset(struct intel_crtc *crtc)
3208{
3209 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3210
3211 if (!crtc->active)
3212 return;
3213
3214 intel_crtc_disable_planes(&crtc->base);
3215 dev_priv->display.crtc_disable(&crtc->base);
3216 dev_priv->display.crtc_enable(&crtc->base);
3217 intel_crtc_enable_planes(&crtc->base);
3218}
3219
Ville Syrjälä75147472014-11-24 18:28:11 +02003220void intel_prepare_reset(struct drm_device *dev)
3221{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003222 struct drm_i915_private *dev_priv = to_i915(dev);
3223 struct intel_crtc *crtc;
3224
Ville Syrjälä75147472014-11-24 18:28:11 +02003225 /* no reset support for gen2 */
3226 if (IS_GEN2(dev))
3227 return;
3228
3229 /* reset doesn't touch the display */
3230 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3231 return;
3232
3233 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003234
3235 /*
3236 * Disabling the crtcs gracefully seems nicer. Also the
3237 * g33 docs say we should at least disable all the planes.
3238 */
3239 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003240 if (!crtc->active)
3241 continue;
3242
3243 intel_crtc_disable_planes(&crtc->base);
3244 dev_priv->display.crtc_disable(&crtc->base);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003245 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003246}
3247
3248void intel_finish_reset(struct drm_device *dev)
3249{
3250 struct drm_i915_private *dev_priv = to_i915(dev);
3251
3252 /*
3253 * Flips in the rings will be nuked by the reset,
3254 * so complete all pending flips so that user space
3255 * will get its events and not get stuck.
3256 */
3257 intel_complete_page_flips(dev);
3258
3259 /* no reset support for gen2 */
3260 if (IS_GEN2(dev))
3261 return;
3262
3263 /* reset doesn't touch the display */
3264 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3265 /*
3266 * Flips in the rings have been nuked by the reset,
3267 * so update the base address of all primary
3268 * planes to the the last fb to make sure we're
3269 * showing the correct fb after a reset.
3270 */
3271 intel_update_primary_planes(dev);
3272 return;
3273 }
3274
3275 /*
3276 * The display has been reset as well,
3277 * so need a full re-initialization.
3278 */
3279 intel_runtime_pm_disable_interrupts(dev_priv);
3280 intel_runtime_pm_enable_interrupts(dev_priv);
3281
3282 intel_modeset_init_hw(dev);
3283
3284 spin_lock_irq(&dev_priv->irq_lock);
3285 if (dev_priv->display.hpd_irq_setup)
3286 dev_priv->display.hpd_irq_setup(dev);
3287 spin_unlock_irq(&dev_priv->irq_lock);
3288
3289 intel_modeset_setup_hw_state(dev, true);
3290
3291 intel_hpd_init(dev_priv);
3292
3293 drm_modeset_unlock_all(dev);
3294}
3295
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003296static int
Chris Wilson14667a42012-04-03 17:58:35 +01003297intel_finish_fb(struct drm_framebuffer *old_fb)
3298{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003299 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01003300 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3301 bool was_interruptible = dev_priv->mm.interruptible;
3302 int ret;
3303
Chris Wilson14667a42012-04-03 17:58:35 +01003304 /* Big Hammer, we also need to ensure that any pending
3305 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3306 * current scanout is retired before unpinning the old
3307 * framebuffer.
3308 *
3309 * This should only fail upon a hung GPU, in which case we
3310 * can safely continue.
3311 */
3312 dev_priv->mm.interruptible = false;
3313 ret = i915_gem_object_finish_gpu(obj);
3314 dev_priv->mm.interruptible = was_interruptible;
3315
3316 return ret;
3317}
3318
Chris Wilson7d5e3792014-03-04 13:15:08 +00003319static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3320{
3321 struct drm_device *dev = crtc->dev;
3322 struct drm_i915_private *dev_priv = dev->dev_private;
3323 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003324 bool pending;
3325
3326 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3327 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3328 return false;
3329
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003330 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003331 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003332 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003333
3334 return pending;
3335}
3336
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003337static void intel_update_pipe_size(struct intel_crtc *crtc)
3338{
3339 struct drm_device *dev = crtc->base.dev;
3340 struct drm_i915_private *dev_priv = dev->dev_private;
3341 const struct drm_display_mode *adjusted_mode;
3342
3343 if (!i915.fastboot)
3344 return;
3345
3346 /*
3347 * Update pipe size and adjust fitter if needed: the reason for this is
3348 * that in compute_mode_changes we check the native mode (not the pfit
3349 * mode) to see if we can flip rather than do a full mode set. In the
3350 * fastboot case, we'll flip, but if we don't update the pipesrc and
3351 * pfit state, we'll end up with a big fb scanned out into the wrong
3352 * sized surface.
3353 *
3354 * To fix this properly, we need to hoist the checks up into
3355 * compute_mode_changes (or above), check the actual pfit state and
3356 * whether the platform allows pfit disable with pipe active, and only
3357 * then update the pipesrc and pfit state, even on the flip path.
3358 */
3359
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003360 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003361
3362 I915_WRITE(PIPESRC(crtc->pipe),
3363 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3364 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003365 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003366 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3367 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003368 I915_WRITE(PF_CTL(crtc->pipe), 0);
3369 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3370 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3371 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003372 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3373 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003374}
3375
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003376static void intel_fdi_normal_train(struct drm_crtc *crtc)
3377{
3378 struct drm_device *dev = crtc->dev;
3379 struct drm_i915_private *dev_priv = dev->dev_private;
3380 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3381 int pipe = intel_crtc->pipe;
3382 u32 reg, temp;
3383
3384 /* enable normal train */
3385 reg = FDI_TX_CTL(pipe);
3386 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003387 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003388 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3389 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003390 } else {
3391 temp &= ~FDI_LINK_TRAIN_NONE;
3392 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003393 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003394 I915_WRITE(reg, temp);
3395
3396 reg = FDI_RX_CTL(pipe);
3397 temp = I915_READ(reg);
3398 if (HAS_PCH_CPT(dev)) {
3399 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3400 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3401 } else {
3402 temp &= ~FDI_LINK_TRAIN_NONE;
3403 temp |= FDI_LINK_TRAIN_NONE;
3404 }
3405 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3406
3407 /* wait one idle pattern time */
3408 POSTING_READ(reg);
3409 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003410
3411 /* IVB wants error correction enabled */
3412 if (IS_IVYBRIDGE(dev))
3413 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3414 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003415}
3416
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003417/* The FDI link training functions for ILK/Ibexpeak. */
3418static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3419{
3420 struct drm_device *dev = crtc->dev;
3421 struct drm_i915_private *dev_priv = dev->dev_private;
3422 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3423 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003424 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003425
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003426 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003427 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003428
Adam Jacksone1a44742010-06-25 15:32:14 -04003429 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3430 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003431 reg = FDI_RX_IMR(pipe);
3432 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003433 temp &= ~FDI_RX_SYMBOL_LOCK;
3434 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003435 I915_WRITE(reg, temp);
3436 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003437 udelay(150);
3438
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003439 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003440 reg = FDI_TX_CTL(pipe);
3441 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003442 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003443 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003444 temp &= ~FDI_LINK_TRAIN_NONE;
3445 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003446 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003447
Chris Wilson5eddb702010-09-11 13:48:45 +01003448 reg = FDI_RX_CTL(pipe);
3449 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003450 temp &= ~FDI_LINK_TRAIN_NONE;
3451 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003452 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3453
3454 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003455 udelay(150);
3456
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003457 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003458 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3459 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3460 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003461
Chris Wilson5eddb702010-09-11 13:48:45 +01003462 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003463 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003464 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003465 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3466
3467 if ((temp & FDI_RX_BIT_LOCK)) {
3468 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003469 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003470 break;
3471 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003472 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003473 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003474 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003475
3476 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003477 reg = FDI_TX_CTL(pipe);
3478 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003479 temp &= ~FDI_LINK_TRAIN_NONE;
3480 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003481 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003482
Chris Wilson5eddb702010-09-11 13:48:45 +01003483 reg = FDI_RX_CTL(pipe);
3484 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003485 temp &= ~FDI_LINK_TRAIN_NONE;
3486 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003487 I915_WRITE(reg, temp);
3488
3489 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003490 udelay(150);
3491
Chris Wilson5eddb702010-09-11 13:48:45 +01003492 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003493 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003494 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003495 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3496
3497 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003498 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003499 DRM_DEBUG_KMS("FDI train 2 done.\n");
3500 break;
3501 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003502 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003503 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003504 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003505
3506 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003507
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003508}
3509
Akshay Joshi0206e352011-08-16 15:34:10 -04003510static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003511 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3512 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3513 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3514 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3515};
3516
3517/* The FDI link training functions for SNB/Cougarpoint. */
3518static void gen6_fdi_link_train(struct drm_crtc *crtc)
3519{
3520 struct drm_device *dev = crtc->dev;
3521 struct drm_i915_private *dev_priv = dev->dev_private;
3522 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3523 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003524 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003525
Adam Jacksone1a44742010-06-25 15:32:14 -04003526 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3527 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003528 reg = FDI_RX_IMR(pipe);
3529 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003530 temp &= ~FDI_RX_SYMBOL_LOCK;
3531 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003532 I915_WRITE(reg, temp);
3533
3534 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003535 udelay(150);
3536
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003537 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003538 reg = FDI_TX_CTL(pipe);
3539 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003540 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003541 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003542 temp &= ~FDI_LINK_TRAIN_NONE;
3543 temp |= FDI_LINK_TRAIN_PATTERN_1;
3544 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3545 /* SNB-B */
3546 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003547 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003548
Daniel Vetterd74cf322012-10-26 10:58:13 +02003549 I915_WRITE(FDI_RX_MISC(pipe),
3550 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3551
Chris Wilson5eddb702010-09-11 13:48:45 +01003552 reg = FDI_RX_CTL(pipe);
3553 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003554 if (HAS_PCH_CPT(dev)) {
3555 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3556 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3557 } else {
3558 temp &= ~FDI_LINK_TRAIN_NONE;
3559 temp |= FDI_LINK_TRAIN_PATTERN_1;
3560 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003561 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3562
3563 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003564 udelay(150);
3565
Akshay Joshi0206e352011-08-16 15:34:10 -04003566 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003567 reg = FDI_TX_CTL(pipe);
3568 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003569 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3570 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003571 I915_WRITE(reg, temp);
3572
3573 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003574 udelay(500);
3575
Sean Paulfa37d392012-03-02 12:53:39 -05003576 for (retry = 0; retry < 5; retry++) {
3577 reg = FDI_RX_IIR(pipe);
3578 temp = I915_READ(reg);
3579 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3580 if (temp & FDI_RX_BIT_LOCK) {
3581 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3582 DRM_DEBUG_KMS("FDI train 1 done.\n");
3583 break;
3584 }
3585 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003586 }
Sean Paulfa37d392012-03-02 12:53:39 -05003587 if (retry < 5)
3588 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003589 }
3590 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003591 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003592
3593 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003594 reg = FDI_TX_CTL(pipe);
3595 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003596 temp &= ~FDI_LINK_TRAIN_NONE;
3597 temp |= FDI_LINK_TRAIN_PATTERN_2;
3598 if (IS_GEN6(dev)) {
3599 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3600 /* SNB-B */
3601 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3602 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003603 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003604
Chris Wilson5eddb702010-09-11 13:48:45 +01003605 reg = FDI_RX_CTL(pipe);
3606 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003607 if (HAS_PCH_CPT(dev)) {
3608 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3609 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3610 } else {
3611 temp &= ~FDI_LINK_TRAIN_NONE;
3612 temp |= FDI_LINK_TRAIN_PATTERN_2;
3613 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003614 I915_WRITE(reg, temp);
3615
3616 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003617 udelay(150);
3618
Akshay Joshi0206e352011-08-16 15:34:10 -04003619 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003620 reg = FDI_TX_CTL(pipe);
3621 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003622 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3623 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003624 I915_WRITE(reg, temp);
3625
3626 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003627 udelay(500);
3628
Sean Paulfa37d392012-03-02 12:53:39 -05003629 for (retry = 0; retry < 5; retry++) {
3630 reg = FDI_RX_IIR(pipe);
3631 temp = I915_READ(reg);
3632 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3633 if (temp & FDI_RX_SYMBOL_LOCK) {
3634 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3635 DRM_DEBUG_KMS("FDI train 2 done.\n");
3636 break;
3637 }
3638 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003639 }
Sean Paulfa37d392012-03-02 12:53:39 -05003640 if (retry < 5)
3641 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003642 }
3643 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003644 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003645
3646 DRM_DEBUG_KMS("FDI train done.\n");
3647}
3648
Jesse Barnes357555c2011-04-28 15:09:55 -07003649/* Manual link training for Ivy Bridge A0 parts */
3650static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3651{
3652 struct drm_device *dev = crtc->dev;
3653 struct drm_i915_private *dev_priv = dev->dev_private;
3654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3655 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003656 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003657
3658 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3659 for train result */
3660 reg = FDI_RX_IMR(pipe);
3661 temp = I915_READ(reg);
3662 temp &= ~FDI_RX_SYMBOL_LOCK;
3663 temp &= ~FDI_RX_BIT_LOCK;
3664 I915_WRITE(reg, temp);
3665
3666 POSTING_READ(reg);
3667 udelay(150);
3668
Daniel Vetter01a415f2012-10-27 15:58:40 +02003669 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3670 I915_READ(FDI_RX_IIR(pipe)));
3671
Jesse Barnes139ccd32013-08-19 11:04:55 -07003672 /* Try each vswing and preemphasis setting twice before moving on */
3673 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3674 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003675 reg = FDI_TX_CTL(pipe);
3676 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003677 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3678 temp &= ~FDI_TX_ENABLE;
3679 I915_WRITE(reg, temp);
3680
3681 reg = FDI_RX_CTL(pipe);
3682 temp = I915_READ(reg);
3683 temp &= ~FDI_LINK_TRAIN_AUTO;
3684 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3685 temp &= ~FDI_RX_ENABLE;
3686 I915_WRITE(reg, temp);
3687
3688 /* enable CPU FDI TX and PCH FDI RX */
3689 reg = FDI_TX_CTL(pipe);
3690 temp = I915_READ(reg);
3691 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003692 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003693 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003694 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003695 temp |= snb_b_fdi_train_param[j/2];
3696 temp |= FDI_COMPOSITE_SYNC;
3697 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3698
3699 I915_WRITE(FDI_RX_MISC(pipe),
3700 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3701
3702 reg = FDI_RX_CTL(pipe);
3703 temp = I915_READ(reg);
3704 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3705 temp |= FDI_COMPOSITE_SYNC;
3706 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3707
3708 POSTING_READ(reg);
3709 udelay(1); /* should be 0.5us */
3710
3711 for (i = 0; i < 4; i++) {
3712 reg = FDI_RX_IIR(pipe);
3713 temp = I915_READ(reg);
3714 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3715
3716 if (temp & FDI_RX_BIT_LOCK ||
3717 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3718 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3719 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3720 i);
3721 break;
3722 }
3723 udelay(1); /* should be 0.5us */
3724 }
3725 if (i == 4) {
3726 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3727 continue;
3728 }
3729
3730 /* Train 2 */
3731 reg = FDI_TX_CTL(pipe);
3732 temp = I915_READ(reg);
3733 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3734 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3735 I915_WRITE(reg, temp);
3736
3737 reg = FDI_RX_CTL(pipe);
3738 temp = I915_READ(reg);
3739 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3740 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003741 I915_WRITE(reg, temp);
3742
3743 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003744 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003745
Jesse Barnes139ccd32013-08-19 11:04:55 -07003746 for (i = 0; i < 4; i++) {
3747 reg = FDI_RX_IIR(pipe);
3748 temp = I915_READ(reg);
3749 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003750
Jesse Barnes139ccd32013-08-19 11:04:55 -07003751 if (temp & FDI_RX_SYMBOL_LOCK ||
3752 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3753 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3754 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3755 i);
3756 goto train_done;
3757 }
3758 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003759 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003760 if (i == 4)
3761 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003762 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003763
Jesse Barnes139ccd32013-08-19 11:04:55 -07003764train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003765 DRM_DEBUG_KMS("FDI train done.\n");
3766}
3767
Daniel Vetter88cefb62012-08-12 19:27:14 +02003768static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003769{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003770 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003771 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003772 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003773 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003774
Jesse Barnesc64e3112010-09-10 11:27:03 -07003775
Jesse Barnes0e23b992010-09-10 11:10:00 -07003776 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003777 reg = FDI_RX_CTL(pipe);
3778 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003779 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003780 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003781 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003782 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3783
3784 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003785 udelay(200);
3786
3787 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003788 temp = I915_READ(reg);
3789 I915_WRITE(reg, temp | FDI_PCDCLK);
3790
3791 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003792 udelay(200);
3793
Paulo Zanoni20749732012-11-23 15:30:38 -02003794 /* Enable CPU FDI TX PLL, always on for Ironlake */
3795 reg = FDI_TX_CTL(pipe);
3796 temp = I915_READ(reg);
3797 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3798 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003799
Paulo Zanoni20749732012-11-23 15:30:38 -02003800 POSTING_READ(reg);
3801 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003802 }
3803}
3804
Daniel Vetter88cefb62012-08-12 19:27:14 +02003805static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3806{
3807 struct drm_device *dev = intel_crtc->base.dev;
3808 struct drm_i915_private *dev_priv = dev->dev_private;
3809 int pipe = intel_crtc->pipe;
3810 u32 reg, temp;
3811
3812 /* Switch from PCDclk to Rawclk */
3813 reg = FDI_RX_CTL(pipe);
3814 temp = I915_READ(reg);
3815 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3816
3817 /* Disable CPU FDI TX PLL */
3818 reg = FDI_TX_CTL(pipe);
3819 temp = I915_READ(reg);
3820 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3821
3822 POSTING_READ(reg);
3823 udelay(100);
3824
3825 reg = FDI_RX_CTL(pipe);
3826 temp = I915_READ(reg);
3827 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3828
3829 /* Wait for the clocks to turn off. */
3830 POSTING_READ(reg);
3831 udelay(100);
3832}
3833
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003834static void ironlake_fdi_disable(struct drm_crtc *crtc)
3835{
3836 struct drm_device *dev = crtc->dev;
3837 struct drm_i915_private *dev_priv = dev->dev_private;
3838 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3839 int pipe = intel_crtc->pipe;
3840 u32 reg, temp;
3841
3842 /* disable CPU FDI tx and PCH FDI rx */
3843 reg = FDI_TX_CTL(pipe);
3844 temp = I915_READ(reg);
3845 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3846 POSTING_READ(reg);
3847
3848 reg = FDI_RX_CTL(pipe);
3849 temp = I915_READ(reg);
3850 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003851 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003852 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3853
3854 POSTING_READ(reg);
3855 udelay(100);
3856
3857 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003858 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003859 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003860
3861 /* still set train pattern 1 */
3862 reg = FDI_TX_CTL(pipe);
3863 temp = I915_READ(reg);
3864 temp &= ~FDI_LINK_TRAIN_NONE;
3865 temp |= FDI_LINK_TRAIN_PATTERN_1;
3866 I915_WRITE(reg, temp);
3867
3868 reg = FDI_RX_CTL(pipe);
3869 temp = I915_READ(reg);
3870 if (HAS_PCH_CPT(dev)) {
3871 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3872 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3873 } else {
3874 temp &= ~FDI_LINK_TRAIN_NONE;
3875 temp |= FDI_LINK_TRAIN_PATTERN_1;
3876 }
3877 /* BPC in FDI rx is consistent with that in PIPECONF */
3878 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003879 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003880 I915_WRITE(reg, temp);
3881
3882 POSTING_READ(reg);
3883 udelay(100);
3884}
3885
Chris Wilson5dce5b932014-01-20 10:17:36 +00003886bool intel_has_pending_fb_unpin(struct drm_device *dev)
3887{
3888 struct intel_crtc *crtc;
3889
3890 /* Note that we don't need to be called with mode_config.lock here
3891 * as our list of CRTC objects is static for the lifetime of the
3892 * device and so cannot disappear as we iterate. Similarly, we can
3893 * happily treat the predicates as racy, atomic checks as userspace
3894 * cannot claim and pin a new fb without at least acquring the
3895 * struct_mutex and so serialising with us.
3896 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003897 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003898 if (atomic_read(&crtc->unpin_work_count) == 0)
3899 continue;
3900
3901 if (crtc->unpin_work)
3902 intel_wait_for_vblank(dev, crtc->pipe);
3903
3904 return true;
3905 }
3906
3907 return false;
3908}
3909
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003910static void page_flip_completed(struct intel_crtc *intel_crtc)
3911{
3912 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3913 struct intel_unpin_work *work = intel_crtc->unpin_work;
3914
3915 /* ensure that the unpin work is consistent wrt ->pending. */
3916 smp_rmb();
3917 intel_crtc->unpin_work = NULL;
3918
3919 if (work->event)
3920 drm_send_vblank_event(intel_crtc->base.dev,
3921 intel_crtc->pipe,
3922 work->event);
3923
3924 drm_crtc_vblank_put(&intel_crtc->base);
3925
3926 wake_up_all(&dev_priv->pending_flip_queue);
3927 queue_work(dev_priv->wq, &work->work);
3928
3929 trace_i915_flip_complete(intel_crtc->plane,
3930 work->pending_flip_obj);
3931}
3932
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003933void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003934{
Chris Wilson0f911282012-04-17 10:05:38 +01003935 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003936 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003937
Daniel Vetter2c10d572012-12-20 21:24:07 +01003938 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003939 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3940 !intel_crtc_has_pending_flip(crtc),
3941 60*HZ) == 0)) {
3942 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003943
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003944 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003945 if (intel_crtc->unpin_work) {
3946 WARN_ONCE(1, "Removing stuck page flip\n");
3947 page_flip_completed(intel_crtc);
3948 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003949 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003950 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003951
Chris Wilson975d5682014-08-20 13:13:34 +01003952 if (crtc->primary->fb) {
3953 mutex_lock(&dev->struct_mutex);
3954 intel_finish_fb(crtc->primary->fb);
3955 mutex_unlock(&dev->struct_mutex);
3956 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003957}
3958
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003959/* Program iCLKIP clock to the desired frequency */
3960static void lpt_program_iclkip(struct drm_crtc *crtc)
3961{
3962 struct drm_device *dev = crtc->dev;
3963 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003964 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003965 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3966 u32 temp;
3967
Daniel Vetter09153002012-12-12 14:06:44 +01003968 mutex_lock(&dev_priv->dpio_lock);
3969
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003970 /* It is necessary to ungate the pixclk gate prior to programming
3971 * the divisors, and gate it back when it is done.
3972 */
3973 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3974
3975 /* Disable SSCCTL */
3976 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003977 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3978 SBI_SSCCTL_DISABLE,
3979 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003980
3981 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003982 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003983 auxdiv = 1;
3984 divsel = 0x41;
3985 phaseinc = 0x20;
3986 } else {
3987 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003988 * but the adjusted_mode->crtc_clock in in KHz. To get the
3989 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003990 * convert the virtual clock precision to KHz here for higher
3991 * precision.
3992 */
3993 u32 iclk_virtual_root_freq = 172800 * 1000;
3994 u32 iclk_pi_range = 64;
3995 u32 desired_divisor, msb_divisor_value, pi_value;
3996
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003997 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003998 msb_divisor_value = desired_divisor / iclk_pi_range;
3999 pi_value = desired_divisor % iclk_pi_range;
4000
4001 auxdiv = 0;
4002 divsel = msb_divisor_value - 2;
4003 phaseinc = pi_value;
4004 }
4005
4006 /* This should not happen with any sane values */
4007 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4008 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4009 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4010 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4011
4012 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 +03004013 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004014 auxdiv,
4015 divsel,
4016 phasedir,
4017 phaseinc);
4018
4019 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004020 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004021 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4022 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4023 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4024 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4025 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4026 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004027 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004028
4029 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004030 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004031 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4032 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004033 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004034
4035 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004036 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004037 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004038 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004039
4040 /* Wait for initialization time */
4041 udelay(24);
4042
4043 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004044
4045 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004046}
4047
Daniel Vetter275f01b22013-05-03 11:49:47 +02004048static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4049 enum pipe pch_transcoder)
4050{
4051 struct drm_device *dev = crtc->base.dev;
4052 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004053 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004054
4055 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4056 I915_READ(HTOTAL(cpu_transcoder)));
4057 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4058 I915_READ(HBLANK(cpu_transcoder)));
4059 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4060 I915_READ(HSYNC(cpu_transcoder)));
4061
4062 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4063 I915_READ(VTOTAL(cpu_transcoder)));
4064 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4065 I915_READ(VBLANK(cpu_transcoder)));
4066 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4067 I915_READ(VSYNC(cpu_transcoder)));
4068 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4069 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4070}
4071
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004072static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004073{
4074 struct drm_i915_private *dev_priv = dev->dev_private;
4075 uint32_t temp;
4076
4077 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004078 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004079 return;
4080
4081 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4082 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4083
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004084 temp &= ~FDI_BC_BIFURCATION_SELECT;
4085 if (enable)
4086 temp |= FDI_BC_BIFURCATION_SELECT;
4087
4088 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004089 I915_WRITE(SOUTH_CHICKEN1, temp);
4090 POSTING_READ(SOUTH_CHICKEN1);
4091}
4092
4093static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4094{
4095 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004096
4097 switch (intel_crtc->pipe) {
4098 case PIPE_A:
4099 break;
4100 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004101 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004102 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004103 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004104 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004105
4106 break;
4107 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004108 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004109
4110 break;
4111 default:
4112 BUG();
4113 }
4114}
4115
Jesse Barnesf67a5592011-01-05 10:31:48 -08004116/*
4117 * Enable PCH resources required for PCH ports:
4118 * - PCH PLLs
4119 * - FDI training & RX/TX
4120 * - update transcoder timings
4121 * - DP transcoding bits
4122 * - transcoder
4123 */
4124static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004125{
4126 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004127 struct drm_i915_private *dev_priv = dev->dev_private;
4128 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4129 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004130 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004131
Daniel Vetterab9412b2013-05-03 11:49:46 +02004132 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004133
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004134 if (IS_IVYBRIDGE(dev))
4135 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4136
Daniel Vettercd986ab2012-10-26 10:58:12 +02004137 /* Write the TU size bits before fdi link training, so that error
4138 * detection works. */
4139 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4140 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4141
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004142 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004143 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004144
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004145 /* We need to program the right clock selection before writing the pixel
4146 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004147 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004148 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004149
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004150 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004151 temp |= TRANS_DPLL_ENABLE(pipe);
4152 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004153 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004154 temp |= sel;
4155 else
4156 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004157 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004158 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004159
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004160 /* XXX: pch pll's can be enabled any time before we enable the PCH
4161 * transcoder, and we actually should do this to not upset any PCH
4162 * transcoder that already use the clock when we share it.
4163 *
4164 * Note that enable_shared_dpll tries to do the right thing, but
4165 * get_shared_dpll unconditionally resets the pll - we need that to have
4166 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004167 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004168
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004169 /* set transcoder timing, panel must allow it */
4170 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004171 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004172
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004173 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004174
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004175 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004176 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004177 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004178 reg = TRANS_DP_CTL(pipe);
4179 temp = I915_READ(reg);
4180 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004181 TRANS_DP_SYNC_MASK |
4182 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01004183 temp |= (TRANS_DP_OUTPUT_ENABLE |
4184 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004185 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004186
4187 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004188 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004189 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004190 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004191
4192 switch (intel_trans_dp_port_sel(crtc)) {
4193 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004194 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004195 break;
4196 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004197 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004198 break;
4199 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004200 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004201 break;
4202 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004203 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004204 }
4205
Chris Wilson5eddb702010-09-11 13:48:45 +01004206 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004207 }
4208
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004209 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004210}
4211
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004212static void lpt_pch_enable(struct drm_crtc *crtc)
4213{
4214 struct drm_device *dev = crtc->dev;
4215 struct drm_i915_private *dev_priv = dev->dev_private;
4216 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004217 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004218
Daniel Vetterab9412b2013-05-03 11:49:46 +02004219 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004220
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004221 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004222
Paulo Zanoni0540e482012-10-31 18:12:40 -02004223 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004224 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004225
Paulo Zanoni937bb612012-10-31 18:12:47 -02004226 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004227}
4228
Daniel Vetter716c2e52014-06-25 22:02:02 +03004229void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004230{
Daniel Vettere2b78262013-06-07 23:10:03 +02004231 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004232
4233 if (pll == NULL)
4234 return;
4235
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004236 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004237 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004238 return;
4239 }
4240
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004241 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4242 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004243 WARN_ON(pll->on);
4244 WARN_ON(pll->active);
4245 }
4246
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004247 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004248}
4249
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004250struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4251 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004252{
Daniel Vettere2b78262013-06-07 23:10:03 +02004253 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004254 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004255 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004256
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004257 if (HAS_PCH_IBX(dev_priv->dev)) {
4258 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004259 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004260 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004261
Daniel Vetter46edb022013-06-05 13:34:12 +02004262 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4263 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004264
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004265 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004266
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004267 goto found;
4268 }
4269
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304270 if (IS_BROXTON(dev_priv->dev)) {
4271 /* PLL is attached to port in bxt */
4272 struct intel_encoder *encoder;
4273 struct intel_digital_port *intel_dig_port;
4274
4275 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4276 if (WARN_ON(!encoder))
4277 return NULL;
4278
4279 intel_dig_port = enc_to_dig_port(&encoder->base);
4280 /* 1:1 mapping between ports and PLLs */
4281 i = (enum intel_dpll_id)intel_dig_port->port;
4282 pll = &dev_priv->shared_dplls[i];
4283 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4284 crtc->base.base.id, pll->name);
4285 WARN_ON(pll->new_config->crtc_mask);
4286
4287 goto found;
4288 }
4289
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004290 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4291 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004292
4293 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004294 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004295 continue;
4296
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004297 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004298 &pll->new_config->hw_state,
4299 sizeof(pll->new_config->hw_state)) == 0) {
4300 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004301 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004302 pll->new_config->crtc_mask,
4303 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004304 goto found;
4305 }
4306 }
4307
4308 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004309 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4310 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004311 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004312 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4313 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004314 goto found;
4315 }
4316 }
4317
4318 return NULL;
4319
4320found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004321 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004322 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004323
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004324 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004325 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4326 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004327
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004328 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004329
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004330 return pll;
4331}
4332
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004333/**
4334 * intel_shared_dpll_start_config - start a new PLL staged config
4335 * @dev_priv: DRM device
4336 * @clear_pipes: mask of pipes that will have their PLLs freed
4337 *
4338 * Starts a new PLL staged config, copying the current config but
4339 * releasing the references of pipes specified in clear_pipes.
4340 */
4341static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4342 unsigned clear_pipes)
4343{
4344 struct intel_shared_dpll *pll;
4345 enum intel_dpll_id i;
4346
4347 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4348 pll = &dev_priv->shared_dplls[i];
4349
4350 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4351 GFP_KERNEL);
4352 if (!pll->new_config)
4353 goto cleanup;
4354
4355 pll->new_config->crtc_mask &= ~clear_pipes;
4356 }
4357
4358 return 0;
4359
4360cleanup:
4361 while (--i >= 0) {
4362 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004363 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004364 pll->new_config = NULL;
4365 }
4366
4367 return -ENOMEM;
4368}
4369
4370static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4371{
4372 struct intel_shared_dpll *pll;
4373 enum intel_dpll_id i;
4374
4375 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4376 pll = &dev_priv->shared_dplls[i];
4377
4378 WARN_ON(pll->new_config == &pll->config);
4379
4380 pll->config = *pll->new_config;
4381 kfree(pll->new_config);
4382 pll->new_config = NULL;
4383 }
4384}
4385
4386static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4387{
4388 struct intel_shared_dpll *pll;
4389 enum intel_dpll_id i;
4390
4391 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4392 pll = &dev_priv->shared_dplls[i];
4393
4394 WARN_ON(pll->new_config == &pll->config);
4395
4396 kfree(pll->new_config);
4397 pll->new_config = NULL;
4398 }
4399}
4400
Daniel Vettera1520312013-05-03 11:49:50 +02004401static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004402{
4403 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004404 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004405 u32 temp;
4406
4407 temp = I915_READ(dslreg);
4408 udelay(500);
4409 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004410 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004411 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004412 }
4413}
4414
Chandra Kondurua1b22782015-04-07 15:28:45 -07004415/**
4416 * skl_update_scaler_users - Stages update to crtc's scaler state
4417 * @intel_crtc: crtc
4418 * @crtc_state: crtc_state
4419 * @plane: plane (NULL indicates crtc is requesting update)
4420 * @plane_state: plane's state
4421 * @force_detach: request unconditional detachment of scaler
4422 *
4423 * This function updates scaler state for requested plane or crtc.
4424 * To request scaler usage update for a plane, caller shall pass plane pointer.
4425 * To request scaler usage update for crtc, caller shall pass plane pointer
4426 * as NULL.
4427 *
4428 * Return
4429 * 0 - scaler_usage updated successfully
4430 * error - requested scaling cannot be supported or other error condition
4431 */
4432int
4433skl_update_scaler_users(
4434 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4435 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4436 int force_detach)
4437{
4438 int need_scaling;
4439 int idx;
4440 int src_w, src_h, dst_w, dst_h;
4441 int *scaler_id;
4442 struct drm_framebuffer *fb;
4443 struct intel_crtc_scaler_state *scaler_state;
Chandra Konduru6156a452015-04-27 13:48:39 -07004444 unsigned int rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004445
4446 if (!intel_crtc || !crtc_state)
4447 return 0;
4448
4449 scaler_state = &crtc_state->scaler_state;
4450
4451 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4452 fb = intel_plane ? plane_state->base.fb : NULL;
4453
4454 if (intel_plane) {
4455 src_w = drm_rect_width(&plane_state->src) >> 16;
4456 src_h = drm_rect_height(&plane_state->src) >> 16;
4457 dst_w = drm_rect_width(&plane_state->dst);
4458 dst_h = drm_rect_height(&plane_state->dst);
4459 scaler_id = &plane_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004460 rotation = plane_state->base.rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004461 } else {
4462 struct drm_display_mode *adjusted_mode =
4463 &crtc_state->base.adjusted_mode;
4464 src_w = crtc_state->pipe_src_w;
4465 src_h = crtc_state->pipe_src_h;
4466 dst_w = adjusted_mode->hdisplay;
4467 dst_h = adjusted_mode->vdisplay;
4468 scaler_id = &scaler_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004469 rotation = DRM_ROTATE_0;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004470 }
Chandra Konduru6156a452015-04-27 13:48:39 -07004471
4472 need_scaling = intel_rotation_90_or_270(rotation) ?
4473 (src_h != dst_w || src_w != dst_h):
4474 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004475
4476 /*
4477 * if plane is being disabled or scaler is no more required or force detach
4478 * - free scaler binded to this plane/crtc
4479 * - in order to do this, update crtc->scaler_usage
4480 *
4481 * Here scaler state in crtc_state is set free so that
4482 * scaler can be assigned to other user. Actual register
4483 * update to free the scaler is done in plane/panel-fit programming.
4484 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4485 */
4486 if (force_detach || !need_scaling || (intel_plane &&
4487 (!fb || !plane_state->visible))) {
4488 if (*scaler_id >= 0) {
4489 scaler_state->scaler_users &= ~(1 << idx);
4490 scaler_state->scalers[*scaler_id].in_use = 0;
4491
4492 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4493 "crtc_state = %p scaler_users = 0x%x\n",
4494 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4495 intel_plane ? intel_plane->base.base.id :
4496 intel_crtc->base.base.id, crtc_state,
4497 scaler_state->scaler_users);
4498 *scaler_id = -1;
4499 }
4500 return 0;
4501 }
4502
4503 /* range checks */
4504 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4505 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4506
4507 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4508 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4509 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4510 "size is out of scaler range\n",
4511 intel_plane ? "PLANE" : "CRTC",
4512 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4513 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4514 return -EINVAL;
4515 }
4516
4517 /* check colorkey */
4518 if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
4519 DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
4520 intel_plane->base.base.id);
4521 return -EINVAL;
4522 }
4523
4524 /* Check src format */
4525 if (intel_plane) {
4526 switch (fb->pixel_format) {
4527 case DRM_FORMAT_RGB565:
4528 case DRM_FORMAT_XBGR8888:
4529 case DRM_FORMAT_XRGB8888:
4530 case DRM_FORMAT_ABGR8888:
4531 case DRM_FORMAT_ARGB8888:
4532 case DRM_FORMAT_XRGB2101010:
4533 case DRM_FORMAT_ARGB2101010:
4534 case DRM_FORMAT_XBGR2101010:
4535 case DRM_FORMAT_ABGR2101010:
4536 case DRM_FORMAT_YUYV:
4537 case DRM_FORMAT_YVYU:
4538 case DRM_FORMAT_UYVY:
4539 case DRM_FORMAT_VYUY:
4540 break;
4541 default:
4542 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4543 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4544 return -EINVAL;
4545 }
4546 }
4547
4548 /* mark this plane as a scaler user in crtc_state */
4549 scaler_state->scaler_users |= (1 << idx);
4550 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4551 "crtc_state = %p scaler_users = 0x%x\n",
4552 intel_plane ? "PLANE" : "CRTC",
4553 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4554 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4555 return 0;
4556}
4557
4558static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004559{
4560 struct drm_device *dev = crtc->base.dev;
4561 struct drm_i915_private *dev_priv = dev->dev_private;
4562 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004563 struct intel_crtc_scaler_state *scaler_state =
4564 &crtc->config->scaler_state;
4565
4566 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4567
4568 /* To update pfit, first update scaler state */
4569 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4570 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4571 skl_detach_scalers(crtc);
4572 if (!enable)
4573 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004574
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004575 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004576 int id;
4577
4578 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4579 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4580 return;
4581 }
4582
4583 id = scaler_state->scaler_id;
4584 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4585 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4586 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4587 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4588
4589 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004590 }
4591}
4592
Jesse Barnesb074cec2013-04-25 12:55:02 -07004593static void ironlake_pfit_enable(struct intel_crtc *crtc)
4594{
4595 struct drm_device *dev = crtc->base.dev;
4596 struct drm_i915_private *dev_priv = dev->dev_private;
4597 int pipe = crtc->pipe;
4598
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004599 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004600 /* Force use of hard-coded filter coefficients
4601 * as some pre-programmed values are broken,
4602 * e.g. x201.
4603 */
4604 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4605 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4606 PF_PIPE_SEL_IVB(pipe));
4607 else
4608 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004609 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4610 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004611 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004612}
4613
Matt Roper4a3b8762014-12-23 10:41:51 -08004614static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004615{
4616 struct drm_device *dev = crtc->dev;
4617 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004618 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004619 struct intel_plane *intel_plane;
4620
Matt Roperaf2b6532014-04-01 15:22:32 -07004621 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4622 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004623 if (intel_plane->pipe == pipe)
4624 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004625 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004626}
4627
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004628void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004629{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004630 struct drm_device *dev = crtc->base.dev;
4631 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004632
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004633 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004634 return;
4635
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004636 /* We can only enable IPS after we enable a plane and wait for a vblank */
4637 intel_wait_for_vblank(dev, crtc->pipe);
4638
Paulo Zanonid77e4532013-09-24 13:52:55 -03004639 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004640 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004641 mutex_lock(&dev_priv->rps.hw_lock);
4642 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4643 mutex_unlock(&dev_priv->rps.hw_lock);
4644 /* Quoting Art Runyan: "its not safe to expect any particular
4645 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004646 * mailbox." Moreover, the mailbox may return a bogus state,
4647 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004648 */
4649 } else {
4650 I915_WRITE(IPS_CTL, IPS_ENABLE);
4651 /* The bit only becomes 1 in the next vblank, so this wait here
4652 * is essentially intel_wait_for_vblank. If we don't have this
4653 * and don't wait for vblanks until the end of crtc_enable, then
4654 * the HW state readout code will complain that the expected
4655 * IPS_CTL value is not the one we read. */
4656 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4657 DRM_ERROR("Timed out waiting for IPS enable\n");
4658 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004659}
4660
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004661void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004662{
4663 struct drm_device *dev = crtc->base.dev;
4664 struct drm_i915_private *dev_priv = dev->dev_private;
4665
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004666 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004667 return;
4668
4669 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004670 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004671 mutex_lock(&dev_priv->rps.hw_lock);
4672 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4673 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004674 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4675 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4676 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004677 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004678 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004679 POSTING_READ(IPS_CTL);
4680 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004681
4682 /* We need to wait for a vblank before we can disable the plane. */
4683 intel_wait_for_vblank(dev, crtc->pipe);
4684}
4685
4686/** Loads the palette/gamma unit for the CRTC with the prepared values */
4687static void intel_crtc_load_lut(struct drm_crtc *crtc)
4688{
4689 struct drm_device *dev = crtc->dev;
4690 struct drm_i915_private *dev_priv = dev->dev_private;
4691 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4692 enum pipe pipe = intel_crtc->pipe;
4693 int palreg = PALETTE(pipe);
4694 int i;
4695 bool reenable_ips = false;
4696
4697 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004698 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004699 return;
4700
Imre Deak50360402015-01-16 00:55:16 -08004701 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004702 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004703 assert_dsi_pll_enabled(dev_priv);
4704 else
4705 assert_pll_enabled(dev_priv, pipe);
4706 }
4707
4708 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304709 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004710 palreg = LGC_PALETTE(pipe);
4711
4712 /* Workaround : Do not read or write the pipe palette/gamma data while
4713 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4714 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004715 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004716 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4717 GAMMA_MODE_MODE_SPLIT)) {
4718 hsw_disable_ips(intel_crtc);
4719 reenable_ips = true;
4720 }
4721
4722 for (i = 0; i < 256; i++) {
4723 I915_WRITE(palreg + 4 * i,
4724 (intel_crtc->lut_r[i] << 16) |
4725 (intel_crtc->lut_g[i] << 8) |
4726 intel_crtc->lut_b[i]);
4727 }
4728
4729 if (reenable_ips)
4730 hsw_enable_ips(intel_crtc);
4731}
4732
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004733static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004734{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004735 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004736 struct drm_device *dev = intel_crtc->base.dev;
4737 struct drm_i915_private *dev_priv = dev->dev_private;
4738
4739 mutex_lock(&dev->struct_mutex);
4740 dev_priv->mm.interruptible = false;
4741 (void) intel_overlay_switch_off(intel_crtc->overlay);
4742 dev_priv->mm.interruptible = true;
4743 mutex_unlock(&dev->struct_mutex);
4744 }
4745
4746 /* Let userspace switch the overlay on again. In most cases userspace
4747 * has to recompute where to put it anyway.
4748 */
4749}
4750
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004751/**
4752 * intel_post_enable_primary - Perform operations after enabling primary plane
4753 * @crtc: the CRTC whose primary plane was just enabled
4754 *
4755 * Performs potentially sleeping operations that must be done after the primary
4756 * plane is enabled, such as updating FBC and IPS. Note that this may be
4757 * called due to an explicit primary plane update, or due to an implicit
4758 * re-enable that is caused when a sprite plane is updated to no longer
4759 * completely hide the primary plane.
4760 */
4761static void
4762intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004763{
4764 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004765 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004766 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4767 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004768
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004769 /*
4770 * BDW signals flip done immediately if the plane
4771 * is disabled, even if the plane enable is already
4772 * armed to occur at the next vblank :(
4773 */
4774 if (IS_BROADWELL(dev))
4775 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004776
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004777 /*
4778 * FIXME IPS should be fine as long as one plane is
4779 * enabled, but in practice it seems to have problems
4780 * when going from primary only to sprite only and vice
4781 * versa.
4782 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004783 hsw_enable_ips(intel_crtc);
4784
4785 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004786 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004787 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004788
4789 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004790 * Gen2 reports pipe underruns whenever all planes are disabled.
4791 * So don't enable underrun reporting before at least some planes
4792 * are enabled.
4793 * FIXME: Need to fix the logic to work when we turn off all planes
4794 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004795 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004796 if (IS_GEN2(dev))
4797 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4798
4799 /* Underruns don't raise interrupts, so check manually. */
4800 if (HAS_GMCH_DISPLAY(dev))
4801 i9xx_check_fifo_underruns(dev_priv);
4802}
4803
4804/**
4805 * intel_pre_disable_primary - Perform operations before disabling primary plane
4806 * @crtc: the CRTC whose primary plane is to be disabled
4807 *
4808 * Performs potentially sleeping operations that must be done before the
4809 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4810 * be called due to an explicit primary plane update, or due to an implicit
4811 * disable that is caused when a sprite plane completely hides the primary
4812 * plane.
4813 */
4814static void
4815intel_pre_disable_primary(struct drm_crtc *crtc)
4816{
4817 struct drm_device *dev = crtc->dev;
4818 struct drm_i915_private *dev_priv = dev->dev_private;
4819 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4820 int pipe = intel_crtc->pipe;
4821
4822 /*
4823 * Gen2 reports pipe underruns whenever all planes are disabled.
4824 * So diasble underrun reporting before all the planes get disabled.
4825 * FIXME: Need to fix the logic to work when we turn off all planes
4826 * but leave the pipe running.
4827 */
4828 if (IS_GEN2(dev))
4829 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4830
4831 /*
4832 * Vblank time updates from the shadow to live plane control register
4833 * are blocked if the memory self-refresh mode is active at that
4834 * moment. So to make sure the plane gets truly disabled, disable
4835 * first the self-refresh mode. The self-refresh enable bit in turn
4836 * will be checked/applied by the HW only at the next frame start
4837 * event which is after the vblank start event, so we need to have a
4838 * wait-for-vblank between disabling the plane and the pipe.
4839 */
4840 if (HAS_GMCH_DISPLAY(dev))
4841 intel_set_memory_cxsr(dev_priv, false);
4842
4843 mutex_lock(&dev->struct_mutex);
4844 if (dev_priv->fbc.crtc == intel_crtc)
4845 intel_fbc_disable(dev);
4846 mutex_unlock(&dev->struct_mutex);
4847
4848 /*
4849 * FIXME IPS should be fine as long as one plane is
4850 * enabled, but in practice it seems to have problems
4851 * when going from primary only to sprite only and vice
4852 * versa.
4853 */
4854 hsw_disable_ips(intel_crtc);
4855}
4856
4857static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4858{
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004859 intel_enable_primary_hw_plane(crtc->primary, crtc);
4860 intel_enable_sprite_planes(crtc);
4861 intel_crtc_update_cursor(crtc, true);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004862
4863 intel_post_enable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004864}
4865
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004866static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004867{
4868 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004869 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004870 struct intel_plane *intel_plane;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004871 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004872
4873 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004874
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004875 intel_pre_disable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004876
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004877 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004878 for_each_intel_plane(dev, intel_plane) {
4879 if (intel_plane->pipe == pipe) {
4880 struct drm_crtc *from = intel_plane->base.crtc;
4881
4882 intel_plane->disable_plane(&intel_plane->base,
4883 from ?: crtc, true);
4884 }
4885 }
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004886
Daniel Vetterf99d7062014-06-19 16:01:59 +02004887 /*
4888 * FIXME: Once we grow proper nuclear flip support out of this we need
4889 * to compute the mask of flip planes precisely. For the time being
4890 * consider this a flip to a NULL plane.
4891 */
4892 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004893}
4894
Jesse Barnesf67a5592011-01-05 10:31:48 -08004895static void ironlake_crtc_enable(struct drm_crtc *crtc)
4896{
4897 struct drm_device *dev = crtc->dev;
4898 struct drm_i915_private *dev_priv = dev->dev_private;
4899 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004900 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004901 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004902
Matt Roper83d65732015-02-25 13:12:16 -08004903 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004904
Jesse Barnesf67a5592011-01-05 10:31:48 -08004905 if (intel_crtc->active)
4906 return;
4907
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004908 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004909 intel_prepare_shared_dpll(intel_crtc);
4910
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004911 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304912 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004913
4914 intel_set_pipe_timings(intel_crtc);
4915
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004916 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004917 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004918 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004919 }
4920
4921 ironlake_set_pipeconf(crtc);
4922
Jesse Barnesf67a5592011-01-05 10:31:48 -08004923 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004924
Daniel Vettera72e4c92014-09-30 10:56:47 +02004925 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4926 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004927
Daniel Vetterf6736a12013-06-05 13:34:30 +02004928 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004929 if (encoder->pre_enable)
4930 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004931
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004932 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004933 /* Note: FDI PLL enabling _must_ be done before we enable the
4934 * cpu pipes, hence this is separate from all the other fdi/pch
4935 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004936 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004937 } else {
4938 assert_fdi_tx_disabled(dev_priv, pipe);
4939 assert_fdi_rx_disabled(dev_priv, pipe);
4940 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004941
Jesse Barnesb074cec2013-04-25 12:55:02 -07004942 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004943
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004944 /*
4945 * On ILK+ LUT must be loaded before the pipe is running but with
4946 * clocks enabled
4947 */
4948 intel_crtc_load_lut(crtc);
4949
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004950 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004951 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004952
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004953 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004954 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004955
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004956 assert_vblank_disabled(crtc);
4957 drm_crtc_vblank_on(crtc);
4958
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004959 for_each_encoder_on_crtc(dev, crtc, encoder)
4960 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004961
4962 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004963 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004964}
4965
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004966/* IPS only exists on ULT machines and is tied to pipe A. */
4967static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4968{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004969 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004970}
4971
Paulo Zanonie4916942013-09-20 16:21:19 -03004972/*
4973 * This implements the workaround described in the "notes" section of the mode
4974 * set sequence documentation. When going from no pipes or single pipe to
4975 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4976 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4977 */
4978static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4979{
4980 struct drm_device *dev = crtc->base.dev;
4981 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4982
4983 /* We want to get the other_active_crtc only if there's only 1 other
4984 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004985 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004986 if (!crtc_it->active || crtc_it == crtc)
4987 continue;
4988
4989 if (other_active_crtc)
4990 return;
4991
4992 other_active_crtc = crtc_it;
4993 }
4994 if (!other_active_crtc)
4995 return;
4996
4997 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4998 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4999}
5000
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005001static void haswell_crtc_enable(struct drm_crtc *crtc)
5002{
5003 struct drm_device *dev = crtc->dev;
5004 struct drm_i915_private *dev_priv = dev->dev_private;
5005 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5006 struct intel_encoder *encoder;
5007 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005008
Matt Roper83d65732015-02-25 13:12:16 -08005009 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005010
5011 if (intel_crtc->active)
5012 return;
5013
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005014 if (intel_crtc_to_shared_dpll(intel_crtc))
5015 intel_enable_shared_dpll(intel_crtc);
5016
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005017 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305018 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005019
5020 intel_set_pipe_timings(intel_crtc);
5021
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005022 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
5023 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
5024 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005025 }
5026
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005027 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005028 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005029 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005030 }
5031
5032 haswell_set_pipeconf(crtc);
5033
5034 intel_set_pipe_csc(crtc);
5035
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005036 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005037
Daniel Vettera72e4c92014-09-30 10:56:47 +02005038 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005039 for_each_encoder_on_crtc(dev, crtc, encoder)
5040 if (encoder->pre_enable)
5041 encoder->pre_enable(encoder);
5042
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005043 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02005044 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5045 true);
Imre Deak4fe94672014-06-25 22:01:49 +03005046 dev_priv->display.fdi_link_train(crtc);
5047 }
5048
Paulo Zanoni1f544382012-10-24 11:32:00 -02005049 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005050
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005051 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005052 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005053 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005054 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005055 else
5056 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005057
5058 /*
5059 * On ILK+ LUT must be loaded before the pipe is running but with
5060 * clocks enabled
5061 */
5062 intel_crtc_load_lut(crtc);
5063
Paulo Zanoni1f544382012-10-24 11:32:00 -02005064 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00005065 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005066
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005067 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005068 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005069
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005070 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005071 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005072
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005073 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005074 intel_ddi_set_vc_payload_alloc(crtc, true);
5075
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005076 assert_vblank_disabled(crtc);
5077 drm_crtc_vblank_on(crtc);
5078
Jani Nikula8807e552013-08-30 19:40:32 +03005079 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005080 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005081 intel_opregion_notify_encoder(encoder, true);
5082 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005083
Paulo Zanonie4916942013-09-20 16:21:19 -03005084 /* If we change the relative order between pipe/planes enabling, we need
5085 * to change the workaround. */
5086 haswell_mode_set_planes_workaround(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005087}
5088
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005089static void ironlake_pfit_disable(struct intel_crtc *crtc)
5090{
5091 struct drm_device *dev = crtc->base.dev;
5092 struct drm_i915_private *dev_priv = dev->dev_private;
5093 int pipe = crtc->pipe;
5094
5095 /* To avoid upsetting the power well on haswell only disable the pfit if
5096 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005097 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005098 I915_WRITE(PF_CTL(pipe), 0);
5099 I915_WRITE(PF_WIN_POS(pipe), 0);
5100 I915_WRITE(PF_WIN_SZ(pipe), 0);
5101 }
5102}
5103
Jesse Barnes6be4a602010-09-10 10:26:01 -07005104static void ironlake_crtc_disable(struct drm_crtc *crtc)
5105{
5106 struct drm_device *dev = crtc->dev;
5107 struct drm_i915_private *dev_priv = dev->dev_private;
5108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005109 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005110 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005111 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005112
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005113 if (!intel_crtc->active)
5114 return;
5115
Daniel Vetterea9d7582012-07-10 10:42:52 +02005116 for_each_encoder_on_crtc(dev, crtc, encoder)
5117 encoder->disable(encoder);
5118
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005119 drm_crtc_vblank_off(crtc);
5120 assert_vblank_disabled(crtc);
5121
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005122 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005123 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005124
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005125 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005126
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005127 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005128
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005129 for_each_encoder_on_crtc(dev, crtc, encoder)
5130 if (encoder->post_disable)
5131 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005132
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005133 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005134 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005135
Daniel Vetterd925c592013-06-05 13:34:04 +02005136 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005137
Daniel Vetterd925c592013-06-05 13:34:04 +02005138 if (HAS_PCH_CPT(dev)) {
5139 /* disable TRANS_DP_CTL */
5140 reg = TRANS_DP_CTL(pipe);
5141 temp = I915_READ(reg);
5142 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5143 TRANS_DP_PORT_SEL_MASK);
5144 temp |= TRANS_DP_PORT_SEL_NONE;
5145 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005146
Daniel Vetterd925c592013-06-05 13:34:04 +02005147 /* disable DPLL_SEL */
5148 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005149 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005150 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005151 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005152
5153 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005154 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005155
5156 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005157 }
5158
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005159 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005160 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005161
5162 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005163 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005164 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005165}
5166
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005167static void haswell_crtc_disable(struct drm_crtc *crtc)
5168{
5169 struct drm_device *dev = crtc->dev;
5170 struct drm_i915_private *dev_priv = dev->dev_private;
5171 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5172 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005173 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005174
5175 if (!intel_crtc->active)
5176 return;
5177
Jani Nikula8807e552013-08-30 19:40:32 +03005178 for_each_encoder_on_crtc(dev, crtc, encoder) {
5179 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005180 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005181 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005182
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005183 drm_crtc_vblank_off(crtc);
5184 assert_vblank_disabled(crtc);
5185
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005186 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005187 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5188 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005189 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005190
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005191 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005192 intel_ddi_set_vc_payload_alloc(crtc, false);
5193
Paulo Zanoniad80a812012-10-24 16:06:19 -02005194 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005195
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005196 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005197 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005198 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005199 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005200 else
5201 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005202
Paulo Zanoni1f544382012-10-24 11:32:00 -02005203 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005204
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005205 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005206 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005207 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005208 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005209
Imre Deak97b040a2014-06-25 22:01:50 +03005210 for_each_encoder_on_crtc(dev, crtc, encoder)
5211 if (encoder->post_disable)
5212 encoder->post_disable(encoder);
5213
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005214 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005215 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005216
5217 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005218 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005219 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005220
5221 if (intel_crtc_to_shared_dpll(intel_crtc))
5222 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005223}
5224
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005225static void ironlake_crtc_off(struct drm_crtc *crtc)
5226{
5227 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005228 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005229}
5230
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005231
Jesse Barnes2dd24552013-04-25 12:55:01 -07005232static void i9xx_pfit_enable(struct intel_crtc *crtc)
5233{
5234 struct drm_device *dev = crtc->base.dev;
5235 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005236 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005237
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005238 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005239 return;
5240
Daniel Vetterc0b03412013-05-28 12:05:54 +02005241 /*
5242 * The panel fitter should only be adjusted whilst the pipe is disabled,
5243 * according to register description and PRM.
5244 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005245 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5246 assert_pipe_disabled(dev_priv, crtc->pipe);
5247
Jesse Barnesb074cec2013-04-25 12:55:02 -07005248 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5249 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005250
5251 /* Border color in case we don't scale up to the full screen. Black by
5252 * default, change to something else for debugging. */
5253 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005254}
5255
Dave Airlied05410f2014-06-05 13:22:59 +10005256static enum intel_display_power_domain port_to_power_domain(enum port port)
5257{
5258 switch (port) {
5259 case PORT_A:
5260 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5261 case PORT_B:
5262 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5263 case PORT_C:
5264 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5265 case PORT_D:
5266 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5267 default:
5268 WARN_ON_ONCE(1);
5269 return POWER_DOMAIN_PORT_OTHER;
5270 }
5271}
5272
Imre Deak77d22dc2014-03-05 16:20:52 +02005273#define for_each_power_domain(domain, mask) \
5274 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5275 if ((1 << (domain)) & (mask))
5276
Imre Deak319be8a2014-03-04 19:22:57 +02005277enum intel_display_power_domain
5278intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005279{
Imre Deak319be8a2014-03-04 19:22:57 +02005280 struct drm_device *dev = intel_encoder->base.dev;
5281 struct intel_digital_port *intel_dig_port;
5282
5283 switch (intel_encoder->type) {
5284 case INTEL_OUTPUT_UNKNOWN:
5285 /* Only DDI platforms should ever use this output type */
5286 WARN_ON_ONCE(!HAS_DDI(dev));
5287 case INTEL_OUTPUT_DISPLAYPORT:
5288 case INTEL_OUTPUT_HDMI:
5289 case INTEL_OUTPUT_EDP:
5290 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005291 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005292 case INTEL_OUTPUT_DP_MST:
5293 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5294 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005295 case INTEL_OUTPUT_ANALOG:
5296 return POWER_DOMAIN_PORT_CRT;
5297 case INTEL_OUTPUT_DSI:
5298 return POWER_DOMAIN_PORT_DSI;
5299 default:
5300 return POWER_DOMAIN_PORT_OTHER;
5301 }
5302}
5303
5304static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5305{
5306 struct drm_device *dev = crtc->dev;
5307 struct intel_encoder *intel_encoder;
5308 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5309 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005310 unsigned long mask;
5311 enum transcoder transcoder;
5312
5313 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5314
5315 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5316 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005317 if (intel_crtc->config->pch_pfit.enabled ||
5318 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005319 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5320
Imre Deak319be8a2014-03-04 19:22:57 +02005321 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5322 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5323
Imre Deak77d22dc2014-03-05 16:20:52 +02005324 return mask;
5325}
5326
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005327static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005328{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005329 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005330 struct drm_i915_private *dev_priv = dev->dev_private;
5331 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5332 struct intel_crtc *crtc;
5333
5334 /*
5335 * First get all needed power domains, then put all unneeded, to avoid
5336 * any unnecessary toggling of the power wells.
5337 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005338 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005339 enum intel_display_power_domain domain;
5340
Matt Roper83d65732015-02-25 13:12:16 -08005341 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005342 continue;
5343
Imre Deak319be8a2014-03-04 19:22:57 +02005344 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005345
5346 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5347 intel_display_power_get(dev_priv, domain);
5348 }
5349
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005350 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005351 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005352
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005353 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005354 enum intel_display_power_domain domain;
5355
5356 for_each_power_domain(domain, crtc->enabled_power_domains)
5357 intel_display_power_put(dev_priv, domain);
5358
5359 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5360 }
5361
5362 intel_display_set_init_power(dev_priv, false);
5363}
5364
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305365void broxton_set_cdclk(struct drm_device *dev, int frequency)
5366{
5367 struct drm_i915_private *dev_priv = dev->dev_private;
5368 uint32_t divider;
5369 uint32_t ratio;
5370 uint32_t current_freq;
5371 int ret;
5372
5373 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5374 switch (frequency) {
5375 case 144000:
5376 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5377 ratio = BXT_DE_PLL_RATIO(60);
5378 break;
5379 case 288000:
5380 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5381 ratio = BXT_DE_PLL_RATIO(60);
5382 break;
5383 case 384000:
5384 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5385 ratio = BXT_DE_PLL_RATIO(60);
5386 break;
5387 case 576000:
5388 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5389 ratio = BXT_DE_PLL_RATIO(60);
5390 break;
5391 case 624000:
5392 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5393 ratio = BXT_DE_PLL_RATIO(65);
5394 break;
5395 case 19200:
5396 /*
5397 * Bypass frequency with DE PLL disabled. Init ratio, divider
5398 * to suppress GCC warning.
5399 */
5400 ratio = 0;
5401 divider = 0;
5402 break;
5403 default:
5404 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5405
5406 return;
5407 }
5408
5409 mutex_lock(&dev_priv->rps.hw_lock);
5410 /* Inform power controller of upcoming frequency change */
5411 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5412 0x80000000);
5413 mutex_unlock(&dev_priv->rps.hw_lock);
5414
5415 if (ret) {
5416 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5417 ret, frequency);
5418 return;
5419 }
5420
5421 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5422 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5423 current_freq = current_freq * 500 + 1000;
5424
5425 /*
5426 * DE PLL has to be disabled when
5427 * - setting to 19.2MHz (bypass, PLL isn't used)
5428 * - before setting to 624MHz (PLL needs toggling)
5429 * - before setting to any frequency from 624MHz (PLL needs toggling)
5430 */
5431 if (frequency == 19200 || frequency == 624000 ||
5432 current_freq == 624000) {
5433 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5434 /* Timeout 200us */
5435 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5436 1))
5437 DRM_ERROR("timout waiting for DE PLL unlock\n");
5438 }
5439
5440 if (frequency != 19200) {
5441 uint32_t val;
5442
5443 val = I915_READ(BXT_DE_PLL_CTL);
5444 val &= ~BXT_DE_PLL_RATIO_MASK;
5445 val |= ratio;
5446 I915_WRITE(BXT_DE_PLL_CTL, val);
5447
5448 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5449 /* Timeout 200us */
5450 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5451 DRM_ERROR("timeout waiting for DE PLL lock\n");
5452
5453 val = I915_READ(CDCLK_CTL);
5454 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5455 val |= divider;
5456 /*
5457 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5458 * enable otherwise.
5459 */
5460 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5461 if (frequency >= 500000)
5462 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5463
5464 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5465 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5466 val |= (frequency - 1000) / 500;
5467 I915_WRITE(CDCLK_CTL, val);
5468 }
5469
5470 mutex_lock(&dev_priv->rps.hw_lock);
5471 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5472 DIV_ROUND_UP(frequency, 25000));
5473 mutex_unlock(&dev_priv->rps.hw_lock);
5474
5475 if (ret) {
5476 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5477 ret, frequency);
5478 return;
5479 }
5480
5481 dev_priv->cdclk_freq = frequency;
5482}
5483
5484void broxton_init_cdclk(struct drm_device *dev)
5485{
5486 struct drm_i915_private *dev_priv = dev->dev_private;
5487 uint32_t val;
5488
5489 /*
5490 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5491 * or else the reset will hang because there is no PCH to respond.
5492 * Move the handshake programming to initialization sequence.
5493 * Previously was left up to BIOS.
5494 */
5495 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5496 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5497 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5498
5499 /* Enable PG1 for cdclk */
5500 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5501
5502 /* check if cd clock is enabled */
5503 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5504 DRM_DEBUG_KMS("Display already initialized\n");
5505 return;
5506 }
5507
5508 /*
5509 * FIXME:
5510 * - The initial CDCLK needs to be read from VBT.
5511 * Need to make this change after VBT has changes for BXT.
5512 * - check if setting the max (or any) cdclk freq is really necessary
5513 * here, it belongs to modeset time
5514 */
5515 broxton_set_cdclk(dev, 624000);
5516
5517 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005518 POSTING_READ(DBUF_CTL);
5519
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305520 udelay(10);
5521
5522 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5523 DRM_ERROR("DBuf power enable timeout!\n");
5524}
5525
5526void broxton_uninit_cdclk(struct drm_device *dev)
5527{
5528 struct drm_i915_private *dev_priv = dev->dev_private;
5529
5530 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005531 POSTING_READ(DBUF_CTL);
5532
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305533 udelay(10);
5534
5535 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5536 DRM_ERROR("DBuf power disable timeout!\n");
5537
5538 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5539 broxton_set_cdclk(dev, 19200);
5540
5541 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5542}
5543
Ville Syrjälädfcab172014-06-13 13:37:47 +03005544/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005545static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005546{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005547 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005548
Jesse Barnes586f49d2013-11-04 16:06:59 -08005549 /* Obtain SKU information */
5550 mutex_lock(&dev_priv->dpio_lock);
5551 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5552 CCK_FUSE_HPLL_FREQ_MASK;
5553 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005554
Ville Syrjälädfcab172014-06-13 13:37:47 +03005555 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005556}
5557
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005558static void vlv_update_cdclk(struct drm_device *dev)
5559{
5560 struct drm_i915_private *dev_priv = dev->dev_private;
5561
Vandana Kannan164dfd22014-11-24 13:37:41 +05305562 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03005563 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Vandana Kannan164dfd22014-11-24 13:37:41 +05305564 dev_priv->cdclk_freq);
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005565
5566 /*
5567 * Program the gmbus_freq based on the cdclk frequency.
5568 * BSpec erroneously claims we should aim for 4MHz, but
5569 * in fact 1MHz is the correct frequency.
5570 */
Vandana Kannan164dfd22014-11-24 13:37:41 +05305571 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005572}
5573
Jesse Barnes30a970c2013-11-04 13:48:12 -08005574/* Adjust CDclk dividers to allow high res or save power if possible */
5575static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5576{
5577 struct drm_i915_private *dev_priv = dev->dev_private;
5578 u32 val, cmd;
5579
Vandana Kannan164dfd22014-11-24 13:37:41 +05305580 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5581 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005582
Ville Syrjälädfcab172014-06-13 13:37:47 +03005583 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005584 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005585 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005586 cmd = 1;
5587 else
5588 cmd = 0;
5589
5590 mutex_lock(&dev_priv->rps.hw_lock);
5591 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5592 val &= ~DSPFREQGUAR_MASK;
5593 val |= (cmd << DSPFREQGUAR_SHIFT);
5594 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5595 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5596 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5597 50)) {
5598 DRM_ERROR("timed out waiting for CDclk change\n");
5599 }
5600 mutex_unlock(&dev_priv->rps.hw_lock);
5601
Ville Syrjälädfcab172014-06-13 13:37:47 +03005602 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005603 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005604
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005605 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005606
5607 mutex_lock(&dev_priv->dpio_lock);
5608 /* adjust cdclk divider */
5609 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005610 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005611 val |= divider;
5612 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005613
5614 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5615 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5616 50))
5617 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005618 mutex_unlock(&dev_priv->dpio_lock);
5619 }
5620
5621 mutex_lock(&dev_priv->dpio_lock);
5622 /* adjust self-refresh exit latency value */
5623 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5624 val &= ~0x7f;
5625
5626 /*
5627 * For high bandwidth configs, we set a higher latency in the bunit
5628 * so that the core display fetch happens in time to avoid underruns.
5629 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005630 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005631 val |= 4500 / 250; /* 4.5 usec */
5632 else
5633 val |= 3000 / 250; /* 3.0 usec */
5634 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5635 mutex_unlock(&dev_priv->dpio_lock);
5636
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005637 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005638}
5639
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005640static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5641{
5642 struct drm_i915_private *dev_priv = dev->dev_private;
5643 u32 val, cmd;
5644
Vandana Kannan164dfd22014-11-24 13:37:41 +05305645 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5646 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005647
5648 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005649 case 333333:
5650 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005651 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005652 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005653 break;
5654 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005655 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005656 return;
5657 }
5658
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005659 /*
5660 * Specs are full of misinformation, but testing on actual
5661 * hardware has shown that we just need to write the desired
5662 * CCK divider into the Punit register.
5663 */
5664 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5665
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005666 mutex_lock(&dev_priv->rps.hw_lock);
5667 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5668 val &= ~DSPFREQGUAR_MASK_CHV;
5669 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5670 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5671 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5672 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5673 50)) {
5674 DRM_ERROR("timed out waiting for CDclk change\n");
5675 }
5676 mutex_unlock(&dev_priv->rps.hw_lock);
5677
5678 vlv_update_cdclk(dev);
5679}
5680
Jesse Barnes30a970c2013-11-04 13:48:12 -08005681static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5682 int max_pixclk)
5683{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005684 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005685 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005686
Jesse Barnes30a970c2013-11-04 13:48:12 -08005687 /*
5688 * Really only a few cases to deal with, as only 4 CDclks are supported:
5689 * 200MHz
5690 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005691 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005692 * 400MHz (VLV only)
5693 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5694 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005695 *
5696 * We seem to get an unstable or solid color picture at 200MHz.
5697 * Not sure what's wrong. For now use 200MHz only when all pipes
5698 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005699 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005700 if (!IS_CHERRYVIEW(dev_priv) &&
5701 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005702 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005703 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005704 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005705 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005706 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005707 else
5708 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005709}
5710
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305711static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5712 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005713{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305714 /*
5715 * FIXME:
5716 * - remove the guardband, it's not needed on BXT
5717 * - set 19.2MHz bypass frequency if there are no active pipes
5718 */
5719 if (max_pixclk > 576000*9/10)
5720 return 624000;
5721 else if (max_pixclk > 384000*9/10)
5722 return 576000;
5723 else if (max_pixclk > 288000*9/10)
5724 return 384000;
5725 else if (max_pixclk > 144000*9/10)
5726 return 288000;
5727 else
5728 return 144000;
5729}
5730
Jesse Barnes30a970c2013-11-04 13:48:12 -08005731/* compute the max pixel clock for new configuration */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005732static int intel_mode_max_pixclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005733{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005734 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005735 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005736 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005737 int max_pixclk = 0;
5738
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005739 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005740 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5741 if (IS_ERR(crtc_state))
5742 return PTR_ERR(crtc_state);
5743
5744 if (!crtc_state->base.enable)
5745 continue;
5746
5747 max_pixclk = max(max_pixclk,
5748 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005749 }
5750
5751 return max_pixclk;
5752}
5753
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005754static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005755{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005756 struct drm_i915_private *dev_priv = to_i915(state->dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005757 struct drm_crtc *crtc;
5758 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005759 int max_pixclk = intel_mode_max_pixclk(state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005760 int cdclk, i;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005761
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005762 if (max_pixclk < 0)
5763 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005764
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305765 if (IS_VALLEYVIEW(dev_priv))
5766 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5767 else
5768 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5769
5770 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005771 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005772
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005773 /* add all active pipes to the state */
5774 for_each_crtc(state->dev, crtc) {
5775 if (!crtc->state->enable)
5776 continue;
5777
5778 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5779 if (IS_ERR(crtc_state))
5780 return PTR_ERR(crtc_state);
5781 }
5782
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005783 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005784 for_each_crtc_in_state(state, crtc, crtc_state, i)
5785 if (crtc_state->enable)
5786 crtc_state->mode_changed = true;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005787
5788 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005789}
5790
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005791static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5792{
5793 unsigned int credits, default_credits;
5794
5795 if (IS_CHERRYVIEW(dev_priv))
5796 default_credits = PFI_CREDIT(12);
5797 else
5798 default_credits = PFI_CREDIT(8);
5799
Vandana Kannan164dfd22014-11-24 13:37:41 +05305800 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005801 /* CHV suggested value is 31 or 63 */
5802 if (IS_CHERRYVIEW(dev_priv))
5803 credits = PFI_CREDIT_31;
5804 else
5805 credits = PFI_CREDIT(15);
5806 } else {
5807 credits = default_credits;
5808 }
5809
5810 /*
5811 * WA - write default credits before re-programming
5812 * FIXME: should we also set the resend bit here?
5813 */
5814 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5815 default_credits);
5816
5817 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5818 credits | PFI_CREDIT_RESEND);
5819
5820 /*
5821 * FIXME is this guaranteed to clear
5822 * immediately or should we poll for it?
5823 */
5824 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5825}
5826
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005827static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005828{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005829 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005830 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005831 int max_pixclk = intel_mode_max_pixclk(state);
5832 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005833
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005834 /* The only reason this can fail is if we fail to add the crtc_state
5835 * to the atomic state. But that can't happen since the call to
5836 * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
5837 * can't have failed otherwise the mode set would be aborted) added all
5838 * the states already. */
5839 if (WARN_ON(max_pixclk < 0))
5840 return;
5841
5842 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005843
Vandana Kannan164dfd22014-11-24 13:37:41 +05305844 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005845 /*
5846 * FIXME: We can end up here with all power domains off, yet
5847 * with a CDCLK frequency other than the minimum. To account
5848 * for this take the PIPE-A power domain, which covers the HW
5849 * blocks needed for the following programming. This can be
5850 * removed once it's guaranteed that we get here either with
5851 * the minimum CDCLK set, or the required power domains
5852 * enabled.
5853 */
5854 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5855
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005856 if (IS_CHERRYVIEW(dev))
5857 cherryview_set_cdclk(dev, req_cdclk);
5858 else
5859 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005860
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005861 vlv_program_pfi_credits(dev_priv);
5862
Imre Deak738c05c2014-11-19 16:25:37 +02005863 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005864 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005865}
5866
Jesse Barnes89b667f2013-04-18 14:51:36 -07005867static void valleyview_crtc_enable(struct drm_crtc *crtc)
5868{
5869 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005870 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005871 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5872 struct intel_encoder *encoder;
5873 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005874 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005875
Matt Roper83d65732015-02-25 13:12:16 -08005876 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005877
5878 if (intel_crtc->active)
5879 return;
5880
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005881 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305882
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005883 if (!is_dsi) {
5884 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005885 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005886 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005887 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005888 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005889
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005890 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305891 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005892
5893 intel_set_pipe_timings(intel_crtc);
5894
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005895 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5896 struct drm_i915_private *dev_priv = dev->dev_private;
5897
5898 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5899 I915_WRITE(CHV_CANVAS(pipe), 0);
5900 }
5901
Daniel Vetter5b18e572014-04-24 23:55:06 +02005902 i9xx_set_pipeconf(intel_crtc);
5903
Jesse Barnes89b667f2013-04-18 14:51:36 -07005904 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005905
Daniel Vettera72e4c92014-09-30 10:56:47 +02005906 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005907
Jesse Barnes89b667f2013-04-18 14:51:36 -07005908 for_each_encoder_on_crtc(dev, crtc, encoder)
5909 if (encoder->pre_pll_enable)
5910 encoder->pre_pll_enable(encoder);
5911
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005912 if (!is_dsi) {
5913 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005914 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005915 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005916 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005917 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005918
5919 for_each_encoder_on_crtc(dev, crtc, encoder)
5920 if (encoder->pre_enable)
5921 encoder->pre_enable(encoder);
5922
Jesse Barnes2dd24552013-04-25 12:55:01 -07005923 i9xx_pfit_enable(intel_crtc);
5924
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005925 intel_crtc_load_lut(crtc);
5926
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005927 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005928 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005929
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005930 assert_vblank_disabled(crtc);
5931 drm_crtc_vblank_on(crtc);
5932
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005933 for_each_encoder_on_crtc(dev, crtc, encoder)
5934 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005935}
5936
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005937static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5938{
5939 struct drm_device *dev = crtc->base.dev;
5940 struct drm_i915_private *dev_priv = dev->dev_private;
5941
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005942 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5943 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005944}
5945
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005946static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005947{
5948 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005949 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005950 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005951 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005952 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005953
Matt Roper83d65732015-02-25 13:12:16 -08005954 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02005955
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005956 if (intel_crtc->active)
5957 return;
5958
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005959 i9xx_set_pll_dividers(intel_crtc);
5960
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005961 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305962 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005963
5964 intel_set_pipe_timings(intel_crtc);
5965
Daniel Vetter5b18e572014-04-24 23:55:06 +02005966 i9xx_set_pipeconf(intel_crtc);
5967
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005968 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005969
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005970 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005971 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005972
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005973 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005974 if (encoder->pre_enable)
5975 encoder->pre_enable(encoder);
5976
Daniel Vetterf6736a12013-06-05 13:34:30 +02005977 i9xx_enable_pll(intel_crtc);
5978
Jesse Barnes2dd24552013-04-25 12:55:01 -07005979 i9xx_pfit_enable(intel_crtc);
5980
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005981 intel_crtc_load_lut(crtc);
5982
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005983 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005984 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005985
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005986 assert_vblank_disabled(crtc);
5987 drm_crtc_vblank_on(crtc);
5988
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005989 for_each_encoder_on_crtc(dev, crtc, encoder)
5990 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005991}
5992
Daniel Vetter87476d62013-04-11 16:29:06 +02005993static void i9xx_pfit_disable(struct intel_crtc *crtc)
5994{
5995 struct drm_device *dev = crtc->base.dev;
5996 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02005997
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005998 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005999 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006000
6001 assert_pipe_disabled(dev_priv, crtc->pipe);
6002
Daniel Vetter328d8e82013-05-08 10:36:31 +02006003 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6004 I915_READ(PFIT_CONTROL));
6005 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006006}
6007
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006008static void i9xx_crtc_disable(struct drm_crtc *crtc)
6009{
6010 struct drm_device *dev = crtc->dev;
6011 struct drm_i915_private *dev_priv = dev->dev_private;
6012 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006013 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006014 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006015
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006016 if (!intel_crtc->active)
6017 return;
6018
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006019 /*
6020 * On gen2 planes are double buffered but the pipe isn't, so we must
6021 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006022 * We also need to wait on all gmch platforms because of the
6023 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006024 */
Imre Deak564ed192014-06-13 14:54:21 +03006025 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006026
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006027 for_each_encoder_on_crtc(dev, crtc, encoder)
6028 encoder->disable(encoder);
6029
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006030 drm_crtc_vblank_off(crtc);
6031 assert_vblank_disabled(crtc);
6032
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006033 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006034
Daniel Vetter87476d62013-04-11 16:29:06 +02006035 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006036
Jesse Barnes89b667f2013-04-18 14:51:36 -07006037 for_each_encoder_on_crtc(dev, crtc, encoder)
6038 if (encoder->post_disable)
6039 encoder->post_disable(encoder);
6040
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006041 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006042 if (IS_CHERRYVIEW(dev))
6043 chv_disable_pll(dev_priv, pipe);
6044 else if (IS_VALLEYVIEW(dev))
6045 vlv_disable_pll(dev_priv, pipe);
6046 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006047 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006048 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006049
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006050 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006051 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006052
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006053 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006054 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006055
Daniel Vetterefa96242014-04-24 23:55:02 +02006056 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006057 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02006058 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006059}
6060
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006061static void i9xx_crtc_off(struct drm_crtc *crtc)
6062{
6063}
6064
Borun Fub04c5bd2014-07-12 10:02:27 +05306065/* Master function to enable/disable CRTC and corresponding power wells */
6066void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01006067{
Chris Wilsoncdd59982010-09-08 16:30:16 +01006068 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006069 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006070 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006071 enum intel_display_power_domain domain;
6072 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006073
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006074 if (enable) {
6075 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006076 domains = get_crtc_power_domains(crtc);
6077 for_each_power_domain(domain, domains)
6078 intel_display_power_get(dev_priv, domain);
6079 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006080
6081 dev_priv->display.crtc_enable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006082 intel_crtc_enable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006083 }
6084 } else {
6085 if (intel_crtc->active) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006086 intel_crtc_disable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006087 dev_priv->display.crtc_disable(crtc);
6088
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006089 domains = intel_crtc->enabled_power_domains;
6090 for_each_power_domain(domain, domains)
6091 intel_display_power_put(dev_priv, domain);
6092 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006093 }
6094 }
Borun Fub04c5bd2014-07-12 10:02:27 +05306095}
6096
6097/**
6098 * Sets the power management mode of the pipe and plane.
6099 */
6100void intel_crtc_update_dpms(struct drm_crtc *crtc)
6101{
6102 struct drm_device *dev = crtc->dev;
6103 struct intel_encoder *intel_encoder;
6104 bool enable = false;
6105
6106 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6107 enable |= intel_encoder->connectors_active;
6108
6109 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006110}
6111
Daniel Vetter976f8a22012-07-08 22:34:21 +02006112static void intel_crtc_disable(struct drm_crtc *crtc)
6113{
6114 struct drm_device *dev = crtc->dev;
6115 struct drm_connector *connector;
6116 struct drm_i915_private *dev_priv = dev->dev_private;
6117
6118 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08006119 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006120
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006121 intel_crtc_disable_planes(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006122 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006123 dev_priv->display.off(crtc);
6124
Matt Roper70a101f2015-04-08 18:56:53 -07006125 drm_plane_helper_disable(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006126
6127 /* Update computed state. */
6128 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6129 if (!connector->encoder || !connector->encoder->crtc)
6130 continue;
6131
6132 if (connector->encoder->crtc != crtc)
6133 continue;
6134
6135 connector->dpms = DRM_MODE_DPMS_OFF;
6136 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01006137 }
6138}
6139
Chris Wilsonea5b2132010-08-04 13:50:23 +01006140void intel_encoder_destroy(struct drm_encoder *encoder)
6141{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006142 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006143
Chris Wilsonea5b2132010-08-04 13:50:23 +01006144 drm_encoder_cleanup(encoder);
6145 kfree(intel_encoder);
6146}
6147
Damien Lespiau92373292013-08-08 22:28:57 +01006148/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006149 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6150 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006151static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006152{
6153 if (mode == DRM_MODE_DPMS_ON) {
6154 encoder->connectors_active = true;
6155
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006156 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006157 } else {
6158 encoder->connectors_active = false;
6159
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006160 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006161 }
6162}
6163
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006164/* Cross check the actual hw state with our own modeset state tracking (and it's
6165 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006166static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006167{
6168 if (connector->get_hw_state(connector)) {
6169 struct intel_encoder *encoder = connector->encoder;
6170 struct drm_crtc *crtc;
6171 bool encoder_enabled;
6172 enum pipe pipe;
6173
6174 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6175 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006176 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006177
Dave Airlie0e32b392014-05-02 14:02:48 +10006178 /* there is no real hw state for MST connectors */
6179 if (connector->mst_port)
6180 return;
6181
Rob Clarke2c719b2014-12-15 13:56:32 -05006182 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006183 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006184 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006185 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006186
Dave Airlie36cd7442014-05-02 13:44:18 +10006187 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006188 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006189 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006190
Dave Airlie36cd7442014-05-02 13:44:18 +10006191 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006192 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6193 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006194 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006195
Dave Airlie36cd7442014-05-02 13:44:18 +10006196 crtc = encoder->base.crtc;
6197
Matt Roper83d65732015-02-25 13:12:16 -08006198 I915_STATE_WARN(!crtc->state->enable,
6199 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006200 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6201 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006202 "encoder active on the wrong pipe\n");
6203 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006204 }
6205}
6206
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006207int intel_connector_init(struct intel_connector *connector)
6208{
6209 struct drm_connector_state *connector_state;
6210
6211 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6212 if (!connector_state)
6213 return -ENOMEM;
6214
6215 connector->base.state = connector_state;
6216 return 0;
6217}
6218
6219struct intel_connector *intel_connector_alloc(void)
6220{
6221 struct intel_connector *connector;
6222
6223 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6224 if (!connector)
6225 return NULL;
6226
6227 if (intel_connector_init(connector) < 0) {
6228 kfree(connector);
6229 return NULL;
6230 }
6231
6232 return connector;
6233}
6234
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006235/* Even simpler default implementation, if there's really no special case to
6236 * consider. */
6237void intel_connector_dpms(struct drm_connector *connector, int mode)
6238{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006239 /* All the simple cases only support two dpms states. */
6240 if (mode != DRM_MODE_DPMS_ON)
6241 mode = DRM_MODE_DPMS_OFF;
6242
6243 if (mode == connector->dpms)
6244 return;
6245
6246 connector->dpms = mode;
6247
6248 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01006249 if (connector->encoder)
6250 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006251
Daniel Vetterb9805142012-08-31 17:37:33 +02006252 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006253}
6254
Daniel Vetterf0947c32012-07-02 13:10:34 +02006255/* Simple connector->get_hw_state implementation for encoders that support only
6256 * one connector and no cloning and hence the encoder state determines the state
6257 * of the connector. */
6258bool intel_connector_get_hw_state(struct intel_connector *connector)
6259{
Daniel Vetter24929352012-07-02 20:28:59 +02006260 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006261 struct intel_encoder *encoder = connector->encoder;
6262
6263 return encoder->get_hw_state(encoder, &pipe);
6264}
6265
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006266static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006267{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006268 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6269 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006270
6271 return 0;
6272}
6273
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006274static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006275 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006276{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006277 struct drm_atomic_state *state = pipe_config->base.state;
6278 struct intel_crtc *other_crtc;
6279 struct intel_crtc_state *other_crtc_state;
6280
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006281 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6282 pipe_name(pipe), pipe_config->fdi_lanes);
6283 if (pipe_config->fdi_lanes > 4) {
6284 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6285 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006286 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006287 }
6288
Paulo Zanonibafb6552013-11-02 21:07:44 -07006289 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006290 if (pipe_config->fdi_lanes > 2) {
6291 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6292 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006293 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006294 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006295 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006296 }
6297 }
6298
6299 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006300 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006301
6302 /* Ivybridge 3 pipe is really complicated */
6303 switch (pipe) {
6304 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006305 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006306 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006307 if (pipe_config->fdi_lanes <= 2)
6308 return 0;
6309
6310 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6311 other_crtc_state =
6312 intel_atomic_get_crtc_state(state, other_crtc);
6313 if (IS_ERR(other_crtc_state))
6314 return PTR_ERR(other_crtc_state);
6315
6316 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006317 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6318 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006319 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006320 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006321 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006322 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006323 if (pipe_config->fdi_lanes > 2) {
6324 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6325 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006326 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006327 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006328
6329 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6330 other_crtc_state =
6331 intel_atomic_get_crtc_state(state, other_crtc);
6332 if (IS_ERR(other_crtc_state))
6333 return PTR_ERR(other_crtc_state);
6334
6335 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006336 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006337 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006338 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006339 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006340 default:
6341 BUG();
6342 }
6343}
6344
Daniel Vettere29c22c2013-02-21 00:00:16 +01006345#define RETRY 1
6346static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006347 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006348{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006349 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006350 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006351 int lane, link_bw, fdi_dotclock, ret;
6352 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006353
Daniel Vettere29c22c2013-02-21 00:00:16 +01006354retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006355 /* FDI is a binary signal running at ~2.7GHz, encoding
6356 * each output octet as 10 bits. The actual frequency
6357 * is stored as a divider into a 100MHz clock, and the
6358 * mode pixel clock is stored in units of 1KHz.
6359 * Hence the bw of each lane in terms of the mode signal
6360 * is:
6361 */
6362 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6363
Damien Lespiau241bfc32013-09-25 16:45:37 +01006364 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006365
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006366 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006367 pipe_config->pipe_bpp);
6368
6369 pipe_config->fdi_lanes = lane;
6370
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006371 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006372 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006373
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006374 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6375 intel_crtc->pipe, pipe_config);
6376 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006377 pipe_config->pipe_bpp -= 2*3;
6378 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6379 pipe_config->pipe_bpp);
6380 needs_recompute = true;
6381 pipe_config->bw_constrained = true;
6382
6383 goto retry;
6384 }
6385
6386 if (needs_recompute)
6387 return RETRY;
6388
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006389 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006390}
6391
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006392static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006393 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006394{
Jani Nikulad330a952014-01-21 11:24:25 +02006395 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03006396 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07006397 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006398}
6399
Daniel Vettera43f6e02013-06-07 23:10:32 +02006400static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006401 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006402{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006403 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006404 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006405 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006406 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006407
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006408 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006409 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006410 int clock_limit =
6411 dev_priv->display.get_display_clock_speed(dev);
6412
6413 /*
6414 * Enable pixel doubling when the dot clock
6415 * is > 90% of the (display) core speed.
6416 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006417 * GDG double wide on either pipe,
6418 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006419 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006420 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006421 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006422 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006423 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006424 }
6425
Damien Lespiau241bfc32013-09-25 16:45:37 +01006426 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006427 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006428 }
Chris Wilson89749352010-09-12 18:25:19 +01006429
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006430 /*
6431 * Pipe horizontal size must be even in:
6432 * - DVO ganged mode
6433 * - LVDS dual channel mode
6434 * - Double wide pipe
6435 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006436 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006437 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6438 pipe_config->pipe_src_w &= ~1;
6439
Damien Lespiau8693a822013-05-03 18:48:11 +01006440 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6441 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006442 */
6443 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6444 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006445 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006446
Damien Lespiauf5adf942013-06-24 18:29:34 +01006447 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006448 hsw_compute_ips_config(crtc, pipe_config);
6449
Daniel Vetter877d48d2013-04-19 11:24:43 +02006450 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006451 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006452
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006453 /* FIXME: remove below call once atomic mode set is place and all crtc
6454 * related checks called from atomic_crtc_check function */
6455 ret = 0;
6456 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6457 crtc, pipe_config->base.state);
6458 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6459
6460 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006461}
6462
Ville Syrjälä1652d192015-03-31 14:12:01 +03006463static int skylake_get_display_clock_speed(struct drm_device *dev)
6464{
6465 struct drm_i915_private *dev_priv = to_i915(dev);
6466 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6467 uint32_t cdctl = I915_READ(CDCLK_CTL);
6468 uint32_t linkrate;
6469
6470 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6471 WARN(1, "LCPLL1 not enabled\n");
6472 return 24000; /* 24MHz is the cd freq with NSSC ref */
6473 }
6474
6475 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6476 return 540000;
6477
6478 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006479 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006480
Damien Lespiau71cd8422015-04-30 16:39:17 +01006481 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6482 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006483 /* vco 8640 */
6484 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6485 case CDCLK_FREQ_450_432:
6486 return 432000;
6487 case CDCLK_FREQ_337_308:
6488 return 308570;
6489 case CDCLK_FREQ_675_617:
6490 return 617140;
6491 default:
6492 WARN(1, "Unknown cd freq selection\n");
6493 }
6494 } else {
6495 /* vco 8100 */
6496 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6497 case CDCLK_FREQ_450_432:
6498 return 450000;
6499 case CDCLK_FREQ_337_308:
6500 return 337500;
6501 case CDCLK_FREQ_675_617:
6502 return 675000;
6503 default:
6504 WARN(1, "Unknown cd freq selection\n");
6505 }
6506 }
6507
6508 /* error case, do as if DPLL0 isn't enabled */
6509 return 24000;
6510}
6511
6512static int broadwell_get_display_clock_speed(struct drm_device *dev)
6513{
6514 struct drm_i915_private *dev_priv = dev->dev_private;
6515 uint32_t lcpll = I915_READ(LCPLL_CTL);
6516 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6517
6518 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6519 return 800000;
6520 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6521 return 450000;
6522 else if (freq == LCPLL_CLK_FREQ_450)
6523 return 450000;
6524 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6525 return 540000;
6526 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6527 return 337500;
6528 else
6529 return 675000;
6530}
6531
6532static int haswell_get_display_clock_speed(struct drm_device *dev)
6533{
6534 struct drm_i915_private *dev_priv = dev->dev_private;
6535 uint32_t lcpll = I915_READ(LCPLL_CTL);
6536 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6537
6538 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6539 return 800000;
6540 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6541 return 450000;
6542 else if (freq == LCPLL_CLK_FREQ_450)
6543 return 450000;
6544 else if (IS_HSW_ULT(dev))
6545 return 337500;
6546 else
6547 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006548}
6549
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006550static int valleyview_get_display_clock_speed(struct drm_device *dev)
6551{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006552 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006553 u32 val;
6554 int divider;
6555
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006556 if (dev_priv->hpll_freq == 0)
6557 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6558
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006559 mutex_lock(&dev_priv->dpio_lock);
6560 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6561 mutex_unlock(&dev_priv->dpio_lock);
6562
6563 divider = val & DISPLAY_FREQUENCY_VALUES;
6564
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006565 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6566 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6567 "cdclk change in progress\n");
6568
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006569 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006570}
6571
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006572static int ilk_get_display_clock_speed(struct drm_device *dev)
6573{
6574 return 450000;
6575}
6576
Jesse Barnese70236a2009-09-21 10:42:27 -07006577static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006578{
Jesse Barnese70236a2009-09-21 10:42:27 -07006579 return 400000;
6580}
Jesse Barnes79e53942008-11-07 14:24:08 -08006581
Jesse Barnese70236a2009-09-21 10:42:27 -07006582static int i915_get_display_clock_speed(struct drm_device *dev)
6583{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006584 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006585}
Jesse Barnes79e53942008-11-07 14:24:08 -08006586
Jesse Barnese70236a2009-09-21 10:42:27 -07006587static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6588{
6589 return 200000;
6590}
Jesse Barnes79e53942008-11-07 14:24:08 -08006591
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006592static int pnv_get_display_clock_speed(struct drm_device *dev)
6593{
6594 u16 gcfgc = 0;
6595
6596 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6597
6598 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6599 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006600 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006601 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006602 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006603 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006604 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006605 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6606 return 200000;
6607 default:
6608 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6609 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006610 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006611 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006612 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006613 }
6614}
6615
Jesse Barnese70236a2009-09-21 10:42:27 -07006616static int i915gm_get_display_clock_speed(struct drm_device *dev)
6617{
6618 u16 gcfgc = 0;
6619
6620 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6621
6622 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006623 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006624 else {
6625 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6626 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006627 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006628 default:
6629 case GC_DISPLAY_CLOCK_190_200_MHZ:
6630 return 190000;
6631 }
6632 }
6633}
Jesse Barnes79e53942008-11-07 14:24:08 -08006634
Jesse Barnese70236a2009-09-21 10:42:27 -07006635static int i865_get_display_clock_speed(struct drm_device *dev)
6636{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006637 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006638}
6639
6640static int i855_get_display_clock_speed(struct drm_device *dev)
6641{
6642 u16 hpllcc = 0;
6643 /* Assume that the hardware is in the high speed state. This
6644 * should be the default.
6645 */
6646 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6647 case GC_CLOCK_133_200:
6648 case GC_CLOCK_100_200:
6649 return 200000;
6650 case GC_CLOCK_166_250:
6651 return 250000;
6652 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006653 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006654 }
6655
6656 /* Shouldn't happen */
6657 return 0;
6658}
6659
6660static int i830_get_display_clock_speed(struct drm_device *dev)
6661{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006662 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006663}
6664
Zhenyu Wang2c072452009-06-05 15:38:42 +08006665static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006666intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006667{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006668 while (*num > DATA_LINK_M_N_MASK ||
6669 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006670 *num >>= 1;
6671 *den >>= 1;
6672 }
6673}
6674
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006675static void compute_m_n(unsigned int m, unsigned int n,
6676 uint32_t *ret_m, uint32_t *ret_n)
6677{
6678 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6679 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6680 intel_reduce_m_n_ratio(ret_m, ret_n);
6681}
6682
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006683void
6684intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6685 int pixel_clock, int link_clock,
6686 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006687{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006688 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006689
6690 compute_m_n(bits_per_pixel * pixel_clock,
6691 link_clock * nlanes * 8,
6692 &m_n->gmch_m, &m_n->gmch_n);
6693
6694 compute_m_n(pixel_clock, link_clock,
6695 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006696}
6697
Chris Wilsona7615032011-01-12 17:04:08 +00006698static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6699{
Jani Nikulad330a952014-01-21 11:24:25 +02006700 if (i915.panel_use_ssc >= 0)
6701 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006702 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006703 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006704}
6705
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006706static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6707 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006708{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006709 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006710 struct drm_i915_private *dev_priv = dev->dev_private;
6711 int refclk;
6712
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006713 WARN_ON(!crtc_state->base.state);
6714
Imre Deak5ab7b0b2015-03-06 03:29:25 +02006715 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006716 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006717 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006718 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006719 refclk = dev_priv->vbt.lvds_ssc_freq;
6720 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006721 } else if (!IS_GEN2(dev)) {
6722 refclk = 96000;
6723 } else {
6724 refclk = 48000;
6725 }
6726
6727 return refclk;
6728}
6729
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006730static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006731{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006732 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006733}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006734
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006735static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6736{
6737 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006738}
6739
Daniel Vetterf47709a2013-03-28 10:42:02 +01006740static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006741 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08006742 intel_clock_t *reduced_clock)
6743{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006744 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006745 u32 fp, fp2 = 0;
6746
6747 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006748 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006749 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006750 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006751 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006752 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006753 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006754 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006755 }
6756
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006757 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006758
Daniel Vetterf47709a2013-03-28 10:42:02 +01006759 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006760 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006761 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006762 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006763 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006764 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006765 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006766 }
6767}
6768
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006769static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6770 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006771{
6772 u32 reg_val;
6773
6774 /*
6775 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6776 * and set it to a reasonable value instead.
6777 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006778 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006779 reg_val &= 0xffffff00;
6780 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006781 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006782
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006783 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006784 reg_val &= 0x8cffffff;
6785 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006786 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006787
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006788 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006789 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006790 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006791
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006792 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006793 reg_val &= 0x00ffffff;
6794 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006795 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006796}
6797
Daniel Vetterb5518422013-05-03 11:49:48 +02006798static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6799 struct intel_link_m_n *m_n)
6800{
6801 struct drm_device *dev = crtc->base.dev;
6802 struct drm_i915_private *dev_priv = dev->dev_private;
6803 int pipe = crtc->pipe;
6804
Daniel Vettere3b95f12013-05-03 11:49:49 +02006805 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6806 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6807 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6808 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006809}
6810
6811static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006812 struct intel_link_m_n *m_n,
6813 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006814{
6815 struct drm_device *dev = crtc->base.dev;
6816 struct drm_i915_private *dev_priv = dev->dev_private;
6817 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006818 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006819
6820 if (INTEL_INFO(dev)->gen >= 5) {
6821 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6822 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6823 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6824 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006825 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6826 * for gen < 8) and if DRRS is supported (to make sure the
6827 * registers are not unnecessarily accessed).
6828 */
Durgadoss R44395bf2015-02-13 15:33:02 +05306829 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006830 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006831 I915_WRITE(PIPE_DATA_M2(transcoder),
6832 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6833 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6834 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6835 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6836 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006837 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006838 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6839 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6840 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6841 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006842 }
6843}
6844
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306845void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006846{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306847 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6848
6849 if (m_n == M1_N1) {
6850 dp_m_n = &crtc->config->dp_m_n;
6851 dp_m2_n2 = &crtc->config->dp_m2_n2;
6852 } else if (m_n == M2_N2) {
6853
6854 /*
6855 * M2_N2 registers are not supported. Hence m2_n2 divider value
6856 * needs to be programmed into M1_N1.
6857 */
6858 dp_m_n = &crtc->config->dp_m2_n2;
6859 } else {
6860 DRM_ERROR("Unsupported divider value\n");
6861 return;
6862 }
6863
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006864 if (crtc->config->has_pch_encoder)
6865 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006866 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306867 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006868}
6869
Ville Syrjäläd288f652014-10-28 13:20:22 +02006870static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006871 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006872{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006873 u32 dpll, dpll_md;
6874
6875 /*
6876 * Enable DPIO clock input. We should never disable the reference
6877 * clock for pipe B, since VGA hotplug / manual detection depends
6878 * on it.
6879 */
6880 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6881 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6882 /* We should never disable this, set it here for state tracking */
6883 if (crtc->pipe == PIPE_B)
6884 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6885 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006886 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006887
Ville Syrjäläd288f652014-10-28 13:20:22 +02006888 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006889 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006890 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006891}
6892
Ville Syrjäläd288f652014-10-28 13:20:22 +02006893static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006894 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006895{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006896 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006897 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006898 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006899 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006900 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006901 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006902
Daniel Vetter09153002012-12-12 14:06:44 +01006903 mutex_lock(&dev_priv->dpio_lock);
6904
Ville Syrjäläd288f652014-10-28 13:20:22 +02006905 bestn = pipe_config->dpll.n;
6906 bestm1 = pipe_config->dpll.m1;
6907 bestm2 = pipe_config->dpll.m2;
6908 bestp1 = pipe_config->dpll.p1;
6909 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006910
Jesse Barnes89b667f2013-04-18 14:51:36 -07006911 /* See eDP HDMI DPIO driver vbios notes doc */
6912
6913 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006914 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006915 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006916
6917 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006918 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006919
6920 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006921 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006922 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006923 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006924
6925 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006926 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006927
6928 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006929 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6930 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6931 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006932 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006933
6934 /*
6935 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6936 * but we don't support that).
6937 * Note: don't use the DAC post divider as it seems unstable.
6938 */
6939 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006940 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006941
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006942 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006943 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006944
Jesse Barnes89b667f2013-04-18 14:51:36 -07006945 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006946 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006947 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6948 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006949 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03006950 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006951 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006952 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006953 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006954
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02006955 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006956 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006957 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006958 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006959 0x0df40000);
6960 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006961 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006962 0x0df70000);
6963 } else { /* HDMI or VGA */
6964 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006965 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006966 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006967 0x0df70000);
6968 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006969 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006970 0x0df40000);
6971 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006972
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006973 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006974 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006975 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6976 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006977 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006978 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006979
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006980 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01006981 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006982}
6983
Ville Syrjäläd288f652014-10-28 13:20:22 +02006984static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006985 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006986{
Ville Syrjäläd288f652014-10-28 13:20:22 +02006987 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006988 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6989 DPLL_VCO_ENABLE;
6990 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006991 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006992
Ville Syrjäläd288f652014-10-28 13:20:22 +02006993 pipe_config->dpll_hw_state.dpll_md =
6994 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006995}
6996
Ville Syrjäläd288f652014-10-28 13:20:22 +02006997static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006998 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006999{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007000 struct drm_device *dev = crtc->base.dev;
7001 struct drm_i915_private *dev_priv = dev->dev_private;
7002 int pipe = crtc->pipe;
7003 int dpll_reg = DPLL(crtc->pipe);
7004 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307005 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007006 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307007 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307008 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007009
Ville Syrjäläd288f652014-10-28 13:20:22 +02007010 bestn = pipe_config->dpll.n;
7011 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7012 bestm1 = pipe_config->dpll.m1;
7013 bestm2 = pipe_config->dpll.m2 >> 22;
7014 bestp1 = pipe_config->dpll.p1;
7015 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307016 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307017 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307018 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007019
7020 /*
7021 * Enable Refclk and SSC
7022 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007023 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007024 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007025
7026 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007027
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007028 /* p1 and p2 divider */
7029 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7030 5 << DPIO_CHV_S1_DIV_SHIFT |
7031 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7032 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7033 1 << DPIO_CHV_K_DIV_SHIFT);
7034
7035 /* Feedback post-divider - m2 */
7036 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7037
7038 /* Feedback refclk divider - n and m1 */
7039 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7040 DPIO_CHV_M1_DIV_BY_2 |
7041 1 << DPIO_CHV_N_DIV_SHIFT);
7042
7043 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307044 if (bestm2_frac)
7045 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007046
7047 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307048 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7049 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7050 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7051 if (bestm2_frac)
7052 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7053 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007054
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307055 /* Program digital lock detect threshold */
7056 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7057 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7058 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7059 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7060 if (!bestm2_frac)
7061 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7062 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7063
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007064 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307065 if (vco == 5400000) {
7066 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7067 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7068 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7069 tribuf_calcntr = 0x9;
7070 } else if (vco <= 6200000) {
7071 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7072 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7073 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7074 tribuf_calcntr = 0x9;
7075 } else if (vco <= 6480000) {
7076 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7077 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7078 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7079 tribuf_calcntr = 0x8;
7080 } else {
7081 /* Not supported. Apply the same limits as in the max case */
7082 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7083 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7084 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7085 tribuf_calcntr = 0;
7086 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007087 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7088
Ville Syrjälä968040b2015-03-11 22:52:08 +02007089 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307090 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7091 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7092 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7093
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007094 /* AFC Recal */
7095 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7096 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7097 DPIO_AFC_RECAL);
7098
7099 mutex_unlock(&dev_priv->dpio_lock);
7100}
7101
Ville Syrjäläd288f652014-10-28 13:20:22 +02007102/**
7103 * vlv_force_pll_on - forcibly enable just the PLL
7104 * @dev_priv: i915 private structure
7105 * @pipe: pipe PLL to enable
7106 * @dpll: PLL configuration
7107 *
7108 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7109 * in cases where we need the PLL enabled even when @pipe is not going to
7110 * be enabled.
7111 */
7112void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7113 const struct dpll *dpll)
7114{
7115 struct intel_crtc *crtc =
7116 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007117 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007118 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007119 .pixel_multiplier = 1,
7120 .dpll = *dpll,
7121 };
7122
7123 if (IS_CHERRYVIEW(dev)) {
7124 chv_update_pll(crtc, &pipe_config);
7125 chv_prepare_pll(crtc, &pipe_config);
7126 chv_enable_pll(crtc, &pipe_config);
7127 } else {
7128 vlv_update_pll(crtc, &pipe_config);
7129 vlv_prepare_pll(crtc, &pipe_config);
7130 vlv_enable_pll(crtc, &pipe_config);
7131 }
7132}
7133
7134/**
7135 * vlv_force_pll_off - forcibly disable just the PLL
7136 * @dev_priv: i915 private structure
7137 * @pipe: pipe PLL to disable
7138 *
7139 * Disable the PLL for @pipe. To be used in cases where we need
7140 * the PLL enabled even when @pipe is not going to be enabled.
7141 */
7142void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7143{
7144 if (IS_CHERRYVIEW(dev))
7145 chv_disable_pll(to_i915(dev), pipe);
7146 else
7147 vlv_disable_pll(to_i915(dev), pipe);
7148}
7149
Daniel Vetterf47709a2013-03-28 10:42:02 +01007150static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007151 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007152 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007153 int num_connectors)
7154{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007155 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007156 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007157 u32 dpll;
7158 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007159 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007160
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007161 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307162
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007163 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7164 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007165
7166 dpll = DPLL_VGA_MODE_DIS;
7167
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007168 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007169 dpll |= DPLLB_MODE_LVDS;
7170 else
7171 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007172
Daniel Vetteref1b4602013-06-01 17:17:04 +02007173 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007174 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007175 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007176 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007177
7178 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007179 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007180
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007181 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007182 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007183
7184 /* compute bitmask from p1 value */
7185 if (IS_PINEVIEW(dev))
7186 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7187 else {
7188 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7189 if (IS_G4X(dev) && reduced_clock)
7190 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7191 }
7192 switch (clock->p2) {
7193 case 5:
7194 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7195 break;
7196 case 7:
7197 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7198 break;
7199 case 10:
7200 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7201 break;
7202 case 14:
7203 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7204 break;
7205 }
7206 if (INTEL_INFO(dev)->gen >= 4)
7207 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7208
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007209 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007210 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007211 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007212 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7213 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7214 else
7215 dpll |= PLL_REF_INPUT_DREFCLK;
7216
7217 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007218 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007219
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007220 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007221 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007222 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007223 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007224 }
7225}
7226
Daniel Vetterf47709a2013-03-28 10:42:02 +01007227static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007228 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007229 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007230 int num_connectors)
7231{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007232 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007233 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007234 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007235 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007236
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007237 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307238
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007239 dpll = DPLL_VGA_MODE_DIS;
7240
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007241 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007242 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7243 } else {
7244 if (clock->p1 == 2)
7245 dpll |= PLL_P1_DIVIDE_BY_TWO;
7246 else
7247 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7248 if (clock->p2 == 4)
7249 dpll |= PLL_P2_DIVIDE_BY_4;
7250 }
7251
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007252 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007253 dpll |= DPLL_DVO_2X_MODE;
7254
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007255 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007256 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7257 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7258 else
7259 dpll |= PLL_REF_INPUT_DREFCLK;
7260
7261 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007262 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007263}
7264
Daniel Vetter8a654f32013-06-01 17:16:22 +02007265static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007266{
7267 struct drm_device *dev = intel_crtc->base.dev;
7268 struct drm_i915_private *dev_priv = dev->dev_private;
7269 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007270 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007271 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007272 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007273 uint32_t crtc_vtotal, crtc_vblank_end;
7274 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007275
7276 /* We need to be careful not to changed the adjusted mode, for otherwise
7277 * the hw state checker will get angry at the mismatch. */
7278 crtc_vtotal = adjusted_mode->crtc_vtotal;
7279 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007280
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007281 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007282 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007283 crtc_vtotal -= 1;
7284 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007285
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007286 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007287 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7288 else
7289 vsyncshift = adjusted_mode->crtc_hsync_start -
7290 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007291 if (vsyncshift < 0)
7292 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007293 }
7294
7295 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007296 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007297
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007298 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007299 (adjusted_mode->crtc_hdisplay - 1) |
7300 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007301 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007302 (adjusted_mode->crtc_hblank_start - 1) |
7303 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007304 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007305 (adjusted_mode->crtc_hsync_start - 1) |
7306 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7307
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007308 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007309 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007310 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007311 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007312 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007313 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007314 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007315 (adjusted_mode->crtc_vsync_start - 1) |
7316 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7317
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007318 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7319 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7320 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7321 * bits. */
7322 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7323 (pipe == PIPE_B || pipe == PIPE_C))
7324 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7325
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007326 /* pipesrc controls the size that is scaled from, which should
7327 * always be the user's requested size.
7328 */
7329 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007330 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7331 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007332}
7333
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007334static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007335 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007336{
7337 struct drm_device *dev = crtc->base.dev;
7338 struct drm_i915_private *dev_priv = dev->dev_private;
7339 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7340 uint32_t tmp;
7341
7342 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007343 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7344 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007345 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007346 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7347 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007348 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007349 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7350 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007351
7352 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007353 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7354 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007355 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007356 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7357 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007358 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007359 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7360 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007361
7362 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007363 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7364 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7365 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007366 }
7367
7368 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007369 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7370 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7371
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007372 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7373 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007374}
7375
Daniel Vetterf6a83282014-02-11 15:28:57 -08007376void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007377 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007378{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007379 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7380 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7381 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7382 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007383
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007384 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7385 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7386 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7387 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007388
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007389 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007390
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007391 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7392 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007393}
7394
Daniel Vetter84b046f2013-02-19 18:48:54 +01007395static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7396{
7397 struct drm_device *dev = intel_crtc->base.dev;
7398 struct drm_i915_private *dev_priv = dev->dev_private;
7399 uint32_t pipeconf;
7400
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007401 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007402
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007403 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7404 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7405 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007406
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007407 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007408 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007409
Daniel Vetterff9ce462013-04-24 14:57:17 +02007410 /* only g4x and later have fancy bpc/dither controls */
7411 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007412 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007413 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007414 pipeconf |= PIPECONF_DITHER_EN |
7415 PIPECONF_DITHER_TYPE_SP;
7416
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007417 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007418 case 18:
7419 pipeconf |= PIPECONF_6BPC;
7420 break;
7421 case 24:
7422 pipeconf |= PIPECONF_8BPC;
7423 break;
7424 case 30:
7425 pipeconf |= PIPECONF_10BPC;
7426 break;
7427 default:
7428 /* Case prevented by intel_choose_pipe_bpp_dither. */
7429 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007430 }
7431 }
7432
7433 if (HAS_PIPE_CXSR(dev)) {
7434 if (intel_crtc->lowfreq_avail) {
7435 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7436 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7437 } else {
7438 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007439 }
7440 }
7441
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007442 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007443 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007444 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007445 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7446 else
7447 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7448 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007449 pipeconf |= PIPECONF_PROGRESSIVE;
7450
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007451 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007452 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007453
Daniel Vetter84b046f2013-02-19 18:48:54 +01007454 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7455 POSTING_READ(PIPECONF(intel_crtc->pipe));
7456}
7457
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007458static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7459 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007460{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007461 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007462 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007463 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007464 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007465 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007466 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007467 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007468 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007469 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007470 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007471 struct drm_connector_state *connector_state;
7472 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007473
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007474 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007475 if (connector_state->crtc != &crtc->base)
7476 continue;
7477
7478 encoder = to_intel_encoder(connector_state->best_encoder);
7479
Chris Wilson5eddb702010-09-11 13:48:45 +01007480 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007481 case INTEL_OUTPUT_LVDS:
7482 is_lvds = true;
7483 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007484 case INTEL_OUTPUT_DSI:
7485 is_dsi = true;
7486 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007487 default:
7488 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007489 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007490
Eric Anholtc751ce42010-03-25 11:48:48 -07007491 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007492 }
7493
Jani Nikulaf2335332013-09-13 11:03:09 +03007494 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007495 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007496
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007497 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007498 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007499
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007500 /*
7501 * Returns a set of divisors for the desired target clock with
7502 * the given refclk, or FALSE. The returned values represent
7503 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7504 * 2) / p1 / p2.
7505 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007506 limit = intel_limit(crtc_state, refclk);
7507 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007508 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007509 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007510 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007511 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7512 return -EINVAL;
7513 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007514
Jani Nikulaf2335332013-09-13 11:03:09 +03007515 if (is_lvds && dev_priv->lvds_downclock_avail) {
7516 /*
7517 * Ensure we match the reduced clock's P to the target
7518 * clock. If the clocks don't match, we can't switch
7519 * the display clock by using the FP0/FP1. In such case
7520 * we will disable the LVDS downclock feature.
7521 */
7522 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007523 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007524 dev_priv->lvds_downclock,
7525 refclk, &clock,
7526 &reduced_clock);
7527 }
7528 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007529 crtc_state->dpll.n = clock.n;
7530 crtc_state->dpll.m1 = clock.m1;
7531 crtc_state->dpll.m2 = clock.m2;
7532 crtc_state->dpll.p1 = clock.p1;
7533 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007534 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007535
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007536 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007537 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307538 has_reduced_clock ? &reduced_clock : NULL,
7539 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007540 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007541 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007542 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007543 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007544 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007545 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007546 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007547 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007548 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007549
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007550 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007551}
7552
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007553static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007554 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007555{
7556 struct drm_device *dev = crtc->base.dev;
7557 struct drm_i915_private *dev_priv = dev->dev_private;
7558 uint32_t tmp;
7559
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007560 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7561 return;
7562
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007563 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007564 if (!(tmp & PFIT_ENABLE))
7565 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007566
Daniel Vetter06922822013-07-11 13:35:40 +02007567 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007568 if (INTEL_INFO(dev)->gen < 4) {
7569 if (crtc->pipe != PIPE_B)
7570 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007571 } else {
7572 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7573 return;
7574 }
7575
Daniel Vetter06922822013-07-11 13:35:40 +02007576 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007577 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7578 if (INTEL_INFO(dev)->gen < 5)
7579 pipe_config->gmch_pfit.lvds_border_bits =
7580 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7581}
7582
Jesse Barnesacbec812013-09-20 11:29:32 -07007583static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007584 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007585{
7586 struct drm_device *dev = crtc->base.dev;
7587 struct drm_i915_private *dev_priv = dev->dev_private;
7588 int pipe = pipe_config->cpu_transcoder;
7589 intel_clock_t clock;
7590 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007591 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007592
Shobhit Kumarf573de52014-07-30 20:32:37 +05307593 /* In case of MIPI DPLL will not even be used */
7594 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7595 return;
7596
Jesse Barnesacbec812013-09-20 11:29:32 -07007597 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007598 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07007599 mutex_unlock(&dev_priv->dpio_lock);
7600
7601 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7602 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7603 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7604 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7605 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7606
Ville Syrjäläf6466282013-10-14 14:50:31 +03007607 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007608
Ville Syrjäläf6466282013-10-14 14:50:31 +03007609 /* clock.dot is the fast clock */
7610 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007611}
7612
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007613static void
7614i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7615 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007616{
7617 struct drm_device *dev = crtc->base.dev;
7618 struct drm_i915_private *dev_priv = dev->dev_private;
7619 u32 val, base, offset;
7620 int pipe = crtc->pipe, plane = crtc->plane;
7621 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007622 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007623 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007624 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007625
Damien Lespiau42a7b082015-02-05 19:35:13 +00007626 val = I915_READ(DSPCNTR(plane));
7627 if (!(val & DISPLAY_PLANE_ENABLE))
7628 return;
7629
Damien Lespiaud9806c92015-01-21 14:07:19 +00007630 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007631 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007632 DRM_DEBUG_KMS("failed to alloc fb\n");
7633 return;
7634 }
7635
Damien Lespiau1b842c82015-01-21 13:50:54 +00007636 fb = &intel_fb->base;
7637
Daniel Vetter18c52472015-02-10 17:16:09 +00007638 if (INTEL_INFO(dev)->gen >= 4) {
7639 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007640 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007641 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7642 }
7643 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007644
7645 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007646 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007647 fb->pixel_format = fourcc;
7648 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007649
7650 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007651 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007652 offset = I915_READ(DSPTILEOFF(plane));
7653 else
7654 offset = I915_READ(DSPLINOFF(plane));
7655 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7656 } else {
7657 base = I915_READ(DSPADDR(plane));
7658 }
7659 plane_config->base = base;
7660
7661 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007662 fb->width = ((val >> 16) & 0xfff) + 1;
7663 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007664
7665 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007666 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007667
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007668 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007669 fb->pixel_format,
7670 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007671
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007672 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007673
Damien Lespiau2844a922015-01-20 12:51:48 +00007674 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7675 pipe_name(pipe), plane, fb->width, fb->height,
7676 fb->bits_per_pixel, base, fb->pitches[0],
7677 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007678
Damien Lespiau2d140302015-02-05 17:22:18 +00007679 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007680}
7681
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007682static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007683 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007684{
7685 struct drm_device *dev = crtc->base.dev;
7686 struct drm_i915_private *dev_priv = dev->dev_private;
7687 int pipe = pipe_config->cpu_transcoder;
7688 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7689 intel_clock_t clock;
7690 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7691 int refclk = 100000;
7692
7693 mutex_lock(&dev_priv->dpio_lock);
7694 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7695 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7696 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7697 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7698 mutex_unlock(&dev_priv->dpio_lock);
7699
7700 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7701 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7702 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7703 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7704 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7705
7706 chv_clock(refclk, &clock);
7707
7708 /* clock.dot is the fast clock */
7709 pipe_config->port_clock = clock.dot / 5;
7710}
7711
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007712static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007713 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007714{
7715 struct drm_device *dev = crtc->base.dev;
7716 struct drm_i915_private *dev_priv = dev->dev_private;
7717 uint32_t tmp;
7718
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007719 if (!intel_display_power_is_enabled(dev_priv,
7720 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007721 return false;
7722
Daniel Vettere143a212013-07-04 12:01:15 +02007723 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007724 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007725
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007726 tmp = I915_READ(PIPECONF(crtc->pipe));
7727 if (!(tmp & PIPECONF_ENABLE))
7728 return false;
7729
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007730 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7731 switch (tmp & PIPECONF_BPC_MASK) {
7732 case PIPECONF_6BPC:
7733 pipe_config->pipe_bpp = 18;
7734 break;
7735 case PIPECONF_8BPC:
7736 pipe_config->pipe_bpp = 24;
7737 break;
7738 case PIPECONF_10BPC:
7739 pipe_config->pipe_bpp = 30;
7740 break;
7741 default:
7742 break;
7743 }
7744 }
7745
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007746 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7747 pipe_config->limited_color_range = true;
7748
Ville Syrjälä282740f2013-09-04 18:30:03 +03007749 if (INTEL_INFO(dev)->gen < 4)
7750 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7751
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007752 intel_get_pipe_timings(crtc, pipe_config);
7753
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007754 i9xx_get_pfit_config(crtc, pipe_config);
7755
Daniel Vetter6c49f242013-06-06 12:45:25 +02007756 if (INTEL_INFO(dev)->gen >= 4) {
7757 tmp = I915_READ(DPLL_MD(crtc->pipe));
7758 pipe_config->pixel_multiplier =
7759 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7760 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007761 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02007762 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7763 tmp = I915_READ(DPLL(crtc->pipe));
7764 pipe_config->pixel_multiplier =
7765 ((tmp & SDVO_MULTIPLIER_MASK)
7766 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7767 } else {
7768 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7769 * port and will be fixed up in the encoder->get_config
7770 * function. */
7771 pipe_config->pixel_multiplier = 1;
7772 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007773 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7774 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007775 /*
7776 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7777 * on 830. Filter it out here so that we don't
7778 * report errors due to that.
7779 */
7780 if (IS_I830(dev))
7781 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7782
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007783 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7784 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007785 } else {
7786 /* Mask out read-only status bits. */
7787 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7788 DPLL_PORTC_READY_MASK |
7789 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007790 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007791
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007792 if (IS_CHERRYVIEW(dev))
7793 chv_crtc_clock_get(crtc, pipe_config);
7794 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07007795 vlv_crtc_clock_get(crtc, pipe_config);
7796 else
7797 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007798
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007799 return true;
7800}
7801
Paulo Zanonidde86e22012-12-01 12:04:25 -02007802static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007803{
7804 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007805 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007806 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007807 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007808 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007809 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007810 bool has_ck505 = false;
7811 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007812
7813 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01007814 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007815 switch (encoder->type) {
7816 case INTEL_OUTPUT_LVDS:
7817 has_panel = true;
7818 has_lvds = true;
7819 break;
7820 case INTEL_OUTPUT_EDP:
7821 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03007822 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007823 has_cpu_edp = true;
7824 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007825 default:
7826 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007827 }
7828 }
7829
Keith Packard99eb6a02011-09-26 14:29:12 -07007830 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007831 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007832 can_ssc = has_ck505;
7833 } else {
7834 has_ck505 = false;
7835 can_ssc = true;
7836 }
7837
Imre Deak2de69052013-05-08 13:14:04 +03007838 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7839 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007840
7841 /* Ironlake: try to setup display ref clock before DPLL
7842 * enabling. This is only under driver's control after
7843 * PCH B stepping, previous chipset stepping should be
7844 * ignoring this setting.
7845 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007846 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007847
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007848 /* As we must carefully and slowly disable/enable each source in turn,
7849 * compute the final state we want first and check if we need to
7850 * make any changes at all.
7851 */
7852 final = val;
7853 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007854 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007855 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007856 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007857 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7858
7859 final &= ~DREF_SSC_SOURCE_MASK;
7860 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7861 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007862
Keith Packard199e5d72011-09-22 12:01:57 -07007863 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007864 final |= DREF_SSC_SOURCE_ENABLE;
7865
7866 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7867 final |= DREF_SSC1_ENABLE;
7868
7869 if (has_cpu_edp) {
7870 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7871 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7872 else
7873 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7874 } else
7875 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7876 } else {
7877 final |= DREF_SSC_SOURCE_DISABLE;
7878 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7879 }
7880
7881 if (final == val)
7882 return;
7883
7884 /* Always enable nonspread source */
7885 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7886
7887 if (has_ck505)
7888 val |= DREF_NONSPREAD_CK505_ENABLE;
7889 else
7890 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7891
7892 if (has_panel) {
7893 val &= ~DREF_SSC_SOURCE_MASK;
7894 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007895
Keith Packard199e5d72011-09-22 12:01:57 -07007896 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007897 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007898 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007899 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007900 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007901 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007902
7903 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007904 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007905 POSTING_READ(PCH_DREF_CONTROL);
7906 udelay(200);
7907
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007908 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007909
7910 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007911 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007912 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007913 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007914 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007915 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007916 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007917 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007918 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007919
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007920 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007921 POSTING_READ(PCH_DREF_CONTROL);
7922 udelay(200);
7923 } else {
7924 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7925
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007926 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007927
7928 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007929 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007930
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007931 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007932 POSTING_READ(PCH_DREF_CONTROL);
7933 udelay(200);
7934
7935 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007936 val &= ~DREF_SSC_SOURCE_MASK;
7937 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007938
7939 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007940 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007941
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007942 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007943 POSTING_READ(PCH_DREF_CONTROL);
7944 udelay(200);
7945 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007946
7947 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007948}
7949
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007950static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007951{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007952 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007953
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007954 tmp = I915_READ(SOUTH_CHICKEN2);
7955 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7956 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007957
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007958 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7959 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7960 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007961
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007962 tmp = I915_READ(SOUTH_CHICKEN2);
7963 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7964 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007965
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007966 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7967 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7968 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007969}
7970
7971/* WaMPhyProgramming:hsw */
7972static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7973{
7974 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007975
7976 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7977 tmp &= ~(0xFF << 24);
7978 tmp |= (0x12 << 24);
7979 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7980
Paulo Zanonidde86e22012-12-01 12:04:25 -02007981 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7982 tmp |= (1 << 11);
7983 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7984
7985 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7986 tmp |= (1 << 11);
7987 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7988
Paulo Zanonidde86e22012-12-01 12:04:25 -02007989 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7990 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7991 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7992
7993 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7994 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7995 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7996
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007997 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7998 tmp &= ~(7 << 13);
7999 tmp |= (5 << 13);
8000 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008001
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008002 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8003 tmp &= ~(7 << 13);
8004 tmp |= (5 << 13);
8005 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008006
8007 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8008 tmp &= ~0xFF;
8009 tmp |= 0x1C;
8010 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8011
8012 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8013 tmp &= ~0xFF;
8014 tmp |= 0x1C;
8015 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8016
8017 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8018 tmp &= ~(0xFF << 16);
8019 tmp |= (0x1C << 16);
8020 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8021
8022 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8023 tmp &= ~(0xFF << 16);
8024 tmp |= (0x1C << 16);
8025 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8026
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008027 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8028 tmp |= (1 << 27);
8029 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008030
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008031 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8032 tmp |= (1 << 27);
8033 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008034
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008035 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8036 tmp &= ~(0xF << 28);
8037 tmp |= (4 << 28);
8038 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008039
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008040 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8041 tmp &= ~(0xF << 28);
8042 tmp |= (4 << 28);
8043 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008044}
8045
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008046/* Implements 3 different sequences from BSpec chapter "Display iCLK
8047 * Programming" based on the parameters passed:
8048 * - Sequence to enable CLKOUT_DP
8049 * - Sequence to enable CLKOUT_DP without spread
8050 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8051 */
8052static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8053 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008054{
8055 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008056 uint32_t reg, tmp;
8057
8058 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8059 with_spread = true;
8060 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8061 with_fdi, "LP PCH doesn't have FDI\n"))
8062 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008063
8064 mutex_lock(&dev_priv->dpio_lock);
8065
8066 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8067 tmp &= ~SBI_SSCCTL_DISABLE;
8068 tmp |= SBI_SSCCTL_PATHALT;
8069 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8070
8071 udelay(24);
8072
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008073 if (with_spread) {
8074 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8075 tmp &= ~SBI_SSCCTL_PATHALT;
8076 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008077
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008078 if (with_fdi) {
8079 lpt_reset_fdi_mphy(dev_priv);
8080 lpt_program_fdi_mphy(dev_priv);
8081 }
8082 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008083
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008084 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8085 SBI_GEN0 : SBI_DBUFF0;
8086 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8087 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8088 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008089
8090 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008091}
8092
Paulo Zanoni47701c32013-07-23 11:19:25 -03008093/* Sequence to disable CLKOUT_DP */
8094static void lpt_disable_clkout_dp(struct drm_device *dev)
8095{
8096 struct drm_i915_private *dev_priv = dev->dev_private;
8097 uint32_t reg, tmp;
8098
8099 mutex_lock(&dev_priv->dpio_lock);
8100
8101 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8102 SBI_GEN0 : SBI_DBUFF0;
8103 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8104 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8105 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8106
8107 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8108 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8109 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8110 tmp |= SBI_SSCCTL_PATHALT;
8111 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8112 udelay(32);
8113 }
8114 tmp |= SBI_SSCCTL_DISABLE;
8115 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8116 }
8117
8118 mutex_unlock(&dev_priv->dpio_lock);
8119}
8120
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008121static void lpt_init_pch_refclk(struct drm_device *dev)
8122{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008123 struct intel_encoder *encoder;
8124 bool has_vga = false;
8125
Damien Lespiaub2784e12014-08-05 11:29:37 +01008126 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008127 switch (encoder->type) {
8128 case INTEL_OUTPUT_ANALOG:
8129 has_vga = true;
8130 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008131 default:
8132 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008133 }
8134 }
8135
Paulo Zanoni47701c32013-07-23 11:19:25 -03008136 if (has_vga)
8137 lpt_enable_clkout_dp(dev, true, true);
8138 else
8139 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008140}
8141
Paulo Zanonidde86e22012-12-01 12:04:25 -02008142/*
8143 * Initialize reference clocks when the driver loads
8144 */
8145void intel_init_pch_refclk(struct drm_device *dev)
8146{
8147 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8148 ironlake_init_pch_refclk(dev);
8149 else if (HAS_PCH_LPT(dev))
8150 lpt_init_pch_refclk(dev);
8151}
8152
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008153static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008154{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008155 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008156 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008157 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008158 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008159 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008160 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008161 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008162 bool is_lvds = false;
8163
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008164 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008165 if (connector_state->crtc != crtc_state->base.crtc)
8166 continue;
8167
8168 encoder = to_intel_encoder(connector_state->best_encoder);
8169
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008170 switch (encoder->type) {
8171 case INTEL_OUTPUT_LVDS:
8172 is_lvds = true;
8173 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008174 default:
8175 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008176 }
8177 num_connectors++;
8178 }
8179
8180 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008181 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008182 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008183 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008184 }
8185
8186 return 120000;
8187}
8188
Daniel Vetter6ff93602013-04-19 11:24:36 +02008189static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008190{
8191 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8192 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8193 int pipe = intel_crtc->pipe;
8194 uint32_t val;
8195
Daniel Vetter78114072013-06-13 00:54:57 +02008196 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008197
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008198 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008199 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008200 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008201 break;
8202 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008203 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008204 break;
8205 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008206 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008207 break;
8208 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008209 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008210 break;
8211 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008212 /* Case prevented by intel_choose_pipe_bpp_dither. */
8213 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008214 }
8215
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008216 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008217 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8218
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008219 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008220 val |= PIPECONF_INTERLACED_ILK;
8221 else
8222 val |= PIPECONF_PROGRESSIVE;
8223
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008224 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008225 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008226
Paulo Zanonic8203562012-09-12 10:06:29 -03008227 I915_WRITE(PIPECONF(pipe), val);
8228 POSTING_READ(PIPECONF(pipe));
8229}
8230
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008231/*
8232 * Set up the pipe CSC unit.
8233 *
8234 * Currently only full range RGB to limited range RGB conversion
8235 * is supported, but eventually this should handle various
8236 * RGB<->YCbCr scenarios as well.
8237 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008238static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008239{
8240 struct drm_device *dev = crtc->dev;
8241 struct drm_i915_private *dev_priv = dev->dev_private;
8242 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8243 int pipe = intel_crtc->pipe;
8244 uint16_t coeff = 0x7800; /* 1.0 */
8245
8246 /*
8247 * TODO: Check what kind of values actually come out of the pipe
8248 * with these coeff/postoff values and adjust to get the best
8249 * accuracy. Perhaps we even need to take the bpc value into
8250 * consideration.
8251 */
8252
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008253 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008254 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8255
8256 /*
8257 * GY/GU and RY/RU should be the other way around according
8258 * to BSpec, but reality doesn't agree. Just set them up in
8259 * a way that results in the correct picture.
8260 */
8261 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8262 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8263
8264 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8265 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8266
8267 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8268 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8269
8270 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8271 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8272 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8273
8274 if (INTEL_INFO(dev)->gen > 6) {
8275 uint16_t postoff = 0;
8276
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008277 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008278 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008279
8280 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8281 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8282 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8283
8284 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8285 } else {
8286 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8287
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008288 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008289 mode |= CSC_BLACK_SCREEN_OFFSET;
8290
8291 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8292 }
8293}
8294
Daniel Vetter6ff93602013-04-19 11:24:36 +02008295static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008296{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008297 struct drm_device *dev = crtc->dev;
8298 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008299 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008300 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008301 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008302 uint32_t val;
8303
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008304 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008305
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008306 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008307 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8308
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008309 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008310 val |= PIPECONF_INTERLACED_ILK;
8311 else
8312 val |= PIPECONF_PROGRESSIVE;
8313
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008314 I915_WRITE(PIPECONF(cpu_transcoder), val);
8315 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008316
8317 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8318 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008319
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308320 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008321 val = 0;
8322
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008323 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008324 case 18:
8325 val |= PIPEMISC_DITHER_6_BPC;
8326 break;
8327 case 24:
8328 val |= PIPEMISC_DITHER_8_BPC;
8329 break;
8330 case 30:
8331 val |= PIPEMISC_DITHER_10_BPC;
8332 break;
8333 case 36:
8334 val |= PIPEMISC_DITHER_12_BPC;
8335 break;
8336 default:
8337 /* Case prevented by pipe_config_set_bpp. */
8338 BUG();
8339 }
8340
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008341 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008342 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8343
8344 I915_WRITE(PIPEMISC(pipe), val);
8345 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008346}
8347
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008348static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008349 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008350 intel_clock_t *clock,
8351 bool *has_reduced_clock,
8352 intel_clock_t *reduced_clock)
8353{
8354 struct drm_device *dev = crtc->dev;
8355 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008356 int refclk;
8357 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008358 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008359
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008360 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008361
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008362 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008363
8364 /*
8365 * Returns a set of divisors for the desired target clock with the given
8366 * refclk, or FALSE. The returned values represent the clock equation:
8367 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8368 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008369 limit = intel_limit(crtc_state, refclk);
8370 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008371 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008372 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008373 if (!ret)
8374 return false;
8375
8376 if (is_lvds && dev_priv->lvds_downclock_avail) {
8377 /*
8378 * Ensure we match the reduced clock's P to the target clock.
8379 * If the clocks don't match, we can't switch the display clock
8380 * by using the FP0/FP1. In such case we will disable the LVDS
8381 * downclock feature.
8382 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008383 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008384 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008385 dev_priv->lvds_downclock,
8386 refclk, clock,
8387 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008388 }
8389
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008390 return true;
8391}
8392
Paulo Zanonid4b19312012-11-29 11:29:32 -02008393int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8394{
8395 /*
8396 * Account for spread spectrum to avoid
8397 * oversubscribing the link. Max center spread
8398 * is 2.5%; use 5% for safety's sake.
8399 */
8400 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008401 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008402}
8403
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008404static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008405{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008406 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008407}
8408
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008409static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008410 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008411 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008412 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008413{
8414 struct drm_crtc *crtc = &intel_crtc->base;
8415 struct drm_device *dev = crtc->dev;
8416 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008417 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008418 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008419 struct drm_connector_state *connector_state;
8420 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008421 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008422 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008423 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008424
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008425 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008426 if (connector_state->crtc != crtc_state->base.crtc)
8427 continue;
8428
8429 encoder = to_intel_encoder(connector_state->best_encoder);
8430
8431 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008432 case INTEL_OUTPUT_LVDS:
8433 is_lvds = true;
8434 break;
8435 case INTEL_OUTPUT_SDVO:
8436 case INTEL_OUTPUT_HDMI:
8437 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008438 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008439 default:
8440 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008441 }
8442
8443 num_connectors++;
8444 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008445
Chris Wilsonc1858122010-12-03 21:35:48 +00008446 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008447 factor = 21;
8448 if (is_lvds) {
8449 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008450 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008451 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008452 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008453 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008454 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008455
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008456 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008457 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008458
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008459 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8460 *fp2 |= FP_CB_TUNE;
8461
Chris Wilson5eddb702010-09-11 13:48:45 +01008462 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008463
Eric Anholta07d6782011-03-30 13:01:08 -07008464 if (is_lvds)
8465 dpll |= DPLLB_MODE_LVDS;
8466 else
8467 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008468
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008469 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008470 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008471
8472 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008473 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008474 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008475 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008476
Eric Anholta07d6782011-03-30 13:01:08 -07008477 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008478 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008479 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008480 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008481
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008482 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008483 case 5:
8484 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8485 break;
8486 case 7:
8487 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8488 break;
8489 case 10:
8490 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8491 break;
8492 case 14:
8493 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8494 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008495 }
8496
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008497 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008498 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008499 else
8500 dpll |= PLL_REF_INPUT_DREFCLK;
8501
Daniel Vetter959e16d2013-06-05 13:34:21 +02008502 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008503}
8504
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008505static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8506 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008507{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008508 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008509 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008510 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008511 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008512 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008513 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008514
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008515 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008516
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008517 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8518 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8519
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008520 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008521 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008522 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008523 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8524 return -EINVAL;
8525 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008526 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008527 if (!crtc_state->clock_set) {
8528 crtc_state->dpll.n = clock.n;
8529 crtc_state->dpll.m1 = clock.m1;
8530 crtc_state->dpll.m2 = clock.m2;
8531 crtc_state->dpll.p1 = clock.p1;
8532 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008533 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008534
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008535 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008536 if (crtc_state->has_pch_encoder) {
8537 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008538 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008539 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008540
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008541 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008542 &fp, &reduced_clock,
8543 has_reduced_clock ? &fp2 : NULL);
8544
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008545 crtc_state->dpll_hw_state.dpll = dpll;
8546 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008547 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008548 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008549 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008550 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008551
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008552 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008553 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008554 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008555 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008556 return -EINVAL;
8557 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008558 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008559
Rodrigo Viviab585de2015-03-24 12:40:09 -07008560 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008561 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008562 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008563 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008564
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008565 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008566}
8567
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008568static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8569 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008570{
8571 struct drm_device *dev = crtc->base.dev;
8572 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008573 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008574
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008575 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8576 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8577 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8578 & ~TU_SIZE_MASK;
8579 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8580 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8581 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8582}
8583
8584static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8585 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008586 struct intel_link_m_n *m_n,
8587 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008588{
8589 struct drm_device *dev = crtc->base.dev;
8590 struct drm_i915_private *dev_priv = dev->dev_private;
8591 enum pipe pipe = crtc->pipe;
8592
8593 if (INTEL_INFO(dev)->gen >= 5) {
8594 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8595 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8596 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8597 & ~TU_SIZE_MASK;
8598 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8599 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8600 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008601 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8602 * gen < 8) and if DRRS is supported (to make sure the
8603 * registers are not unnecessarily read).
8604 */
8605 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008606 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008607 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8608 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8609 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8610 & ~TU_SIZE_MASK;
8611 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8612 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8613 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8614 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008615 } else {
8616 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8617 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8618 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8619 & ~TU_SIZE_MASK;
8620 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8621 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8622 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8623 }
8624}
8625
8626void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008627 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008628{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008629 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008630 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8631 else
8632 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008633 &pipe_config->dp_m_n,
8634 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008635}
8636
Daniel Vetter72419202013-04-04 13:28:53 +02008637static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008638 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008639{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008640 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008641 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008642}
8643
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008644static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008645 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008646{
8647 struct drm_device *dev = crtc->base.dev;
8648 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008649 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8650 uint32_t ps_ctrl = 0;
8651 int id = -1;
8652 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008653
Chandra Kondurua1b22782015-04-07 15:28:45 -07008654 /* find scaler attached to this pipe */
8655 for (i = 0; i < crtc->num_scalers; i++) {
8656 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8657 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8658 id = i;
8659 pipe_config->pch_pfit.enabled = true;
8660 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8661 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8662 break;
8663 }
8664 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008665
Chandra Kondurua1b22782015-04-07 15:28:45 -07008666 scaler_state->scaler_id = id;
8667 if (id >= 0) {
8668 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8669 } else {
8670 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008671 }
8672}
8673
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008674static void
8675skylake_get_initial_plane_config(struct intel_crtc *crtc,
8676 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008677{
8678 struct drm_device *dev = crtc->base.dev;
8679 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008680 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008681 int pipe = crtc->pipe;
8682 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008683 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008684 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008685 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008686
Damien Lespiaud9806c92015-01-21 14:07:19 +00008687 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008688 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008689 DRM_DEBUG_KMS("failed to alloc fb\n");
8690 return;
8691 }
8692
Damien Lespiau1b842c82015-01-21 13:50:54 +00008693 fb = &intel_fb->base;
8694
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008695 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008696 if (!(val & PLANE_CTL_ENABLE))
8697 goto error;
8698
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008699 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8700 fourcc = skl_format_to_fourcc(pixel_format,
8701 val & PLANE_CTL_ORDER_RGBX,
8702 val & PLANE_CTL_ALPHA_MASK);
8703 fb->pixel_format = fourcc;
8704 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8705
Damien Lespiau40f46282015-02-27 11:15:21 +00008706 tiling = val & PLANE_CTL_TILED_MASK;
8707 switch (tiling) {
8708 case PLANE_CTL_TILED_LINEAR:
8709 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8710 break;
8711 case PLANE_CTL_TILED_X:
8712 plane_config->tiling = I915_TILING_X;
8713 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8714 break;
8715 case PLANE_CTL_TILED_Y:
8716 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8717 break;
8718 case PLANE_CTL_TILED_YF:
8719 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8720 break;
8721 default:
8722 MISSING_CASE(tiling);
8723 goto error;
8724 }
8725
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008726 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8727 plane_config->base = base;
8728
8729 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8730
8731 val = I915_READ(PLANE_SIZE(pipe, 0));
8732 fb->height = ((val >> 16) & 0xfff) + 1;
8733 fb->width = ((val >> 0) & 0x1fff) + 1;
8734
8735 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00008736 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8737 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008738 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8739
8740 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008741 fb->pixel_format,
8742 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008743
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008744 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008745
8746 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8747 pipe_name(pipe), fb->width, fb->height,
8748 fb->bits_per_pixel, base, fb->pitches[0],
8749 plane_config->size);
8750
Damien Lespiau2d140302015-02-05 17:22:18 +00008751 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008752 return;
8753
8754error:
8755 kfree(fb);
8756}
8757
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008758static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008759 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008760{
8761 struct drm_device *dev = crtc->base.dev;
8762 struct drm_i915_private *dev_priv = dev->dev_private;
8763 uint32_t tmp;
8764
8765 tmp = I915_READ(PF_CTL(crtc->pipe));
8766
8767 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008768 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008769 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8770 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008771
8772 /* We currently do not free assignements of panel fitters on
8773 * ivb/hsw (since we don't use the higher upscaling modes which
8774 * differentiates them) so just WARN about this case for now. */
8775 if (IS_GEN7(dev)) {
8776 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8777 PF_PIPE_SEL_IVB(crtc->pipe));
8778 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008779 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008780}
8781
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008782static void
8783ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8784 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008785{
8786 struct drm_device *dev = crtc->base.dev;
8787 struct drm_i915_private *dev_priv = dev->dev_private;
8788 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008789 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008790 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008791 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008792 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008793 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008794
Damien Lespiau42a7b082015-02-05 19:35:13 +00008795 val = I915_READ(DSPCNTR(pipe));
8796 if (!(val & DISPLAY_PLANE_ENABLE))
8797 return;
8798
Damien Lespiaud9806c92015-01-21 14:07:19 +00008799 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008800 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008801 DRM_DEBUG_KMS("failed to alloc fb\n");
8802 return;
8803 }
8804
Damien Lespiau1b842c82015-01-21 13:50:54 +00008805 fb = &intel_fb->base;
8806
Daniel Vetter18c52472015-02-10 17:16:09 +00008807 if (INTEL_INFO(dev)->gen >= 4) {
8808 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008809 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008810 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8811 }
8812 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008813
8814 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008815 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008816 fb->pixel_format = fourcc;
8817 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008818
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008819 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008820 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008821 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008822 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00008823 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008824 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008825 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008826 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008827 }
8828 plane_config->base = base;
8829
8830 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008831 fb->width = ((val >> 16) & 0xfff) + 1;
8832 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008833
8834 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008835 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008836
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008837 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008838 fb->pixel_format,
8839 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008840
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008841 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008842
Damien Lespiau2844a922015-01-20 12:51:48 +00008843 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8844 pipe_name(pipe), fb->width, fb->height,
8845 fb->bits_per_pixel, base, fb->pitches[0],
8846 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008847
Damien Lespiau2d140302015-02-05 17:22:18 +00008848 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008849}
8850
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008851static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008852 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008853{
8854 struct drm_device *dev = crtc->base.dev;
8855 struct drm_i915_private *dev_priv = dev->dev_private;
8856 uint32_t tmp;
8857
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008858 if (!intel_display_power_is_enabled(dev_priv,
8859 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008860 return false;
8861
Daniel Vettere143a212013-07-04 12:01:15 +02008862 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008863 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008864
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008865 tmp = I915_READ(PIPECONF(crtc->pipe));
8866 if (!(tmp & PIPECONF_ENABLE))
8867 return false;
8868
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008869 switch (tmp & PIPECONF_BPC_MASK) {
8870 case PIPECONF_6BPC:
8871 pipe_config->pipe_bpp = 18;
8872 break;
8873 case PIPECONF_8BPC:
8874 pipe_config->pipe_bpp = 24;
8875 break;
8876 case PIPECONF_10BPC:
8877 pipe_config->pipe_bpp = 30;
8878 break;
8879 case PIPECONF_12BPC:
8880 pipe_config->pipe_bpp = 36;
8881 break;
8882 default:
8883 break;
8884 }
8885
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008886 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8887 pipe_config->limited_color_range = true;
8888
Daniel Vetterab9412b2013-05-03 11:49:46 +02008889 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008890 struct intel_shared_dpll *pll;
8891
Daniel Vetter88adfff2013-03-28 10:42:01 +01008892 pipe_config->has_pch_encoder = true;
8893
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008894 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8895 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8896 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008897
8898 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008899
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008900 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02008901 pipe_config->shared_dpll =
8902 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008903 } else {
8904 tmp = I915_READ(PCH_DPLL_SEL);
8905 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8906 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8907 else
8908 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8909 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008910
8911 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8912
8913 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8914 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008915
8916 tmp = pipe_config->dpll_hw_state.dpll;
8917 pipe_config->pixel_multiplier =
8918 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8919 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008920
8921 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008922 } else {
8923 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008924 }
8925
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008926 intel_get_pipe_timings(crtc, pipe_config);
8927
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008928 ironlake_get_pfit_config(crtc, pipe_config);
8929
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008930 return true;
8931}
8932
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008933static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8934{
8935 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008936 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008937
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008938 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008939 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008940 pipe_name(crtc->pipe));
8941
Rob Clarke2c719b2014-12-15 13:56:32 -05008942 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8943 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8944 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8945 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8946 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8947 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008948 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03008949 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05008950 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008951 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008952 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008953 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008954 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008955 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008956 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008957
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008958 /*
8959 * In theory we can still leave IRQs enabled, as long as only the HPD
8960 * interrupts remain enabled. We used to check for that, but since it's
8961 * gen-specific and since we only disable LCPLL after we fully disable
8962 * the interrupts, the check below should be enough.
8963 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008964 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008965}
8966
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008967static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8968{
8969 struct drm_device *dev = dev_priv->dev;
8970
8971 if (IS_HASWELL(dev))
8972 return I915_READ(D_COMP_HSW);
8973 else
8974 return I915_READ(D_COMP_BDW);
8975}
8976
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008977static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8978{
8979 struct drm_device *dev = dev_priv->dev;
8980
8981 if (IS_HASWELL(dev)) {
8982 mutex_lock(&dev_priv->rps.hw_lock);
8983 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8984 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03008985 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008986 mutex_unlock(&dev_priv->rps.hw_lock);
8987 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008988 I915_WRITE(D_COMP_BDW, val);
8989 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008990 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008991}
8992
8993/*
8994 * This function implements pieces of two sequences from BSpec:
8995 * - Sequence for display software to disable LCPLL
8996 * - Sequence for display software to allow package C8+
8997 * The steps implemented here are just the steps that actually touch the LCPLL
8998 * register. Callers should take care of disabling all the display engine
8999 * functions, doing the mode unset, fixing interrupts, etc.
9000 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009001static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9002 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009003{
9004 uint32_t val;
9005
9006 assert_can_disable_lcpll(dev_priv);
9007
9008 val = I915_READ(LCPLL_CTL);
9009
9010 if (switch_to_fclk) {
9011 val |= LCPLL_CD_SOURCE_FCLK;
9012 I915_WRITE(LCPLL_CTL, val);
9013
9014 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9015 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9016 DRM_ERROR("Switching to FCLK failed\n");
9017
9018 val = I915_READ(LCPLL_CTL);
9019 }
9020
9021 val |= LCPLL_PLL_DISABLE;
9022 I915_WRITE(LCPLL_CTL, val);
9023 POSTING_READ(LCPLL_CTL);
9024
9025 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9026 DRM_ERROR("LCPLL still locked\n");
9027
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009028 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009029 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009030 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009031 ndelay(100);
9032
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009033 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9034 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009035 DRM_ERROR("D_COMP RCOMP still in progress\n");
9036
9037 if (allow_power_down) {
9038 val = I915_READ(LCPLL_CTL);
9039 val |= LCPLL_POWER_DOWN_ALLOW;
9040 I915_WRITE(LCPLL_CTL, val);
9041 POSTING_READ(LCPLL_CTL);
9042 }
9043}
9044
9045/*
9046 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9047 * source.
9048 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009049static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009050{
9051 uint32_t val;
9052
9053 val = I915_READ(LCPLL_CTL);
9054
9055 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9056 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9057 return;
9058
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009059 /*
9060 * Make sure we're not on PC8 state before disabling PC8, otherwise
9061 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009062 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009063 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009064
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009065 if (val & LCPLL_POWER_DOWN_ALLOW) {
9066 val &= ~LCPLL_POWER_DOWN_ALLOW;
9067 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009068 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009069 }
9070
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009071 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009072 val |= D_COMP_COMP_FORCE;
9073 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009074 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009075
9076 val = I915_READ(LCPLL_CTL);
9077 val &= ~LCPLL_PLL_DISABLE;
9078 I915_WRITE(LCPLL_CTL, val);
9079
9080 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9081 DRM_ERROR("LCPLL not locked yet\n");
9082
9083 if (val & LCPLL_CD_SOURCE_FCLK) {
9084 val = I915_READ(LCPLL_CTL);
9085 val &= ~LCPLL_CD_SOURCE_FCLK;
9086 I915_WRITE(LCPLL_CTL, val);
9087
9088 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9089 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9090 DRM_ERROR("Switching back to LCPLL failed\n");
9091 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009092
Mika Kuoppala59bad942015-01-16 11:34:40 +02009093 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009094}
9095
Paulo Zanoni765dab672014-03-07 20:08:18 -03009096/*
9097 * Package states C8 and deeper are really deep PC states that can only be
9098 * reached when all the devices on the system allow it, so even if the graphics
9099 * device allows PC8+, it doesn't mean the system will actually get to these
9100 * states. Our driver only allows PC8+ when going into runtime PM.
9101 *
9102 * The requirements for PC8+ are that all the outputs are disabled, the power
9103 * well is disabled and most interrupts are disabled, and these are also
9104 * requirements for runtime PM. When these conditions are met, we manually do
9105 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9106 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9107 * hang the machine.
9108 *
9109 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9110 * the state of some registers, so when we come back from PC8+ we need to
9111 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9112 * need to take care of the registers kept by RC6. Notice that this happens even
9113 * if we don't put the device in PCI D3 state (which is what currently happens
9114 * because of the runtime PM support).
9115 *
9116 * For more, read "Display Sequences for Package C8" on the hardware
9117 * documentation.
9118 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009119void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009120{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009121 struct drm_device *dev = dev_priv->dev;
9122 uint32_t val;
9123
Paulo Zanonic67a4702013-08-19 13:18:09 -03009124 DRM_DEBUG_KMS("Enabling package C8+\n");
9125
Paulo Zanonic67a4702013-08-19 13:18:09 -03009126 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9127 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9128 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9129 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9130 }
9131
9132 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009133 hsw_disable_lcpll(dev_priv, true, true);
9134}
9135
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009136void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009137{
9138 struct drm_device *dev = dev_priv->dev;
9139 uint32_t val;
9140
Paulo Zanonic67a4702013-08-19 13:18:09 -03009141 DRM_DEBUG_KMS("Disabling package C8+\n");
9142
9143 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009144 lpt_init_pch_refclk(dev);
9145
9146 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9147 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9148 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9149 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9150 }
9151
9152 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009153}
9154
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309155static void broxton_modeset_global_resources(struct drm_atomic_state *state)
9156{
9157 struct drm_device *dev = state->dev;
9158 struct drm_i915_private *dev_priv = dev->dev_private;
9159 int max_pixclk = intel_mode_max_pixclk(state);
9160 int req_cdclk;
9161
9162 /* see the comment in valleyview_modeset_global_resources */
9163 if (WARN_ON(max_pixclk < 0))
9164 return;
9165
9166 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9167
9168 if (req_cdclk != dev_priv->cdclk_freq)
9169 broxton_set_cdclk(dev, req_cdclk);
9170}
9171
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009172static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9173 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009174{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009175 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009176 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009177
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009178 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009179
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009180 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009181}
9182
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309183static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9184 enum port port,
9185 struct intel_crtc_state *pipe_config)
9186{
9187 switch (port) {
9188 case PORT_A:
9189 pipe_config->ddi_pll_sel = SKL_DPLL0;
9190 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9191 break;
9192 case PORT_B:
9193 pipe_config->ddi_pll_sel = SKL_DPLL1;
9194 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9195 break;
9196 case PORT_C:
9197 pipe_config->ddi_pll_sel = SKL_DPLL2;
9198 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9199 break;
9200 default:
9201 DRM_ERROR("Incorrect port type\n");
9202 }
9203}
9204
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009205static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9206 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009207 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009208{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009209 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009210
9211 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9212 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9213
9214 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009215 case SKL_DPLL0:
9216 /*
9217 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9218 * of the shared DPLL framework and thus needs to be read out
9219 * separately
9220 */
9221 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9222 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9223 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009224 case SKL_DPLL1:
9225 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9226 break;
9227 case SKL_DPLL2:
9228 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9229 break;
9230 case SKL_DPLL3:
9231 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9232 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009233 }
9234}
9235
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009236static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9237 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009238 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009239{
9240 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9241
9242 switch (pipe_config->ddi_pll_sel) {
9243 case PORT_CLK_SEL_WRPLL1:
9244 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9245 break;
9246 case PORT_CLK_SEL_WRPLL2:
9247 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9248 break;
9249 }
9250}
9251
Daniel Vetter26804af2014-06-25 22:01:55 +03009252static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009253 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009254{
9255 struct drm_device *dev = crtc->base.dev;
9256 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009257 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009258 enum port port;
9259 uint32_t tmp;
9260
9261 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9262
9263 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9264
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009265 if (IS_SKYLAKE(dev))
9266 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309267 else if (IS_BROXTON(dev))
9268 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009269 else
9270 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009271
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009272 if (pipe_config->shared_dpll >= 0) {
9273 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9274
9275 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9276 &pipe_config->dpll_hw_state));
9277 }
9278
Daniel Vetter26804af2014-06-25 22:01:55 +03009279 /*
9280 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9281 * DDI E. So just check whether this pipe is wired to DDI E and whether
9282 * the PCH transcoder is on.
9283 */
Damien Lespiauca370452013-12-03 13:56:24 +00009284 if (INTEL_INFO(dev)->gen < 9 &&
9285 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009286 pipe_config->has_pch_encoder = true;
9287
9288 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9289 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9290 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9291
9292 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9293 }
9294}
9295
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009296static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009297 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009298{
9299 struct drm_device *dev = crtc->base.dev;
9300 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009301 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009302 uint32_t tmp;
9303
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009304 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009305 POWER_DOMAIN_PIPE(crtc->pipe)))
9306 return false;
9307
Daniel Vettere143a212013-07-04 12:01:15 +02009308 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009309 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9310
Daniel Vettereccb1402013-05-22 00:50:22 +02009311 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9312 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9313 enum pipe trans_edp_pipe;
9314 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9315 default:
9316 WARN(1, "unknown pipe linked to edp transcoder\n");
9317 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9318 case TRANS_DDI_EDP_INPUT_A_ON:
9319 trans_edp_pipe = PIPE_A;
9320 break;
9321 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9322 trans_edp_pipe = PIPE_B;
9323 break;
9324 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9325 trans_edp_pipe = PIPE_C;
9326 break;
9327 }
9328
9329 if (trans_edp_pipe == crtc->pipe)
9330 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9331 }
9332
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009333 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009334 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009335 return false;
9336
Daniel Vettereccb1402013-05-22 00:50:22 +02009337 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009338 if (!(tmp & PIPECONF_ENABLE))
9339 return false;
9340
Daniel Vetter26804af2014-06-25 22:01:55 +03009341 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009342
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009343 intel_get_pipe_timings(crtc, pipe_config);
9344
Chandra Kondurua1b22782015-04-07 15:28:45 -07009345 if (INTEL_INFO(dev)->gen >= 9) {
9346 skl_init_scalers(dev, crtc, pipe_config);
9347 }
9348
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009349 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009350 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009351 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009352 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009353 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009354 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009355 else
9356 MISSING_CASE(INTEL_INFO(dev)->gen);
9357
Chandra Kondurua1b22782015-04-07 15:28:45 -07009358 } else {
9359 pipe_config->scaler_state.scaler_id = -1;
9360 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009361 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009362
Jesse Barnese59150d2014-01-07 13:30:45 -08009363 if (IS_HASWELL(dev))
9364 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9365 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009366
Clint Taylorebb69c92014-09-30 10:30:22 -07009367 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9368 pipe_config->pixel_multiplier =
9369 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9370 } else {
9371 pipe_config->pixel_multiplier = 1;
9372 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009373
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009374 return true;
9375}
9376
Chris Wilson560b85b2010-08-07 11:01:38 +01009377static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9378{
9379 struct drm_device *dev = crtc->dev;
9380 struct drm_i915_private *dev_priv = dev->dev_private;
9381 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009382 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009383
Ville Syrjälädc41c152014-08-13 11:57:05 +03009384 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009385 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9386 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009387 unsigned int stride = roundup_pow_of_two(width) * 4;
9388
9389 switch (stride) {
9390 default:
9391 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9392 width, stride);
9393 stride = 256;
9394 /* fallthrough */
9395 case 256:
9396 case 512:
9397 case 1024:
9398 case 2048:
9399 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009400 }
9401
Ville Syrjälädc41c152014-08-13 11:57:05 +03009402 cntl |= CURSOR_ENABLE |
9403 CURSOR_GAMMA_ENABLE |
9404 CURSOR_FORMAT_ARGB |
9405 CURSOR_STRIDE(stride);
9406
9407 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009408 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009409
Ville Syrjälädc41c152014-08-13 11:57:05 +03009410 if (intel_crtc->cursor_cntl != 0 &&
9411 (intel_crtc->cursor_base != base ||
9412 intel_crtc->cursor_size != size ||
9413 intel_crtc->cursor_cntl != cntl)) {
9414 /* On these chipsets we can only modify the base/size/stride
9415 * whilst the cursor is disabled.
9416 */
9417 I915_WRITE(_CURACNTR, 0);
9418 POSTING_READ(_CURACNTR);
9419 intel_crtc->cursor_cntl = 0;
9420 }
9421
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009422 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009423 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009424 intel_crtc->cursor_base = base;
9425 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009426
9427 if (intel_crtc->cursor_size != size) {
9428 I915_WRITE(CURSIZE, size);
9429 intel_crtc->cursor_size = size;
9430 }
9431
Chris Wilson4b0e3332014-05-30 16:35:26 +03009432 if (intel_crtc->cursor_cntl != cntl) {
9433 I915_WRITE(_CURACNTR, cntl);
9434 POSTING_READ(_CURACNTR);
9435 intel_crtc->cursor_cntl = cntl;
9436 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009437}
9438
9439static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9440{
9441 struct drm_device *dev = crtc->dev;
9442 struct drm_i915_private *dev_priv = dev->dev_private;
9443 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9444 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009445 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009446
Chris Wilson4b0e3332014-05-30 16:35:26 +03009447 cntl = 0;
9448 if (base) {
9449 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009450 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309451 case 64:
9452 cntl |= CURSOR_MODE_64_ARGB_AX;
9453 break;
9454 case 128:
9455 cntl |= CURSOR_MODE_128_ARGB_AX;
9456 break;
9457 case 256:
9458 cntl |= CURSOR_MODE_256_ARGB_AX;
9459 break;
9460 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009461 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309462 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009463 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009464 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009465
9466 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9467 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009468 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009469
Matt Roper8e7d6882015-01-21 16:35:41 -08009470 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009471 cntl |= CURSOR_ROTATE_180;
9472
Chris Wilson4b0e3332014-05-30 16:35:26 +03009473 if (intel_crtc->cursor_cntl != cntl) {
9474 I915_WRITE(CURCNTR(pipe), cntl);
9475 POSTING_READ(CURCNTR(pipe));
9476 intel_crtc->cursor_cntl = cntl;
9477 }
9478
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009479 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009480 I915_WRITE(CURBASE(pipe), base);
9481 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009482
9483 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009484}
9485
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009486/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009487static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9488 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009489{
9490 struct drm_device *dev = crtc->dev;
9491 struct drm_i915_private *dev_priv = dev->dev_private;
9492 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9493 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009494 int x = crtc->cursor_x;
9495 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009496 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009497
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009498 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009499 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009500
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009501 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009502 base = 0;
9503
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009504 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009505 base = 0;
9506
9507 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009508 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009509 base = 0;
9510
9511 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9512 x = -x;
9513 }
9514 pos |= x << CURSOR_X_SHIFT;
9515
9516 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009517 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009518 base = 0;
9519
9520 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9521 y = -y;
9522 }
9523 pos |= y << CURSOR_Y_SHIFT;
9524
Chris Wilson4b0e3332014-05-30 16:35:26 +03009525 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009526 return;
9527
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009528 I915_WRITE(CURPOS(pipe), pos);
9529
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009530 /* ILK+ do this automagically */
9531 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009532 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009533 base += (intel_crtc->base.cursor->state->crtc_h *
9534 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009535 }
9536
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009537 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009538 i845_update_cursor(crtc, base);
9539 else
9540 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009541}
9542
Ville Syrjälädc41c152014-08-13 11:57:05 +03009543static bool cursor_size_ok(struct drm_device *dev,
9544 uint32_t width, uint32_t height)
9545{
9546 if (width == 0 || height == 0)
9547 return false;
9548
9549 /*
9550 * 845g/865g are special in that they are only limited by
9551 * the width of their cursors, the height is arbitrary up to
9552 * the precision of the register. Everything else requires
9553 * square cursors, limited to a few power-of-two sizes.
9554 */
9555 if (IS_845G(dev) || IS_I865G(dev)) {
9556 if ((width & 63) != 0)
9557 return false;
9558
9559 if (width > (IS_845G(dev) ? 64 : 512))
9560 return false;
9561
9562 if (height > 1023)
9563 return false;
9564 } else {
9565 switch (width | height) {
9566 case 256:
9567 case 128:
9568 if (IS_GEN2(dev))
9569 return false;
9570 case 64:
9571 break;
9572 default:
9573 return false;
9574 }
9575 }
9576
9577 return true;
9578}
9579
Jesse Barnes79e53942008-11-07 14:24:08 -08009580static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01009581 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08009582{
James Simmons72034252010-08-03 01:33:19 +01009583 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08009584 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08009585
James Simmons72034252010-08-03 01:33:19 +01009586 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009587 intel_crtc->lut_r[i] = red[i] >> 8;
9588 intel_crtc->lut_g[i] = green[i] >> 8;
9589 intel_crtc->lut_b[i] = blue[i] >> 8;
9590 }
9591
9592 intel_crtc_load_lut(crtc);
9593}
9594
Jesse Barnes79e53942008-11-07 14:24:08 -08009595/* VESA 640x480x72Hz mode to set on the pipe */
9596static struct drm_display_mode load_detect_mode = {
9597 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9598 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9599};
9600
Daniel Vettera8bb6812014-02-10 18:00:39 +01009601struct drm_framebuffer *
9602__intel_framebuffer_create(struct drm_device *dev,
9603 struct drm_mode_fb_cmd2 *mode_cmd,
9604 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01009605{
9606 struct intel_framebuffer *intel_fb;
9607 int ret;
9608
9609 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9610 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009611 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01009612 return ERR_PTR(-ENOMEM);
9613 }
9614
9615 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009616 if (ret)
9617 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009618
9619 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009620err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009621 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009622 kfree(intel_fb);
9623
9624 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009625}
9626
Daniel Vetterb5ea6422014-03-02 21:18:00 +01009627static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01009628intel_framebuffer_create(struct drm_device *dev,
9629 struct drm_mode_fb_cmd2 *mode_cmd,
9630 struct drm_i915_gem_object *obj)
9631{
9632 struct drm_framebuffer *fb;
9633 int ret;
9634
9635 ret = i915_mutex_lock_interruptible(dev);
9636 if (ret)
9637 return ERR_PTR(ret);
9638 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9639 mutex_unlock(&dev->struct_mutex);
9640
9641 return fb;
9642}
9643
Chris Wilsond2dff872011-04-19 08:36:26 +01009644static u32
9645intel_framebuffer_pitch_for_width(int width, int bpp)
9646{
9647 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9648 return ALIGN(pitch, 64);
9649}
9650
9651static u32
9652intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9653{
9654 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009655 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009656}
9657
9658static struct drm_framebuffer *
9659intel_framebuffer_create_for_mode(struct drm_device *dev,
9660 struct drm_display_mode *mode,
9661 int depth, int bpp)
9662{
9663 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009664 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009665
9666 obj = i915_gem_alloc_object(dev,
9667 intel_framebuffer_size_for_mode(mode, bpp));
9668 if (obj == NULL)
9669 return ERR_PTR(-ENOMEM);
9670
9671 mode_cmd.width = mode->hdisplay;
9672 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009673 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9674 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009675 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009676
9677 return intel_framebuffer_create(dev, &mode_cmd, obj);
9678}
9679
9680static struct drm_framebuffer *
9681mode_fits_in_fbdev(struct drm_device *dev,
9682 struct drm_display_mode *mode)
9683{
Daniel Vetter4520f532013-10-09 09:18:51 +02009684#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01009685 struct drm_i915_private *dev_priv = dev->dev_private;
9686 struct drm_i915_gem_object *obj;
9687 struct drm_framebuffer *fb;
9688
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009689 if (!dev_priv->fbdev)
9690 return NULL;
9691
9692 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009693 return NULL;
9694
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009695 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009696 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009697
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009698 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009699 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9700 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01009701 return NULL;
9702
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009703 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009704 return NULL;
9705
9706 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009707#else
9708 return NULL;
9709#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009710}
9711
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009712static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9713 struct drm_crtc *crtc,
9714 struct drm_display_mode *mode,
9715 struct drm_framebuffer *fb,
9716 int x, int y)
9717{
9718 struct drm_plane_state *plane_state;
9719 int hdisplay, vdisplay;
9720 int ret;
9721
9722 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9723 if (IS_ERR(plane_state))
9724 return PTR_ERR(plane_state);
9725
9726 if (mode)
9727 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
9728 else
9729 hdisplay = vdisplay = 0;
9730
9731 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9732 if (ret)
9733 return ret;
9734 drm_atomic_set_fb_for_plane(plane_state, fb);
9735 plane_state->crtc_x = 0;
9736 plane_state->crtc_y = 0;
9737 plane_state->crtc_w = hdisplay;
9738 plane_state->crtc_h = vdisplay;
9739 plane_state->src_x = x << 16;
9740 plane_state->src_y = y << 16;
9741 plane_state->src_w = hdisplay << 16;
9742 plane_state->src_h = vdisplay << 16;
9743
9744 return 0;
9745}
9746
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009747bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01009748 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05009749 struct intel_load_detect_pipe *old,
9750 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009751{
9752 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009753 struct intel_encoder *intel_encoder =
9754 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009755 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009756 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009757 struct drm_crtc *crtc = NULL;
9758 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02009759 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009760 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009761 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009762 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009763 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009764 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009765
Chris Wilsond2dff872011-04-19 08:36:26 +01009766 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009767 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009768 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009769
Rob Clark51fd3712013-11-19 12:10:12 -05009770retry:
9771 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9772 if (ret)
9773 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009774
Jesse Barnes79e53942008-11-07 14:24:08 -08009775 /*
9776 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009777 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009778 * - if the connector already has an assigned crtc, use it (but make
9779 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009780 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009781 * - try to find the first unused crtc that can drive this connector,
9782 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009783 */
9784
9785 /* See if we already have a CRTC for this connector */
9786 if (encoder->crtc) {
9787 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009788
Rob Clark51fd3712013-11-19 12:10:12 -05009789 ret = drm_modeset_lock(&crtc->mutex, ctx);
9790 if (ret)
9791 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009792 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9793 if (ret)
9794 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01009795
Daniel Vetter24218aa2012-08-12 19:27:11 +02009796 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009797 old->load_detect_temp = false;
9798
9799 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009800 if (connector->dpms != DRM_MODE_DPMS_ON)
9801 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01009802
Chris Wilson71731882011-04-19 23:10:58 +01009803 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08009804 }
9805
9806 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009807 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009808 i++;
9809 if (!(encoder->possible_crtcs & (1 << i)))
9810 continue;
Matt Roper83d65732015-02-25 13:12:16 -08009811 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +03009812 continue;
9813 /* This can occur when applying the pipe A quirk on resume. */
9814 if (to_intel_crtc(possible_crtc)->new_enabled)
9815 continue;
9816
9817 crtc = possible_crtc;
9818 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009819 }
9820
9821 /*
9822 * If we didn't find an unused CRTC, don't use any.
9823 */
9824 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01009825 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05009826 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08009827 }
9828
Rob Clark51fd3712013-11-19 12:10:12 -05009829 ret = drm_modeset_lock(&crtc->mutex, ctx);
9830 if (ret)
9831 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009832 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9833 if (ret)
9834 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02009835 intel_encoder->new_crtc = to_intel_crtc(crtc);
9836 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009837
9838 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009839 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +02009840 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009841 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01009842 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08009843
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009844 state = drm_atomic_state_alloc(dev);
9845 if (!state)
9846 return false;
9847
9848 state->acquire_ctx = ctx;
9849
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009850 connector_state = drm_atomic_get_connector_state(state, connector);
9851 if (IS_ERR(connector_state)) {
9852 ret = PTR_ERR(connector_state);
9853 goto fail;
9854 }
9855
9856 connector_state->crtc = crtc;
9857 connector_state->best_encoder = &intel_encoder->base;
9858
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009859 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9860 if (IS_ERR(crtc_state)) {
9861 ret = PTR_ERR(crtc_state);
9862 goto fail;
9863 }
9864
9865 crtc_state->base.enable = true;
9866
Chris Wilson64927112011-04-20 07:25:26 +01009867 if (!mode)
9868 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08009869
Chris Wilsond2dff872011-04-19 08:36:26 +01009870 /* We need a framebuffer large enough to accommodate all accesses
9871 * that the plane may generate whilst we perform load detection.
9872 * We can not rely on the fbcon either being present (we get called
9873 * during its initialisation to detect all boot displays, or it may
9874 * not even exist) or that it is large enough to satisfy the
9875 * requested mode.
9876 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02009877 fb = mode_fits_in_fbdev(dev, mode);
9878 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009879 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009880 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9881 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009882 } else
9883 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009884 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009885 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009886 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009887 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009888
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009889 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
9890 if (ret)
9891 goto fail;
9892
Ander Conselvan de Oliveiracf6d0d72015-04-21 17:13:10 +03009893 if (intel_set_mode(crtc, mode, state)) {
Chris Wilson64927112011-04-20 07:25:26 +01009894 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01009895 if (old->release_fb)
9896 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009897 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009898 }
Daniel Vetter9128b042015-03-03 17:31:21 +01009899 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +01009900
Jesse Barnes79e53942008-11-07 14:24:08 -08009901 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009902 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01009903 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009904
9905 fail:
Matt Roper83d65732015-02-25 13:12:16 -08009906 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -05009907fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +03009908 drm_atomic_state_free(state);
9909 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009910
Rob Clark51fd3712013-11-19 12:10:12 -05009911 if (ret == -EDEADLK) {
9912 drm_modeset_backoff(ctx);
9913 goto retry;
9914 }
9915
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009916 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009917}
9918
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009919void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +02009920 struct intel_load_detect_pipe *old,
9921 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009922{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009923 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009924 struct intel_encoder *intel_encoder =
9925 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01009926 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01009927 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009928 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009929 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009930 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009931 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009932 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009933
Chris Wilsond2dff872011-04-19 08:36:26 +01009934 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009935 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009936 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009937
Chris Wilson8261b192011-04-19 23:18:09 +01009938 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009939 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009940 if (!state)
9941 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009942
9943 state->acquire_ctx = ctx;
9944
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009945 connector_state = drm_atomic_get_connector_state(state, connector);
9946 if (IS_ERR(connector_state))
9947 goto fail;
9948
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009949 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9950 if (IS_ERR(crtc_state))
9951 goto fail;
9952
Daniel Vetterfc303102012-07-09 10:40:58 +02009953 to_intel_connector(connector)->new_encoder = NULL;
9954 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009955 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009956
9957 connector_state->best_encoder = NULL;
9958 connector_state->crtc = NULL;
9959
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009960 crtc_state->base.enable = false;
9961
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009962 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
9963 0, 0);
9964 if (ret)
9965 goto fail;
9966
Ander Conselvan de Oliveiracf6d0d72015-04-21 17:13:10 +03009967 intel_set_mode(crtc, NULL, state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009968
9969 drm_atomic_state_free(state);
Chris Wilsond2dff872011-04-19 08:36:26 +01009970
Daniel Vetter36206362012-12-10 20:42:17 +01009971 if (old->release_fb) {
9972 drm_framebuffer_unregister_private(old->release_fb);
9973 drm_framebuffer_unreference(old->release_fb);
9974 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009975
Chris Wilson0622a532011-04-21 09:32:11 +01009976 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009977 }
9978
Eric Anholtc751ce42010-03-25 11:48:48 -07009979 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009980 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9981 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009982
9983 return;
9984fail:
9985 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9986 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -08009987}
9988
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009989static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009990 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009991{
9992 struct drm_i915_private *dev_priv = dev->dev_private;
9993 u32 dpll = pipe_config->dpll_hw_state.dpll;
9994
9995 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009996 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009997 else if (HAS_PCH_SPLIT(dev))
9998 return 120000;
9999 else if (!IS_GEN2(dev))
10000 return 96000;
10001 else
10002 return 48000;
10003}
10004
Jesse Barnes79e53942008-11-07 14:24:08 -080010005/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010006static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010007 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010008{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010009 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010010 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010011 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010012 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010013 u32 fp;
10014 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010015 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010016
10017 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010018 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010019 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010020 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010021
10022 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010023 if (IS_PINEVIEW(dev)) {
10024 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10025 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010026 } else {
10027 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10028 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10029 }
10030
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010031 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010032 if (IS_PINEVIEW(dev))
10033 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10034 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010035 else
10036 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010037 DPLL_FPA01_P1_POST_DIV_SHIFT);
10038
10039 switch (dpll & DPLL_MODE_MASK) {
10040 case DPLLB_MODE_DAC_SERIAL:
10041 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10042 5 : 10;
10043 break;
10044 case DPLLB_MODE_LVDS:
10045 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10046 7 : 14;
10047 break;
10048 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010049 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010050 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010051 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010052 }
10053
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010054 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010055 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010056 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010057 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010058 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010059 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010060 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010061
10062 if (is_lvds) {
10063 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10064 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010065
10066 if (lvds & LVDS_CLKB_POWER_UP)
10067 clock.p2 = 7;
10068 else
10069 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010070 } else {
10071 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10072 clock.p1 = 2;
10073 else {
10074 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10075 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10076 }
10077 if (dpll & PLL_P2_DIVIDE_BY_4)
10078 clock.p2 = 4;
10079 else
10080 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010081 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010082
10083 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010084 }
10085
Ville Syrjälä18442d02013-09-13 16:00:08 +030010086 /*
10087 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010088 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010089 * encoder's get_config() function.
10090 */
10091 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010092}
10093
Ville Syrjälä6878da02013-09-13 15:59:11 +030010094int intel_dotclock_calculate(int link_freq,
10095 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010096{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010097 /*
10098 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010099 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010100 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010101 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010102 *
10103 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010104 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010105 */
10106
Ville Syrjälä6878da02013-09-13 15:59:11 +030010107 if (!m_n->link_n)
10108 return 0;
10109
10110 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10111}
10112
Ville Syrjälä18442d02013-09-13 16:00:08 +030010113static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010114 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010115{
10116 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010117
10118 /* read out port_clock from the DPLL */
10119 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010120
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010121 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010122 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010123 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010124 * agree once we know their relationship in the encoder's
10125 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010126 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010127 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010128 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10129 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010130}
10131
10132/** Returns the currently programmed mode of the given pipe. */
10133struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10134 struct drm_crtc *crtc)
10135{
Jesse Barnes548f2452011-02-17 10:40:53 -080010136 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010137 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010138 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010139 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010140 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010141 int htot = I915_READ(HTOTAL(cpu_transcoder));
10142 int hsync = I915_READ(HSYNC(cpu_transcoder));
10143 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10144 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010145 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010146
10147 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10148 if (!mode)
10149 return NULL;
10150
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010151 /*
10152 * Construct a pipe_config sufficient for getting the clock info
10153 * back out of crtc_clock_get.
10154 *
10155 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10156 * to use a real value here instead.
10157 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010158 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010159 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010160 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10161 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10162 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010163 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10164
Ville Syrjälä773ae032013-09-23 17:48:20 +030010165 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010166 mode->hdisplay = (htot & 0xffff) + 1;
10167 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10168 mode->hsync_start = (hsync & 0xffff) + 1;
10169 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10170 mode->vdisplay = (vtot & 0xffff) + 1;
10171 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10172 mode->vsync_start = (vsync & 0xffff) + 1;
10173 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10174
10175 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010176
10177 return mode;
10178}
10179
Jesse Barnes652c3932009-08-17 13:31:43 -070010180static void intel_decrease_pllclock(struct drm_crtc *crtc)
10181{
10182 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010183 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010184 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010185
Sonika Jindalbaff2962014-07-22 11:16:35 +053010186 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010187 return;
10188
10189 if (!dev_priv->lvds_downclock_avail)
10190 return;
10191
10192 /*
10193 * Since this is called by a timer, we should never get here in
10194 * the manual case.
10195 */
10196 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010197 int pipe = intel_crtc->pipe;
10198 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010199 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010200
Zhao Yakui44d98a62009-10-09 11:39:40 +080010201 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010202
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010203 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010204
Chris Wilson074b5e12012-05-02 12:07:06 +010010205 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010206 dpll |= DISPLAY_RATE_SELECT_FPA1;
10207 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010208 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010209 dpll = I915_READ(dpll_reg);
10210 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010211 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010212 }
10213
10214}
10215
Chris Wilsonf047e392012-07-21 12:31:41 +010010216void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010217{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010218 struct drm_i915_private *dev_priv = dev->dev_private;
10219
Chris Wilsonf62a0072014-02-21 17:55:39 +000010220 if (dev_priv->mm.busy)
10221 return;
10222
Paulo Zanoni43694d62014-03-07 20:08:08 -030010223 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010224 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010225 if (INTEL_INFO(dev)->gen >= 6)
10226 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010227 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010228}
10229
10230void intel_mark_idle(struct drm_device *dev)
10231{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010232 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010233 struct drm_crtc *crtc;
10234
Chris Wilsonf62a0072014-02-21 17:55:39 +000010235 if (!dev_priv->mm.busy)
10236 return;
10237
10238 dev_priv->mm.busy = false;
10239
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010240 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010241 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010242 continue;
10243
10244 intel_decrease_pllclock(crtc);
10245 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010246
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010247 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010248 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010249
Paulo Zanoni43694d62014-03-07 20:08:08 -030010250 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010251}
10252
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010253static void intel_crtc_set_state(struct intel_crtc *crtc,
10254 struct intel_crtc_state *crtc_state)
10255{
10256 kfree(crtc->config);
10257 crtc->config = crtc_state;
Ander Conselvan de Oliveira16f3f652015-01-15 14:55:27 +020010258 crtc->base.state = &crtc_state->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010259}
10260
Jesse Barnes79e53942008-11-07 14:24:08 -080010261static void intel_crtc_destroy(struct drm_crtc *crtc)
10262{
10263 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010264 struct drm_device *dev = crtc->dev;
10265 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010266
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010267 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010268 work = intel_crtc->unpin_work;
10269 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010270 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010271
10272 if (work) {
10273 cancel_work_sync(&work->work);
10274 kfree(work);
10275 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010276
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010277 intel_crtc_set_state(intel_crtc, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -080010278 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010279
Jesse Barnes79e53942008-11-07 14:24:08 -080010280 kfree(intel_crtc);
10281}
10282
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010283static void intel_unpin_work_fn(struct work_struct *__work)
10284{
10285 struct intel_unpin_work *work =
10286 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010287 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010288 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010289
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010290 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010291 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010292 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010293
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010294 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010295
10296 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010297 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010298 mutex_unlock(&dev->struct_mutex);
10299
Daniel Vetterf99d7062014-06-19 16:01:59 +020010300 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010301 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010302
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010303 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10304 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10305
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010306 kfree(work);
10307}
10308
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010309static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010310 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010311{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010312 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10313 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010314 unsigned long flags;
10315
10316 /* Ignore early vblank irqs */
10317 if (intel_crtc == NULL)
10318 return;
10319
Daniel Vetterf3260382014-09-15 14:55:23 +020010320 /*
10321 * This is called both by irq handlers and the reset code (to complete
10322 * lost pageflips) so needs the full irqsave spinlocks.
10323 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010324 spin_lock_irqsave(&dev->event_lock, flags);
10325 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010326
10327 /* Ensure we don't miss a work->pending update ... */
10328 smp_rmb();
10329
10330 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010331 spin_unlock_irqrestore(&dev->event_lock, flags);
10332 return;
10333 }
10334
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010335 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010336
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010337 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010338}
10339
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010340void intel_finish_page_flip(struct drm_device *dev, int pipe)
10341{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010342 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010343 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10344
Mario Kleiner49b14a52010-12-09 07:00:07 +010010345 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010346}
10347
10348void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10349{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010350 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010351 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10352
Mario Kleiner49b14a52010-12-09 07:00:07 +010010353 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010354}
10355
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010356/* Is 'a' after or equal to 'b'? */
10357static bool g4x_flip_count_after_eq(u32 a, u32 b)
10358{
10359 return !((a - b) & 0x80000000);
10360}
10361
10362static bool page_flip_finished(struct intel_crtc *crtc)
10363{
10364 struct drm_device *dev = crtc->base.dev;
10365 struct drm_i915_private *dev_priv = dev->dev_private;
10366
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010367 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10368 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10369 return true;
10370
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010371 /*
10372 * The relevant registers doen't exist on pre-ctg.
10373 * As the flip done interrupt doesn't trigger for mmio
10374 * flips on gmch platforms, a flip count check isn't
10375 * really needed there. But since ctg has the registers,
10376 * include it in the check anyway.
10377 */
10378 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10379 return true;
10380
10381 /*
10382 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10383 * used the same base address. In that case the mmio flip might
10384 * have completed, but the CS hasn't even executed the flip yet.
10385 *
10386 * A flip count check isn't enough as the CS might have updated
10387 * the base address just after start of vblank, but before we
10388 * managed to process the interrupt. This means we'd complete the
10389 * CS flip too soon.
10390 *
10391 * Combining both checks should get us a good enough result. It may
10392 * still happen that the CS flip has been executed, but has not
10393 * yet actually completed. But in case the base address is the same
10394 * anyway, we don't really care.
10395 */
10396 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10397 crtc->unpin_work->gtt_offset &&
10398 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10399 crtc->unpin_work->flip_count);
10400}
10401
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010402void intel_prepare_page_flip(struct drm_device *dev, int plane)
10403{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010404 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010405 struct intel_crtc *intel_crtc =
10406 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10407 unsigned long flags;
10408
Daniel Vetterf3260382014-09-15 14:55:23 +020010409
10410 /*
10411 * This is called both by irq handlers and the reset code (to complete
10412 * lost pageflips) so needs the full irqsave spinlocks.
10413 *
10414 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010415 * generate a page-flip completion irq, i.e. every modeset
10416 * is also accompanied by a spurious intel_prepare_page_flip().
10417 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010418 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010419 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010420 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010421 spin_unlock_irqrestore(&dev->event_lock, flags);
10422}
10423
Robin Schroereba905b2014-05-18 02:24:50 +020010424static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010425{
10426 /* Ensure that the work item is consistent when activating it ... */
10427 smp_wmb();
10428 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10429 /* and that it is marked active as soon as the irq could fire. */
10430 smp_wmb();
10431}
10432
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010433static int intel_gen2_queue_flip(struct drm_device *dev,
10434 struct drm_crtc *crtc,
10435 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010436 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010437 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010438 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010439{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010440 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010441 u32 flip_mask;
10442 int ret;
10443
Daniel Vetter6d90c952012-04-26 23:28:05 +020010444 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010445 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010446 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010447
10448 /* Can't queue multiple flips, so wait for the previous
10449 * one to finish before executing the next.
10450 */
10451 if (intel_crtc->plane)
10452 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10453 else
10454 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010455 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10456 intel_ring_emit(ring, MI_NOOP);
10457 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10458 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10459 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010460 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010461 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010462
10463 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010464 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010465 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010466}
10467
10468static int intel_gen3_queue_flip(struct drm_device *dev,
10469 struct drm_crtc *crtc,
10470 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010471 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010472 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010473 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010474{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010475 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010476 u32 flip_mask;
10477 int ret;
10478
Daniel Vetter6d90c952012-04-26 23:28:05 +020010479 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010480 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010481 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010482
10483 if (intel_crtc->plane)
10484 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10485 else
10486 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010487 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10488 intel_ring_emit(ring, MI_NOOP);
10489 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10490 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10491 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010492 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010493 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010494
Chris Wilsone7d841c2012-12-03 11:36:30 +000010495 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010496 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010497 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010498}
10499
10500static int intel_gen4_queue_flip(struct drm_device *dev,
10501 struct drm_crtc *crtc,
10502 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010503 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010504 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010505 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010506{
10507 struct drm_i915_private *dev_priv = dev->dev_private;
10508 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10509 uint32_t pf, pipesrc;
10510 int ret;
10511
Daniel Vetter6d90c952012-04-26 23:28:05 +020010512 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010513 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010514 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010515
10516 /* i965+ uses the linear or tiled offsets from the
10517 * Display Registers (which do not change across a page-flip)
10518 * so we need only reprogram the base address.
10519 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010520 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10521 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10522 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010523 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010524 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010525
10526 /* XXX Enabling the panel-fitter across page-flip is so far
10527 * untested on non-native modes, so ignore it for now.
10528 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10529 */
10530 pf = 0;
10531 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010532 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010533
10534 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010535 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010536 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010537}
10538
10539static int intel_gen6_queue_flip(struct drm_device *dev,
10540 struct drm_crtc *crtc,
10541 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010542 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010543 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010544 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010545{
10546 struct drm_i915_private *dev_priv = dev->dev_private;
10547 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10548 uint32_t pf, pipesrc;
10549 int ret;
10550
Daniel Vetter6d90c952012-04-26 23:28:05 +020010551 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010552 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010553 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010554
Daniel Vetter6d90c952012-04-26 23:28:05 +020010555 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10556 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10557 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010558 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010559
Chris Wilson99d9acd2012-04-17 20:37:00 +010010560 /* Contrary to the suggestions in the documentation,
10561 * "Enable Panel Fitter" does not seem to be required when page
10562 * flipping with a non-native mode, and worse causes a normal
10563 * modeset to fail.
10564 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10565 */
10566 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010567 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010568 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010569
10570 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010571 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010572 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010573}
10574
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010575static int intel_gen7_queue_flip(struct drm_device *dev,
10576 struct drm_crtc *crtc,
10577 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010578 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010579 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010580 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010581{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010582 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010583 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010584 int len, ret;
10585
Robin Schroereba905b2014-05-18 02:24:50 +020010586 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010587 case PLANE_A:
10588 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10589 break;
10590 case PLANE_B:
10591 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10592 break;
10593 case PLANE_C:
10594 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10595 break;
10596 default:
10597 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010598 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010599 }
10600
Chris Wilsonffe74d72013-08-26 20:58:12 +010010601 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010602 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010603 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010604 /*
10605 * On Gen 8, SRM is now taking an extra dword to accommodate
10606 * 48bits addresses, and we need a NOOP for the batch size to
10607 * stay even.
10608 */
10609 if (IS_GEN8(dev))
10610 len += 2;
10611 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010612
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010613 /*
10614 * BSpec MI_DISPLAY_FLIP for IVB:
10615 * "The full packet must be contained within the same cache line."
10616 *
10617 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10618 * cacheline, if we ever start emitting more commands before
10619 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10620 * then do the cacheline alignment, and finally emit the
10621 * MI_DISPLAY_FLIP.
10622 */
10623 ret = intel_ring_cacheline_align(ring);
10624 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010625 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010626
Chris Wilsonffe74d72013-08-26 20:58:12 +010010627 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010628 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010629 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010630
Chris Wilsonffe74d72013-08-26 20:58:12 +010010631 /* Unmask the flip-done completion message. Note that the bspec says that
10632 * we should do this for both the BCS and RCS, and that we must not unmask
10633 * more than one flip event at any time (or ensure that one flip message
10634 * can be sent by waiting for flip-done prior to queueing new flips).
10635 * Experimentation says that BCS works despite DERRMR masking all
10636 * flip-done completion events and that unmasking all planes at once
10637 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10638 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10639 */
10640 if (ring->id == RCS) {
10641 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10642 intel_ring_emit(ring, DERRMR);
10643 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10644 DERRMR_PIPEB_PRI_FLIP_DONE |
10645 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010010646 if (IS_GEN8(dev))
10647 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10648 MI_SRM_LRM_GLOBAL_GTT);
10649 else
10650 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10651 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010010652 intel_ring_emit(ring, DERRMR);
10653 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010010654 if (IS_GEN8(dev)) {
10655 intel_ring_emit(ring, 0);
10656 intel_ring_emit(ring, MI_NOOP);
10657 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010658 }
10659
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010660 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010661 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010662 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010663 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000010664
10665 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010666 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010667 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010668}
10669
Sourab Gupta84c33a62014-06-02 16:47:17 +053010670static bool use_mmio_flip(struct intel_engine_cs *ring,
10671 struct drm_i915_gem_object *obj)
10672{
10673 /*
10674 * This is not being used for older platforms, because
10675 * non-availability of flip done interrupt forces us to use
10676 * CS flips. Older platforms derive flip done using some clever
10677 * tricks involving the flip_pending status bits and vblank irqs.
10678 * So using MMIO flips there would disrupt this mechanism.
10679 */
10680
Chris Wilson8e09bf82014-07-08 10:40:30 +010010681 if (ring == NULL)
10682 return true;
10683
Sourab Gupta84c33a62014-06-02 16:47:17 +053010684 if (INTEL_INFO(ring->dev)->gen < 5)
10685 return false;
10686
10687 if (i915.use_mmio_flip < 0)
10688 return false;
10689 else if (i915.use_mmio_flip > 0)
10690 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010010691 else if (i915.enable_execlists)
10692 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010693 else
John Harrison41c52412014-11-24 18:49:43 +000010694 return ring != i915_gem_request_get_ring(obj->last_read_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010695}
10696
Damien Lespiauff944562014-11-20 14:58:16 +000010697static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10698{
10699 struct drm_device *dev = intel_crtc->base.dev;
10700 struct drm_i915_private *dev_priv = dev->dev_private;
10701 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000010702 const enum pipe pipe = intel_crtc->pipe;
10703 u32 ctl, stride;
10704
10705 ctl = I915_READ(PLANE_CTL(pipe, 0));
10706 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010707 switch (fb->modifier[0]) {
10708 case DRM_FORMAT_MOD_NONE:
10709 break;
10710 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000010711 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010712 break;
10713 case I915_FORMAT_MOD_Y_TILED:
10714 ctl |= PLANE_CTL_TILED_Y;
10715 break;
10716 case I915_FORMAT_MOD_Yf_TILED:
10717 ctl |= PLANE_CTL_TILED_YF;
10718 break;
10719 default:
10720 MISSING_CASE(fb->modifier[0]);
10721 }
Damien Lespiauff944562014-11-20 14:58:16 +000010722
10723 /*
10724 * The stride is either expressed as a multiple of 64 bytes chunks for
10725 * linear buffers or in number of tiles for tiled buffers.
10726 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010727 stride = fb->pitches[0] /
10728 intel_fb_stride_alignment(dev, fb->modifier[0],
10729 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000010730
10731 /*
10732 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10733 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10734 */
10735 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10736 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10737
10738 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10739 POSTING_READ(PLANE_SURF(pipe, 0));
10740}
10741
10742static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010743{
10744 struct drm_device *dev = intel_crtc->base.dev;
10745 struct drm_i915_private *dev_priv = dev->dev_private;
10746 struct intel_framebuffer *intel_fb =
10747 to_intel_framebuffer(intel_crtc->base.primary->fb);
10748 struct drm_i915_gem_object *obj = intel_fb->obj;
10749 u32 dspcntr;
10750 u32 reg;
10751
Sourab Gupta84c33a62014-06-02 16:47:17 +053010752 reg = DSPCNTR(intel_crtc->plane);
10753 dspcntr = I915_READ(reg);
10754
Damien Lespiauc5d97472014-10-25 00:11:11 +010010755 if (obj->tiling_mode != I915_TILING_NONE)
10756 dspcntr |= DISPPLANE_TILED;
10757 else
10758 dspcntr &= ~DISPPLANE_TILED;
10759
Sourab Gupta84c33a62014-06-02 16:47:17 +053010760 I915_WRITE(reg, dspcntr);
10761
10762 I915_WRITE(DSPSURF(intel_crtc->plane),
10763 intel_crtc->unpin_work->gtt_offset);
10764 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010765
Damien Lespiauff944562014-11-20 14:58:16 +000010766}
10767
10768/*
10769 * XXX: This is the temporary way to update the plane registers until we get
10770 * around to using the usual plane update functions for MMIO flips
10771 */
10772static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10773{
10774 struct drm_device *dev = intel_crtc->base.dev;
10775 bool atomic_update;
10776 u32 start_vbl_count;
10777
10778 intel_mark_page_flip_active(intel_crtc);
10779
10780 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10781
10782 if (INTEL_INFO(dev)->gen >= 9)
10783 skl_do_mmio_flip(intel_crtc);
10784 else
10785 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10786 ilk_do_mmio_flip(intel_crtc);
10787
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010788 if (atomic_update)
10789 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010790}
10791
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010792static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010793{
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010794 struct intel_crtc *crtc =
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010795 container_of(work, struct intel_crtc, mmio_flip.work);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010796 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010797
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010798 mmio_flip = &crtc->mmio_flip;
10799 if (mmio_flip->req)
John Harrison9c654812014-11-24 18:49:35 +000010800 WARN_ON(__i915_wait_request(mmio_flip->req,
10801 crtc->reset_counter,
10802 false, NULL, NULL) != 0);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010803
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010804 intel_do_mmio_flip(crtc);
10805 if (mmio_flip->req) {
10806 mutex_lock(&crtc->base.dev->struct_mutex);
John Harrison146d84f2014-12-05 13:49:33 +000010807 i915_gem_request_assign(&mmio_flip->req, NULL);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010808 mutex_unlock(&crtc->base.dev->struct_mutex);
10809 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053010810}
10811
10812static int intel_queue_mmio_flip(struct drm_device *dev,
10813 struct drm_crtc *crtc,
10814 struct drm_framebuffer *fb,
10815 struct drm_i915_gem_object *obj,
10816 struct intel_engine_cs *ring,
10817 uint32_t flags)
10818{
Sourab Gupta84c33a62014-06-02 16:47:17 +053010819 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010820
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010821 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
10822 obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010823
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020010824 schedule_work(&intel_crtc->mmio_flip.work);
10825
Sourab Gupta84c33a62014-06-02 16:47:17 +053010826 return 0;
10827}
10828
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010829static int intel_default_queue_flip(struct drm_device *dev,
10830 struct drm_crtc *crtc,
10831 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010832 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010833 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010834 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010835{
10836 return -ENODEV;
10837}
10838
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010839static bool __intel_pageflip_stall_check(struct drm_device *dev,
10840 struct drm_crtc *crtc)
10841{
10842 struct drm_i915_private *dev_priv = dev->dev_private;
10843 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10844 struct intel_unpin_work *work = intel_crtc->unpin_work;
10845 u32 addr;
10846
10847 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10848 return true;
10849
10850 if (!work->enable_stall_check)
10851 return false;
10852
10853 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010010854 if (work->flip_queued_req &&
10855 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010856 return false;
10857
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010858 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010859 }
10860
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010861 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010862 return false;
10863
10864 /* Potential stall - if we see that the flip has happened,
10865 * assume a missed interrupt. */
10866 if (INTEL_INFO(dev)->gen >= 4)
10867 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10868 else
10869 addr = I915_READ(DSPADDR(intel_crtc->plane));
10870
10871 /* There is a potential issue here with a false positive after a flip
10872 * to the same address. We could address this by checking for a
10873 * non-incrementing frame counter.
10874 */
10875 return addr == work->gtt_offset;
10876}
10877
10878void intel_check_page_flip(struct drm_device *dev, int pipe)
10879{
10880 struct drm_i915_private *dev_priv = dev->dev_private;
10881 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010883 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020010884
Dave Gordon6c51d462015-03-06 15:34:26 +000010885 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010886
10887 if (crtc == NULL)
10888 return;
10889
Daniel Vetterf3260382014-09-15 14:55:23 +020010890 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010891 work = intel_crtc->unpin_work;
10892 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010893 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010010894 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010895 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010896 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010897 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010010898 if (work != NULL &&
10899 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10900 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020010901 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010902}
10903
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010904static int intel_crtc_page_flip(struct drm_crtc *crtc,
10905 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010906 struct drm_pending_vblank_event *event,
10907 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010908{
10909 struct drm_device *dev = crtc->dev;
10910 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070010911 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070010912 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010913 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080010914 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020010915 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010916 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010917 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010918 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010010919 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010920
Matt Roper2ff8fde2014-07-08 07:50:07 -070010921 /*
10922 * drm_mode_page_flip_ioctl() should already catch this, but double
10923 * check to be safe. In the future we may enable pageflipping from
10924 * a disabled primary plane.
10925 */
10926 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10927 return -EBUSY;
10928
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010929 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070010930 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010931 return -EINVAL;
10932
10933 /*
10934 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10935 * Note that pitch changes could also affect these register.
10936 */
10937 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070010938 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10939 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010940 return -EINVAL;
10941
Chris Wilsonf900db42014-02-20 09:26:13 +000010942 if (i915_terminally_wedged(&dev_priv->gpu_error))
10943 goto out_hang;
10944
Daniel Vetterb14c5672013-09-19 12:18:32 +020010945 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010946 if (work == NULL)
10947 return -ENOMEM;
10948
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010949 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010950 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010951 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010952 INIT_WORK(&work->work, intel_unpin_work_fn);
10953
Daniel Vetter87b6b102014-05-15 15:33:46 +020010954 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010955 if (ret)
10956 goto free_work;
10957
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010958 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010959 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010960 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010961 /* Before declaring the flip queue wedged, check if
10962 * the hardware completed the operation behind our backs.
10963 */
10964 if (__intel_pageflip_stall_check(dev, crtc)) {
10965 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10966 page_flip_completed(intel_crtc);
10967 } else {
10968 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010969 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010010970
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010971 drm_crtc_vblank_put(crtc);
10972 kfree(work);
10973 return -EBUSY;
10974 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010975 }
10976 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010977 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010978
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010979 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10980 flush_workqueue(dev_priv->wq);
10981
Jesse Barnes75dfca82010-02-10 15:09:44 -080010982 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010983 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010984 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010985
Matt Roperf4510a22014-04-01 15:22:40 -070010986 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010987 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080010988
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010989 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010990
Chris Wilson89ed88b2015-02-16 14:31:49 +000010991 ret = i915_mutex_lock_interruptible(dev);
10992 if (ret)
10993 goto cleanup;
10994
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010995 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020010996 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010997
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010998 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020010999 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011000
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011001 if (IS_VALLEYVIEW(dev)) {
11002 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011003 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011004 /* vlv: DISPLAY_FLIP fails to change tiling */
11005 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011006 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011007 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011008 } else if (INTEL_INFO(dev)->gen >= 7) {
John Harrison41c52412014-11-24 18:49:43 +000011009 ring = i915_gem_request_get_ring(obj->last_read_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011010 if (ring == NULL || ring->id != RCS)
11011 ring = &dev_priv->ring[BCS];
11012 } else {
11013 ring = &dev_priv->ring[RCS];
11014 }
11015
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011016 mmio_flip = use_mmio_flip(ring, obj);
11017
11018 /* When using CS flips, we want to emit semaphores between rings.
11019 * However, when using mmio flips we will create a task to do the
11020 * synchronisation, so all we want here is to pin the framebuffer
11021 * into the display plane and skip any waits.
11022 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011023 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011024 crtc->primary->state,
11025 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011026 if (ret)
11027 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011028
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011029 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11030 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011031
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011032 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011033 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11034 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011035 if (ret)
11036 goto cleanup_unpin;
11037
John Harrisonf06cc1b2014-11-24 18:49:37 +000011038 i915_gem_request_assign(&work->flip_queued_req,
11039 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011040 } else {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011041 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011042 page_flip_flags);
11043 if (ret)
11044 goto cleanup_unpin;
11045
John Harrisonf06cc1b2014-11-24 18:49:37 +000011046 i915_gem_request_assign(&work->flip_queued_req,
11047 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011048 }
11049
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011050 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011051 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011052
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011053 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020011054 INTEL_FRONTBUFFER_PRIMARY(pipe));
11055
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011056 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011057 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011058 mutex_unlock(&dev->struct_mutex);
11059
Jesse Barnese5510fa2010-07-01 16:48:37 -070011060 trace_i915_flip_request(intel_crtc->plane, obj);
11061
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011062 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011063
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011064cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011065 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011066cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011067 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011068 mutex_unlock(&dev->struct_mutex);
11069cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011070 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011071 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011072
Chris Wilson89ed88b2015-02-16 14:31:49 +000011073 drm_gem_object_unreference_unlocked(&obj->base);
11074 drm_framebuffer_unreference(work->old_fb);
11075
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011076 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011077 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011078 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011079
Daniel Vetter87b6b102014-05-15 15:33:46 +020011080 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011081free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011082 kfree(work);
11083
Chris Wilsonf900db42014-02-20 09:26:13 +000011084 if (ret == -EIO) {
11085out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080011086 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011087 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011088 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011089 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011090 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011091 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011092 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011093 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011094}
11095
Jani Nikula65b38e02015-04-13 11:26:56 +030011096static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011097 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11098 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011099 .atomic_begin = intel_begin_crtc_commit,
11100 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011101};
11102
Daniel Vetter9a935852012-07-05 22:34:27 +020011103/**
11104 * intel_modeset_update_staged_output_state
11105 *
11106 * Updates the staged output configuration state, e.g. after we've read out the
11107 * current hw state.
11108 */
11109static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11110{
Ville Syrjälä76688512014-01-10 11:28:06 +020011111 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011112 struct intel_encoder *encoder;
11113 struct intel_connector *connector;
11114
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011115 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011116 connector->new_encoder =
11117 to_intel_encoder(connector->base.encoder);
11118 }
11119
Damien Lespiaub2784e12014-08-05 11:29:37 +010011120 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011121 encoder->new_crtc =
11122 to_intel_crtc(encoder->base.crtc);
11123 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011124
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011125 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011126 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011127 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011128}
11129
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011130/* Transitional helper to copy current connector/encoder state to
11131 * connector->state. This is needed so that code that is partially
11132 * converted to atomic does the right thing.
11133 */
11134static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11135{
11136 struct intel_connector *connector;
11137
11138 for_each_intel_connector(dev, connector) {
11139 if (connector->base.encoder) {
11140 connector->base.state->best_encoder =
11141 connector->base.encoder;
11142 connector->base.state->crtc =
11143 connector->base.encoder->crtc;
11144 } else {
11145 connector->base.state->best_encoder = NULL;
11146 connector->base.state->crtc = NULL;
11147 }
11148 }
11149}
11150
Daniel Vetter9a935852012-07-05 22:34:27 +020011151/**
11152 * intel_modeset_commit_output_state
11153 *
11154 * This function copies the stage display pipe configuration to the real one.
11155 */
11156static void intel_modeset_commit_output_state(struct drm_device *dev)
11157{
Ville Syrjälä76688512014-01-10 11:28:06 +020011158 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011159 struct intel_encoder *encoder;
11160 struct intel_connector *connector;
11161
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011162 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011163 connector->base.encoder = &connector->new_encoder->base;
11164 }
11165
Damien Lespiaub2784e12014-08-05 11:29:37 +010011166 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011167 encoder->base.crtc = &encoder->new_crtc->base;
11168 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011169
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011170 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011171 crtc->base.state->enable = crtc->new_enabled;
Ville Syrjälä76688512014-01-10 11:28:06 +020011172 crtc->base.enabled = crtc->new_enabled;
11173 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011174
11175 intel_modeset_update_connector_atomic_state(dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020011176}
11177
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011178static void
Robin Schroereba905b2014-05-18 02:24:50 +020011179connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011180 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011181{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011182 int bpp = pipe_config->pipe_bpp;
11183
11184 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11185 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011186 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011187
11188 /* Don't use an invalid EDID bpc value */
11189 if (connector->base.display_info.bpc &&
11190 connector->base.display_info.bpc * 3 < bpp) {
11191 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11192 bpp, connector->base.display_info.bpc*3);
11193 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11194 }
11195
11196 /* Clamp bpp to 8 on screens without EDID 1.4 */
11197 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11198 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11199 bpp);
11200 pipe_config->pipe_bpp = 24;
11201 }
11202}
11203
11204static int
11205compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011206 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011207{
11208 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011209 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011210 struct drm_connector *connector;
11211 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011212 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011213
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011214 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011215 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011216 else if (INTEL_INFO(dev)->gen >= 5)
11217 bpp = 12*3;
11218 else
11219 bpp = 8*3;
11220
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011221
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011222 pipe_config->pipe_bpp = bpp;
11223
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011224 state = pipe_config->base.state;
11225
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011226 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011227 for_each_connector_in_state(state, connector, connector_state, i) {
11228 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011229 continue;
11230
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011231 connected_sink_compute_bpp(to_intel_connector(connector),
11232 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011233 }
11234
11235 return bpp;
11236}
11237
Daniel Vetter644db712013-09-19 14:53:58 +020011238static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11239{
11240 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11241 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011242 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011243 mode->crtc_hdisplay, mode->crtc_hsync_start,
11244 mode->crtc_hsync_end, mode->crtc_htotal,
11245 mode->crtc_vdisplay, mode->crtc_vsync_start,
11246 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11247}
11248
Daniel Vetterc0b03412013-05-28 12:05:54 +020011249static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011250 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011251 const char *context)
11252{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011253 struct drm_device *dev = crtc->base.dev;
11254 struct drm_plane *plane;
11255 struct intel_plane *intel_plane;
11256 struct intel_plane_state *state;
11257 struct drm_framebuffer *fb;
11258
11259 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11260 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011261
11262 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11263 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11264 pipe_config->pipe_bpp, pipe_config->dither);
11265 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11266 pipe_config->has_pch_encoder,
11267 pipe_config->fdi_lanes,
11268 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11269 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11270 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011271 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11272 pipe_config->has_dp_encoder,
11273 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11274 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11275 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011276
11277 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11278 pipe_config->has_dp_encoder,
11279 pipe_config->dp_m2_n2.gmch_m,
11280 pipe_config->dp_m2_n2.gmch_n,
11281 pipe_config->dp_m2_n2.link_m,
11282 pipe_config->dp_m2_n2.link_n,
11283 pipe_config->dp_m2_n2.tu);
11284
Daniel Vetter55072d12014-11-20 16:10:28 +010011285 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11286 pipe_config->has_audio,
11287 pipe_config->has_infoframe);
11288
Daniel Vetterc0b03412013-05-28 12:05:54 +020011289 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011290 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011291 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011292 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11293 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011294 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011295 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11296 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011297 DRM_DEBUG_KMS("num_scalers: %d\n", crtc->num_scalers);
11298 DRM_DEBUG_KMS("scaler_users: 0x%x\n", pipe_config->scaler_state.scaler_users);
11299 DRM_DEBUG_KMS("scaler id: %d\n", pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011300 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11301 pipe_config->gmch_pfit.control,
11302 pipe_config->gmch_pfit.pgm_ratios,
11303 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011304 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011305 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011306 pipe_config->pch_pfit.size,
11307 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011308 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011309 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011310
11311 DRM_DEBUG_KMS("planes on this crtc\n");
11312 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11313 intel_plane = to_intel_plane(plane);
11314 if (intel_plane->pipe != crtc->pipe)
11315 continue;
11316
11317 state = to_intel_plane_state(plane->state);
11318 fb = state->base.fb;
11319 if (!fb) {
11320 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11321 "disabled, scaler_id = %d\n",
11322 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11323 plane->base.id, intel_plane->pipe,
11324 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11325 drm_plane_index(plane), state->scaler_id);
11326 continue;
11327 }
11328
11329 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11330 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11331 plane->base.id, intel_plane->pipe,
11332 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11333 drm_plane_index(plane));
11334 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11335 fb->base.id, fb->width, fb->height, fb->pixel_format);
11336 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11337 state->scaler_id,
11338 state->src.x1 >> 16, state->src.y1 >> 16,
11339 drm_rect_width(&state->src) >> 16,
11340 drm_rect_height(&state->src) >> 16,
11341 state->dst.x1, state->dst.y1,
11342 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11343 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011344}
11345
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011346static bool encoders_cloneable(const struct intel_encoder *a,
11347 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011348{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011349 /* masks could be asymmetric, so check both ways */
11350 return a == b || (a->cloneable & (1 << b->type) &&
11351 b->cloneable & (1 << a->type));
11352}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011353
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011354static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11355 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011356 struct intel_encoder *encoder)
11357{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011358 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011359 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011360 struct drm_connector_state *connector_state;
11361 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011362
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011363 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011364 if (connector_state->crtc != &crtc->base)
11365 continue;
11366
11367 source_encoder =
11368 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011369 if (!encoders_cloneable(encoder, source_encoder))
11370 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011371 }
11372
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011373 return true;
11374}
11375
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011376static bool check_encoder_cloning(struct drm_atomic_state *state,
11377 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011378{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011379 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011380 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011381 struct drm_connector_state *connector_state;
11382 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011383
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011384 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011385 if (connector_state->crtc != &crtc->base)
11386 continue;
11387
11388 encoder = to_intel_encoder(connector_state->best_encoder);
11389 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011390 return false;
11391 }
11392
11393 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011394}
11395
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011396static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011397{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011398 struct drm_device *dev = state->dev;
11399 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011400 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011401 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011402 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011403 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011404
11405 /*
11406 * Walk the connector list instead of the encoder
11407 * list to detect the problem on ddi platforms
11408 * where there's just one encoder per digital port.
11409 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011410 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011411 if (!connector_state->best_encoder)
11412 continue;
11413
11414 encoder = to_intel_encoder(connector_state->best_encoder);
11415
11416 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011417
11418 switch (encoder->type) {
11419 unsigned int port_mask;
11420 case INTEL_OUTPUT_UNKNOWN:
11421 if (WARN_ON(!HAS_DDI(dev)))
11422 break;
11423 case INTEL_OUTPUT_DISPLAYPORT:
11424 case INTEL_OUTPUT_HDMI:
11425 case INTEL_OUTPUT_EDP:
11426 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11427
11428 /* the same port mustn't appear more than once */
11429 if (used_ports & port_mask)
11430 return false;
11431
11432 used_ports |= port_mask;
11433 default:
11434 break;
11435 }
11436 }
11437
11438 return true;
11439}
11440
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011441static void
11442clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11443{
11444 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011445 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011446
Chandra Konduru663a3642015-04-07 15:28:41 -070011447 /* Clear only the intel specific part of the crtc state excluding scalers */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011448 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070011449 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011450 memset(crtc_state, 0, sizeof *crtc_state);
11451 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011452 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011453}
11454
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011455static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011456intel_modeset_pipe_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011457 struct drm_display_mode *mode,
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011458 struct drm_atomic_state *state,
11459 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020011460{
Daniel Vetter7758a112012-07-08 19:40:39 +020011461 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011462 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011463 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011464 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011465 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011466 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011467
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011468 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011469 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011470 return -EINVAL;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011471 }
11472
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011473 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011474 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011475 return -EINVAL;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011476 }
11477
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011478 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011479
Matt Roper07878242015-02-25 11:43:26 -080011480 pipe_config->base.crtc = crtc;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011481 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
11482 drm_mode_copy(&pipe_config->base.mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011483
Daniel Vettere143a212013-07-04 12:01:15 +020011484 pipe_config->cpu_transcoder =
11485 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011486 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011487
Imre Deak2960bc92013-07-30 13:36:32 +030011488 /*
11489 * Sanitize sync polarity flags based on requested ones. If neither
11490 * positive or negative polarity is requested, treat this as meaning
11491 * negative polarity.
11492 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011493 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011494 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011495 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011496
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011497 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011498 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011499 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011500
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011501 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11502 * plane pixel format and any sink constraints into account. Returns the
11503 * source plane bpp so that dithering can be selected on mismatches
11504 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011505 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11506 pipe_config);
11507 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011508 goto fail;
11509
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011510 /*
11511 * Determine the real pipe dimensions. Note that stereo modes can
11512 * increase the actual pipe size due to the frame doubling and
11513 * insertion of additional space for blanks between the frame. This
11514 * is stored in the crtc timings. We use the requested mode to do this
11515 * computation to clearly distinguish it from the adjusted mode, which
11516 * can be changed by the connectors in the below retry loop.
11517 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011518 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011519 &pipe_config->pipe_src_w,
11520 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011521
Daniel Vettere29c22c2013-02-21 00:00:16 +010011522encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011523 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011524 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011525 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011526
Daniel Vetter135c81b2013-07-21 21:37:09 +020011527 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011528 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11529 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011530
Daniel Vetter7758a112012-07-08 19:40:39 +020011531 /* Pass our mode to the connectors and the CRTC to give them a chance to
11532 * adjust it according to limitations or connector properties, and also
11533 * a chance to reject the mode entirely.
11534 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011535 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011536 if (connector_state->crtc != crtc)
11537 continue;
11538
11539 encoder = to_intel_encoder(connector_state->best_encoder);
11540
Daniel Vetterefea6e82013-07-21 21:36:59 +020011541 if (!(encoder->compute_config(encoder, pipe_config))) {
11542 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011543 goto fail;
11544 }
11545 }
11546
Daniel Vetterff9a6752013-06-01 17:16:21 +020011547 /* Set default port clock if not overwritten by the encoder. Needs to be
11548 * done afterwards in case the encoder adjusts the mode. */
11549 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011550 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011551 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011552
Daniel Vettera43f6e02013-06-07 23:10:32 +020011553 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011554 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011555 DRM_DEBUG_KMS("CRTC fixup failed\n");
11556 goto fail;
11557 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011558
11559 if (ret == RETRY) {
11560 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11561 ret = -EINVAL;
11562 goto fail;
11563 }
11564
11565 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11566 retry = false;
11567 goto encoder_retry;
11568 }
11569
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011570 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011571 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011572 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011573
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011574 return 0;
Daniel Vetter7758a112012-07-08 19:40:39 +020011575fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011576 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020011577}
11578
Daniel Vetterea9d7582012-07-10 10:42:52 +020011579static bool intel_crtc_in_use(struct drm_crtc *crtc)
11580{
11581 struct drm_encoder *encoder;
11582 struct drm_device *dev = crtc->dev;
11583
11584 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11585 if (encoder->crtc == crtc)
11586 return true;
11587
11588 return false;
11589}
11590
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011591static bool
11592needs_modeset(struct drm_crtc_state *state)
Daniel Vetterea9d7582012-07-10 10:42:52 +020011593{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011594 return state->mode_changed || state->active_changed;
11595}
11596
11597static void
11598intel_modeset_update_state(struct drm_atomic_state *state)
11599{
11600 struct drm_device *dev = state->dev;
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011601 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011602 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011603 struct drm_crtc *crtc;
11604 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011605 struct drm_connector *connector;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011606 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011607
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011608 intel_shared_dpll_commit(dev_priv);
11609
Damien Lespiaub2784e12014-08-05 11:29:37 +010011610 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020011611 if (!intel_encoder->base.crtc)
11612 continue;
11613
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011614 for_each_crtc_in_state(state, crtc, crtc_state, i)
11615 if (crtc == intel_encoder->base.crtc)
11616 break;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011617
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011618 if (crtc != intel_encoder->base.crtc)
11619 continue;
11620
11621 if (crtc_state->enable && needs_modeset(crtc_state))
Daniel Vetterea9d7582012-07-10 10:42:52 +020011622 intel_encoder->connectors_active = false;
11623 }
11624
11625 intel_modeset_commit_output_state(dev);
11626
Ville Syrjälä76688512014-01-10 11:28:06 +020011627 /* Double check state. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011628 for_each_crtc(dev, crtc) {
11629 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Daniel Vetterea9d7582012-07-10 10:42:52 +020011630 }
11631
11632 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11633 if (!connector->encoder || !connector->encoder->crtc)
11634 continue;
11635
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011636 for_each_crtc_in_state(state, crtc, crtc_state, i)
11637 if (crtc == connector->encoder->crtc)
11638 break;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011639
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011640 if (crtc != connector->encoder->crtc)
11641 continue;
11642
11643 if (crtc_state->enable && needs_modeset(crtc_state)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020011644 struct drm_property *dpms_property =
11645 dev->mode_config.dpms_property;
11646
Daniel Vetterea9d7582012-07-10 10:42:52 +020011647 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050011648 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020011649 dpms_property,
11650 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011651
11652 intel_encoder = to_intel_encoder(connector->encoder);
11653 intel_encoder->connectors_active = true;
11654 }
11655 }
11656
11657}
11658
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011659static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011660{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011661 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011662
11663 if (clock1 == clock2)
11664 return true;
11665
11666 if (!clock1 || !clock2)
11667 return false;
11668
11669 diff = abs(clock1 - clock2);
11670
11671 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11672 return true;
11673
11674 return false;
11675}
11676
Daniel Vetter25c5b262012-07-08 22:08:04 +020011677#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11678 list_for_each_entry((intel_crtc), \
11679 &(dev)->mode_config.crtc_list, \
11680 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020011681 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020011682
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011683static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011684intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011685 struct intel_crtc_state *current_config,
11686 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011687{
Daniel Vetter66e985c2013-06-05 13:34:20 +020011688#define PIPE_CONF_CHECK_X(name) \
11689 if (current_config->name != pipe_config->name) { \
11690 DRM_ERROR("mismatch in " #name " " \
11691 "(expected 0x%08x, found 0x%08x)\n", \
11692 current_config->name, \
11693 pipe_config->name); \
11694 return false; \
11695 }
11696
Daniel Vetter08a24032013-04-19 11:25:34 +020011697#define PIPE_CONF_CHECK_I(name) \
11698 if (current_config->name != pipe_config->name) { \
11699 DRM_ERROR("mismatch in " #name " " \
11700 "(expected %i, found %i)\n", \
11701 current_config->name, \
11702 pipe_config->name); \
11703 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011704 }
11705
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011706/* This is required for BDW+ where there is only one set of registers for
11707 * switching between high and low RR.
11708 * This macro can be used whenever a comparison has to be made between one
11709 * hw state and multiple sw state variables.
11710 */
11711#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11712 if ((current_config->name != pipe_config->name) && \
11713 (current_config->alt_name != pipe_config->name)) { \
11714 DRM_ERROR("mismatch in " #name " " \
11715 "(expected %i or %i, found %i)\n", \
11716 current_config->name, \
11717 current_config->alt_name, \
11718 pipe_config->name); \
11719 return false; \
11720 }
11721
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011722#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11723 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070011724 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011725 "(expected %i, found %i)\n", \
11726 current_config->name & (mask), \
11727 pipe_config->name & (mask)); \
11728 return false; \
11729 }
11730
Ville Syrjälä5e550652013-09-06 23:29:07 +030011731#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11732 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11733 DRM_ERROR("mismatch in " #name " " \
11734 "(expected %i, found %i)\n", \
11735 current_config->name, \
11736 pipe_config->name); \
11737 return false; \
11738 }
11739
Daniel Vetterbb760062013-06-06 14:55:52 +020011740#define PIPE_CONF_QUIRK(quirk) \
11741 ((current_config->quirks | pipe_config->quirks) & (quirk))
11742
Daniel Vettereccb1402013-05-22 00:50:22 +020011743 PIPE_CONF_CHECK_I(cpu_transcoder);
11744
Daniel Vetter08a24032013-04-19 11:25:34 +020011745 PIPE_CONF_CHECK_I(has_pch_encoder);
11746 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020011747 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11748 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11749 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11750 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11751 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020011752
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011753 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011754
11755 if (INTEL_INFO(dev)->gen < 8) {
11756 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11757 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11758 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11759 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11760 PIPE_CONF_CHECK_I(dp_m_n.tu);
11761
11762 if (current_config->has_drrs) {
11763 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11764 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11765 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11766 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11767 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11768 }
11769 } else {
11770 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11771 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11772 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11773 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11774 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11775 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011776
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011777 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11778 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11779 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11780 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11781 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11782 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011783
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011784 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11785 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11786 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11787 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11788 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11789 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011790
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011791 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020011792 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020011793 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11794 IS_VALLEYVIEW(dev))
11795 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080011796 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011797
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011798 PIPE_CONF_CHECK_I(has_audio);
11799
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011800 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011801 DRM_MODE_FLAG_INTERLACE);
11802
Daniel Vetterbb760062013-06-06 14:55:52 +020011803 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011804 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011805 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011806 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011807 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011808 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011809 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011810 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011811 DRM_MODE_FLAG_NVSYNC);
11812 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011813
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011814 PIPE_CONF_CHECK_I(pipe_src_w);
11815 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011816
Daniel Vetter99535992014-04-13 12:00:33 +020011817 /*
11818 * FIXME: BIOS likes to set up a cloned config with lvds+external
11819 * screen. Since we don't yet re-compute the pipe config when moving
11820 * just the lvds port away to another pipe the sw tracking won't match.
11821 *
11822 * Proper atomic modesets with recomputed global state will fix this.
11823 * Until then just don't check gmch state for inherited modes.
11824 */
11825 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11826 PIPE_CONF_CHECK_I(gmch_pfit.control);
11827 /* pfit ratios are autocomputed by the hw on gen4+ */
11828 if (INTEL_INFO(dev)->gen < 4)
11829 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11830 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11831 }
11832
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011833 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11834 if (current_config->pch_pfit.enabled) {
11835 PIPE_CONF_CHECK_I(pch_pfit.pos);
11836 PIPE_CONF_CHECK_I(pch_pfit.size);
11837 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011838
Chandra Kondurua1b22782015-04-07 15:28:45 -070011839 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
11840
Jesse Barnese59150d2014-01-07 13:30:45 -080011841 /* BDW+ don't expose a synchronous way to read the state */
11842 if (IS_HASWELL(dev))
11843 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011844
Ville Syrjälä282740f2013-09-04 18:30:03 +030011845 PIPE_CONF_CHECK_I(double_wide);
11846
Daniel Vetter26804af2014-06-25 22:01:55 +030011847 PIPE_CONF_CHECK_X(ddi_pll_sel);
11848
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011849 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011850 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011851 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011852 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11853 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011854 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011855 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11856 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11857 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011858
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011859 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11860 PIPE_CONF_CHECK_I(pipe_bpp);
11861
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011862 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011863 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011864
Daniel Vetter66e985c2013-06-05 13:34:20 +020011865#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011866#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011867#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011868#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011869#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011870#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011871
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011872 return true;
11873}
11874
Damien Lespiau08db6652014-11-04 17:06:52 +000011875static void check_wm_state(struct drm_device *dev)
11876{
11877 struct drm_i915_private *dev_priv = dev->dev_private;
11878 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11879 struct intel_crtc *intel_crtc;
11880 int plane;
11881
11882 if (INTEL_INFO(dev)->gen < 9)
11883 return;
11884
11885 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11886 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11887
11888 for_each_intel_crtc(dev, intel_crtc) {
11889 struct skl_ddb_entry *hw_entry, *sw_entry;
11890 const enum pipe pipe = intel_crtc->pipe;
11891
11892 if (!intel_crtc->active)
11893 continue;
11894
11895 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000011896 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000011897 hw_entry = &hw_ddb.plane[pipe][plane];
11898 sw_entry = &sw_ddb->plane[pipe][plane];
11899
11900 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11901 continue;
11902
11903 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11904 "(expected (%u,%u), found (%u,%u))\n",
11905 pipe_name(pipe), plane + 1,
11906 sw_entry->start, sw_entry->end,
11907 hw_entry->start, hw_entry->end);
11908 }
11909
11910 /* cursor */
11911 hw_entry = &hw_ddb.cursor[pipe];
11912 sw_entry = &sw_ddb->cursor[pipe];
11913
11914 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11915 continue;
11916
11917 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11918 "(expected (%u,%u), found (%u,%u))\n",
11919 pipe_name(pipe),
11920 sw_entry->start, sw_entry->end,
11921 hw_entry->start, hw_entry->end);
11922 }
11923}
11924
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011925static void
11926check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011927{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011928 struct intel_connector *connector;
11929
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011930 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011931 /* This also checks the encoder/connector hw state with the
11932 * ->get_hw_state callbacks. */
11933 intel_connector_check_state(connector);
11934
Rob Clarke2c719b2014-12-15 13:56:32 -050011935 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011936 "connector's staged encoder doesn't match current encoder\n");
11937 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011938}
11939
11940static void
11941check_encoder_state(struct drm_device *dev)
11942{
11943 struct intel_encoder *encoder;
11944 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011945
Damien Lespiaub2784e12014-08-05 11:29:37 +010011946 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011947 bool enabled = false;
11948 bool active = false;
11949 enum pipe pipe, tracked_pipe;
11950
11951 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11952 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011953 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011954
Rob Clarke2c719b2014-12-15 13:56:32 -050011955 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011956 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011957 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011958 "encoder's active_connectors set, but no crtc\n");
11959
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011960 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011961 if (connector->base.encoder != &encoder->base)
11962 continue;
11963 enabled = true;
11964 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11965 active = true;
11966 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011967 /*
11968 * for MST connectors if we unplug the connector is gone
11969 * away but the encoder is still connected to a crtc
11970 * until a modeset happens in response to the hotplug.
11971 */
11972 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
11973 continue;
11974
Rob Clarke2c719b2014-12-15 13:56:32 -050011975 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011976 "encoder's enabled state mismatch "
11977 "(expected %i, found %i)\n",
11978 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050011979 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011980 "active encoder with no crtc\n");
11981
Rob Clarke2c719b2014-12-15 13:56:32 -050011982 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011983 "encoder's computed active state doesn't match tracked active state "
11984 "(expected %i, found %i)\n", active, encoder->connectors_active);
11985
11986 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050011987 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011988 "encoder's hw state doesn't match sw tracking "
11989 "(expected %i, found %i)\n",
11990 encoder->connectors_active, active);
11991
11992 if (!encoder->base.crtc)
11993 continue;
11994
11995 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050011996 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011997 "active encoder's pipe doesn't match"
11998 "(expected %i, found %i)\n",
11999 tracked_pipe, pipe);
12000
12001 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012002}
12003
12004static void
12005check_crtc_state(struct drm_device *dev)
12006{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012007 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012008 struct intel_crtc *crtc;
12009 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012010 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012011
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012012 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012013 bool enabled = false;
12014 bool active = false;
12015
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012016 memset(&pipe_config, 0, sizeof(pipe_config));
12017
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012018 DRM_DEBUG_KMS("[CRTC:%d]\n",
12019 crtc->base.base.id);
12020
Matt Roper83d65732015-02-25 13:12:16 -080012021 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012022 "active crtc, but not enabled in sw tracking\n");
12023
Damien Lespiaub2784e12014-08-05 11:29:37 +010012024 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012025 if (encoder->base.crtc != &crtc->base)
12026 continue;
12027 enabled = true;
12028 if (encoder->connectors_active)
12029 active = true;
12030 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012031
Rob Clarke2c719b2014-12-15 13:56:32 -050012032 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012033 "crtc's computed active state doesn't match tracked active state "
12034 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012035 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012036 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012037 "(expected %i, found %i)\n", enabled,
12038 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012039
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012040 active = dev_priv->display.get_pipe_config(crtc,
12041 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012042
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012043 /* hw state is inconsistent with the pipe quirk */
12044 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12045 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012046 active = crtc->active;
12047
Damien Lespiaub2784e12014-08-05 11:29:37 +010012048 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012049 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012050 if (encoder->base.crtc != &crtc->base)
12051 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012052 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012053 encoder->get_config(encoder, &pipe_config);
12054 }
12055
Rob Clarke2c719b2014-12-15 13:56:32 -050012056 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012057 "crtc active state doesn't match with hw state "
12058 "(expected %i, found %i)\n", crtc->active, active);
12059
Daniel Vetterc0b03412013-05-28 12:05:54 +020012060 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012061 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012062 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012063 intel_dump_pipe_config(crtc, &pipe_config,
12064 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012065 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012066 "[sw state]");
12067 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012068 }
12069}
12070
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012071static void
12072check_shared_dpll_state(struct drm_device *dev)
12073{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012074 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012075 struct intel_crtc *crtc;
12076 struct intel_dpll_hw_state dpll_hw_state;
12077 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012078
12079 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12080 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12081 int enabled_crtcs = 0, active_crtcs = 0;
12082 bool active;
12083
12084 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12085
12086 DRM_DEBUG_KMS("%s\n", pll->name);
12087
12088 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12089
Rob Clarke2c719b2014-12-15 13:56:32 -050012090 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012091 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012092 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012093 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012094 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012095 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012096 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012097 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012098 "pll on state mismatch (expected %i, found %i)\n",
12099 pll->on, active);
12100
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012101 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012102 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012103 enabled_crtcs++;
12104 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12105 active_crtcs++;
12106 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012107 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012108 "pll active crtcs mismatch (expected %i, found %i)\n",
12109 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012110 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012111 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012112 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012113
Rob Clarke2c719b2014-12-15 13:56:32 -050012114 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012115 sizeof(dpll_hw_state)),
12116 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012117 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012118}
12119
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012120void
12121intel_modeset_check_state(struct drm_device *dev)
12122{
Damien Lespiau08db6652014-11-04 17:06:52 +000012123 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012124 check_connector_state(dev);
12125 check_encoder_state(dev);
12126 check_crtc_state(dev);
12127 check_shared_dpll_state(dev);
12128}
12129
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012130void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012131 int dotclock)
12132{
12133 /*
12134 * FDI already provided one idea for the dotclock.
12135 * Yell if the encoder disagrees.
12136 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012137 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012138 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012139 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012140}
12141
Ville Syrjälä80715b22014-05-15 20:23:23 +030012142static void update_scanline_offset(struct intel_crtc *crtc)
12143{
12144 struct drm_device *dev = crtc->base.dev;
12145
12146 /*
12147 * The scanline counter increments at the leading edge of hsync.
12148 *
12149 * On most platforms it starts counting from vtotal-1 on the
12150 * first active line. That means the scanline counter value is
12151 * always one less than what we would expect. Ie. just after
12152 * start of vblank, which also occurs at start of hsync (on the
12153 * last active line), the scanline counter will read vblank_start-1.
12154 *
12155 * On gen2 the scanline counter starts counting from 1 instead
12156 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12157 * to keep the value positive), instead of adding one.
12158 *
12159 * On HSW+ the behaviour of the scanline counter depends on the output
12160 * type. For DP ports it behaves like most other platforms, but on HDMI
12161 * there's an extra 1 line difference. So we need to add two instead of
12162 * one to the value.
12163 */
12164 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012165 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012166 int vtotal;
12167
12168 vtotal = mode->crtc_vtotal;
12169 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12170 vtotal /= 2;
12171
12172 crtc->scanline_offset = vtotal - 1;
12173 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012174 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012175 crtc->scanline_offset = 2;
12176 } else
12177 crtc->scanline_offset = 1;
12178}
12179
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012180static void
12181intel_atomic_modeset_compute_changed_flags(struct drm_atomic_state *state,
12182 struct drm_crtc *modeset_crtc)
12183{
12184 struct drm_crtc_state *crtc_state;
12185 struct drm_crtc *crtc;
12186 int i;
12187
12188 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12189 if (crtc_state->enable != crtc->state->enable)
12190 crtc_state->mode_changed = true;
12191
12192 /* FIXME: Do we need to always set mode_changed for
12193 * modeset_crtc if it is enabled? modeset_affect_pipes()
12194 * did that. */
12195 }
12196}
12197
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012198static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012199intel_modeset_compute_config(struct drm_crtc *crtc,
12200 struct drm_display_mode *mode,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012201 struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012202{
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012203 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012204 int ret = 0;
12205
12206 ret = drm_atomic_add_affected_connectors(state, crtc);
12207 if (ret)
12208 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012209
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012210 intel_atomic_modeset_compute_changed_flags(state, crtc);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012211
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012212 /*
12213 * Note this needs changes when we start tracking multiple modes
12214 * and crtcs. At that point we'll need to compute the whole config
12215 * (i.e. one pipe_config for each crtc) rather than just the one
12216 * for this crtc.
12217 */
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012218 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
12219 if (IS_ERR(pipe_config))
12220 return pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012221
Ander Conselvan de Oliveira4fed33f2015-04-21 17:13:03 +030012222 if (!pipe_config->base.enable)
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012223 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012224
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012225 ret = intel_modeset_pipe_config(crtc, mode, state, pipe_config);
12226 if (ret)
12227 return ERR_PTR(ret);
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012228
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012229 /* Check things that can only be changed through modeset */
12230 if (pipe_config->has_audio !=
12231 to_intel_crtc(crtc)->config->has_audio)
12232 pipe_config->base.mode_changed = true;
12233
12234 /*
12235 * Note we have an issue here with infoframes: current code
12236 * only updates them on the full mode set path per hw
12237 * requirements. So here we should be checking for any
12238 * required changes and forcing a mode set.
12239 */
12240
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012241 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,"[modeset]");
12242
12243 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012244}
12245
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012246static int __intel_set_mode_setup_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012247{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012248 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012249 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012250 unsigned clear_pipes = 0;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012251 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012252 struct intel_crtc_state *intel_crtc_state;
12253 struct drm_crtc *crtc;
12254 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012255 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012256 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012257
12258 if (!dev_priv->display.crtc_compute_clock)
12259 return 0;
12260
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012261 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12262 intel_crtc = to_intel_crtc(crtc);
12263
12264 if (needs_modeset(crtc_state))
12265 clear_pipes |= 1 << intel_crtc->pipe;
12266 }
12267
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012268 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12269 if (ret)
12270 goto done;
12271
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012272 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12273 if (!needs_modeset(crtc_state) || !crtc_state->enable)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012274 continue;
12275
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012276 intel_crtc = to_intel_crtc(crtc);
12277 intel_crtc_state = to_intel_crtc_state(crtc_state);
12278
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012279 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012280 intel_crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012281 if (ret) {
12282 intel_shared_dpll_abort_config(dev_priv);
12283 goto done;
12284 }
12285 }
12286
12287done:
12288 return ret;
12289}
12290
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012291/* Code that should eventually be part of atomic_check() */
12292static int __intel_set_mode_checks(struct drm_atomic_state *state)
12293{
12294 struct drm_device *dev = state->dev;
12295 int ret;
12296
12297 /*
12298 * See if the config requires any additional preparation, e.g.
12299 * to adjust global state with pipes off. We need to do this
12300 * here so we can get the modeset_pipe updated config for the new
12301 * mode set on this crtc. For other crtcs we need to use the
12302 * adjusted_mode bits in the crtc directly.
12303 */
12304 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
12305 ret = valleyview_modeset_global_pipes(state);
12306 if (ret)
12307 return ret;
12308 }
12309
12310 ret = __intel_set_mode_setup_plls(state);
12311 if (ret)
12312 return ret;
12313
12314 return 0;
12315}
12316
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012317static int __intel_set_mode(struct drm_crtc *modeset_crtc,
Daniel Vetterf30da182013-04-11 20:22:50 +020012318 struct drm_display_mode *mode,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012319 struct intel_crtc_state *pipe_config)
Daniel Vettera6778b32012-07-02 09:56:42 +020012320{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012321 struct drm_device *dev = modeset_crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012322 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012323 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012324 struct intel_crtc_state *crtc_state_copy = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020012325 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012326 struct drm_crtc *crtc;
12327 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030012328 struct drm_plane *plane;
12329 struct drm_plane_state *plane_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012330 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012331 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020012332
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012333 ret = __intel_set_mode_checks(state);
12334 if (ret < 0)
12335 return ret;
12336
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012337 crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030012338 if (!crtc_state_copy)
12339 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012340
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012341 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12342 if (!needs_modeset(crtc_state))
12343 continue;
Daniel Vetter460da9162013-03-27 00:44:51 +010012344
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012345 if (!crtc_state->enable) {
12346 intel_crtc_disable(crtc);
12347 } else if (crtc->state->enable) {
12348 intel_crtc_disable_planes(crtc);
12349 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030012350 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012351 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012352
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012353 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12354 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012355 *
12356 * Note we'll need to fix this up when we start tracking multiple
12357 * pipes; here we assume a single modeset_pipe and only track the
12358 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012359 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012360 if (pipe_config->base.enable && needs_modeset(&pipe_config->base)) {
12361 modeset_crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012362 /* mode_set/enable/disable functions rely on a correct pipe
12363 * config. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012364 intel_crtc_set_state(to_intel_crtc(modeset_crtc), pipe_config);
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020012365
12366 /*
12367 * Calculate and store various constants which
12368 * are later needed by vblank and swap-completion
12369 * timestamping. They are derived from true hwmode.
12370 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012371 drm_calc_timestamping_constants(modeset_crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012372 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012373 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012374
Daniel Vetterea9d7582012-07-10 10:42:52 +020012375 /* Only after disabling all output pipelines that will be changed can we
12376 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012377 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012378
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012379 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020012380
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030012381 for_each_plane_in_state(state, plane, plane_state, i) {
12382 if (WARN_ON(plane != modeset_crtc->primary))
12383 continue;
Daniel Vetter4c107942014-04-24 23:55:05 +020012384
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030012385 /* Primary plane is disabled in intel_crtc_disable() */
12386 if (!pipe_config->base.enable)
12387 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012388
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030012389 ret = drm_plane_helper_update(plane, plane_state->crtc,
12390 plane_state->fb,
12391 plane_state->crtc_x,
12392 plane_state->crtc_y,
12393 plane_state->crtc_w,
12394 plane_state->crtc_h,
12395 plane_state->src_x,
12396 plane_state->src_y,
12397 plane_state->src_w,
12398 plane_state->src_h);
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030012399 WARN_ON(ret != 0);
Daniel Vettera6778b32012-07-02 09:56:42 +020012400 }
12401
12402 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012403 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12404 if (!needs_modeset(crtc_state) || !crtc_state->enable)
12405 continue;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012406
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012407 update_scanline_offset(to_intel_crtc(crtc));
12408
12409 dev_priv->display.crtc_enable(crtc);
12410 intel_crtc_enable_planes(crtc);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012411 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012412
Daniel Vettera6778b32012-07-02 09:56:42 +020012413 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012414
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030012415 intel_crtc = to_intel_crtc(modeset_crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012416
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030012417 /* The pipe_config will be freed with the atomic state, so
12418 * make a copy. */
12419 memcpy(crtc_state_copy, intel_crtc->config, sizeof *crtc_state_copy);
12420 intel_crtc->config = crtc_state_copy;
12421 intel_crtc->base.state = &crtc_state_copy->base;
12422
12423 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020012424}
12425
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012426static int intel_set_mode_with_config(struct drm_crtc *crtc,
12427 struct drm_display_mode *mode,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012428 struct intel_crtc_state *pipe_config)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012429{
12430 int ret;
12431
Ander Conselvan de Oliveiracf6d0d72015-04-21 17:13:10 +030012432 ret = __intel_set_mode(crtc, mode, pipe_config);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012433
12434 if (ret == 0)
12435 intel_modeset_check_state(crtc->dev);
12436
12437 return ret;
12438}
12439
Damien Lespiaue7457a92013-08-08 22:28:59 +010012440static int intel_set_mode(struct drm_crtc *crtc,
12441 struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012442 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020012443{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012444 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012445 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020012446
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012447 pipe_config = intel_modeset_compute_config(crtc, mode, state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012448 if (IS_ERR(pipe_config)) {
12449 ret = PTR_ERR(pipe_config);
12450 goto out;
12451 }
Daniel Vetterf30da182013-04-11 20:22:50 +020012452
Ander Conselvan de Oliveiracf6d0d72015-04-21 17:13:10 +030012453 ret = intel_set_mode_with_config(crtc, mode, pipe_config);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012454 if (ret)
12455 goto out;
12456
12457out:
12458 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020012459}
12460
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012461void intel_crtc_restore_mode(struct drm_crtc *crtc)
12462{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012463 struct drm_device *dev = crtc->dev;
12464 struct drm_atomic_state *state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012465 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012466 struct intel_encoder *encoder;
12467 struct intel_connector *connector;
12468 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012469 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012470
12471 state = drm_atomic_state_alloc(dev);
12472 if (!state) {
12473 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12474 crtc->base.id);
12475 return;
12476 }
12477
12478 state->acquire_ctx = dev->mode_config.acquire_ctx;
12479
12480 /* The force restore path in the HW readout code relies on the staged
12481 * config still keeping the user requested config while the actual
12482 * state has been overwritten by the configuration read from HW. We
12483 * need to copy the staged config to the atomic state, otherwise the
12484 * mode set will just reapply the state the HW is already in. */
12485 for_each_intel_encoder(dev, encoder) {
12486 if (&encoder->new_crtc->base != crtc)
12487 continue;
12488
12489 for_each_intel_connector(dev, connector) {
12490 if (connector->new_encoder != encoder)
12491 continue;
12492
12493 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12494 if (IS_ERR(connector_state)) {
12495 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12496 connector->base.base.id,
12497 connector->base.name,
12498 PTR_ERR(connector_state));
12499 continue;
12500 }
12501
12502 connector_state->crtc = crtc;
12503 connector_state->best_encoder = &encoder->base;
12504 }
12505 }
12506
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012507 for_each_intel_crtc(dev, intel_crtc) {
12508 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
12509 continue;
12510
12511 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
12512 if (IS_ERR(crtc_state)) {
12513 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
12514 intel_crtc->base.base.id,
12515 PTR_ERR(crtc_state));
12516 continue;
12517 }
12518
12519 crtc_state->base.enable = intel_crtc->new_enabled;
12520 }
12521
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030012522 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
12523 crtc->primary->fb, crtc->x, crtc->y);
12524
Ander Conselvan de Oliveiracf6d0d72015-04-21 17:13:10 +030012525 intel_set_mode(crtc, &crtc->mode, state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012526
12527 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012528}
12529
Daniel Vetter25c5b262012-07-08 22:08:04 +020012530#undef for_each_intel_crtc_masked
12531
Daniel Vetterd9e55602012-07-04 22:16:09 +020012532static void intel_set_config_free(struct intel_set_config *config)
12533{
12534 if (!config)
12535 return;
12536
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012537 kfree(config->save_connector_encoders);
12538 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020012539 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020012540 kfree(config);
12541}
12542
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012543static int intel_set_config_save_state(struct drm_device *dev,
12544 struct intel_set_config *config)
12545{
Ville Syrjälä76688512014-01-10 11:28:06 +020012546 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012547 struct drm_encoder *encoder;
12548 struct drm_connector *connector;
12549 int count;
12550
Ville Syrjälä76688512014-01-10 11:28:06 +020012551 config->save_crtc_enabled =
12552 kcalloc(dev->mode_config.num_crtc,
12553 sizeof(bool), GFP_KERNEL);
12554 if (!config->save_crtc_enabled)
12555 return -ENOMEM;
12556
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012557 config->save_encoder_crtcs =
12558 kcalloc(dev->mode_config.num_encoder,
12559 sizeof(struct drm_crtc *), GFP_KERNEL);
12560 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012561 return -ENOMEM;
12562
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012563 config->save_connector_encoders =
12564 kcalloc(dev->mode_config.num_connector,
12565 sizeof(struct drm_encoder *), GFP_KERNEL);
12566 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012567 return -ENOMEM;
12568
12569 /* Copy data. Note that driver private data is not affected.
12570 * Should anything bad happen only the expected state is
12571 * restored, not the drivers personal bookkeeping.
12572 */
12573 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010012574 for_each_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012575 config->save_crtc_enabled[count++] = crtc->state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020012576 }
12577
12578 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012579 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012580 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012581 }
12582
12583 count = 0;
12584 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012585 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012586 }
12587
12588 return 0;
12589}
12590
12591static void intel_set_config_restore_state(struct drm_device *dev,
12592 struct intel_set_config *config)
12593{
Ville Syrjälä76688512014-01-10 11:28:06 +020012594 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020012595 struct intel_encoder *encoder;
12596 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012597 int count;
12598
12599 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012600 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012601 crtc->new_enabled = config->save_crtc_enabled[count++];
12602 }
12603
12604 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010012605 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012606 encoder->new_crtc =
12607 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012608 }
12609
12610 count = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012611 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012612 connector->new_encoder =
12613 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012614 }
12615}
12616
Imre Deake3de42b2013-05-03 19:44:07 +020012617static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010012618is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020012619{
12620 int i;
12621
Chris Wilson2e57f472013-07-17 12:14:40 +010012622 if (set->num_connectors == 0)
12623 return false;
12624
12625 if (WARN_ON(set->connectors == NULL))
12626 return false;
12627
12628 for (i = 0; i < set->num_connectors; i++)
12629 if (set->connectors[i]->encoder &&
12630 set->connectors[i]->encoder->crtc == set->crtc &&
12631 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020012632 return true;
12633
12634 return false;
12635}
12636
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012637static void
12638intel_set_config_compute_mode_changes(struct drm_mode_set *set,
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012639 struct intel_crtc_state *pipe_config)
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012640{
Ander Conselvan de Oliveira41227c82015-04-21 17:13:12 +030012641 struct drm_atomic_state *state;
12642 struct drm_connector *connector;
12643 struct drm_connector_state *connector_state;
12644 struct drm_crtc *crtc;
12645 struct drm_crtc_state *crtc_state;
12646 int i;
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012647
12648 /* We should be able to check here if the fb has the same properties
12649 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010012650 if (is_crtc_connector_off(set)) {
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012651 pipe_config->base.mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070012652 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070012653 /*
12654 * If we have no fb, we can only flip as long as the crtc is
12655 * active, otherwise we need a full mode set. The crtc may
12656 * be active if we've only disabled the primary plane, or
12657 * in fastboot situations.
12658 */
Matt Roperf4510a22014-04-01 15:22:40 -070012659 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012660 struct intel_crtc *intel_crtc =
12661 to_intel_crtc(set->crtc);
12662
Matt Roper3b150f02014-05-29 08:06:53 -070012663 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012664 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012665 pipe_config->base.planes_changed = true;
Jesse Barnes319d9822013-06-26 01:38:19 +030012666 } else {
12667 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012668 pipe_config->base.mode_changed = true;
Jesse Barnes319d9822013-06-26 01:38:19 +030012669 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012670 } else if (set->fb == NULL) {
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012671 pipe_config->base.mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010012672 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070012673 set->crtc->primary->fb->pixel_format) {
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012674 pipe_config->base.mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012675 } else {
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012676 pipe_config->base.planes_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012677 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012678 }
12679
Daniel Vetter835c5872012-07-10 18:11:08 +020012680 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012681 pipe_config->base.planes_changed = true;
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012682
12683 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
12684 DRM_DEBUG_KMS("modes are different, full mode set\n");
12685 drm_mode_debug_printmodeline(&set->crtc->mode);
12686 drm_mode_debug_printmodeline(set->mode);
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012687 pipe_config->base.mode_changed = true;
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012688 }
Chris Wilsona1d95702013-08-13 18:48:47 +010012689
Ander Conselvan de Oliveira41227c82015-04-21 17:13:12 +030012690 state = pipe_config->base.state;
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030012691
Ander Conselvan de Oliveira41227c82015-04-21 17:13:12 +030012692 for_each_connector_in_state(state, connector, connector_state, i) {
12693 if (connector_state->best_encoder !=
12694 connector->state->best_encoder) {
12695 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
12696 connector->base.id,
12697 connector->name);
12698 pipe_config->base.mode_changed = true;
12699 }
12700
12701 if (connector_state->crtc != connector->state->crtc) {
12702 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] crtc changed, full mode switch\n",
12703 connector->base.id,
12704 connector->name);
12705 pipe_config->base.mode_changed = true;
12706 }
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030012707 }
12708
Ander Conselvan de Oliveira41227c82015-04-21 17:13:12 +030012709 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12710 if (crtc_state->enable == crtc->state->enable)
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030012711 continue;
12712
12713 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
Ander Conselvan de Oliveira41227c82015-04-21 17:13:12 +030012714 crtc->base.id,
12715 crtc_state->enable ? "en" : "dis");
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012716 pipe_config->base.mode_changed = true;
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030012717 }
12718
Chris Wilsona1d95702013-08-13 18:48:47 +010012719 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012720 set->crtc->base.id, pipe_config->base.mode_changed,
12721 pipe_config->base.planes_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012722}
12723
Daniel Vetter2e431052012-07-04 22:42:15 +020012724static int
Daniel Vetter9a935852012-07-05 22:34:27 +020012725intel_modeset_stage_output_state(struct drm_device *dev,
12726 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012727 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020012728{
Daniel Vetter9a935852012-07-05 22:34:27 +020012729 struct intel_connector *connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012730 struct drm_connector_state *connector_state;
Daniel Vetter9a935852012-07-05 22:34:27 +020012731 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020012732 struct intel_crtc *crtc;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012733 struct intel_crtc_state *crtc_state;
Paulo Zanonif3f08572013-08-12 14:56:53 -030012734 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020012735
Damien Lespiau9abdda72013-02-13 13:29:23 +000012736 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020012737 * of connectors. For paranoia, double-check this. */
12738 WARN_ON(!set->fb && (set->num_connectors != 0));
12739 WARN_ON(set->fb && (set->num_connectors == 0));
12740
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012741 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012742 /* Otherwise traverse passed in connector list and get encoders
12743 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020012744 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012745 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012746 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020012747 break;
12748 }
12749 }
12750
Daniel Vetter9a935852012-07-05 22:34:27 +020012751 /* If we disable the crtc, disable all its connectors. Also, if
12752 * the connector is on the changing crtc but not on the new
12753 * connector list, disable it. */
12754 if ((!set->fb || ro == set->num_connectors) &&
12755 connector->base.encoder &&
12756 connector->base.encoder->crtc == set->crtc) {
12757 connector->new_encoder = NULL;
12758
12759 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12760 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012761 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020012762 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012763 }
12764 /* connector->new_encoder is now updated for all connectors. */
12765
12766 /* Update crtc of enabled connectors. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012767 for_each_intel_connector(dev, connector) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012768 struct drm_crtc *new_crtc;
12769
Daniel Vetter9a935852012-07-05 22:34:27 +020012770 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020012771 continue;
12772
Daniel Vetter9a935852012-07-05 22:34:27 +020012773 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020012774
12775 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012776 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020012777 new_crtc = set->crtc;
12778 }
12779
12780 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010012781 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
12782 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012783 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012784 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012785 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020012786
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012787 connector_state =
12788 drm_atomic_get_connector_state(state, &connector->base);
12789 if (IS_ERR(connector_state))
12790 return PTR_ERR(connector_state);
12791
12792 connector_state->crtc = new_crtc;
12793 connector_state->best_encoder = &connector->new_encoder->base;
12794
Daniel Vetter9a935852012-07-05 22:34:27 +020012795 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12796 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012797 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020012798 new_crtc->base.id);
12799 }
12800
12801 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010012802 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012803 int num_connectors = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012804 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012805 if (connector->new_encoder == encoder) {
12806 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012807 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020012808 }
12809 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012810
12811 if (num_connectors == 0)
12812 encoder->new_crtc = NULL;
12813 else if (num_connectors > 1)
12814 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012815 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012816 /* Now we've also updated encoder->new_crtc for all encoders. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012817 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012818 connector_state =
12819 drm_atomic_get_connector_state(state, &connector->base);
Ander Conselvan de Oliveira9d918c12015-03-27 15:33:51 +020012820 if (IS_ERR(connector_state))
12821 return PTR_ERR(connector_state);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012822
12823 if (connector->new_encoder) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012824 if (connector->new_encoder != connector->encoder)
12825 connector->encoder = connector->new_encoder;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012826 } else {
12827 connector_state->crtc = NULL;
Ander Conselvan de Oliveiraf61cccf2015-03-31 11:35:00 +030012828 connector_state->best_encoder = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012829 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012830 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012831 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012832 crtc->new_enabled = false;
12833
Damien Lespiaub2784e12014-08-05 11:29:37 +010012834 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012835 if (encoder->new_crtc == crtc) {
12836 crtc->new_enabled = true;
12837 break;
12838 }
12839 }
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012840
12841 if (crtc->new_enabled != crtc->base.state->enable) {
12842 crtc_state = intel_atomic_get_crtc_state(state, crtc);
12843 if (IS_ERR(crtc_state))
12844 return PTR_ERR(crtc_state);
12845
12846 crtc_state->base.enable = crtc->new_enabled;
12847 }
Ville Syrjälä76688512014-01-10 11:28:06 +020012848 }
12849
Daniel Vetter2e431052012-07-04 22:42:15 +020012850 return 0;
12851}
12852
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012853static bool primary_plane_visible(struct drm_crtc *crtc)
12854{
12855 struct intel_plane_state *plane_state =
12856 to_intel_plane_state(crtc->primary->state);
12857
12858 return plane_state->visible;
12859}
12860
Daniel Vetter2e431052012-07-04 22:42:15 +020012861static int intel_crtc_set_config(struct drm_mode_set *set)
12862{
12863 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012864 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020012865 struct intel_set_config *config;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012866 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012867 bool primary_plane_was_visible;
Daniel Vetter2e431052012-07-04 22:42:15 +020012868 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020012869
Daniel Vetter8d3e3752012-07-05 16:09:09 +020012870 BUG_ON(!set);
12871 BUG_ON(!set->crtc);
12872 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020012873
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010012874 /* Enforce sane interface api - has been abused by the fb helper. */
12875 BUG_ON(!set->mode && set->fb);
12876 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020012877
Daniel Vetter2e431052012-07-04 22:42:15 +020012878 if (set->fb) {
12879 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12880 set->crtc->base.id, set->fb->base.id,
12881 (int)set->num_connectors, set->x, set->y);
12882 } else {
12883 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020012884 }
12885
12886 dev = set->crtc->dev;
12887
12888 ret = -ENOMEM;
12889 config = kzalloc(sizeof(*config), GFP_KERNEL);
12890 if (!config)
12891 goto out_config;
12892
12893 ret = intel_set_config_save_state(dev, config);
12894 if (ret)
12895 goto out_config;
12896
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012897 state = drm_atomic_state_alloc(dev);
12898 if (!state) {
12899 ret = -ENOMEM;
12900 goto out_config;
12901 }
12902
12903 state->acquire_ctx = dev->mode_config.acquire_ctx;
12904
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030012905 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020012906 if (ret)
12907 goto fail;
12908
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030012909 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
12910 set->fb, set->x, set->y);
12911 if (ret)
12912 goto fail;
12913
Jesse Barnes50f52752014-11-07 13:11:00 -080012914 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012915 state);
Jesse Barnes20664592014-11-05 14:26:09 -080012916 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080012917 ret = PTR_ERR(pipe_config);
Jesse Barnes50f52752014-11-07 13:11:00 -080012918 goto fail;
Jesse Barnes20664592014-11-05 14:26:09 -080012919 }
Jesse Barnes50f52752014-11-07 13:11:00 -080012920
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012921 /* Compute whether we need a full modeset, only an fb base update or no
12922 * change at all. In the future we might also check whether only the
12923 * mode changed, e.g. for LVDS where we only change the panel fitter in
12924 * such cases. */
12925 intel_set_config_compute_mode_changes(set, pipe_config);
12926
Jesse Barnes1f9954d2014-11-05 14:26:10 -080012927 intel_update_pipe_size(to_intel_crtc(set->crtc));
12928
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012929 primary_plane_was_visible = primary_plane_visible(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012930
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012931 ret = intel_set_mode_with_config(set->crtc, set->mode,
12932 pipe_config);
12933
12934 if (ret == 0 &&
12935 pipe_config->base.enable &&
12936 pipe_config->base.planes_changed &&
12937 !needs_modeset(&pipe_config->base)) {
12938 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012939
12940 /*
12941 * We need to make sure the primary plane is re-enabled if it
12942 * has previously been turned off.
12943 */
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012944 if (ret == 0 && !primary_plane_was_visible &&
12945 primary_plane_visible(set->crtc)) {
Matt Roper3b150f02014-05-29 08:06:53 -070012946 WARN_ON(!intel_crtc->active);
Maarten Lankhorst87d43002015-04-21 17:12:54 +030012947 intel_post_enable_primary(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012948 }
12949
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012950 /*
12951 * In the fastboot case this may be our only check of the
12952 * state after boot. It would be better to only do it on
12953 * the first update, but we don't have a nice way of doing that
12954 * (and really, set_config isn't used much for high freq page
12955 * flipping, so increasing its cost here shouldn't be a big
12956 * deal).
12957 */
Jani Nikulad330a952014-01-21 11:24:25 +020012958 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012959 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020012960 }
12961
Chris Wilson2d05eae2013-05-03 17:36:25 +010012962 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020012963 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12964 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020012965fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010012966 intel_set_config_restore_state(dev, config);
Chris Wilson2d05eae2013-05-03 17:36:25 +010012967 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012968
Daniel Vetterd9e55602012-07-04 22:16:09 +020012969out_config:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030012970 drm_atomic_state_free(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012971
Daniel Vetterd9e55602012-07-04 22:16:09 +020012972 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012973 return ret;
12974}
12975
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012976static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012977 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020012978 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012979 .destroy = intel_crtc_destroy,
12980 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080012981 .atomic_duplicate_state = intel_crtc_duplicate_state,
12982 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012983};
12984
Daniel Vetter53589012013-06-05 13:34:16 +020012985static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
12986 struct intel_shared_dpll *pll,
12987 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012988{
Daniel Vetter53589012013-06-05 13:34:16 +020012989 uint32_t val;
12990
Daniel Vetterf458ebb2014-09-30 10:56:39 +020012991 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030012992 return false;
12993
Daniel Vetter53589012013-06-05 13:34:16 +020012994 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020012995 hw_state->dpll = val;
12996 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
12997 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020012998
12999 return val & DPLL_VCO_ENABLE;
13000}
13001
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013002static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13003 struct intel_shared_dpll *pll)
13004{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013005 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13006 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013007}
13008
Daniel Vettere7b903d2013-06-05 13:34:14 +020013009static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13010 struct intel_shared_dpll *pll)
13011{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013012 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013013 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013014
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013015 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013016
13017 /* Wait for the clocks to stabilize. */
13018 POSTING_READ(PCH_DPLL(pll->id));
13019 udelay(150);
13020
13021 /* The pixel multiplier can only be updated once the
13022 * DPLL is enabled and the clocks are stable.
13023 *
13024 * So write it again.
13025 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013026 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013027 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013028 udelay(200);
13029}
13030
13031static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13032 struct intel_shared_dpll *pll)
13033{
13034 struct drm_device *dev = dev_priv->dev;
13035 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013036
13037 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013038 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013039 if (intel_crtc_to_shared_dpll(crtc) == pll)
13040 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13041 }
13042
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013043 I915_WRITE(PCH_DPLL(pll->id), 0);
13044 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013045 udelay(200);
13046}
13047
Daniel Vetter46edb022013-06-05 13:34:12 +020013048static char *ibx_pch_dpll_names[] = {
13049 "PCH DPLL A",
13050 "PCH DPLL B",
13051};
13052
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013053static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013054{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013055 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013056 int i;
13057
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013058 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013059
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013060 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013061 dev_priv->shared_dplls[i].id = i;
13062 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013063 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013064 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13065 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013066 dev_priv->shared_dplls[i].get_hw_state =
13067 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013068 }
13069}
13070
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013071static void intel_shared_dpll_init(struct drm_device *dev)
13072{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013073 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013074
Daniel Vetter9cd86932014-06-25 22:01:57 +030013075 if (HAS_DDI(dev))
13076 intel_ddi_pll_init(dev);
13077 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013078 ibx_pch_dpll_init(dev);
13079 else
13080 dev_priv->num_shared_dpll = 0;
13081
13082 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013083}
13084
Matt Roper6beb8c232014-12-01 15:40:14 -080013085/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013086 * intel_wm_need_update - Check whether watermarks need updating
13087 * @plane: drm plane
13088 * @state: new plane state
13089 *
13090 * Check current plane state versus the new one to determine whether
13091 * watermarks need to be recalculated.
13092 *
13093 * Returns true or false.
13094 */
13095bool intel_wm_need_update(struct drm_plane *plane,
13096 struct drm_plane_state *state)
13097{
13098 /* Update watermarks on tiling changes. */
13099 if (!plane->state->fb || !state->fb ||
13100 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13101 plane->state->rotation != state->rotation)
13102 return true;
13103
13104 return false;
13105}
13106
13107/**
Matt Roper6beb8c232014-12-01 15:40:14 -080013108 * intel_prepare_plane_fb - Prepare fb for usage on plane
13109 * @plane: drm plane to prepare for
13110 * @fb: framebuffer to prepare for presentation
13111 *
13112 * Prepares a framebuffer for usage on a display plane. Generally this
13113 * involves pinning the underlying object and updating the frontbuffer tracking
13114 * bits. Some older platforms need special physical address handling for
13115 * cursor planes.
13116 *
13117 * Returns 0 on success, negative error code on failure.
13118 */
13119int
13120intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013121 struct drm_framebuffer *fb,
13122 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013123{
13124 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013125 struct intel_plane *intel_plane = to_intel_plane(plane);
13126 enum pipe pipe = intel_plane->pipe;
13127 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13128 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13129 unsigned frontbuffer_bits = 0;
13130 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013131
Matt Roperea2c67b2014-12-23 10:41:52 -080013132 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013133 return 0;
13134
Matt Roper6beb8c232014-12-01 15:40:14 -080013135 switch (plane->type) {
13136 case DRM_PLANE_TYPE_PRIMARY:
13137 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13138 break;
13139 case DRM_PLANE_TYPE_CURSOR:
13140 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13141 break;
13142 case DRM_PLANE_TYPE_OVERLAY:
13143 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13144 break;
13145 }
Matt Roper465c1202014-05-29 08:06:54 -070013146
Matt Roper4c345742014-07-09 16:22:10 -070013147 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013148
Matt Roper6beb8c232014-12-01 15:40:14 -080013149 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13150 INTEL_INFO(dev)->cursor_needs_physical) {
13151 int align = IS_I830(dev) ? 16 * 1024 : 256;
13152 ret = i915_gem_object_attach_phys(obj, align);
13153 if (ret)
13154 DRM_DEBUG_KMS("failed to attach phys object\n");
13155 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013156 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013157 }
13158
13159 if (ret == 0)
13160 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13161
13162 mutex_unlock(&dev->struct_mutex);
13163
13164 return ret;
13165}
13166
Matt Roper38f3ce32014-12-02 07:45:25 -080013167/**
13168 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13169 * @plane: drm plane to clean up for
13170 * @fb: old framebuffer that was on plane
13171 *
13172 * Cleans up a framebuffer that has just been removed from a plane.
13173 */
13174void
13175intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013176 struct drm_framebuffer *fb,
13177 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013178{
13179 struct drm_device *dev = plane->dev;
13180 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13181
13182 if (WARN_ON(!obj))
13183 return;
13184
13185 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13186 !INTEL_INFO(dev)->cursor_needs_physical) {
13187 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013188 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013189 mutex_unlock(&dev->struct_mutex);
13190 }
Matt Roper465c1202014-05-29 08:06:54 -070013191}
13192
Chandra Konduru6156a452015-04-27 13:48:39 -070013193int
13194skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13195{
13196 int max_scale;
13197 struct drm_device *dev;
13198 struct drm_i915_private *dev_priv;
13199 int crtc_clock, cdclk;
13200
13201 if (!intel_crtc || !crtc_state)
13202 return DRM_PLANE_HELPER_NO_SCALING;
13203
13204 dev = intel_crtc->base.dev;
13205 dev_priv = dev->dev_private;
13206 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13207 cdclk = dev_priv->display.get_display_clock_speed(dev);
13208
13209 if (!crtc_clock || !cdclk)
13210 return DRM_PLANE_HELPER_NO_SCALING;
13211
13212 /*
13213 * skl max scale is lower of:
13214 * close to 3 but not 3, -1 is for that purpose
13215 * or
13216 * cdclk/crtc_clock
13217 */
13218 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13219
13220 return max_scale;
13221}
13222
Matt Roper465c1202014-05-29 08:06:54 -070013223static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013224intel_check_primary_plane(struct drm_plane *plane,
13225 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013226{
Matt Roper32b7eee2014-12-24 07:59:06 -080013227 struct drm_device *dev = plane->dev;
13228 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013229 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013230 struct intel_crtc *intel_crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013231 struct intel_crtc_state *crtc_state;
Matt Roper2b875c22014-12-01 15:40:13 -080013232 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013233 struct drm_rect *dest = &state->dst;
13234 struct drm_rect *src = &state->src;
13235 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013236 bool can_position = false;
Chandra Konduru6156a452015-04-27 13:48:39 -070013237 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13238 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013239 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013240
Matt Roperea2c67b2014-12-23 10:41:52 -080013241 crtc = crtc ? crtc : plane->crtc;
13242 intel_crtc = to_intel_crtc(crtc);
Chandra Konduru6156a452015-04-27 13:48:39 -070013243 crtc_state = state->base.state ?
13244 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
Matt Roperea2c67b2014-12-23 10:41:52 -080013245
Chandra Konduru6156a452015-04-27 13:48:39 -070013246 if (INTEL_INFO(dev)->gen >= 9) {
13247 min_scale = 1;
13248 max_scale = skl_max_scale(intel_crtc, crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013249 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013250 }
Sonika Jindald8106362015-04-10 14:37:28 +053013251
Matt Roperc59cb172014-12-01 15:40:16 -080013252 ret = drm_plane_helper_check_update(plane, crtc, fb,
13253 src, dest, clip,
Chandra Konduru6156a452015-04-27 13:48:39 -070013254 min_scale,
13255 max_scale,
Sonika Jindald8106362015-04-10 14:37:28 +053013256 can_position, true,
13257 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013258 if (ret)
13259 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013260
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013261 if (intel_crtc->active) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013262 struct intel_plane_state *old_state =
13263 to_intel_plane_state(plane->state);
13264
Matt Roper32b7eee2014-12-24 07:59:06 -080013265 intel_crtc->atomic.wait_for_flips = true;
13266
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013267 /*
13268 * FBC does not work on some platforms for rotated
13269 * planes, so disable it when rotation is not 0 and
13270 * update it when rotation is set back to 0.
13271 *
13272 * FIXME: This is redundant with the fbc update done in
13273 * the primary plane enable function except that that
13274 * one is done too late. We eventually need to unify
13275 * this.
13276 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013277 if (state->visible &&
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013278 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013279 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013280 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013281 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013282 }
13283
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013284 if (state->visible && !old_state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013285 /*
13286 * BDW signals flip done immediately if the plane
13287 * is disabled, even if the plane enable is already
13288 * armed to occur at the next vblank :(
13289 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013290 if (IS_BROADWELL(dev))
Matt Roper32b7eee2014-12-24 07:59:06 -080013291 intel_crtc->atomic.wait_vblank = true;
13292 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013293
Matt Roper32b7eee2014-12-24 07:59:06 -080013294 intel_crtc->atomic.fb_bits |=
13295 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13296
13297 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013298
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013299 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013300 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013301 }
13302
Chandra Konduru6156a452015-04-27 13:48:39 -070013303 if (INTEL_INFO(dev)->gen >= 9) {
13304 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13305 to_intel_plane(plane), state, 0);
13306 if (ret)
13307 return ret;
13308 }
13309
Matt Roperc59cb172014-12-01 15:40:16 -080013310 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013311}
13312
Sonika Jindal48404c12014-08-22 14:06:04 +053013313static void
13314intel_commit_primary_plane(struct drm_plane *plane,
13315 struct intel_plane_state *state)
13316{
Matt Roper2b875c22014-12-01 15:40:13 -080013317 struct drm_crtc *crtc = state->base.crtc;
13318 struct drm_framebuffer *fb = state->base.fb;
13319 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013320 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013321 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013322 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013323
Matt Roperea2c67b2014-12-23 10:41:52 -080013324 crtc = crtc ? crtc : plane->crtc;
13325 intel_crtc = to_intel_crtc(crtc);
13326
Matt Ropercf4c7c12014-12-04 10:27:42 -080013327 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013328 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013329 crtc->y = src->y1 >> 16;
13330
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013331 if (intel_crtc->active) {
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013332 if (state->visible)
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013333 /* FIXME: kill this fastboot hack */
13334 intel_update_pipe_size(intel_crtc);
13335
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013336 dev_priv->display.update_primary_plane(crtc, plane->fb,
13337 crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013338 }
13339}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013340
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013341static void
13342intel_disable_primary_plane(struct drm_plane *plane,
13343 struct drm_crtc *crtc,
13344 bool force)
13345{
13346 struct drm_device *dev = plane->dev;
13347 struct drm_i915_private *dev_priv = dev->dev_private;
13348
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013349 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13350}
13351
Matt Roper32b7eee2014-12-24 07:59:06 -080013352static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13353{
13354 struct drm_device *dev = crtc->dev;
13355 struct drm_i915_private *dev_priv = dev->dev_private;
13356 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013357 struct intel_plane *intel_plane;
13358 struct drm_plane *p;
13359 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013360
Matt Roperea2c67b2014-12-23 10:41:52 -080013361 /* Track fb's for any planes being disabled */
13362 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13363 intel_plane = to_intel_plane(p);
13364
13365 if (intel_crtc->atomic.disabled_planes &
13366 (1 << drm_plane_index(p))) {
13367 switch (p->type) {
13368 case DRM_PLANE_TYPE_PRIMARY:
13369 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13370 break;
13371 case DRM_PLANE_TYPE_CURSOR:
13372 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13373 break;
13374 case DRM_PLANE_TYPE_OVERLAY:
13375 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13376 break;
13377 }
13378
13379 mutex_lock(&dev->struct_mutex);
13380 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13381 mutex_unlock(&dev->struct_mutex);
13382 }
13383 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013384
Matt Roper32b7eee2014-12-24 07:59:06 -080013385 if (intel_crtc->atomic.wait_for_flips)
13386 intel_crtc_wait_for_pending_flips(crtc);
13387
13388 if (intel_crtc->atomic.disable_fbc)
13389 intel_fbc_disable(dev);
13390
13391 if (intel_crtc->atomic.pre_disable_primary)
13392 intel_pre_disable_primary(crtc);
13393
13394 if (intel_crtc->atomic.update_wm)
13395 intel_update_watermarks(crtc);
13396
13397 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013398
13399 /* Perform vblank evasion around commit operation */
13400 if (intel_crtc->active)
13401 intel_crtc->atomic.evade =
13402 intel_pipe_update_start(intel_crtc,
13403 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013404}
13405
13406static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13407{
13408 struct drm_device *dev = crtc->dev;
13409 struct drm_i915_private *dev_priv = dev->dev_private;
13410 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13411 struct drm_plane *p;
13412
Matt Roperc34c9ee2014-12-23 10:41:50 -080013413 if (intel_crtc->atomic.evade)
13414 intel_pipe_update_end(intel_crtc,
13415 intel_crtc->atomic.start_vbl_count);
13416
Matt Roper32b7eee2014-12-24 07:59:06 -080013417 intel_runtime_pm_put(dev_priv);
13418
13419 if (intel_crtc->atomic.wait_vblank)
13420 intel_wait_for_vblank(dev, intel_crtc->pipe);
13421
13422 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13423
13424 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013425 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013426 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013427 mutex_unlock(&dev->struct_mutex);
13428 }
Matt Roper465c1202014-05-29 08:06:54 -070013429
Matt Roper32b7eee2014-12-24 07:59:06 -080013430 if (intel_crtc->atomic.post_enable_primary)
13431 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013432
Matt Roper32b7eee2014-12-24 07:59:06 -080013433 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13434 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13435 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13436 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013437
Matt Roper32b7eee2014-12-24 07:59:06 -080013438 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013439}
13440
Matt Ropercf4c7c12014-12-04 10:27:42 -080013441/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013442 * intel_plane_destroy - destroy a plane
13443 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013444 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013445 * Common destruction function for all types of planes (primary, cursor,
13446 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013447 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013448void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013449{
13450 struct intel_plane *intel_plane = to_intel_plane(plane);
13451 drm_plane_cleanup(plane);
13452 kfree(intel_plane);
13453}
13454
Matt Roper65a3fea2015-01-21 16:35:42 -080013455const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013456 .update_plane = drm_atomic_helper_update_plane,
13457 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013458 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013459 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013460 .atomic_get_property = intel_plane_atomic_get_property,
13461 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013462 .atomic_duplicate_state = intel_plane_duplicate_state,
13463 .atomic_destroy_state = intel_plane_destroy_state,
13464
Matt Roper465c1202014-05-29 08:06:54 -070013465};
13466
13467static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13468 int pipe)
13469{
13470 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013471 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013472 const uint32_t *intel_primary_formats;
13473 int num_formats;
13474
13475 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13476 if (primary == NULL)
13477 return NULL;
13478
Matt Roper8e7d6882015-01-21 16:35:41 -080013479 state = intel_create_plane_state(&primary->base);
13480 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013481 kfree(primary);
13482 return NULL;
13483 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013484 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013485
Matt Roper465c1202014-05-29 08:06:54 -070013486 primary->can_scale = false;
13487 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013488 if (INTEL_INFO(dev)->gen >= 9) {
13489 primary->can_scale = true;
13490 }
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013491 state->scaler_id = -1;
Matt Roper465c1202014-05-29 08:06:54 -070013492 primary->pipe = pipe;
13493 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013494 primary->check_plane = intel_check_primary_plane;
13495 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013496 primary->disable_plane = intel_disable_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013497 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013498 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13499 primary->plane = !pipe;
13500
13501 if (INTEL_INFO(dev)->gen <= 3) {
13502 intel_primary_formats = intel_primary_formats_gen2;
13503 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
13504 } else {
13505 intel_primary_formats = intel_primary_formats_gen4;
13506 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
13507 }
13508
13509 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013510 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013511 intel_primary_formats, num_formats,
13512 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013513
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013514 if (INTEL_INFO(dev)->gen >= 4)
13515 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013516
Matt Roperea2c67b2014-12-23 10:41:52 -080013517 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13518
Matt Roper465c1202014-05-29 08:06:54 -070013519 return &primary->base;
13520}
13521
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013522void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13523{
13524 if (!dev->mode_config.rotation_property) {
13525 unsigned long flags = BIT(DRM_ROTATE_0) |
13526 BIT(DRM_ROTATE_180);
13527
13528 if (INTEL_INFO(dev)->gen >= 9)
13529 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13530
13531 dev->mode_config.rotation_property =
13532 drm_mode_create_rotation_property(dev, flags);
13533 }
13534 if (dev->mode_config.rotation_property)
13535 drm_object_attach_property(&plane->base.base,
13536 dev->mode_config.rotation_property,
13537 plane->base.state->rotation);
13538}
13539
Matt Roper3d7d6512014-06-10 08:28:13 -070013540static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013541intel_check_cursor_plane(struct drm_plane *plane,
13542 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013543{
Matt Roper2b875c22014-12-01 15:40:13 -080013544 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013545 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013546 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013547 struct drm_rect *dest = &state->dst;
13548 struct drm_rect *src = &state->src;
13549 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013550 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013551 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013552 unsigned stride;
13553 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013554
Matt Roperea2c67b2014-12-23 10:41:52 -080013555 crtc = crtc ? crtc : plane->crtc;
13556 intel_crtc = to_intel_crtc(crtc);
13557
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013558 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013559 src, dest, clip,
13560 DRM_PLANE_HELPER_NO_SCALING,
13561 DRM_PLANE_HELPER_NO_SCALING,
13562 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013563 if (ret)
13564 return ret;
13565
13566
13567 /* if we want to turn off the cursor ignore width and height */
13568 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013569 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013570
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013571 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013572 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13573 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13574 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013575 return -EINVAL;
13576 }
13577
Matt Roperea2c67b2014-12-23 10:41:52 -080013578 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13579 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013580 DRM_DEBUG_KMS("buffer is too small\n");
13581 return -ENOMEM;
13582 }
13583
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013584 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013585 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13586 ret = -EINVAL;
13587 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013588
Matt Roper32b7eee2014-12-24 07:59:06 -080013589finish:
13590 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013591 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013592 intel_crtc->atomic.update_wm = true;
13593
13594 intel_crtc->atomic.fb_bits |=
13595 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13596 }
13597
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013598 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013599}
13600
Matt Roperf4a2cf22014-12-01 15:40:12 -080013601static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013602intel_disable_cursor_plane(struct drm_plane *plane,
13603 struct drm_crtc *crtc,
13604 bool force)
13605{
13606 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13607
13608 if (!force) {
13609 plane->fb = NULL;
13610 intel_crtc->cursor_bo = NULL;
13611 intel_crtc->cursor_addr = 0;
13612 }
13613
13614 intel_crtc_update_cursor(crtc, false);
13615}
13616
13617static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013618intel_commit_cursor_plane(struct drm_plane *plane,
13619 struct intel_plane_state *state)
13620{
Matt Roper2b875c22014-12-01 15:40:13 -080013621 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013622 struct drm_device *dev = plane->dev;
13623 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013624 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013625 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013626
Matt Roperea2c67b2014-12-23 10:41:52 -080013627 crtc = crtc ? crtc : plane->crtc;
13628 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013629
Matt Roperea2c67b2014-12-23 10:41:52 -080013630 plane->fb = state->base.fb;
13631 crtc->cursor_x = state->base.crtc_x;
13632 crtc->cursor_y = state->base.crtc_y;
13633
Gustavo Padovana912f122014-12-01 15:40:10 -080013634 if (intel_crtc->cursor_bo == obj)
13635 goto update;
13636
Matt Roperf4a2cf22014-12-01 15:40:12 -080013637 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013638 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013639 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013640 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013641 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013642 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013643
Gustavo Padovana912f122014-12-01 15:40:10 -080013644 intel_crtc->cursor_addr = addr;
13645 intel_crtc->cursor_bo = obj;
13646update:
Gustavo Padovana912f122014-12-01 15:40:10 -080013647
Matt Roper32b7eee2014-12-24 07:59:06 -080013648 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013649 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013650}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013651
Matt Roper3d7d6512014-06-10 08:28:13 -070013652static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13653 int pipe)
13654{
13655 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013656 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013657
13658 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13659 if (cursor == NULL)
13660 return NULL;
13661
Matt Roper8e7d6882015-01-21 16:35:41 -080013662 state = intel_create_plane_state(&cursor->base);
13663 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013664 kfree(cursor);
13665 return NULL;
13666 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013667 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013668
Matt Roper3d7d6512014-06-10 08:28:13 -070013669 cursor->can_scale = false;
13670 cursor->max_downscale = 1;
13671 cursor->pipe = pipe;
13672 cursor->plane = pipe;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013673 state->scaler_id = -1;
Matt Roperc59cb172014-12-01 15:40:16 -080013674 cursor->check_plane = intel_check_cursor_plane;
13675 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013676 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013677
13678 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013679 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013680 intel_cursor_formats,
13681 ARRAY_SIZE(intel_cursor_formats),
13682 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013683
13684 if (INTEL_INFO(dev)->gen >= 4) {
13685 if (!dev->mode_config.rotation_property)
13686 dev->mode_config.rotation_property =
13687 drm_mode_create_rotation_property(dev,
13688 BIT(DRM_ROTATE_0) |
13689 BIT(DRM_ROTATE_180));
13690 if (dev->mode_config.rotation_property)
13691 drm_object_attach_property(&cursor->base.base,
13692 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013693 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013694 }
13695
Matt Roperea2c67b2014-12-23 10:41:52 -080013696 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13697
Matt Roper3d7d6512014-06-10 08:28:13 -070013698 return &cursor->base;
13699}
13700
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013701static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13702 struct intel_crtc_state *crtc_state)
13703{
13704 int i;
13705 struct intel_scaler *intel_scaler;
13706 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13707
13708 for (i = 0; i < intel_crtc->num_scalers; i++) {
13709 intel_scaler = &scaler_state->scalers[i];
13710 intel_scaler->in_use = 0;
13711 intel_scaler->id = i;
13712
13713 intel_scaler->mode = PS_SCALER_MODE_DYN;
13714 }
13715
13716 scaler_state->scaler_id = -1;
13717}
13718
Hannes Ederb358d0a2008-12-18 21:18:47 +010013719static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013720{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013721 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013722 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013723 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013724 struct drm_plane *primary = NULL;
13725 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013726 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013727
Daniel Vetter955382f2013-09-19 14:05:45 +020013728 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013729 if (intel_crtc == NULL)
13730 return;
13731
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013732 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13733 if (!crtc_state)
13734 goto fail;
13735 intel_crtc_set_state(intel_crtc, crtc_state);
Matt Roper07878242015-02-25 11:43:26 -080013736 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013737
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013738 /* initialize shared scalers */
13739 if (INTEL_INFO(dev)->gen >= 9) {
13740 if (pipe == PIPE_C)
13741 intel_crtc->num_scalers = 1;
13742 else
13743 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13744
13745 skl_init_scalers(dev, intel_crtc, crtc_state);
13746 }
13747
Matt Roper465c1202014-05-29 08:06:54 -070013748 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013749 if (!primary)
13750 goto fail;
13751
13752 cursor = intel_cursor_plane_create(dev, pipe);
13753 if (!cursor)
13754 goto fail;
13755
Matt Roper465c1202014-05-29 08:06:54 -070013756 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013757 cursor, &intel_crtc_funcs);
13758 if (ret)
13759 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013760
13761 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013762 for (i = 0; i < 256; i++) {
13763 intel_crtc->lut_r[i] = i;
13764 intel_crtc->lut_g[i] = i;
13765 intel_crtc->lut_b[i] = i;
13766 }
13767
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013768 /*
13769 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013770 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013771 */
Jesse Barnes80824002009-09-10 15:28:06 -070013772 intel_crtc->pipe = pipe;
13773 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013774 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013775 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013776 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013777 }
13778
Chris Wilson4b0e3332014-05-30 16:35:26 +030013779 intel_crtc->cursor_base = ~0;
13780 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013781 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013782
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013783 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13784 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13785 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13786 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13787
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020013788 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
13789
Jesse Barnes79e53942008-11-07 14:24:08 -080013790 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013791
13792 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013793 return;
13794
13795fail:
13796 if (primary)
13797 drm_plane_cleanup(primary);
13798 if (cursor)
13799 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013800 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013801 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013802}
13803
Jesse Barnes752aa882013-10-31 18:55:49 +020013804enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13805{
13806 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013807 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013808
Rob Clark51fd3712013-11-19 12:10:12 -050013809 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013810
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013811 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013812 return INVALID_PIPE;
13813
13814 return to_intel_crtc(encoder->crtc)->pipe;
13815}
13816
Carl Worth08d7b3d2009-04-29 14:43:54 -070013817int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013818 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013819{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013820 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013821 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013822 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013823
Rob Clark7707e652014-07-17 23:30:04 -040013824 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013825
Rob Clark7707e652014-07-17 23:30:04 -040013826 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013827 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013828 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013829 }
13830
Rob Clark7707e652014-07-17 23:30:04 -040013831 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013832 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013833
Daniel Vetterc05422d2009-08-11 16:05:30 +020013834 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013835}
13836
Daniel Vetter66a92782012-07-12 20:08:18 +020013837static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013838{
Daniel Vetter66a92782012-07-12 20:08:18 +020013839 struct drm_device *dev = encoder->base.dev;
13840 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013841 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013842 int entry = 0;
13843
Damien Lespiaub2784e12014-08-05 11:29:37 +010013844 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013845 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013846 index_mask |= (1 << entry);
13847
Jesse Barnes79e53942008-11-07 14:24:08 -080013848 entry++;
13849 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013850
Jesse Barnes79e53942008-11-07 14:24:08 -080013851 return index_mask;
13852}
13853
Chris Wilson4d302442010-12-14 19:21:29 +000013854static bool has_edp_a(struct drm_device *dev)
13855{
13856 struct drm_i915_private *dev_priv = dev->dev_private;
13857
13858 if (!IS_MOBILE(dev))
13859 return false;
13860
13861 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13862 return false;
13863
Damien Lespiaue3589902014-02-07 19:12:50 +000013864 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013865 return false;
13866
13867 return true;
13868}
13869
Jesse Barnes84b4e042014-06-25 08:24:29 -070013870static bool intel_crt_present(struct drm_device *dev)
13871{
13872 struct drm_i915_private *dev_priv = dev->dev_private;
13873
Damien Lespiau884497e2013-12-03 13:56:23 +000013874 if (INTEL_INFO(dev)->gen >= 9)
13875 return false;
13876
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013877 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013878 return false;
13879
13880 if (IS_CHERRYVIEW(dev))
13881 return false;
13882
13883 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13884 return false;
13885
13886 return true;
13887}
13888
Jesse Barnes79e53942008-11-07 14:24:08 -080013889static void intel_setup_outputs(struct drm_device *dev)
13890{
Eric Anholt725e30a2009-01-22 13:01:02 -080013891 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013892 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013893 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013894
Daniel Vetterc9093352013-06-06 22:22:47 +020013895 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013896
Jesse Barnes84b4e042014-06-25 08:24:29 -070013897 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013898 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013899
Vandana Kannanc776eb22014-08-19 12:05:01 +053013900 if (IS_BROXTON(dev)) {
13901 /*
13902 * FIXME: Broxton doesn't support port detection via the
13903 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13904 * detect the ports.
13905 */
13906 intel_ddi_init(dev, PORT_A);
13907 intel_ddi_init(dev, PORT_B);
13908 intel_ddi_init(dev, PORT_C);
13909 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013910 int found;
13911
Jesse Barnesde31fac2015-03-06 15:53:32 -080013912 /*
13913 * Haswell uses DDI functions to detect digital outputs.
13914 * On SKL pre-D0 the strap isn't connected, so we assume
13915 * it's there.
13916 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013917 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013918 /* WaIgnoreDDIAStrap: skl */
13919 if (found ||
13920 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013921 intel_ddi_init(dev, PORT_A);
13922
13923 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13924 * register */
13925 found = I915_READ(SFUSE_STRAP);
13926
13927 if (found & SFUSE_STRAP_DDIB_DETECTED)
13928 intel_ddi_init(dev, PORT_B);
13929 if (found & SFUSE_STRAP_DDIC_DETECTED)
13930 intel_ddi_init(dev, PORT_C);
13931 if (found & SFUSE_STRAP_DDID_DETECTED)
13932 intel_ddi_init(dev, PORT_D);
13933 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013934 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013935 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013936
13937 if (has_edp_a(dev))
13938 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013939
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013940 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013941 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013942 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013943 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013944 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013945 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013946 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013947 }
13948
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013949 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013950 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013951
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013952 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013953 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013954
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013955 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013956 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013957
Daniel Vetter270b3042012-10-27 15:52:05 +020013958 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013959 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013960 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013961 /*
13962 * The DP_DETECTED bit is the latched state of the DDC
13963 * SDA pin at boot. However since eDP doesn't require DDC
13964 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13965 * eDP ports may have been muxed to an alternate function.
13966 * Thus we can't rely on the DP_DETECTED bit alone to detect
13967 * eDP ports. Consult the VBT as well as DP_DETECTED to
13968 * detect eDP ports.
13969 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013970 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13971 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013972 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13973 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013974 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13975 intel_dp_is_edp(dev, PORT_B))
13976 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013977
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013978 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13979 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070013980 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13981 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013982 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13983 intel_dp_is_edp(dev, PORT_C))
13984 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053013985
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013986 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013987 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013988 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
13989 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013990 /* eDP not supported on port D, so don't check VBT */
13991 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
13992 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013993 }
13994
Jani Nikula3cfca972013-08-27 15:12:26 +030013995 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080013996 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013997 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080013998
Paulo Zanonie2debe92013-02-18 19:00:27 -030013999 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014000 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014001 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014002 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14003 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014004 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014005 }
Ma Ling27185ae2009-08-24 13:50:23 +080014006
Imre Deake7281ea2013-05-08 13:14:08 +030014007 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014008 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014009 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014010
14011 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014012
Paulo Zanonie2debe92013-02-18 19:00:27 -030014013 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014014 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014015 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014016 }
Ma Ling27185ae2009-08-24 13:50:23 +080014017
Paulo Zanonie2debe92013-02-18 19:00:27 -030014018 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014019
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014020 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14021 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014022 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014023 }
Imre Deake7281ea2013-05-08 13:14:08 +030014024 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014025 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014026 }
Ma Ling27185ae2009-08-24 13:50:23 +080014027
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014028 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014029 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014030 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014031 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014032 intel_dvo_init(dev);
14033
Zhenyu Wang103a1962009-11-27 11:44:36 +080014034 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014035 intel_tv_init(dev);
14036
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014037 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014038
Damien Lespiaub2784e12014-08-05 11:29:37 +010014039 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014040 encoder->base.possible_crtcs = encoder->crtc_mask;
14041 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014042 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014043 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014044
Paulo Zanonidde86e22012-12-01 12:04:25 -020014045 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014046
14047 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014048}
14049
14050static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14051{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014052 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014053 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014054
Daniel Vetteref2d6332014-02-10 18:00:38 +010014055 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014056 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014057 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014058 drm_gem_object_unreference(&intel_fb->obj->base);
14059 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014060 kfree(intel_fb);
14061}
14062
14063static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014064 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014065 unsigned int *handle)
14066{
14067 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014068 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014069
Chris Wilson05394f32010-11-08 19:18:58 +000014070 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014071}
14072
14073static const struct drm_framebuffer_funcs intel_fb_funcs = {
14074 .destroy = intel_user_framebuffer_destroy,
14075 .create_handle = intel_user_framebuffer_create_handle,
14076};
14077
Damien Lespiaub3218032015-02-27 11:15:18 +000014078static
14079u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14080 uint32_t pixel_format)
14081{
14082 u32 gen = INTEL_INFO(dev)->gen;
14083
14084 if (gen >= 9) {
14085 /* "The stride in bytes must not exceed the of the size of 8K
14086 * pixels and 32K bytes."
14087 */
14088 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14089 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14090 return 32*1024;
14091 } else if (gen >= 4) {
14092 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14093 return 16*1024;
14094 else
14095 return 32*1024;
14096 } else if (gen >= 3) {
14097 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14098 return 8*1024;
14099 else
14100 return 16*1024;
14101 } else {
14102 /* XXX DSPC is limited to 4k tiled */
14103 return 8*1024;
14104 }
14105}
14106
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014107static int intel_framebuffer_init(struct drm_device *dev,
14108 struct intel_framebuffer *intel_fb,
14109 struct drm_mode_fb_cmd2 *mode_cmd,
14110 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014111{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014112 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014113 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014114 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014115
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014116 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14117
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014118 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14119 /* Enforce that fb modifier and tiling mode match, but only for
14120 * X-tiled. This is needed for FBC. */
14121 if (!!(obj->tiling_mode == I915_TILING_X) !=
14122 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14123 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14124 return -EINVAL;
14125 }
14126 } else {
14127 if (obj->tiling_mode == I915_TILING_X)
14128 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14129 else if (obj->tiling_mode == I915_TILING_Y) {
14130 DRM_DEBUG("No Y tiling for legacy addfb\n");
14131 return -EINVAL;
14132 }
14133 }
14134
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014135 /* Passed in modifier sanity checking. */
14136 switch (mode_cmd->modifier[0]) {
14137 case I915_FORMAT_MOD_Y_TILED:
14138 case I915_FORMAT_MOD_Yf_TILED:
14139 if (INTEL_INFO(dev)->gen < 9) {
14140 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14141 mode_cmd->modifier[0]);
14142 return -EINVAL;
14143 }
14144 case DRM_FORMAT_MOD_NONE:
14145 case I915_FORMAT_MOD_X_TILED:
14146 break;
14147 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014148 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14149 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014150 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014151 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014152
Damien Lespiaub3218032015-02-27 11:15:18 +000014153 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14154 mode_cmd->pixel_format);
14155 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14156 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14157 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014158 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014159 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014160
Damien Lespiaub3218032015-02-27 11:15:18 +000014161 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14162 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014163 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014164 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14165 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014166 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014167 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014168 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014169 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014170
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014171 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014172 mode_cmd->pitches[0] != obj->stride) {
14173 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14174 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014175 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014176 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014177
Ville Syrjälä57779d02012-10-31 17:50:14 +020014178 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014179 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014180 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014181 case DRM_FORMAT_RGB565:
14182 case DRM_FORMAT_XRGB8888:
14183 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014184 break;
14185 case DRM_FORMAT_XRGB1555:
14186 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014187 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014188 DRM_DEBUG("unsupported pixel format: %s\n",
14189 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014190 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014191 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014192 break;
14193 case DRM_FORMAT_XBGR8888:
14194 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014195 case DRM_FORMAT_XRGB2101010:
14196 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014197 case DRM_FORMAT_XBGR2101010:
14198 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014199 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014200 DRM_DEBUG("unsupported pixel format: %s\n",
14201 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014202 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014203 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014204 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014205 case DRM_FORMAT_YUYV:
14206 case DRM_FORMAT_UYVY:
14207 case DRM_FORMAT_YVYU:
14208 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014209 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014210 DRM_DEBUG("unsupported pixel format: %s\n",
14211 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014212 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014213 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014214 break;
14215 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014216 DRM_DEBUG("unsupported pixel format: %s\n",
14217 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014218 return -EINVAL;
14219 }
14220
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014221 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14222 if (mode_cmd->offsets[0] != 0)
14223 return -EINVAL;
14224
Damien Lespiauec2c9812015-01-20 12:51:45 +000014225 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014226 mode_cmd->pixel_format,
14227 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014228 /* FIXME drm helper for size checks (especially planar formats)? */
14229 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14230 return -EINVAL;
14231
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014232 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14233 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014234 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014235
Jesse Barnes79e53942008-11-07 14:24:08 -080014236 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14237 if (ret) {
14238 DRM_ERROR("framebuffer init failed %d\n", ret);
14239 return ret;
14240 }
14241
Jesse Barnes79e53942008-11-07 14:24:08 -080014242 return 0;
14243}
14244
Jesse Barnes79e53942008-11-07 14:24:08 -080014245static struct drm_framebuffer *
14246intel_user_framebuffer_create(struct drm_device *dev,
14247 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014248 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014249{
Chris Wilson05394f32010-11-08 19:18:58 +000014250 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014251
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014252 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14253 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014254 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014255 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014256
Chris Wilsond2dff872011-04-19 08:36:26 +010014257 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014258}
14259
Daniel Vetter4520f532013-10-09 09:18:51 +020014260#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014261static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014262{
14263}
14264#endif
14265
Jesse Barnes79e53942008-11-07 14:24:08 -080014266static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014267 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014268 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014269 .atomic_check = intel_atomic_check,
14270 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080014271};
14272
Jesse Barnese70236a2009-09-21 10:42:27 -070014273/* Set up chip specific display functions */
14274static void intel_init_display(struct drm_device *dev)
14275{
14276 struct drm_i915_private *dev_priv = dev->dev_private;
14277
Daniel Vetteree9300b2013-06-03 22:40:22 +020014278 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14279 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014280 else if (IS_CHERRYVIEW(dev))
14281 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014282 else if (IS_VALLEYVIEW(dev))
14283 dev_priv->display.find_dpll = vlv_find_best_dpll;
14284 else if (IS_PINEVIEW(dev))
14285 dev_priv->display.find_dpll = pnv_find_best_dpll;
14286 else
14287 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14288
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014289 if (INTEL_INFO(dev)->gen >= 9) {
14290 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014291 dev_priv->display.get_initial_plane_config =
14292 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014293 dev_priv->display.crtc_compute_clock =
14294 haswell_crtc_compute_clock;
14295 dev_priv->display.crtc_enable = haswell_crtc_enable;
14296 dev_priv->display.crtc_disable = haswell_crtc_disable;
14297 dev_priv->display.off = ironlake_crtc_off;
14298 dev_priv->display.update_primary_plane =
14299 skylake_update_primary_plane;
14300 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014301 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014302 dev_priv->display.get_initial_plane_config =
14303 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014304 dev_priv->display.crtc_compute_clock =
14305 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014306 dev_priv->display.crtc_enable = haswell_crtc_enable;
14307 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030014308 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014309 dev_priv->display.update_primary_plane =
14310 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014311 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014312 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014313 dev_priv->display.get_initial_plane_config =
14314 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014315 dev_priv->display.crtc_compute_clock =
14316 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014317 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14318 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014319 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014320 dev_priv->display.update_primary_plane =
14321 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014322 } else if (IS_VALLEYVIEW(dev)) {
14323 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014324 dev_priv->display.get_initial_plane_config =
14325 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014326 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014327 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14328 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14329 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014330 dev_priv->display.update_primary_plane =
14331 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014332 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014333 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014334 dev_priv->display.get_initial_plane_config =
14335 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014336 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014337 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14338 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014339 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014340 dev_priv->display.update_primary_plane =
14341 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014342 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014343
Jesse Barnese70236a2009-09-21 10:42:27 -070014344 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014345 if (IS_SKYLAKE(dev))
14346 dev_priv->display.get_display_clock_speed =
14347 skylake_get_display_clock_speed;
14348 else if (IS_BROADWELL(dev))
14349 dev_priv->display.get_display_clock_speed =
14350 broadwell_get_display_clock_speed;
14351 else if (IS_HASWELL(dev))
14352 dev_priv->display.get_display_clock_speed =
14353 haswell_get_display_clock_speed;
14354 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014355 dev_priv->display.get_display_clock_speed =
14356 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014357 else if (IS_GEN5(dev))
14358 dev_priv->display.get_display_clock_speed =
14359 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014360 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14361 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070014362 dev_priv->display.get_display_clock_speed =
14363 i945_get_display_clock_speed;
14364 else if (IS_I915G(dev))
14365 dev_priv->display.get_display_clock_speed =
14366 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014367 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014368 dev_priv->display.get_display_clock_speed =
14369 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014370 else if (IS_PINEVIEW(dev))
14371 dev_priv->display.get_display_clock_speed =
14372 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014373 else if (IS_I915GM(dev))
14374 dev_priv->display.get_display_clock_speed =
14375 i915gm_get_display_clock_speed;
14376 else if (IS_I865G(dev))
14377 dev_priv->display.get_display_clock_speed =
14378 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014379 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014380 dev_priv->display.get_display_clock_speed =
14381 i855_get_display_clock_speed;
14382 else /* 852, 830 */
14383 dev_priv->display.get_display_clock_speed =
14384 i830_get_display_clock_speed;
14385
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014386 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014387 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014388 } else if (IS_GEN6(dev)) {
14389 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014390 } else if (IS_IVYBRIDGE(dev)) {
14391 /* FIXME: detect B0+ stepping and use auto training */
14392 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014393 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014394 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014395 } else if (IS_VALLEYVIEW(dev)) {
14396 dev_priv->display.modeset_global_resources =
14397 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014398 } else if (IS_BROXTON(dev)) {
14399 dev_priv->display.modeset_global_resources =
14400 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014401 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014402
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014403 switch (INTEL_INFO(dev)->gen) {
14404 case 2:
14405 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14406 break;
14407
14408 case 3:
14409 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14410 break;
14411
14412 case 4:
14413 case 5:
14414 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14415 break;
14416
14417 case 6:
14418 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14419 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014420 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014421 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014422 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14423 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014424 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014425 /* Drop through - unsupported since execlist only. */
14426 default:
14427 /* Default just returns -ENODEV to indicate unsupported */
14428 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014429 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014430
14431 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014432
14433 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014434}
14435
Jesse Barnesb690e962010-07-19 13:53:12 -070014436/*
14437 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14438 * resume, or other times. This quirk makes sure that's the case for
14439 * affected systems.
14440 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014441static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014442{
14443 struct drm_i915_private *dev_priv = dev->dev_private;
14444
14445 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014446 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014447}
14448
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014449static void quirk_pipeb_force(struct drm_device *dev)
14450{
14451 struct drm_i915_private *dev_priv = dev->dev_private;
14452
14453 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14454 DRM_INFO("applying pipe b force quirk\n");
14455}
14456
Keith Packard435793d2011-07-12 14:56:22 -070014457/*
14458 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14459 */
14460static void quirk_ssc_force_disable(struct drm_device *dev)
14461{
14462 struct drm_i915_private *dev_priv = dev->dev_private;
14463 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014464 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014465}
14466
Carsten Emde4dca20e2012-03-15 15:56:26 +010014467/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014468 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14469 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014470 */
14471static void quirk_invert_brightness(struct drm_device *dev)
14472{
14473 struct drm_i915_private *dev_priv = dev->dev_private;
14474 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014475 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014476}
14477
Scot Doyle9c72cc62014-07-03 23:27:50 +000014478/* Some VBT's incorrectly indicate no backlight is present */
14479static void quirk_backlight_present(struct drm_device *dev)
14480{
14481 struct drm_i915_private *dev_priv = dev->dev_private;
14482 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14483 DRM_INFO("applying backlight present quirk\n");
14484}
14485
Jesse Barnesb690e962010-07-19 13:53:12 -070014486struct intel_quirk {
14487 int device;
14488 int subsystem_vendor;
14489 int subsystem_device;
14490 void (*hook)(struct drm_device *dev);
14491};
14492
Egbert Eich5f85f172012-10-14 15:46:38 +020014493/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14494struct intel_dmi_quirk {
14495 void (*hook)(struct drm_device *dev);
14496 const struct dmi_system_id (*dmi_id_list)[];
14497};
14498
14499static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14500{
14501 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14502 return 1;
14503}
14504
14505static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14506 {
14507 .dmi_id_list = &(const struct dmi_system_id[]) {
14508 {
14509 .callback = intel_dmi_reverse_brightness,
14510 .ident = "NCR Corporation",
14511 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14512 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14513 },
14514 },
14515 { } /* terminating entry */
14516 },
14517 .hook = quirk_invert_brightness,
14518 },
14519};
14520
Ben Widawskyc43b5632012-04-16 14:07:40 -070014521static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014522 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040014523 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070014524
Jesse Barnesb690e962010-07-19 13:53:12 -070014525 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14526 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14527
Jesse Barnesb690e962010-07-19 13:53:12 -070014528 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14529 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14530
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014531 /* 830 needs to leave pipe A & dpll A up */
14532 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14533
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014534 /* 830 needs to leave pipe B & dpll B up */
14535 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14536
Keith Packard435793d2011-07-12 14:56:22 -070014537 /* Lenovo U160 cannot use SSC on LVDS */
14538 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014539
14540 /* Sony Vaio Y cannot use SSC on LVDS */
14541 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014542
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014543 /* Acer Aspire 5734Z must invert backlight brightness */
14544 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14545
14546 /* Acer/eMachines G725 */
14547 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14548
14549 /* Acer/eMachines e725 */
14550 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14551
14552 /* Acer/Packard Bell NCL20 */
14553 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14554
14555 /* Acer Aspire 4736Z */
14556 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014557
14558 /* Acer Aspire 5336 */
14559 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014560
14561 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14562 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014563
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014564 /* Acer C720 Chromebook (Core i3 4005U) */
14565 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14566
jens steinb2a96012014-10-28 20:25:53 +010014567 /* Apple Macbook 2,1 (Core 2 T7400) */
14568 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14569
Scot Doyled4967d82014-07-03 23:27:52 +000014570 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14571 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014572
14573 /* HP Chromebook 14 (Celeron 2955U) */
14574 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014575
14576 /* Dell Chromebook 11 */
14577 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014578};
14579
14580static void intel_init_quirks(struct drm_device *dev)
14581{
14582 struct pci_dev *d = dev->pdev;
14583 int i;
14584
14585 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14586 struct intel_quirk *q = &intel_quirks[i];
14587
14588 if (d->device == q->device &&
14589 (d->subsystem_vendor == q->subsystem_vendor ||
14590 q->subsystem_vendor == PCI_ANY_ID) &&
14591 (d->subsystem_device == q->subsystem_device ||
14592 q->subsystem_device == PCI_ANY_ID))
14593 q->hook(dev);
14594 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014595 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14596 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14597 intel_dmi_quirks[i].hook(dev);
14598 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014599}
14600
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014601/* Disable the VGA plane that we never use */
14602static void i915_disable_vga(struct drm_device *dev)
14603{
14604 struct drm_i915_private *dev_priv = dev->dev_private;
14605 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014606 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014607
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014608 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014609 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014610 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014611 sr1 = inb(VGA_SR_DATA);
14612 outb(sr1 | 1<<5, VGA_SR_DATA);
14613 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14614 udelay(300);
14615
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014616 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014617 POSTING_READ(vga_reg);
14618}
14619
Daniel Vetterf8175862012-04-10 15:50:11 +020014620void intel_modeset_init_hw(struct drm_device *dev)
14621{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014622 intel_prepare_ddi(dev);
14623
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030014624 if (IS_VALLEYVIEW(dev))
14625 vlv_update_cdclk(dev);
14626
Daniel Vetterf8175862012-04-10 15:50:11 +020014627 intel_init_clock_gating(dev);
14628
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014629 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014630}
14631
Jesse Barnes79e53942008-11-07 14:24:08 -080014632void intel_modeset_init(struct drm_device *dev)
14633{
Jesse Barnes652c3932009-08-17 13:31:43 -070014634 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014635 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014636 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014637 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014638
14639 drm_mode_config_init(dev);
14640
14641 dev->mode_config.min_width = 0;
14642 dev->mode_config.min_height = 0;
14643
Dave Airlie019d96c2011-09-29 16:20:42 +010014644 dev->mode_config.preferred_depth = 24;
14645 dev->mode_config.prefer_shadow = 1;
14646
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014647 dev->mode_config.allow_fb_modifiers = true;
14648
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014649 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014650
Jesse Barnesb690e962010-07-19 13:53:12 -070014651 intel_init_quirks(dev);
14652
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014653 intel_init_pm(dev);
14654
Ben Widawskye3c74752013-04-05 13:12:39 -070014655 if (INTEL_INFO(dev)->num_pipes == 0)
14656 return;
14657
Jesse Barnese70236a2009-09-21 10:42:27 -070014658 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014659 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014660
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014661 if (IS_GEN2(dev)) {
14662 dev->mode_config.max_width = 2048;
14663 dev->mode_config.max_height = 2048;
14664 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014665 dev->mode_config.max_width = 4096;
14666 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014667 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014668 dev->mode_config.max_width = 8192;
14669 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014670 }
Damien Lespiau068be562014-03-28 14:17:49 +000014671
Ville Syrjälädc41c152014-08-13 11:57:05 +030014672 if (IS_845G(dev) || IS_I865G(dev)) {
14673 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14674 dev->mode_config.cursor_height = 1023;
14675 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014676 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14677 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14678 } else {
14679 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14680 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14681 }
14682
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014683 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014684
Zhao Yakui28c97732009-10-09 11:39:41 +080014685 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014686 INTEL_INFO(dev)->num_pipes,
14687 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014688
Damien Lespiau055e3932014-08-18 13:49:10 +010014689 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014690 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014691 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014692 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014693 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014694 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014695 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014696 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014697 }
14698
Jesse Barnesf42bb702013-12-16 16:34:23 -080014699 intel_init_dpio(dev);
14700
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014701 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014702
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014703 /* Just disable it once at startup */
14704 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014705 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014706
14707 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014708 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014709
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014710 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014711 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014712 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014713
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014714 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080014715 if (!crtc->active)
14716 continue;
14717
Jesse Barnes46f297f2014-03-07 08:57:48 -080014718 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014719 * Note that reserving the BIOS fb up front prevents us
14720 * from stuffing other stolen allocations like the ring
14721 * on top. This prevents some ugliness at boot time, and
14722 * can even allow for smooth boot transitions if the BIOS
14723 * fb is large enough for the active pipe configuration.
14724 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014725 if (dev_priv->display.get_initial_plane_config) {
14726 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080014727 &crtc->plane_config);
14728 /*
14729 * If the fb is shared between multiple heads, we'll
14730 * just get the first one.
14731 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010014732 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014733 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080014734 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014735}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014736
Daniel Vetter7fad7982012-07-04 17:51:47 +020014737static void intel_enable_pipe_a(struct drm_device *dev)
14738{
14739 struct intel_connector *connector;
14740 struct drm_connector *crt = NULL;
14741 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014742 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014743
14744 /* We can't just switch on the pipe A, we need to set things up with a
14745 * proper mode and output configuration. As a gross hack, enable pipe A
14746 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014747 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014748 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14749 crt = &connector->base;
14750 break;
14751 }
14752 }
14753
14754 if (!crt)
14755 return;
14756
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014757 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014758 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014759}
14760
Daniel Vetterfa555832012-10-10 23:14:00 +020014761static bool
14762intel_check_plane_mapping(struct intel_crtc *crtc)
14763{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014764 struct drm_device *dev = crtc->base.dev;
14765 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014766 u32 reg, val;
14767
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014768 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014769 return true;
14770
14771 reg = DSPCNTR(!crtc->plane);
14772 val = I915_READ(reg);
14773
14774 if ((val & DISPLAY_PLANE_ENABLE) &&
14775 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14776 return false;
14777
14778 return true;
14779}
14780
Daniel Vetter24929352012-07-02 20:28:59 +020014781static void intel_sanitize_crtc(struct intel_crtc *crtc)
14782{
14783 struct drm_device *dev = crtc->base.dev;
14784 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014785 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014786
Daniel Vetter24929352012-07-02 20:28:59 +020014787 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014788 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014789 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14790
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014791 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014792 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014793 if (crtc->active) {
14794 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014795 drm_crtc_vblank_on(&crtc->base);
14796 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014797
Daniel Vetter24929352012-07-02 20:28:59 +020014798 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014799 * disable the crtc (and hence change the state) if it is wrong. Note
14800 * that gen4+ has a fixed plane -> pipe mapping. */
14801 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014802 struct intel_connector *connector;
14803 bool plane;
14804
Daniel Vetter24929352012-07-02 20:28:59 +020014805 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14806 crtc->base.base.id);
14807
14808 /* Pipe has the wrong plane attached and the plane is active.
14809 * Temporarily change the plane mapping and disable everything
14810 * ... */
14811 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014812 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014813 crtc->plane = !plane;
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030014814 intel_crtc_disable_planes(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020014815 dev_priv->display.crtc_disable(&crtc->base);
14816 crtc->plane = plane;
14817
14818 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014819 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014820 if (connector->encoder->base.crtc != &crtc->base)
14821 continue;
14822
Egbert Eich7f1950f2014-04-25 10:56:22 +020014823 connector->base.dpms = DRM_MODE_DPMS_OFF;
14824 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014825 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014826 /* multiple connectors may have the same encoder:
14827 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014828 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020014829 if (connector->encoder->base.crtc == &crtc->base) {
14830 connector->encoder->base.crtc = NULL;
14831 connector->encoder->connectors_active = false;
14832 }
Daniel Vetter24929352012-07-02 20:28:59 +020014833
14834 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080014835 crtc->base.state->enable = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014836 crtc->base.enabled = false;
14837 }
Daniel Vetter24929352012-07-02 20:28:59 +020014838
Daniel Vetter7fad7982012-07-04 17:51:47 +020014839 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14840 crtc->pipe == PIPE_A && !crtc->active) {
14841 /* BIOS forgot to enable pipe A, this mostly happens after
14842 * resume. Force-enable the pipe to fix this, the update_dpms
14843 * call below we restore the pipe to the right state, but leave
14844 * the required bits on. */
14845 intel_enable_pipe_a(dev);
14846 }
14847
Daniel Vetter24929352012-07-02 20:28:59 +020014848 /* Adjust the state of the output pipe according to whether we
14849 * have active connectors/encoders. */
14850 intel_crtc_update_dpms(&crtc->base);
14851
Matt Roper83d65732015-02-25 13:12:16 -080014852 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020014853 struct intel_encoder *encoder;
14854
14855 /* This can happen either due to bugs in the get_hw_state
14856 * functions or because the pipe is force-enabled due to the
14857 * pipe A quirk. */
14858 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14859 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014860 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014861 crtc->active ? "enabled" : "disabled");
14862
Matt Roper83d65732015-02-25 13:12:16 -080014863 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014864 crtc->base.enabled = crtc->active;
14865
14866 /* Because we only establish the connector -> encoder ->
14867 * crtc links if something is active, this means the
14868 * crtc is now deactivated. Break the links. connector
14869 * -> encoder links are only establish when things are
14870 * actually up, hence no need to break them. */
14871 WARN_ON(crtc->active);
14872
14873 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14874 WARN_ON(encoder->connectors_active);
14875 encoder->base.crtc = NULL;
14876 }
14877 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014878
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014879 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014880 /*
14881 * We start out with underrun reporting disabled to avoid races.
14882 * For correct bookkeeping mark this on active crtcs.
14883 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014884 * Also on gmch platforms we dont have any hardware bits to
14885 * disable the underrun reporting. Which means we need to start
14886 * out with underrun reporting disabled also on inactive pipes,
14887 * since otherwise we'll complain about the garbage we read when
14888 * e.g. coming up after runtime pm.
14889 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014890 * No protection against concurrent access is required - at
14891 * worst a fifo underrun happens which also sets this to false.
14892 */
14893 crtc->cpu_fifo_underrun_disabled = true;
14894 crtc->pch_fifo_underrun_disabled = true;
14895 }
Daniel Vetter24929352012-07-02 20:28:59 +020014896}
14897
14898static void intel_sanitize_encoder(struct intel_encoder *encoder)
14899{
14900 struct intel_connector *connector;
14901 struct drm_device *dev = encoder->base.dev;
14902
14903 /* We need to check both for a crtc link (meaning that the
14904 * encoder is active and trying to read from a pipe) and the
14905 * pipe itself being active. */
14906 bool has_active_crtc = encoder->base.crtc &&
14907 to_intel_crtc(encoder->base.crtc)->active;
14908
14909 if (encoder->connectors_active && !has_active_crtc) {
14910 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14911 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014912 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014913
14914 /* Connector is active, but has no active pipe. This is
14915 * fallout from our resume register restoring. Disable
14916 * the encoder manually again. */
14917 if (encoder->base.crtc) {
14918 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14919 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014920 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014921 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014922 if (encoder->post_disable)
14923 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014924 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014925 encoder->base.crtc = NULL;
14926 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014927
14928 /* Inconsistent output/port/pipe state happens presumably due to
14929 * a bug in one of the get_hw_state functions. Or someplace else
14930 * in our code, like the register restore mess on resume. Clamp
14931 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014932 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014933 if (connector->encoder != encoder)
14934 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014935 connector->base.dpms = DRM_MODE_DPMS_OFF;
14936 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014937 }
14938 }
14939 /* Enabled encoders without active connectors will be fixed in
14940 * the crtc fixup. */
14941}
14942
Imre Deak04098752014-02-18 00:02:16 +020014943void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014944{
14945 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014946 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014947
Imre Deak04098752014-02-18 00:02:16 +020014948 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14949 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14950 i915_disable_vga(dev);
14951 }
14952}
14953
14954void i915_redisable_vga(struct drm_device *dev)
14955{
14956 struct drm_i915_private *dev_priv = dev->dev_private;
14957
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014958 /* This function can be called both from intel_modeset_setup_hw_state or
14959 * at a very early point in our resume sequence, where the power well
14960 * structures are not yet restored. Since this function is at a very
14961 * paranoid "someone might have enabled VGA while we were not looking"
14962 * level, just check if the power well is enabled instead of trying to
14963 * follow the "don't touch the power well if we don't need it" policy
14964 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014965 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014966 return;
14967
Imre Deak04098752014-02-18 00:02:16 +020014968 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014969}
14970
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014971static bool primary_get_hw_state(struct intel_crtc *crtc)
14972{
14973 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14974
14975 if (!crtc->active)
14976 return false;
14977
14978 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
14979}
14980
Daniel Vetter30e984d2013-06-05 13:34:17 +020014981static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020014982{
14983 struct drm_i915_private *dev_priv = dev->dev_private;
14984 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020014985 struct intel_crtc *crtc;
14986 struct intel_encoder *encoder;
14987 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020014988 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020014989
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014990 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014991 struct drm_plane *primary = crtc->base.primary;
14992 struct intel_plane_state *plane_state;
14993
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014994 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020014995
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014996 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020014997
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014998 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014999 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015000
Matt Roper83d65732015-02-25 13:12:16 -080015001 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015002 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015003
15004 plane_state = to_intel_plane_state(primary->state);
15005 plane_state->visible = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015006
15007 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15008 crtc->base.base.id,
15009 crtc->active ? "enabled" : "disabled");
15010 }
15011
Daniel Vetter53589012013-06-05 13:34:16 +020015012 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15013 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15014
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015015 pll->on = pll->get_hw_state(dev_priv, pll,
15016 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015017 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015018 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015019 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015020 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015021 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015022 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015023 }
Daniel Vetter53589012013-06-05 13:34:16 +020015024 }
Daniel Vetter53589012013-06-05 13:34:16 +020015025
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015026 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015027 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015028
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015029 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015030 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015031 }
15032
Damien Lespiaub2784e12014-08-05 11:29:37 +010015033 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015034 pipe = 0;
15035
15036 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015037 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15038 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015039 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015040 } else {
15041 encoder->base.crtc = NULL;
15042 }
15043
15044 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015045 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015046 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015047 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015048 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015049 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015050 }
15051
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015052 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015053 if (connector->get_hw_state(connector)) {
15054 connector->base.dpms = DRM_MODE_DPMS_ON;
15055 connector->encoder->connectors_active = true;
15056 connector->base.encoder = &connector->encoder->base;
15057 } else {
15058 connector->base.dpms = DRM_MODE_DPMS_OFF;
15059 connector->base.encoder = NULL;
15060 }
15061 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15062 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015063 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015064 connector->base.encoder ? "enabled" : "disabled");
15065 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015066}
15067
15068/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15069 * and i915 state tracking structures. */
15070void intel_modeset_setup_hw_state(struct drm_device *dev,
15071 bool force_restore)
15072{
15073 struct drm_i915_private *dev_priv = dev->dev_private;
15074 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015075 struct intel_crtc *crtc;
15076 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015077 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015078
15079 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015080
Jesse Barnesbabea612013-06-26 18:57:38 +030015081 /*
15082 * Now that we have the config, copy it to each CRTC struct
15083 * Note that this could go away if we move to using crtc_config
15084 * checking everywhere.
15085 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015086 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020015087 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015088 intel_mode_from_pipe_config(&crtc->base.mode,
15089 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030015090 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15091 crtc->base.base.id);
15092 drm_mode_debug_printmodeline(&crtc->base.mode);
15093 }
15094 }
15095
Daniel Vetter24929352012-07-02 20:28:59 +020015096 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015097 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015098 intel_sanitize_encoder(encoder);
15099 }
15100
Damien Lespiau055e3932014-08-18 13:49:10 +010015101 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015102 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15103 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015104 intel_dump_pipe_config(crtc, crtc->config,
15105 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015106 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015107
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015108 intel_modeset_update_connector_atomic_state(dev);
15109
Daniel Vetter35c95372013-07-17 06:55:04 +020015110 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15111 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15112
15113 if (!pll->on || pll->active)
15114 continue;
15115
15116 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15117
15118 pll->disable(dev_priv, pll);
15119 pll->on = false;
15120 }
15121
Pradeep Bhat30789992014-11-04 17:06:45 +000015122 if (IS_GEN9(dev))
15123 skl_wm_get_hw_state(dev);
15124 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015125 ilk_wm_get_hw_state(dev);
15126
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015127 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015128 i915_redisable_vga(dev);
15129
Daniel Vetterf30da182013-04-11 20:22:50 +020015130 /*
15131 * We need to use raw interfaces for restoring state to avoid
15132 * checking (bogus) intermediate states.
15133 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015134 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015135 struct drm_crtc *crtc =
15136 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015137
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015138 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015139 }
15140 } else {
15141 intel_modeset_update_staged_output_state(dev);
15142 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015143
15144 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015145}
15146
15147void intel_modeset_gem_init(struct drm_device *dev)
15148{
Jesse Barnes92122782014-10-09 12:57:42 -070015149 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015150 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015151 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015152 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015153
Imre Deakae484342014-03-31 15:10:44 +030015154 mutex_lock(&dev->struct_mutex);
15155 intel_init_gt_powersave(dev);
15156 mutex_unlock(&dev->struct_mutex);
15157
Jesse Barnes92122782014-10-09 12:57:42 -070015158 /*
15159 * There may be no VBT; and if the BIOS enabled SSC we can
15160 * just keep using it to avoid unnecessary flicker. Whereas if the
15161 * BIOS isn't using it, don't assume it will work even if the VBT
15162 * indicates as much.
15163 */
15164 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15165 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15166 DREF_SSC1_ENABLE);
15167
Chris Wilson1833b132012-05-09 11:56:28 +010015168 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015169
15170 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015171
15172 /*
15173 * Make sure any fbs we allocated at startup are properly
15174 * pinned & fenced. When we do the allocation it's too early
15175 * for this.
15176 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015177 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015178 obj = intel_fb_obj(c->primary->fb);
15179 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015180 continue;
15181
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015182 mutex_lock(&dev->struct_mutex);
15183 ret = intel_pin_and_fence_fb_obj(c->primary,
15184 c->primary->fb,
15185 c->primary->state,
15186 NULL);
15187 mutex_unlock(&dev->struct_mutex);
15188 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015189 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15190 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015191 drm_framebuffer_unreference(c->primary->fb);
15192 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015193 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015194 }
15195 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015196
15197 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015198}
15199
Imre Deak4932e2c2014-02-11 17:12:48 +020015200void intel_connector_unregister(struct intel_connector *intel_connector)
15201{
15202 struct drm_connector *connector = &intel_connector->base;
15203
15204 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015205 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015206}
15207
Jesse Barnes79e53942008-11-07 14:24:08 -080015208void intel_modeset_cleanup(struct drm_device *dev)
15209{
Jesse Barnes652c3932009-08-17 13:31:43 -070015210 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015211 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015212
Imre Deak2eb52522014-11-19 15:30:05 +020015213 intel_disable_gt_powersave(dev);
15214
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015215 intel_backlight_unregister(dev);
15216
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015217 /*
15218 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015219 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015220 * experience fancy races otherwise.
15221 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015222 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015223
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015224 /*
15225 * Due to the hpd irq storm handling the hotplug work can re-arm the
15226 * poll handlers. Hence disable polling after hpd handling is shut down.
15227 */
Keith Packardf87ea762010-10-03 19:36:26 -070015228 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015229
Jesse Barnes652c3932009-08-17 13:31:43 -070015230 mutex_lock(&dev->struct_mutex);
15231
Jesse Barnes723bfd72010-10-07 16:01:13 -070015232 intel_unregister_dsm_handler();
15233
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015234 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015235
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015236 mutex_unlock(&dev->struct_mutex);
15237
Chris Wilson1630fe72011-07-08 12:22:42 +010015238 /* flush any delayed tasks or pending work */
15239 flush_scheduled_work();
15240
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015241 /* destroy the backlight and sysfs files before encoders/connectors */
15242 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015243 struct intel_connector *intel_connector;
15244
15245 intel_connector = to_intel_connector(connector);
15246 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015247 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015248
Jesse Barnes79e53942008-11-07 14:24:08 -080015249 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015250
15251 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015252
15253 mutex_lock(&dev->struct_mutex);
15254 intel_cleanup_gt_powersave(dev);
15255 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015256}
15257
Dave Airlie28d52042009-09-21 14:33:58 +100015258/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015259 * Return which encoder is currently attached for connector.
15260 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015261struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015262{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015263 return &intel_attached_encoder(connector)->base;
15264}
Jesse Barnes79e53942008-11-07 14:24:08 -080015265
Chris Wilsondf0e9242010-09-09 16:20:55 +010015266void intel_connector_attach_encoder(struct intel_connector *connector,
15267 struct intel_encoder *encoder)
15268{
15269 connector->encoder = encoder;
15270 drm_mode_connector_attach_encoder(&connector->base,
15271 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015272}
Dave Airlie28d52042009-09-21 14:33:58 +100015273
15274/*
15275 * set vga decode state - true == enable VGA decode
15276 */
15277int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15278{
15279 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015280 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015281 u16 gmch_ctrl;
15282
Chris Wilson75fa0412014-02-07 18:37:02 -020015283 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15284 DRM_ERROR("failed to read control word\n");
15285 return -EIO;
15286 }
15287
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015288 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15289 return 0;
15290
Dave Airlie28d52042009-09-21 14:33:58 +100015291 if (state)
15292 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15293 else
15294 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015295
15296 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15297 DRM_ERROR("failed to write control word\n");
15298 return -EIO;
15299 }
15300
Dave Airlie28d52042009-09-21 14:33:58 +100015301 return 0;
15302}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015303
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015304struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015305
15306 u32 power_well_driver;
15307
Chris Wilson63b66e52013-08-08 15:12:06 +020015308 int num_transcoders;
15309
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015310 struct intel_cursor_error_state {
15311 u32 control;
15312 u32 position;
15313 u32 base;
15314 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015315 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015316
15317 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015318 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015319 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015320 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015321 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015322
15323 struct intel_plane_error_state {
15324 u32 control;
15325 u32 stride;
15326 u32 size;
15327 u32 pos;
15328 u32 addr;
15329 u32 surface;
15330 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015331 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015332
15333 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015334 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015335 enum transcoder cpu_transcoder;
15336
15337 u32 conf;
15338
15339 u32 htotal;
15340 u32 hblank;
15341 u32 hsync;
15342 u32 vtotal;
15343 u32 vblank;
15344 u32 vsync;
15345 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015346};
15347
15348struct intel_display_error_state *
15349intel_display_capture_error_state(struct drm_device *dev)
15350{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015351 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015352 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015353 int transcoders[] = {
15354 TRANSCODER_A,
15355 TRANSCODER_B,
15356 TRANSCODER_C,
15357 TRANSCODER_EDP,
15358 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015359 int i;
15360
Chris Wilson63b66e52013-08-08 15:12:06 +020015361 if (INTEL_INFO(dev)->num_pipes == 0)
15362 return NULL;
15363
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015364 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015365 if (error == NULL)
15366 return NULL;
15367
Imre Deak190be112013-11-25 17:15:31 +020015368 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015369 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15370
Damien Lespiau055e3932014-08-18 13:49:10 +010015371 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015372 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015373 __intel_display_power_is_enabled(dev_priv,
15374 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015375 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015376 continue;
15377
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015378 error->cursor[i].control = I915_READ(CURCNTR(i));
15379 error->cursor[i].position = I915_READ(CURPOS(i));
15380 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015381
15382 error->plane[i].control = I915_READ(DSPCNTR(i));
15383 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015384 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015385 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015386 error->plane[i].pos = I915_READ(DSPPOS(i));
15387 }
Paulo Zanonica291362013-03-06 20:03:14 -030015388 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15389 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015390 if (INTEL_INFO(dev)->gen >= 4) {
15391 error->plane[i].surface = I915_READ(DSPSURF(i));
15392 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15393 }
15394
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015395 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015396
Sonika Jindal3abfce72014-07-21 15:23:43 +053015397 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015398 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015399 }
15400
15401 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15402 if (HAS_DDI(dev_priv->dev))
15403 error->num_transcoders++; /* Account for eDP. */
15404
15405 for (i = 0; i < error->num_transcoders; i++) {
15406 enum transcoder cpu_transcoder = transcoders[i];
15407
Imre Deakddf9c532013-11-27 22:02:02 +020015408 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015409 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015410 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015411 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015412 continue;
15413
Chris Wilson63b66e52013-08-08 15:12:06 +020015414 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15415
15416 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15417 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15418 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15419 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15420 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15421 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15422 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015423 }
15424
15425 return error;
15426}
15427
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015428#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15429
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015430void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015431intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015432 struct drm_device *dev,
15433 struct intel_display_error_state *error)
15434{
Damien Lespiau055e3932014-08-18 13:49:10 +010015435 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015436 int i;
15437
Chris Wilson63b66e52013-08-08 15:12:06 +020015438 if (!error)
15439 return;
15440
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015441 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015442 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015443 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015444 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015445 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015446 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015447 err_printf(m, " Power: %s\n",
15448 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015449 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015450 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015451
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015452 err_printf(m, "Plane [%d]:\n", i);
15453 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15454 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015455 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015456 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15457 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015458 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015459 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015460 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015461 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015462 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15463 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015464 }
15465
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015466 err_printf(m, "Cursor [%d]:\n", i);
15467 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15468 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15469 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015470 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015471
15472 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015473 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015474 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015475 err_printf(m, " Power: %s\n",
15476 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015477 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15478 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15479 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15480 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15481 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15482 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15483 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15484 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015485}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015486
15487void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15488{
15489 struct intel_crtc *crtc;
15490
15491 for_each_intel_crtc(dev, crtc) {
15492 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015493
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015494 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015495
15496 work = crtc->unpin_work;
15497
15498 if (work && work->event &&
15499 work->event->base.file_priv == file) {
15500 kfree(work->event);
15501 work->event = NULL;
15502 }
15503
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015504 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015505 }
15506}