blob: 49d722795ab7d8ac9019cc9b8fc77584a1e43a59 [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
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030085static int intel_set_mode(struct drm_crtc *crtc,
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,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001847 struct intel_digital_port *dport,
1848 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001849{
1850 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001851 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001852
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001853 switch (dport->port) {
1854 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001855 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001856 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001857 break;
1858 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001859 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001860 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001861 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001862 break;
1863 case PORT_D:
1864 port_mask = DPLL_PORTD_READY_MASK;
1865 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001866 break;
1867 default:
1868 BUG();
1869 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001870
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001871 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1872 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1873 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001874}
1875
Daniel Vetterb14b1052014-04-24 23:55:13 +02001876static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1877{
1878 struct drm_device *dev = crtc->base.dev;
1879 struct drm_i915_private *dev_priv = dev->dev_private;
1880 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1881
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001882 if (WARN_ON(pll == NULL))
1883 return;
1884
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001885 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001886 if (pll->active == 0) {
1887 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1888 WARN_ON(pll->on);
1889 assert_shared_dpll_disabled(dev_priv, pll);
1890
1891 pll->mode_set(dev_priv, pll);
1892 }
1893}
1894
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001895/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001896 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001897 * @dev_priv: i915 private structure
1898 * @pipe: pipe PLL to enable
1899 *
1900 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1901 * drives the transcoder clock.
1902 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001903static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001904{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001905 struct drm_device *dev = crtc->base.dev;
1906 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001907 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001908
Daniel Vetter87a875b2013-06-05 13:34:19 +02001909 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001910 return;
1911
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001912 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001913 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001914
Damien Lespiau74dd6922014-07-29 18:06:17 +01001915 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001916 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001917 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001918
Daniel Vettercdbd2312013-06-05 13:34:03 +02001919 if (pll->active++) {
1920 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001921 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001922 return;
1923 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001924 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001925
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001926 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1927
Daniel Vetter46edb022013-06-05 13:34:12 +02001928 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001929 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001930 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001931}
1932
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001933static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001934{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001935 struct drm_device *dev = crtc->base.dev;
1936 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001937 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001938
Jesse Barnes92f25842011-01-04 15:09:34 -08001939 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001940 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001941 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001942 return;
1943
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001944 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001945 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001946
Daniel Vetter46edb022013-06-05 13:34:12 +02001947 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1948 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001949 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001950
Chris Wilson48da64a2012-05-13 20:16:12 +01001951 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001952 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001953 return;
1954 }
1955
Daniel Vettere9d69442013-06-05 13:34:15 +02001956 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001957 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001958 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001959 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001960
Daniel Vetter46edb022013-06-05 13:34:12 +02001961 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001962 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001963 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001964
1965 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001966}
1967
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001968static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1969 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001970{
Daniel Vetter23670b322012-11-01 09:15:30 +01001971 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001972 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001973 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001974 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001975
1976 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001977 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001978
1979 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001980 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001981 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001982
1983 /* FDI must be feeding us bits for PCH ports */
1984 assert_fdi_tx_enabled(dev_priv, pipe);
1985 assert_fdi_rx_enabled(dev_priv, pipe);
1986
Daniel Vetter23670b322012-11-01 09:15:30 +01001987 if (HAS_PCH_CPT(dev)) {
1988 /* Workaround: Set the timing override bit before enabling the
1989 * pch transcoder. */
1990 reg = TRANS_CHICKEN2(pipe);
1991 val = I915_READ(reg);
1992 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1993 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001994 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001995
Daniel Vetterab9412b2013-05-03 11:49:46 +02001996 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001997 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001998 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001999
2000 if (HAS_PCH_IBX(dev_priv->dev)) {
2001 /*
2002 * make the BPC in transcoder be consistent with
2003 * that in pipeconf reg.
2004 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002005 val &= ~PIPECONF_BPC_MASK;
2006 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002007 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002008
2009 val &= ~TRANS_INTERLACE_MASK;
2010 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002011 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002012 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002013 val |= TRANS_LEGACY_INTERLACED_ILK;
2014 else
2015 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002016 else
2017 val |= TRANS_PROGRESSIVE;
2018
Jesse Barnes040484a2011-01-03 12:14:26 -08002019 I915_WRITE(reg, val | TRANS_ENABLE);
2020 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002021 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002022}
2023
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002024static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002025 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002026{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002027 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002028
2029 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002030 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002031
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002032 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002033 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002034 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002035
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002036 /* Workaround: set timing override bit. */
2037 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002038 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002039 I915_WRITE(_TRANSA_CHICKEN2, val);
2040
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002041 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002042 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002043
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002044 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2045 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002046 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002047 else
2048 val |= TRANS_PROGRESSIVE;
2049
Daniel Vetterab9412b2013-05-03 11:49:46 +02002050 I915_WRITE(LPT_TRANSCONF, val);
2051 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002052 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002053}
2054
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002055static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2056 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002057{
Daniel Vetter23670b322012-11-01 09:15:30 +01002058 struct drm_device *dev = dev_priv->dev;
2059 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002060
2061 /* FDI relies on the transcoder */
2062 assert_fdi_tx_disabled(dev_priv, pipe);
2063 assert_fdi_rx_disabled(dev_priv, pipe);
2064
Jesse Barnes291906f2011-02-02 12:28:03 -08002065 /* Ports must be off as well */
2066 assert_pch_ports_disabled(dev_priv, pipe);
2067
Daniel Vetterab9412b2013-05-03 11:49:46 +02002068 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002069 val = I915_READ(reg);
2070 val &= ~TRANS_ENABLE;
2071 I915_WRITE(reg, val);
2072 /* wait for PCH transcoder off, transcoder state */
2073 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002074 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002075
2076 if (!HAS_PCH_IBX(dev)) {
2077 /* Workaround: Clear the timing override chicken bit again. */
2078 reg = TRANS_CHICKEN2(pipe);
2079 val = I915_READ(reg);
2080 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2081 I915_WRITE(reg, val);
2082 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002083}
2084
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002085static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002086{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002087 u32 val;
2088
Daniel Vetterab9412b2013-05-03 11:49:46 +02002089 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002090 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002091 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002092 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002093 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002094 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002095
2096 /* Workaround: clear timing override bit. */
2097 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002098 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002099 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002100}
2101
2102/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002103 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002104 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002105 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002106 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002107 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002108 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002109static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002110{
Paulo Zanoni03722642014-01-17 13:51:09 -02002111 struct drm_device *dev = crtc->base.dev;
2112 struct drm_i915_private *dev_priv = dev->dev_private;
2113 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002114 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2115 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002116 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002117 int reg;
2118 u32 val;
2119
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002120 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002121 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002122 assert_sprites_disabled(dev_priv, pipe);
2123
Paulo Zanoni681e5812012-12-06 11:12:38 -02002124 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002125 pch_transcoder = TRANSCODER_A;
2126 else
2127 pch_transcoder = pipe;
2128
Jesse Barnesb24e7172011-01-04 15:09:30 -08002129 /*
2130 * A pipe without a PLL won't actually be able to drive bits from
2131 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2132 * need the check.
2133 */
Imre Deak50360402015-01-16 00:55:16 -08002134 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002135 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002136 assert_dsi_pll_enabled(dev_priv);
2137 else
2138 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002139 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002140 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002141 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002142 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002143 assert_fdi_tx_pll_enabled(dev_priv,
2144 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002145 }
2146 /* FIXME: assert CPU port conditions for SNB+ */
2147 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002148
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002149 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002150 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002151 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002152 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2153 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002154 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002155 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002156
2157 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002158 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002159}
2160
2161/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002162 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002163 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002164 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002165 * Disable the pipe of @crtc, making sure that various hardware
2166 * specific requirements are met, if applicable, e.g. plane
2167 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002168 *
2169 * Will wait until the pipe has shut down before returning.
2170 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002171static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002172{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002173 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002174 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002175 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002176 int reg;
2177 u32 val;
2178
2179 /*
2180 * Make sure planes won't keep trying to pump pixels to us,
2181 * or we might hang the display.
2182 */
2183 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002184 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002185 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002186
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002187 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002188 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002189 if ((val & PIPECONF_ENABLE) == 0)
2190 return;
2191
Ville Syrjälä67adc642014-08-15 01:21:57 +03002192 /*
2193 * Double wide has implications for planes
2194 * so best keep it disabled when not needed.
2195 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002196 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002197 val &= ~PIPECONF_DOUBLE_WIDE;
2198
2199 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002200 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2201 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002202 val &= ~PIPECONF_ENABLE;
2203
2204 I915_WRITE(reg, val);
2205 if ((val & PIPECONF_ENABLE) == 0)
2206 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002207}
2208
Keith Packardd74362c2011-07-28 14:47:14 -07002209/*
2210 * Plane regs are double buffered, going from enabled->disabled needs a
2211 * trigger in order to latch. The display address reg provides this.
2212 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002213void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2214 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002215{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002216 struct drm_device *dev = dev_priv->dev;
2217 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002218
2219 I915_WRITE(reg, I915_READ(reg));
2220 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002221}
2222
Jesse Barnesb24e7172011-01-04 15:09:30 -08002223/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002224 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002225 * @plane: plane to be enabled
2226 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002227 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002228 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002229 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002230static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2231 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002232{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002233 struct drm_device *dev = plane->dev;
2234 struct drm_i915_private *dev_priv = dev->dev_private;
2235 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002236
2237 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002238 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002239 to_intel_plane_state(plane->state)->visible = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002240
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002241 dev_priv->display.update_primary_plane(crtc, plane->fb,
2242 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002243}
2244
Chris Wilson693db182013-03-05 14:52:39 +00002245static bool need_vtd_wa(struct drm_device *dev)
2246{
2247#ifdef CONFIG_INTEL_IOMMU
2248 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2249 return true;
2250#endif
2251 return false;
2252}
2253
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002254unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002255intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2256 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002257{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002258 unsigned int tile_height;
2259 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002260
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002261 switch (fb_format_modifier) {
2262 case DRM_FORMAT_MOD_NONE:
2263 tile_height = 1;
2264 break;
2265 case I915_FORMAT_MOD_X_TILED:
2266 tile_height = IS_GEN2(dev) ? 16 : 8;
2267 break;
2268 case I915_FORMAT_MOD_Y_TILED:
2269 tile_height = 32;
2270 break;
2271 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002272 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2273 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002274 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002275 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002276 tile_height = 64;
2277 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002278 case 2:
2279 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002280 tile_height = 32;
2281 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002282 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002283 tile_height = 16;
2284 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002285 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002286 WARN_ONCE(1,
2287 "128-bit pixels are not supported for display!");
2288 tile_height = 16;
2289 break;
2290 }
2291 break;
2292 default:
2293 MISSING_CASE(fb_format_modifier);
2294 tile_height = 1;
2295 break;
2296 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002297
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002298 return tile_height;
2299}
2300
2301unsigned int
2302intel_fb_align_height(struct drm_device *dev, unsigned int height,
2303 uint32_t pixel_format, uint64_t fb_format_modifier)
2304{
2305 return ALIGN(height, intel_tile_height(dev, pixel_format,
2306 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002307}
2308
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002309static int
2310intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2311 const struct drm_plane_state *plane_state)
2312{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002313 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002314
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002315 *view = i915_ggtt_view_normal;
2316
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002317 if (!plane_state)
2318 return 0;
2319
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002320 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002321 return 0;
2322
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002323 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002324
2325 info->height = fb->height;
2326 info->pixel_format = fb->pixel_format;
2327 info->pitch = fb->pitches[0];
2328 info->fb_modifier = fb->modifier[0];
2329
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002330 return 0;
2331}
2332
Chris Wilson127bd2a2010-07-23 23:32:05 +01002333int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002334intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2335 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002336 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002337 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002338{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002339 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002340 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002341 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002342 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002343 u32 alignment;
2344 int ret;
2345
Matt Roperebcdd392014-07-09 16:22:11 -07002346 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2347
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002348 switch (fb->modifier[0]) {
2349 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002350 if (INTEL_INFO(dev)->gen >= 9)
2351 alignment = 256 * 1024;
2352 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002353 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002354 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002355 alignment = 4 * 1024;
2356 else
2357 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002358 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002359 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002360 if (INTEL_INFO(dev)->gen >= 9)
2361 alignment = 256 * 1024;
2362 else {
2363 /* pin() will align the object as required by fence */
2364 alignment = 0;
2365 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002366 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002367 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002368 case I915_FORMAT_MOD_Yf_TILED:
2369 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2370 "Y tiling bo slipped through, driver bug!\n"))
2371 return -EINVAL;
2372 alignment = 1 * 1024 * 1024;
2373 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002374 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002375 MISSING_CASE(fb->modifier[0]);
2376 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002377 }
2378
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002379 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2380 if (ret)
2381 return ret;
2382
Chris Wilson693db182013-03-05 14:52:39 +00002383 /* Note that the w/a also requires 64 PTE of padding following the
2384 * bo. We currently fill all unused PTE with the shadow page and so
2385 * we should always have valid PTE following the scanout preventing
2386 * the VT-d warning.
2387 */
2388 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2389 alignment = 256 * 1024;
2390
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002391 /*
2392 * Global gtt pte registers are special registers which actually forward
2393 * writes to a chunk of system memory. Which means that there is no risk
2394 * that the register values disappear as soon as we call
2395 * intel_runtime_pm_put(), so it is correct to wrap only the
2396 * pin/unpin/fence and not more.
2397 */
2398 intel_runtime_pm_get(dev_priv);
2399
Chris Wilsonce453d82011-02-21 14:43:56 +00002400 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002401 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002402 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002403 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002404 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002405
2406 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2407 * fence, whereas 965+ only requires a fence if using
2408 * framebuffer compression. For simplicity, we always install
2409 * a fence as the cost is not that onerous.
2410 */
Chris Wilson06d98132012-04-17 15:31:24 +01002411 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002412 if (ret)
2413 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002414
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002415 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002416
Chris Wilsonce453d82011-02-21 14:43:56 +00002417 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002418 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002419 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002420
2421err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002422 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002423err_interruptible:
2424 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002425 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002426 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002427}
2428
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002429static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2430 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002431{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002432 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002433 struct i915_ggtt_view view;
2434 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002435
Matt Roperebcdd392014-07-09 16:22:11 -07002436 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2437
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002438 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2439 WARN_ONCE(ret, "Couldn't get view from plane state!");
2440
Chris Wilson1690e1e2011-12-14 13:57:08 +01002441 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002442 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002443}
2444
Daniel Vetterc2c75132012-07-05 12:17:30 +02002445/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2446 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002447unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2448 unsigned int tiling_mode,
2449 unsigned int cpp,
2450 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002451{
Chris Wilsonbc752862013-02-21 20:04:31 +00002452 if (tiling_mode != I915_TILING_NONE) {
2453 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002454
Chris Wilsonbc752862013-02-21 20:04:31 +00002455 tile_rows = *y / 8;
2456 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002457
Chris Wilsonbc752862013-02-21 20:04:31 +00002458 tiles = *x / (512/cpp);
2459 *x %= 512/cpp;
2460
2461 return tile_rows * pitch * 8 + tiles * 4096;
2462 } else {
2463 unsigned int offset;
2464
2465 offset = *y * pitch + *x * cpp;
2466 *y = 0;
2467 *x = (offset & 4095) / cpp;
2468 return offset & -4096;
2469 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002470}
2471
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002472static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002473{
2474 switch (format) {
2475 case DISPPLANE_8BPP:
2476 return DRM_FORMAT_C8;
2477 case DISPPLANE_BGRX555:
2478 return DRM_FORMAT_XRGB1555;
2479 case DISPPLANE_BGRX565:
2480 return DRM_FORMAT_RGB565;
2481 default:
2482 case DISPPLANE_BGRX888:
2483 return DRM_FORMAT_XRGB8888;
2484 case DISPPLANE_RGBX888:
2485 return DRM_FORMAT_XBGR8888;
2486 case DISPPLANE_BGRX101010:
2487 return DRM_FORMAT_XRGB2101010;
2488 case DISPPLANE_RGBX101010:
2489 return DRM_FORMAT_XBGR2101010;
2490 }
2491}
2492
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002493static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2494{
2495 switch (format) {
2496 case PLANE_CTL_FORMAT_RGB_565:
2497 return DRM_FORMAT_RGB565;
2498 default:
2499 case PLANE_CTL_FORMAT_XRGB_8888:
2500 if (rgb_order) {
2501 if (alpha)
2502 return DRM_FORMAT_ABGR8888;
2503 else
2504 return DRM_FORMAT_XBGR8888;
2505 } else {
2506 if (alpha)
2507 return DRM_FORMAT_ARGB8888;
2508 else
2509 return DRM_FORMAT_XRGB8888;
2510 }
2511 case PLANE_CTL_FORMAT_XRGB_2101010:
2512 if (rgb_order)
2513 return DRM_FORMAT_XBGR2101010;
2514 else
2515 return DRM_FORMAT_XRGB2101010;
2516 }
2517}
2518
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002519static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002520intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2521 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002522{
2523 struct drm_device *dev = crtc->base.dev;
2524 struct drm_i915_gem_object *obj = NULL;
2525 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002526 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002527 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2528 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2529 PAGE_SIZE);
2530
2531 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002532
Chris Wilsonff2652e2014-03-10 08:07:02 +00002533 if (plane_config->size == 0)
2534 return false;
2535
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002536 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2537 base_aligned,
2538 base_aligned,
2539 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002540 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002541 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002542
Damien Lespiau49af4492015-01-20 12:51:44 +00002543 obj->tiling_mode = plane_config->tiling;
2544 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002545 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002546
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002547 mode_cmd.pixel_format = fb->pixel_format;
2548 mode_cmd.width = fb->width;
2549 mode_cmd.height = fb->height;
2550 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002551 mode_cmd.modifier[0] = fb->modifier[0];
2552 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002553
2554 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002555 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002556 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002557 DRM_DEBUG_KMS("intel fb init failed\n");
2558 goto out_unref_obj;
2559 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002560 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002561
Daniel Vetterf6936e22015-03-26 12:17:05 +01002562 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002563 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002564
2565out_unref_obj:
2566 drm_gem_object_unreference(&obj->base);
2567 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002568 return false;
2569}
2570
Matt Roperafd65eb2015-02-03 13:10:04 -08002571/* Update plane->state->fb to match plane->fb after driver-internal updates */
2572static void
2573update_state_fb(struct drm_plane *plane)
2574{
2575 if (plane->fb == plane->state->fb)
2576 return;
2577
2578 if (plane->state->fb)
2579 drm_framebuffer_unreference(plane->state->fb);
2580 plane->state->fb = plane->fb;
2581 if (plane->state->fb)
2582 drm_framebuffer_reference(plane->state->fb);
2583}
2584
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002585static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002586intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2587 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002588{
2589 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002590 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002591 struct drm_crtc *c;
2592 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002593 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002594 struct drm_plane *primary = intel_crtc->base.primary;
2595 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002596
Damien Lespiau2d140302015-02-05 17:22:18 +00002597 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002598 return;
2599
Daniel Vetterf6936e22015-03-26 12:17:05 +01002600 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002601 fb = &plane_config->fb->base;
2602 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002603 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002604
Damien Lespiau2d140302015-02-05 17:22:18 +00002605 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002606
2607 /*
2608 * Failed to alloc the obj, check to see if we should share
2609 * an fb with another CRTC instead
2610 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002611 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002612 i = to_intel_crtc(c);
2613
2614 if (c == &intel_crtc->base)
2615 continue;
2616
Matt Roper2ff8fde2014-07-08 07:50:07 -07002617 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002618 continue;
2619
Daniel Vetter88595ac2015-03-26 12:42:24 +01002620 fb = c->primary->fb;
2621 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002622 continue;
2623
Daniel Vetter88595ac2015-03-26 12:42:24 +01002624 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002625 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002626 drm_framebuffer_reference(fb);
2627 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002628 }
2629 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002630
2631 return;
2632
2633valid_fb:
2634 obj = intel_fb_obj(fb);
2635 if (obj->tiling_mode != I915_TILING_NONE)
2636 dev_priv->preserve_bios_swizzle = true;
2637
2638 primary->fb = fb;
2639 primary->state->crtc = &intel_crtc->base;
2640 primary->crtc = &intel_crtc->base;
2641 update_state_fb(primary);
2642 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002643}
2644
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002645static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2646 struct drm_framebuffer *fb,
2647 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002648{
2649 struct drm_device *dev = crtc->dev;
2650 struct drm_i915_private *dev_priv = dev->dev_private;
2651 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002652 struct drm_plane *primary = crtc->primary;
2653 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002654 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002655 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002656 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002657 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002658 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302659 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002660
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002661 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002662 I915_WRITE(reg, 0);
2663 if (INTEL_INFO(dev)->gen >= 4)
2664 I915_WRITE(DSPSURF(plane), 0);
2665 else
2666 I915_WRITE(DSPADDR(plane), 0);
2667 POSTING_READ(reg);
2668 return;
2669 }
2670
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002671 obj = intel_fb_obj(fb);
2672 if (WARN_ON(obj == NULL))
2673 return;
2674
2675 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2676
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002677 dspcntr = DISPPLANE_GAMMA_ENABLE;
2678
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002679 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002680
2681 if (INTEL_INFO(dev)->gen < 4) {
2682 if (intel_crtc->pipe == PIPE_B)
2683 dspcntr |= DISPPLANE_SEL_PIPE_B;
2684
2685 /* pipesrc and dspsize control the size that is scaled from,
2686 * which should always be the user's requested size.
2687 */
2688 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002689 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2690 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002691 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002692 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2693 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002694 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2695 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002696 I915_WRITE(PRIMPOS(plane), 0);
2697 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002698 }
2699
Ville Syrjälä57779d02012-10-31 17:50:14 +02002700 switch (fb->pixel_format) {
2701 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002702 dspcntr |= DISPPLANE_8BPP;
2703 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002704 case DRM_FORMAT_XRGB1555:
2705 case DRM_FORMAT_ARGB1555:
2706 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002707 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002708 case DRM_FORMAT_RGB565:
2709 dspcntr |= DISPPLANE_BGRX565;
2710 break;
2711 case DRM_FORMAT_XRGB8888:
2712 case DRM_FORMAT_ARGB8888:
2713 dspcntr |= DISPPLANE_BGRX888;
2714 break;
2715 case DRM_FORMAT_XBGR8888:
2716 case DRM_FORMAT_ABGR8888:
2717 dspcntr |= DISPPLANE_RGBX888;
2718 break;
2719 case DRM_FORMAT_XRGB2101010:
2720 case DRM_FORMAT_ARGB2101010:
2721 dspcntr |= DISPPLANE_BGRX101010;
2722 break;
2723 case DRM_FORMAT_XBGR2101010:
2724 case DRM_FORMAT_ABGR2101010:
2725 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002726 break;
2727 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002728 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002729 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002730
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002731 if (INTEL_INFO(dev)->gen >= 4 &&
2732 obj->tiling_mode != I915_TILING_NONE)
2733 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002734
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002735 if (IS_G4X(dev))
2736 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2737
Ville Syrjäläb98971272014-08-27 16:51:22 +03002738 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002739
Daniel Vetterc2c75132012-07-05 12:17:30 +02002740 if (INTEL_INFO(dev)->gen >= 4) {
2741 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002742 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002743 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002744 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002745 linear_offset -= intel_crtc->dspaddr_offset;
2746 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002747 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002748 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002749
Matt Roper8e7d6882015-01-21 16:35:41 -08002750 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302751 dspcntr |= DISPPLANE_ROTATE_180;
2752
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002753 x += (intel_crtc->config->pipe_src_w - 1);
2754 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302755
2756 /* Finding the last pixel of the last line of the display
2757 data and adding to linear_offset*/
2758 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002759 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2760 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302761 }
2762
2763 I915_WRITE(reg, dspcntr);
2764
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002765 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002766 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002767 I915_WRITE(DSPSURF(plane),
2768 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002769 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002770 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002771 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002772 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002773 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002774}
2775
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002776static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2777 struct drm_framebuffer *fb,
2778 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002779{
2780 struct drm_device *dev = crtc->dev;
2781 struct drm_i915_private *dev_priv = dev->dev_private;
2782 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002783 struct drm_plane *primary = crtc->primary;
2784 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002785 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002786 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002787 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002788 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002789 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302790 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002791
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002792 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002793 I915_WRITE(reg, 0);
2794 I915_WRITE(DSPSURF(plane), 0);
2795 POSTING_READ(reg);
2796 return;
2797 }
2798
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002799 obj = intel_fb_obj(fb);
2800 if (WARN_ON(obj == NULL))
2801 return;
2802
2803 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2804
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002805 dspcntr = DISPPLANE_GAMMA_ENABLE;
2806
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002807 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002808
2809 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2810 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2811
Ville Syrjälä57779d02012-10-31 17:50:14 +02002812 switch (fb->pixel_format) {
2813 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002814 dspcntr |= DISPPLANE_8BPP;
2815 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002816 case DRM_FORMAT_RGB565:
2817 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002818 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002819 case DRM_FORMAT_XRGB8888:
2820 case DRM_FORMAT_ARGB8888:
2821 dspcntr |= DISPPLANE_BGRX888;
2822 break;
2823 case DRM_FORMAT_XBGR8888:
2824 case DRM_FORMAT_ABGR8888:
2825 dspcntr |= DISPPLANE_RGBX888;
2826 break;
2827 case DRM_FORMAT_XRGB2101010:
2828 case DRM_FORMAT_ARGB2101010:
2829 dspcntr |= DISPPLANE_BGRX101010;
2830 break;
2831 case DRM_FORMAT_XBGR2101010:
2832 case DRM_FORMAT_ABGR2101010:
2833 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002834 break;
2835 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002836 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002837 }
2838
2839 if (obj->tiling_mode != I915_TILING_NONE)
2840 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002841
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002842 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002843 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002844
Ville Syrjäläb98971272014-08-27 16:51:22 +03002845 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002846 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002847 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002848 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002849 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002850 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002851 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302852 dspcntr |= DISPPLANE_ROTATE_180;
2853
2854 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002855 x += (intel_crtc->config->pipe_src_w - 1);
2856 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302857
2858 /* Finding the last pixel of the last line of the display
2859 data and adding to linear_offset*/
2860 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002861 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2862 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302863 }
2864 }
2865
2866 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002867
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002868 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002869 I915_WRITE(DSPSURF(plane),
2870 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002871 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002872 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2873 } else {
2874 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2875 I915_WRITE(DSPLINOFF(plane), linear_offset);
2876 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002877 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002878}
2879
Damien Lespiaub3218032015-02-27 11:15:18 +00002880u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2881 uint32_t pixel_format)
2882{
2883 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2884
2885 /*
2886 * The stride is either expressed as a multiple of 64 bytes
2887 * chunks for linear buffers or in number of tiles for tiled
2888 * buffers.
2889 */
2890 switch (fb_modifier) {
2891 case DRM_FORMAT_MOD_NONE:
2892 return 64;
2893 case I915_FORMAT_MOD_X_TILED:
2894 if (INTEL_INFO(dev)->gen == 2)
2895 return 128;
2896 return 512;
2897 case I915_FORMAT_MOD_Y_TILED:
2898 /* No need to check for old gens and Y tiling since this is
2899 * about the display engine and those will be blocked before
2900 * we get here.
2901 */
2902 return 128;
2903 case I915_FORMAT_MOD_Yf_TILED:
2904 if (bits_per_pixel == 8)
2905 return 64;
2906 else
2907 return 128;
2908 default:
2909 MISSING_CASE(fb_modifier);
2910 return 64;
2911 }
2912}
2913
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002914unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2915 struct drm_i915_gem_object *obj)
2916{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002917 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002918
2919 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002920 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002921
2922 return i915_gem_obj_ggtt_offset_view(obj, view);
2923}
2924
Chandra Kondurua1b22782015-04-07 15:28:45 -07002925/*
2926 * This function detaches (aka. unbinds) unused scalers in hardware
2927 */
2928void skl_detach_scalers(struct intel_crtc *intel_crtc)
2929{
2930 struct drm_device *dev;
2931 struct drm_i915_private *dev_priv;
2932 struct intel_crtc_scaler_state *scaler_state;
2933 int i;
2934
2935 if (!intel_crtc || !intel_crtc->config)
2936 return;
2937
2938 dev = intel_crtc->base.dev;
2939 dev_priv = dev->dev_private;
2940 scaler_state = &intel_crtc->config->scaler_state;
2941
2942 /* loop through and disable scalers that aren't in use */
2943 for (i = 0; i < intel_crtc->num_scalers; i++) {
2944 if (!scaler_state->scalers[i].in_use) {
2945 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2946 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2947 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2948 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2949 intel_crtc->base.base.id, intel_crtc->pipe, i);
2950 }
2951 }
2952}
2953
Chandra Konduru6156a452015-04-27 13:48:39 -07002954u32 skl_plane_ctl_format(uint32_t pixel_format)
2955{
Damien Lespiau65438bc2015-05-12 16:13:15 +01002956 u32 format = 0;
Damien Lespiaub250a4c2015-05-12 16:13:13 +01002957
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 switch (pixel_format) {
2959 case DRM_FORMAT_RGB565:
Damien Lespiau65438bc2015-05-12 16:13:15 +01002960 format = PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 break;
2962 case DRM_FORMAT_XBGR8888:
Damien Lespiau65438bc2015-05-12 16:13:15 +01002963 format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002964 break;
2965 case DRM_FORMAT_XRGB8888:
Damien Lespiau65438bc2015-05-12 16:13:15 +01002966 format = PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002967 break;
2968 /*
2969 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2970 * to be already pre-multiplied. We need to add a knob (or a different
2971 * DRM_FORMAT) for user-space to configure that.
2972 */
2973 case DRM_FORMAT_ABGR8888:
Damien Lespiau65438bc2015-05-12 16:13:15 +01002974 format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002975 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2976 break;
2977 case DRM_FORMAT_ARGB8888:
Damien Lespiau65438bc2015-05-12 16:13:15 +01002978 format = PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002979 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2980 break;
2981 case DRM_FORMAT_XRGB2101010:
Damien Lespiau65438bc2015-05-12 16:13:15 +01002982 format = PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002983 break;
2984 case DRM_FORMAT_XBGR2101010:
Damien Lespiau65438bc2015-05-12 16:13:15 +01002985 format = PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002986 break;
2987 case DRM_FORMAT_YUYV:
Damien Lespiau65438bc2015-05-12 16:13:15 +01002988 format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002989 break;
2990 case DRM_FORMAT_YVYU:
Damien Lespiau65438bc2015-05-12 16:13:15 +01002991 format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002992 break;
2993 case DRM_FORMAT_UYVY:
Damien Lespiau65438bc2015-05-12 16:13:15 +01002994 format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002995 break;
2996 case DRM_FORMAT_VYUY:
Damien Lespiau65438bc2015-05-12 16:13:15 +01002997 format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002998 break;
2999 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01003000 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07003001 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003002
Damien Lespiau65438bc2015-05-12 16:13:15 +01003003 return format;
Chandra Konduru6156a452015-04-27 13:48:39 -07003004}
3005
3006u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3007{
3008 u32 plane_ctl_tiling = 0;
Damien Lespiaub250a4c2015-05-12 16:13:13 +01003009
Chandra Konduru6156a452015-04-27 13:48:39 -07003010 switch (fb_modifier) {
3011 case DRM_FORMAT_MOD_NONE:
3012 break;
3013 case I915_FORMAT_MOD_X_TILED:
3014 plane_ctl_tiling = PLANE_CTL_TILED_X;
3015 break;
3016 case I915_FORMAT_MOD_Y_TILED:
3017 plane_ctl_tiling = PLANE_CTL_TILED_Y;
3018 break;
3019 case I915_FORMAT_MOD_Yf_TILED:
3020 plane_ctl_tiling = PLANE_CTL_TILED_YF;
3021 break;
3022 default:
3023 MISSING_CASE(fb_modifier);
3024 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003025
Chandra Konduru6156a452015-04-27 13:48:39 -07003026 return plane_ctl_tiling;
3027}
3028
3029u32 skl_plane_ctl_rotation(unsigned int rotation)
3030{
3031 u32 plane_ctl_rotation = 0;
Damien Lespiaub250a4c2015-05-12 16:13:13 +01003032
Chandra Konduru6156a452015-04-27 13:48:39 -07003033 switch (rotation) {
3034 case BIT(DRM_ROTATE_0):
3035 break;
3036 case BIT(DRM_ROTATE_90):
3037 plane_ctl_rotation = PLANE_CTL_ROTATE_90;
3038 break;
3039 case BIT(DRM_ROTATE_180):
3040 plane_ctl_rotation = PLANE_CTL_ROTATE_180;
3041 break;
3042 case BIT(DRM_ROTATE_270):
3043 plane_ctl_rotation = PLANE_CTL_ROTATE_270;
3044 break;
3045 default:
3046 MISSING_CASE(rotation);
3047 }
3048
3049 return plane_ctl_rotation;
3050}
3051
Damien Lespiau70d21f02013-07-03 21:06:04 +01003052static void skylake_update_primary_plane(struct drm_crtc *crtc,
3053 struct drm_framebuffer *fb,
3054 int x, int y)
3055{
3056 struct drm_device *dev = crtc->dev;
3057 struct drm_i915_private *dev_priv = dev->dev_private;
3058 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003059 struct drm_plane *plane = crtc->primary;
3060 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003061 struct drm_i915_gem_object *obj;
3062 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303063 u32 plane_ctl, stride_div, stride;
3064 u32 tile_height, plane_offset, plane_size;
3065 unsigned int rotation;
3066 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003067 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003068 struct intel_crtc_state *crtc_state = intel_crtc->config;
3069 struct intel_plane_state *plane_state;
3070 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3071 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3072 int scaler_id = -1;
3073
Chandra Konduru6156a452015-04-27 13:48:39 -07003074 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003075
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003076 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003077 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3078 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3079 POSTING_READ(PLANE_CTL(pipe, 0));
3080 return;
3081 }
3082
3083 plane_ctl = PLANE_CTL_ENABLE |
3084 PLANE_CTL_PIPE_GAMMA_ENABLE |
3085 PLANE_CTL_PIPE_CSC_ENABLE;
3086
Chandra Konduru6156a452015-04-27 13:48:39 -07003087 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3088 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003089 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303090
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303091 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003092 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003093
Damien Lespiaub3218032015-02-27 11:15:18 +00003094 obj = intel_fb_obj(fb);
3095 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3096 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303097 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3098
Chandra Konduru6156a452015-04-27 13:48:39 -07003099 /*
3100 * FIXME: intel_plane_state->src, dst aren't set when transitional
3101 * update_plane helpers are called from legacy paths.
3102 * Once full atomic crtc is available, below check can be avoided.
3103 */
3104 if (drm_rect_width(&plane_state->src)) {
3105 scaler_id = plane_state->scaler_id;
3106 src_x = plane_state->src.x1 >> 16;
3107 src_y = plane_state->src.y1 >> 16;
3108 src_w = drm_rect_width(&plane_state->src) >> 16;
3109 src_h = drm_rect_height(&plane_state->src) >> 16;
3110 dst_x = plane_state->dst.x1;
3111 dst_y = plane_state->dst.y1;
3112 dst_w = drm_rect_width(&plane_state->dst);
3113 dst_h = drm_rect_height(&plane_state->dst);
3114
3115 WARN_ON(x != src_x || y != src_y);
3116 } else {
3117 src_w = intel_crtc->config->pipe_src_w;
3118 src_h = intel_crtc->config->pipe_src_h;
3119 }
3120
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303121 if (intel_rotation_90_or_270(rotation)) {
3122 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003123 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303124 fb->modifier[0]);
3125 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003126 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303127 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003128 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303129 } else {
3130 stride = fb->pitches[0] / stride_div;
3131 x_offset = x;
3132 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003133 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303134 }
3135 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003136
Damien Lespiau70d21f02013-07-03 21:06:04 +01003137 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303138 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3139 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3140 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003141
3142 if (scaler_id >= 0) {
3143 uint32_t ps_ctrl = 0;
3144
3145 WARN_ON(!dst_w || !dst_h);
3146 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3147 crtc_state->scaler_state.scalers[scaler_id].mode;
3148 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3149 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3150 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3151 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3152 I915_WRITE(PLANE_POS(pipe, 0), 0);
3153 } else {
3154 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3155 }
3156
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003157 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003158
3159 POSTING_READ(PLANE_SURF(pipe, 0));
3160}
3161
Jesse Barnes17638cd2011-06-24 12:19:23 -07003162/* Assume fb object is pinned & idle & fenced and just update base pointers */
3163static int
3164intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3165 int x, int y, enum mode_set_atomic state)
3166{
3167 struct drm_device *dev = crtc->dev;
3168 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003169
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003170 if (dev_priv->display.disable_fbc)
3171 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003172
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003173 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3174
3175 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003176}
3177
Ville Syrjälä75147472014-11-24 18:28:11 +02003178static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003179{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003180 struct drm_crtc *crtc;
3181
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003182 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3184 enum plane plane = intel_crtc->plane;
3185
3186 intel_prepare_page_flip(dev, plane);
3187 intel_finish_page_flip_plane(dev, plane);
3188 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003189}
3190
3191static void intel_update_primary_planes(struct drm_device *dev)
3192{
3193 struct drm_i915_private *dev_priv = dev->dev_private;
3194 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003195
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003196 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003197 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3198
Rob Clark51fd3712013-11-19 12:10:12 -05003199 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003200 /*
3201 * FIXME: Once we have proper support for primary planes (and
3202 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003203 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003204 */
Matt Roperf4510a22014-04-01 15:22:40 -07003205 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003206 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003207 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003208 crtc->x,
3209 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003210 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003211 }
3212}
3213
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003214void intel_crtc_reset(struct intel_crtc *crtc)
3215{
3216 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3217
3218 if (!crtc->active)
3219 return;
3220
3221 intel_crtc_disable_planes(&crtc->base);
3222 dev_priv->display.crtc_disable(&crtc->base);
3223 dev_priv->display.crtc_enable(&crtc->base);
3224 intel_crtc_enable_planes(&crtc->base);
3225}
3226
Ville Syrjälä75147472014-11-24 18:28:11 +02003227void intel_prepare_reset(struct drm_device *dev)
3228{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003229 struct drm_i915_private *dev_priv = to_i915(dev);
3230 struct intel_crtc *crtc;
3231
Ville Syrjälä75147472014-11-24 18:28:11 +02003232 /* no reset support for gen2 */
3233 if (IS_GEN2(dev))
3234 return;
3235
3236 /* reset doesn't touch the display */
3237 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3238 return;
3239
3240 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003241
3242 /*
3243 * Disabling the crtcs gracefully seems nicer. Also the
3244 * g33 docs say we should at least disable all the planes.
3245 */
3246 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003247 if (!crtc->active)
3248 continue;
3249
3250 intel_crtc_disable_planes(&crtc->base);
3251 dev_priv->display.crtc_disable(&crtc->base);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003252 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003253}
3254
3255void intel_finish_reset(struct drm_device *dev)
3256{
3257 struct drm_i915_private *dev_priv = to_i915(dev);
3258
3259 /*
3260 * Flips in the rings will be nuked by the reset,
3261 * so complete all pending flips so that user space
3262 * will get its events and not get stuck.
3263 */
3264 intel_complete_page_flips(dev);
3265
3266 /* no reset support for gen2 */
3267 if (IS_GEN2(dev))
3268 return;
3269
3270 /* reset doesn't touch the display */
3271 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3272 /*
3273 * Flips in the rings have been nuked by the reset,
3274 * so update the base address of all primary
3275 * planes to the the last fb to make sure we're
3276 * showing the correct fb after a reset.
3277 */
3278 intel_update_primary_planes(dev);
3279 return;
3280 }
3281
3282 /*
3283 * The display has been reset as well,
3284 * so need a full re-initialization.
3285 */
3286 intel_runtime_pm_disable_interrupts(dev_priv);
3287 intel_runtime_pm_enable_interrupts(dev_priv);
3288
3289 intel_modeset_init_hw(dev);
3290
3291 spin_lock_irq(&dev_priv->irq_lock);
3292 if (dev_priv->display.hpd_irq_setup)
3293 dev_priv->display.hpd_irq_setup(dev);
3294 spin_unlock_irq(&dev_priv->irq_lock);
3295
3296 intel_modeset_setup_hw_state(dev, true);
3297
3298 intel_hpd_init(dev_priv);
3299
3300 drm_modeset_unlock_all(dev);
3301}
3302
Chris Wilson2e2f3512015-04-27 13:41:14 +01003303static void
Chris Wilson14667a42012-04-03 17:58:35 +01003304intel_finish_fb(struct drm_framebuffer *old_fb)
3305{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003306 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003307 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003308 bool was_interruptible = dev_priv->mm.interruptible;
3309 int ret;
3310
Chris Wilson14667a42012-04-03 17:58:35 +01003311 /* Big Hammer, we also need to ensure that any pending
3312 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3313 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003314 * framebuffer. Note that we rely on userspace rendering
3315 * into the buffer attached to the pipe they are waiting
3316 * on. If not, userspace generates a GPU hang with IPEHR
3317 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003318 *
3319 * This should only fail upon a hung GPU, in which case we
3320 * can safely continue.
3321 */
3322 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003323 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003324 dev_priv->mm.interruptible = was_interruptible;
3325
Chris Wilson2e2f3512015-04-27 13:41:14 +01003326 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003327}
3328
Chris Wilson7d5e3792014-03-04 13:15:08 +00003329static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3330{
3331 struct drm_device *dev = crtc->dev;
3332 struct drm_i915_private *dev_priv = dev->dev_private;
3333 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003334 bool pending;
3335
3336 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3337 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3338 return false;
3339
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003340 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003341 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003342 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003343
3344 return pending;
3345}
3346
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003347static void intel_update_pipe_size(struct intel_crtc *crtc)
3348{
3349 struct drm_device *dev = crtc->base.dev;
3350 struct drm_i915_private *dev_priv = dev->dev_private;
3351 const struct drm_display_mode *adjusted_mode;
3352
3353 if (!i915.fastboot)
3354 return;
3355
3356 /*
3357 * Update pipe size and adjust fitter if needed: the reason for this is
3358 * that in compute_mode_changes we check the native mode (not the pfit
3359 * mode) to see if we can flip rather than do a full mode set. In the
3360 * fastboot case, we'll flip, but if we don't update the pipesrc and
3361 * pfit state, we'll end up with a big fb scanned out into the wrong
3362 * sized surface.
3363 *
3364 * To fix this properly, we need to hoist the checks up into
3365 * compute_mode_changes (or above), check the actual pfit state and
3366 * whether the platform allows pfit disable with pipe active, and only
3367 * then update the pipesrc and pfit state, even on the flip path.
3368 */
3369
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003370 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003371
3372 I915_WRITE(PIPESRC(crtc->pipe),
3373 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3374 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003375 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003376 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3377 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003378 I915_WRITE(PF_CTL(crtc->pipe), 0);
3379 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3380 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3381 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003382 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3383 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003384}
3385
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003386static void intel_fdi_normal_train(struct drm_crtc *crtc)
3387{
3388 struct drm_device *dev = crtc->dev;
3389 struct drm_i915_private *dev_priv = dev->dev_private;
3390 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3391 int pipe = intel_crtc->pipe;
3392 u32 reg, temp;
3393
3394 /* enable normal train */
3395 reg = FDI_TX_CTL(pipe);
3396 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003397 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003398 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3399 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003400 } else {
3401 temp &= ~FDI_LINK_TRAIN_NONE;
3402 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003403 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003404 I915_WRITE(reg, temp);
3405
3406 reg = FDI_RX_CTL(pipe);
3407 temp = I915_READ(reg);
3408 if (HAS_PCH_CPT(dev)) {
3409 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3410 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3411 } else {
3412 temp &= ~FDI_LINK_TRAIN_NONE;
3413 temp |= FDI_LINK_TRAIN_NONE;
3414 }
3415 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3416
3417 /* wait one idle pattern time */
3418 POSTING_READ(reg);
3419 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003420
3421 /* IVB wants error correction enabled */
3422 if (IS_IVYBRIDGE(dev))
3423 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3424 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003425}
3426
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003427/* The FDI link training functions for ILK/Ibexpeak. */
3428static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3429{
3430 struct drm_device *dev = crtc->dev;
3431 struct drm_i915_private *dev_priv = dev->dev_private;
3432 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3433 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003434 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003435
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003436 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003437 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003438
Adam Jacksone1a44742010-06-25 15:32:14 -04003439 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3440 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003441 reg = FDI_RX_IMR(pipe);
3442 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003443 temp &= ~FDI_RX_SYMBOL_LOCK;
3444 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003445 I915_WRITE(reg, temp);
3446 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003447 udelay(150);
3448
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003449 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003450 reg = FDI_TX_CTL(pipe);
3451 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003452 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003453 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003454 temp &= ~FDI_LINK_TRAIN_NONE;
3455 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003456 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003457
Chris Wilson5eddb702010-09-11 13:48:45 +01003458 reg = FDI_RX_CTL(pipe);
3459 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003460 temp &= ~FDI_LINK_TRAIN_NONE;
3461 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003462 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3463
3464 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003465 udelay(150);
3466
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003467 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003468 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3469 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3470 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003471
Chris Wilson5eddb702010-09-11 13:48:45 +01003472 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003473 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003474 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003475 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3476
3477 if ((temp & FDI_RX_BIT_LOCK)) {
3478 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003479 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003480 break;
3481 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003482 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003483 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003484 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003485
3486 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003487 reg = FDI_TX_CTL(pipe);
3488 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003489 temp &= ~FDI_LINK_TRAIN_NONE;
3490 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003491 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003492
Chris Wilson5eddb702010-09-11 13:48:45 +01003493 reg = FDI_RX_CTL(pipe);
3494 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003495 temp &= ~FDI_LINK_TRAIN_NONE;
3496 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003497 I915_WRITE(reg, temp);
3498
3499 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003500 udelay(150);
3501
Chris Wilson5eddb702010-09-11 13:48:45 +01003502 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003503 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003504 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003505 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3506
3507 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003508 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003509 DRM_DEBUG_KMS("FDI train 2 done.\n");
3510 break;
3511 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003512 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003513 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003514 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003515
3516 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003517
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003518}
3519
Akshay Joshi0206e352011-08-16 15:34:10 -04003520static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003521 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3522 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3523 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3524 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3525};
3526
3527/* The FDI link training functions for SNB/Cougarpoint. */
3528static void gen6_fdi_link_train(struct drm_crtc *crtc)
3529{
3530 struct drm_device *dev = crtc->dev;
3531 struct drm_i915_private *dev_priv = dev->dev_private;
3532 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3533 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003534 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003535
Adam Jacksone1a44742010-06-25 15:32:14 -04003536 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3537 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003538 reg = FDI_RX_IMR(pipe);
3539 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003540 temp &= ~FDI_RX_SYMBOL_LOCK;
3541 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003542 I915_WRITE(reg, temp);
3543
3544 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003545 udelay(150);
3546
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003547 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003548 reg = FDI_TX_CTL(pipe);
3549 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003550 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003551 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003552 temp &= ~FDI_LINK_TRAIN_NONE;
3553 temp |= FDI_LINK_TRAIN_PATTERN_1;
3554 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3555 /* SNB-B */
3556 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003557 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003558
Daniel Vetterd74cf322012-10-26 10:58:13 +02003559 I915_WRITE(FDI_RX_MISC(pipe),
3560 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3561
Chris Wilson5eddb702010-09-11 13:48:45 +01003562 reg = FDI_RX_CTL(pipe);
3563 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003564 if (HAS_PCH_CPT(dev)) {
3565 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3566 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3567 } else {
3568 temp &= ~FDI_LINK_TRAIN_NONE;
3569 temp |= FDI_LINK_TRAIN_PATTERN_1;
3570 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003571 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3572
3573 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003574 udelay(150);
3575
Akshay Joshi0206e352011-08-16 15:34:10 -04003576 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003577 reg = FDI_TX_CTL(pipe);
3578 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003579 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3580 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003581 I915_WRITE(reg, temp);
3582
3583 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003584 udelay(500);
3585
Sean Paulfa37d392012-03-02 12:53:39 -05003586 for (retry = 0; retry < 5; retry++) {
3587 reg = FDI_RX_IIR(pipe);
3588 temp = I915_READ(reg);
3589 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3590 if (temp & FDI_RX_BIT_LOCK) {
3591 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3592 DRM_DEBUG_KMS("FDI train 1 done.\n");
3593 break;
3594 }
3595 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003596 }
Sean Paulfa37d392012-03-02 12:53:39 -05003597 if (retry < 5)
3598 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003599 }
3600 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003601 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003602
3603 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003604 reg = FDI_TX_CTL(pipe);
3605 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003606 temp &= ~FDI_LINK_TRAIN_NONE;
3607 temp |= FDI_LINK_TRAIN_PATTERN_2;
3608 if (IS_GEN6(dev)) {
3609 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3610 /* SNB-B */
3611 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3612 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003613 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003614
Chris Wilson5eddb702010-09-11 13:48:45 +01003615 reg = FDI_RX_CTL(pipe);
3616 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003617 if (HAS_PCH_CPT(dev)) {
3618 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3619 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3620 } else {
3621 temp &= ~FDI_LINK_TRAIN_NONE;
3622 temp |= FDI_LINK_TRAIN_PATTERN_2;
3623 }
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(150);
3628
Akshay Joshi0206e352011-08-16 15:34:10 -04003629 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003630 reg = FDI_TX_CTL(pipe);
3631 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003632 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3633 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003634 I915_WRITE(reg, temp);
3635
3636 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003637 udelay(500);
3638
Sean Paulfa37d392012-03-02 12:53:39 -05003639 for (retry = 0; retry < 5; retry++) {
3640 reg = FDI_RX_IIR(pipe);
3641 temp = I915_READ(reg);
3642 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3643 if (temp & FDI_RX_SYMBOL_LOCK) {
3644 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3645 DRM_DEBUG_KMS("FDI train 2 done.\n");
3646 break;
3647 }
3648 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003649 }
Sean Paulfa37d392012-03-02 12:53:39 -05003650 if (retry < 5)
3651 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003652 }
3653 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003654 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003655
3656 DRM_DEBUG_KMS("FDI train done.\n");
3657}
3658
Jesse Barnes357555c2011-04-28 15:09:55 -07003659/* Manual link training for Ivy Bridge A0 parts */
3660static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3661{
3662 struct drm_device *dev = crtc->dev;
3663 struct drm_i915_private *dev_priv = dev->dev_private;
3664 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3665 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003666 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003667
3668 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3669 for train result */
3670 reg = FDI_RX_IMR(pipe);
3671 temp = I915_READ(reg);
3672 temp &= ~FDI_RX_SYMBOL_LOCK;
3673 temp &= ~FDI_RX_BIT_LOCK;
3674 I915_WRITE(reg, temp);
3675
3676 POSTING_READ(reg);
3677 udelay(150);
3678
Daniel Vetter01a415f2012-10-27 15:58:40 +02003679 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3680 I915_READ(FDI_RX_IIR(pipe)));
3681
Jesse Barnes139ccd32013-08-19 11:04:55 -07003682 /* Try each vswing and preemphasis setting twice before moving on */
3683 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3684 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003685 reg = FDI_TX_CTL(pipe);
3686 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003687 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3688 temp &= ~FDI_TX_ENABLE;
3689 I915_WRITE(reg, temp);
3690
3691 reg = FDI_RX_CTL(pipe);
3692 temp = I915_READ(reg);
3693 temp &= ~FDI_LINK_TRAIN_AUTO;
3694 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3695 temp &= ~FDI_RX_ENABLE;
3696 I915_WRITE(reg, temp);
3697
3698 /* enable CPU FDI TX and PCH FDI RX */
3699 reg = FDI_TX_CTL(pipe);
3700 temp = I915_READ(reg);
3701 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003702 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003703 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003704 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003705 temp |= snb_b_fdi_train_param[j/2];
3706 temp |= FDI_COMPOSITE_SYNC;
3707 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3708
3709 I915_WRITE(FDI_RX_MISC(pipe),
3710 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3711
3712 reg = FDI_RX_CTL(pipe);
3713 temp = I915_READ(reg);
3714 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3715 temp |= FDI_COMPOSITE_SYNC;
3716 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3717
3718 POSTING_READ(reg);
3719 udelay(1); /* should be 0.5us */
3720
3721 for (i = 0; i < 4; i++) {
3722 reg = FDI_RX_IIR(pipe);
3723 temp = I915_READ(reg);
3724 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3725
3726 if (temp & FDI_RX_BIT_LOCK ||
3727 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3728 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3729 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3730 i);
3731 break;
3732 }
3733 udelay(1); /* should be 0.5us */
3734 }
3735 if (i == 4) {
3736 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3737 continue;
3738 }
3739
3740 /* Train 2 */
3741 reg = FDI_TX_CTL(pipe);
3742 temp = I915_READ(reg);
3743 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3744 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3745 I915_WRITE(reg, temp);
3746
3747 reg = FDI_RX_CTL(pipe);
3748 temp = I915_READ(reg);
3749 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3750 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003751 I915_WRITE(reg, temp);
3752
3753 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003754 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003755
Jesse Barnes139ccd32013-08-19 11:04:55 -07003756 for (i = 0; i < 4; i++) {
3757 reg = FDI_RX_IIR(pipe);
3758 temp = I915_READ(reg);
3759 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003760
Jesse Barnes139ccd32013-08-19 11:04:55 -07003761 if (temp & FDI_RX_SYMBOL_LOCK ||
3762 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3763 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3764 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3765 i);
3766 goto train_done;
3767 }
3768 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003769 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003770 if (i == 4)
3771 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003772 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003773
Jesse Barnes139ccd32013-08-19 11:04:55 -07003774train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003775 DRM_DEBUG_KMS("FDI train done.\n");
3776}
3777
Daniel Vetter88cefb62012-08-12 19:27:14 +02003778static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003779{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003780 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003781 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003782 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003783 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003784
Jesse Barnesc64e3112010-09-10 11:27:03 -07003785
Jesse Barnes0e23b992010-09-10 11:10:00 -07003786 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003787 reg = FDI_RX_CTL(pipe);
3788 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003789 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003790 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003791 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003792 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3793
3794 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003795 udelay(200);
3796
3797 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003798 temp = I915_READ(reg);
3799 I915_WRITE(reg, temp | FDI_PCDCLK);
3800
3801 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003802 udelay(200);
3803
Paulo Zanoni20749732012-11-23 15:30:38 -02003804 /* Enable CPU FDI TX PLL, always on for Ironlake */
3805 reg = FDI_TX_CTL(pipe);
3806 temp = I915_READ(reg);
3807 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3808 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003809
Paulo Zanoni20749732012-11-23 15:30:38 -02003810 POSTING_READ(reg);
3811 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003812 }
3813}
3814
Daniel Vetter88cefb62012-08-12 19:27:14 +02003815static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3816{
3817 struct drm_device *dev = intel_crtc->base.dev;
3818 struct drm_i915_private *dev_priv = dev->dev_private;
3819 int pipe = intel_crtc->pipe;
3820 u32 reg, temp;
3821
3822 /* Switch from PCDclk to Rawclk */
3823 reg = FDI_RX_CTL(pipe);
3824 temp = I915_READ(reg);
3825 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3826
3827 /* Disable CPU FDI TX PLL */
3828 reg = FDI_TX_CTL(pipe);
3829 temp = I915_READ(reg);
3830 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3831
3832 POSTING_READ(reg);
3833 udelay(100);
3834
3835 reg = FDI_RX_CTL(pipe);
3836 temp = I915_READ(reg);
3837 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3838
3839 /* Wait for the clocks to turn off. */
3840 POSTING_READ(reg);
3841 udelay(100);
3842}
3843
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003844static void ironlake_fdi_disable(struct drm_crtc *crtc)
3845{
3846 struct drm_device *dev = crtc->dev;
3847 struct drm_i915_private *dev_priv = dev->dev_private;
3848 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3849 int pipe = intel_crtc->pipe;
3850 u32 reg, temp;
3851
3852 /* disable CPU FDI tx and PCH FDI rx */
3853 reg = FDI_TX_CTL(pipe);
3854 temp = I915_READ(reg);
3855 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3856 POSTING_READ(reg);
3857
3858 reg = FDI_RX_CTL(pipe);
3859 temp = I915_READ(reg);
3860 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003861 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003862 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3863
3864 POSTING_READ(reg);
3865 udelay(100);
3866
3867 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003868 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003869 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003870
3871 /* still set train pattern 1 */
3872 reg = FDI_TX_CTL(pipe);
3873 temp = I915_READ(reg);
3874 temp &= ~FDI_LINK_TRAIN_NONE;
3875 temp |= FDI_LINK_TRAIN_PATTERN_1;
3876 I915_WRITE(reg, temp);
3877
3878 reg = FDI_RX_CTL(pipe);
3879 temp = I915_READ(reg);
3880 if (HAS_PCH_CPT(dev)) {
3881 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3882 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3883 } else {
3884 temp &= ~FDI_LINK_TRAIN_NONE;
3885 temp |= FDI_LINK_TRAIN_PATTERN_1;
3886 }
3887 /* BPC in FDI rx is consistent with that in PIPECONF */
3888 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003889 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003890 I915_WRITE(reg, temp);
3891
3892 POSTING_READ(reg);
3893 udelay(100);
3894}
3895
Chris Wilson5dce5b932014-01-20 10:17:36 +00003896bool intel_has_pending_fb_unpin(struct drm_device *dev)
3897{
3898 struct intel_crtc *crtc;
3899
3900 /* Note that we don't need to be called with mode_config.lock here
3901 * as our list of CRTC objects is static for the lifetime of the
3902 * device and so cannot disappear as we iterate. Similarly, we can
3903 * happily treat the predicates as racy, atomic checks as userspace
3904 * cannot claim and pin a new fb without at least acquring the
3905 * struct_mutex and so serialising with us.
3906 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003907 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003908 if (atomic_read(&crtc->unpin_work_count) == 0)
3909 continue;
3910
3911 if (crtc->unpin_work)
3912 intel_wait_for_vblank(dev, crtc->pipe);
3913
3914 return true;
3915 }
3916
3917 return false;
3918}
3919
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003920static void page_flip_completed(struct intel_crtc *intel_crtc)
3921{
3922 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3923 struct intel_unpin_work *work = intel_crtc->unpin_work;
3924
3925 /* ensure that the unpin work is consistent wrt ->pending. */
3926 smp_rmb();
3927 intel_crtc->unpin_work = NULL;
3928
3929 if (work->event)
3930 drm_send_vblank_event(intel_crtc->base.dev,
3931 intel_crtc->pipe,
3932 work->event);
3933
3934 drm_crtc_vblank_put(&intel_crtc->base);
3935
3936 wake_up_all(&dev_priv->pending_flip_queue);
3937 queue_work(dev_priv->wq, &work->work);
3938
3939 trace_i915_flip_complete(intel_crtc->plane,
3940 work->pending_flip_obj);
3941}
3942
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003943void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003944{
Chris Wilson0f911282012-04-17 10:05:38 +01003945 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003946 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003947
Daniel Vetter2c10d572012-12-20 21:24:07 +01003948 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003949 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3950 !intel_crtc_has_pending_flip(crtc),
3951 60*HZ) == 0)) {
3952 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003953
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003954 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003955 if (intel_crtc->unpin_work) {
3956 WARN_ONCE(1, "Removing stuck page flip\n");
3957 page_flip_completed(intel_crtc);
3958 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003959 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003960 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003961
Chris Wilson975d5682014-08-20 13:13:34 +01003962 if (crtc->primary->fb) {
3963 mutex_lock(&dev->struct_mutex);
3964 intel_finish_fb(crtc->primary->fb);
3965 mutex_unlock(&dev->struct_mutex);
3966 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003967}
3968
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003969/* Program iCLKIP clock to the desired frequency */
3970static void lpt_program_iclkip(struct drm_crtc *crtc)
3971{
3972 struct drm_device *dev = crtc->dev;
3973 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003974 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003975 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3976 u32 temp;
3977
Daniel Vetter09153002012-12-12 14:06:44 +01003978 mutex_lock(&dev_priv->dpio_lock);
3979
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003980 /* It is necessary to ungate the pixclk gate prior to programming
3981 * the divisors, and gate it back when it is done.
3982 */
3983 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3984
3985 /* Disable SSCCTL */
3986 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003987 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3988 SBI_SSCCTL_DISABLE,
3989 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003990
3991 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003992 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003993 auxdiv = 1;
3994 divsel = 0x41;
3995 phaseinc = 0x20;
3996 } else {
3997 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003998 * but the adjusted_mode->crtc_clock in in KHz. To get the
3999 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004000 * convert the virtual clock precision to KHz here for higher
4001 * precision.
4002 */
4003 u32 iclk_virtual_root_freq = 172800 * 1000;
4004 u32 iclk_pi_range = 64;
4005 u32 desired_divisor, msb_divisor_value, pi_value;
4006
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03004007 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004008 msb_divisor_value = desired_divisor / iclk_pi_range;
4009 pi_value = desired_divisor % iclk_pi_range;
4010
4011 auxdiv = 0;
4012 divsel = msb_divisor_value - 2;
4013 phaseinc = pi_value;
4014 }
4015
4016 /* This should not happen with any sane values */
4017 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4018 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4019 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4020 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4021
4022 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 +03004023 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004024 auxdiv,
4025 divsel,
4026 phasedir,
4027 phaseinc);
4028
4029 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004030 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004031 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4032 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4033 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4034 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4035 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4036 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004037 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004038
4039 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004040 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004041 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4042 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004043 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004044
4045 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004046 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004047 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004048 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004049
4050 /* Wait for initialization time */
4051 udelay(24);
4052
4053 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004054
4055 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004056}
4057
Daniel Vetter275f01b22013-05-03 11:49:47 +02004058static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4059 enum pipe pch_transcoder)
4060{
4061 struct drm_device *dev = crtc->base.dev;
4062 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004063 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004064
4065 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4066 I915_READ(HTOTAL(cpu_transcoder)));
4067 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4068 I915_READ(HBLANK(cpu_transcoder)));
4069 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4070 I915_READ(HSYNC(cpu_transcoder)));
4071
4072 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4073 I915_READ(VTOTAL(cpu_transcoder)));
4074 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4075 I915_READ(VBLANK(cpu_transcoder)));
4076 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4077 I915_READ(VSYNC(cpu_transcoder)));
4078 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4079 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4080}
4081
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004082static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004083{
4084 struct drm_i915_private *dev_priv = dev->dev_private;
4085 uint32_t temp;
4086
4087 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004088 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004089 return;
4090
4091 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4092 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4093
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004094 temp &= ~FDI_BC_BIFURCATION_SELECT;
4095 if (enable)
4096 temp |= FDI_BC_BIFURCATION_SELECT;
4097
4098 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004099 I915_WRITE(SOUTH_CHICKEN1, temp);
4100 POSTING_READ(SOUTH_CHICKEN1);
4101}
4102
4103static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4104{
4105 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004106
4107 switch (intel_crtc->pipe) {
4108 case PIPE_A:
4109 break;
4110 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004111 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004112 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004113 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004114 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004115
4116 break;
4117 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004118 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004119
4120 break;
4121 default:
4122 BUG();
4123 }
4124}
4125
Jesse Barnesf67a5592011-01-05 10:31:48 -08004126/*
4127 * Enable PCH resources required for PCH ports:
4128 * - PCH PLLs
4129 * - FDI training & RX/TX
4130 * - update transcoder timings
4131 * - DP transcoding bits
4132 * - transcoder
4133 */
4134static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004135{
4136 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004137 struct drm_i915_private *dev_priv = dev->dev_private;
4138 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4139 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004140 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004141
Daniel Vetterab9412b2013-05-03 11:49:46 +02004142 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004143
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004144 if (IS_IVYBRIDGE(dev))
4145 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4146
Daniel Vettercd986ab2012-10-26 10:58:12 +02004147 /* Write the TU size bits before fdi link training, so that error
4148 * detection works. */
4149 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4150 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4151
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004152 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004153 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004154
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004155 /* We need to program the right clock selection before writing the pixel
4156 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004157 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004158 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004159
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004160 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004161 temp |= TRANS_DPLL_ENABLE(pipe);
4162 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004163 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004164 temp |= sel;
4165 else
4166 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004167 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004168 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004169
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004170 /* XXX: pch pll's can be enabled any time before we enable the PCH
4171 * transcoder, and we actually should do this to not upset any PCH
4172 * transcoder that already use the clock when we share it.
4173 *
4174 * Note that enable_shared_dpll tries to do the right thing, but
4175 * get_shared_dpll unconditionally resets the pll - we need that to have
4176 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004177 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004178
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004179 /* set transcoder timing, panel must allow it */
4180 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004181 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004182
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004183 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004184
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004185 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004186 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004187 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004188 reg = TRANS_DP_CTL(pipe);
4189 temp = I915_READ(reg);
4190 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004191 TRANS_DP_SYNC_MASK |
4192 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01004193 temp |= (TRANS_DP_OUTPUT_ENABLE |
4194 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004195 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004196
4197 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004198 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004199 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004200 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004201
4202 switch (intel_trans_dp_port_sel(crtc)) {
4203 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004204 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004205 break;
4206 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004207 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004208 break;
4209 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004210 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004211 break;
4212 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004213 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004214 }
4215
Chris Wilson5eddb702010-09-11 13:48:45 +01004216 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004217 }
4218
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004219 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004220}
4221
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004222static void lpt_pch_enable(struct drm_crtc *crtc)
4223{
4224 struct drm_device *dev = crtc->dev;
4225 struct drm_i915_private *dev_priv = dev->dev_private;
4226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004227 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004228
Daniel Vetterab9412b2013-05-03 11:49:46 +02004229 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004230
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004231 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004232
Paulo Zanoni0540e482012-10-31 18:12:40 -02004233 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004234 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004235
Paulo Zanoni937bb612012-10-31 18:12:47 -02004236 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004237}
4238
Daniel Vetter716c2e52014-06-25 22:02:02 +03004239void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004240{
Daniel Vettere2b78262013-06-07 23:10:03 +02004241 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004242
4243 if (pll == NULL)
4244 return;
4245
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004246 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004247 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004248 return;
4249 }
4250
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004251 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4252 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004253 WARN_ON(pll->on);
4254 WARN_ON(pll->active);
4255 }
4256
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004257 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004258}
4259
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004260struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4261 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004262{
Daniel Vettere2b78262013-06-07 23:10:03 +02004263 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004264 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004265 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004266
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004267 if (HAS_PCH_IBX(dev_priv->dev)) {
4268 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004269 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004270 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004271
Daniel Vetter46edb022013-06-05 13:34:12 +02004272 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4273 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004274
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004275 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004276
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004277 goto found;
4278 }
4279
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304280 if (IS_BROXTON(dev_priv->dev)) {
4281 /* PLL is attached to port in bxt */
4282 struct intel_encoder *encoder;
4283 struct intel_digital_port *intel_dig_port;
4284
4285 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4286 if (WARN_ON(!encoder))
4287 return NULL;
4288
4289 intel_dig_port = enc_to_dig_port(&encoder->base);
4290 /* 1:1 mapping between ports and PLLs */
4291 i = (enum intel_dpll_id)intel_dig_port->port;
4292 pll = &dev_priv->shared_dplls[i];
4293 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4294 crtc->base.base.id, pll->name);
4295 WARN_ON(pll->new_config->crtc_mask);
4296
4297 goto found;
4298 }
4299
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004300 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4301 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004302
4303 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004304 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004305 continue;
4306
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004307 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004308 &pll->new_config->hw_state,
4309 sizeof(pll->new_config->hw_state)) == 0) {
4310 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004311 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004312 pll->new_config->crtc_mask,
4313 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004314 goto found;
4315 }
4316 }
4317
4318 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004319 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4320 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004321 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004322 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4323 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004324 goto found;
4325 }
4326 }
4327
4328 return NULL;
4329
4330found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004331 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004332 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004333
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004334 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004335 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4336 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004337
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004338 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004339
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004340 return pll;
4341}
4342
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004343/**
4344 * intel_shared_dpll_start_config - start a new PLL staged config
4345 * @dev_priv: DRM device
4346 * @clear_pipes: mask of pipes that will have their PLLs freed
4347 *
4348 * Starts a new PLL staged config, copying the current config but
4349 * releasing the references of pipes specified in clear_pipes.
4350 */
4351static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4352 unsigned clear_pipes)
4353{
4354 struct intel_shared_dpll *pll;
4355 enum intel_dpll_id i;
4356
4357 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4358 pll = &dev_priv->shared_dplls[i];
4359
4360 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4361 GFP_KERNEL);
4362 if (!pll->new_config)
4363 goto cleanup;
4364
4365 pll->new_config->crtc_mask &= ~clear_pipes;
4366 }
4367
4368 return 0;
4369
4370cleanup:
4371 while (--i >= 0) {
4372 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004373 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004374 pll->new_config = NULL;
4375 }
4376
4377 return -ENOMEM;
4378}
4379
4380static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4381{
4382 struct intel_shared_dpll *pll;
4383 enum intel_dpll_id i;
4384
4385 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4386 pll = &dev_priv->shared_dplls[i];
4387
4388 WARN_ON(pll->new_config == &pll->config);
4389
4390 pll->config = *pll->new_config;
4391 kfree(pll->new_config);
4392 pll->new_config = NULL;
4393 }
4394}
4395
4396static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4397{
4398 struct intel_shared_dpll *pll;
4399 enum intel_dpll_id i;
4400
4401 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4402 pll = &dev_priv->shared_dplls[i];
4403
4404 WARN_ON(pll->new_config == &pll->config);
4405
4406 kfree(pll->new_config);
4407 pll->new_config = NULL;
4408 }
4409}
4410
Daniel Vettera1520312013-05-03 11:49:50 +02004411static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004412{
4413 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004414 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004415 u32 temp;
4416
4417 temp = I915_READ(dslreg);
4418 udelay(500);
4419 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004420 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004421 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004422 }
4423}
4424
Chandra Kondurua1b22782015-04-07 15:28:45 -07004425/**
4426 * skl_update_scaler_users - Stages update to crtc's scaler state
4427 * @intel_crtc: crtc
4428 * @crtc_state: crtc_state
4429 * @plane: plane (NULL indicates crtc is requesting update)
4430 * @plane_state: plane's state
4431 * @force_detach: request unconditional detachment of scaler
4432 *
4433 * This function updates scaler state for requested plane or crtc.
4434 * To request scaler usage update for a plane, caller shall pass plane pointer.
4435 * To request scaler usage update for crtc, caller shall pass plane pointer
4436 * as NULL.
4437 *
4438 * Return
4439 * 0 - scaler_usage updated successfully
4440 * error - requested scaling cannot be supported or other error condition
4441 */
4442int
4443skl_update_scaler_users(
4444 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4445 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4446 int force_detach)
4447{
4448 int need_scaling;
4449 int idx;
4450 int src_w, src_h, dst_w, dst_h;
4451 int *scaler_id;
4452 struct drm_framebuffer *fb;
4453 struct intel_crtc_scaler_state *scaler_state;
Chandra Konduru6156a452015-04-27 13:48:39 -07004454 unsigned int rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004455
4456 if (!intel_crtc || !crtc_state)
4457 return 0;
4458
4459 scaler_state = &crtc_state->scaler_state;
4460
4461 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4462 fb = intel_plane ? plane_state->base.fb : NULL;
4463
4464 if (intel_plane) {
4465 src_w = drm_rect_width(&plane_state->src) >> 16;
4466 src_h = drm_rect_height(&plane_state->src) >> 16;
4467 dst_w = drm_rect_width(&plane_state->dst);
4468 dst_h = drm_rect_height(&plane_state->dst);
4469 scaler_id = &plane_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004470 rotation = plane_state->base.rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004471 } else {
4472 struct drm_display_mode *adjusted_mode =
4473 &crtc_state->base.adjusted_mode;
4474 src_w = crtc_state->pipe_src_w;
4475 src_h = crtc_state->pipe_src_h;
4476 dst_w = adjusted_mode->hdisplay;
4477 dst_h = adjusted_mode->vdisplay;
4478 scaler_id = &scaler_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004479 rotation = DRM_ROTATE_0;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004480 }
Chandra Konduru6156a452015-04-27 13:48:39 -07004481
4482 need_scaling = intel_rotation_90_or_270(rotation) ?
4483 (src_h != dst_w || src_w != dst_h):
4484 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004485
4486 /*
4487 * if plane is being disabled or scaler is no more required or force detach
4488 * - free scaler binded to this plane/crtc
4489 * - in order to do this, update crtc->scaler_usage
4490 *
4491 * Here scaler state in crtc_state is set free so that
4492 * scaler can be assigned to other user. Actual register
4493 * update to free the scaler is done in plane/panel-fit programming.
4494 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4495 */
4496 if (force_detach || !need_scaling || (intel_plane &&
4497 (!fb || !plane_state->visible))) {
4498 if (*scaler_id >= 0) {
4499 scaler_state->scaler_users &= ~(1 << idx);
4500 scaler_state->scalers[*scaler_id].in_use = 0;
4501
4502 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4503 "crtc_state = %p scaler_users = 0x%x\n",
4504 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4505 intel_plane ? intel_plane->base.base.id :
4506 intel_crtc->base.base.id, crtc_state,
4507 scaler_state->scaler_users);
4508 *scaler_id = -1;
4509 }
4510 return 0;
4511 }
4512
4513 /* range checks */
4514 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4515 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4516
4517 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4518 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4519 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4520 "size is out of scaler range\n",
4521 intel_plane ? "PLANE" : "CRTC",
4522 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4523 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4524 return -EINVAL;
4525 }
4526
4527 /* check colorkey */
4528 if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
4529 DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
4530 intel_plane->base.base.id);
4531 return -EINVAL;
4532 }
4533
4534 /* Check src format */
4535 if (intel_plane) {
4536 switch (fb->pixel_format) {
4537 case DRM_FORMAT_RGB565:
4538 case DRM_FORMAT_XBGR8888:
4539 case DRM_FORMAT_XRGB8888:
4540 case DRM_FORMAT_ABGR8888:
4541 case DRM_FORMAT_ARGB8888:
4542 case DRM_FORMAT_XRGB2101010:
4543 case DRM_FORMAT_ARGB2101010:
4544 case DRM_FORMAT_XBGR2101010:
4545 case DRM_FORMAT_ABGR2101010:
4546 case DRM_FORMAT_YUYV:
4547 case DRM_FORMAT_YVYU:
4548 case DRM_FORMAT_UYVY:
4549 case DRM_FORMAT_VYUY:
4550 break;
4551 default:
4552 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4553 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4554 return -EINVAL;
4555 }
4556 }
4557
4558 /* mark this plane as a scaler user in crtc_state */
4559 scaler_state->scaler_users |= (1 << idx);
4560 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4561 "crtc_state = %p scaler_users = 0x%x\n",
4562 intel_plane ? "PLANE" : "CRTC",
4563 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4564 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4565 return 0;
4566}
4567
4568static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004569{
4570 struct drm_device *dev = crtc->base.dev;
4571 struct drm_i915_private *dev_priv = dev->dev_private;
4572 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004573 struct intel_crtc_scaler_state *scaler_state =
4574 &crtc->config->scaler_state;
4575
4576 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4577
4578 /* To update pfit, first update scaler state */
4579 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4580 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4581 skl_detach_scalers(crtc);
4582 if (!enable)
4583 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004584
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004585 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004586 int id;
4587
4588 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4589 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4590 return;
4591 }
4592
4593 id = scaler_state->scaler_id;
4594 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4595 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4596 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4597 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4598
4599 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004600 }
4601}
4602
Jesse Barnesb074cec2013-04-25 12:55:02 -07004603static void ironlake_pfit_enable(struct intel_crtc *crtc)
4604{
4605 struct drm_device *dev = crtc->base.dev;
4606 struct drm_i915_private *dev_priv = dev->dev_private;
4607 int pipe = crtc->pipe;
4608
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004609 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004610 /* Force use of hard-coded filter coefficients
4611 * as some pre-programmed values are broken,
4612 * e.g. x201.
4613 */
4614 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4615 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4616 PF_PIPE_SEL_IVB(pipe));
4617 else
4618 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004619 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4620 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004621 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004622}
4623
Matt Roper4a3b8762014-12-23 10:41:51 -08004624static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004625{
4626 struct drm_device *dev = crtc->dev;
4627 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004628 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004629 struct intel_plane *intel_plane;
4630
Matt Roperaf2b6532014-04-01 15:22:32 -07004631 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4632 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004633 if (intel_plane->pipe == pipe)
4634 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004635 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004636}
4637
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004638void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004639{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004640 struct drm_device *dev = crtc->base.dev;
4641 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004642
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004643 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004644 return;
4645
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004646 /* We can only enable IPS after we enable a plane and wait for a vblank */
4647 intel_wait_for_vblank(dev, crtc->pipe);
4648
Paulo Zanonid77e4532013-09-24 13:52:55 -03004649 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004650 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004651 mutex_lock(&dev_priv->rps.hw_lock);
4652 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4653 mutex_unlock(&dev_priv->rps.hw_lock);
4654 /* Quoting Art Runyan: "its not safe to expect any particular
4655 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004656 * mailbox." Moreover, the mailbox may return a bogus state,
4657 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004658 */
4659 } else {
4660 I915_WRITE(IPS_CTL, IPS_ENABLE);
4661 /* The bit only becomes 1 in the next vblank, so this wait here
4662 * is essentially intel_wait_for_vblank. If we don't have this
4663 * and don't wait for vblanks until the end of crtc_enable, then
4664 * the HW state readout code will complain that the expected
4665 * IPS_CTL value is not the one we read. */
4666 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4667 DRM_ERROR("Timed out waiting for IPS enable\n");
4668 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004669}
4670
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004671void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004672{
4673 struct drm_device *dev = crtc->base.dev;
4674 struct drm_i915_private *dev_priv = dev->dev_private;
4675
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004676 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004677 return;
4678
4679 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004680 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004681 mutex_lock(&dev_priv->rps.hw_lock);
4682 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4683 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004684 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4685 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4686 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004687 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004688 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004689 POSTING_READ(IPS_CTL);
4690 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004691
4692 /* We need to wait for a vblank before we can disable the plane. */
4693 intel_wait_for_vblank(dev, crtc->pipe);
4694}
4695
4696/** Loads the palette/gamma unit for the CRTC with the prepared values */
4697static void intel_crtc_load_lut(struct drm_crtc *crtc)
4698{
4699 struct drm_device *dev = crtc->dev;
4700 struct drm_i915_private *dev_priv = dev->dev_private;
4701 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4702 enum pipe pipe = intel_crtc->pipe;
4703 int palreg = PALETTE(pipe);
4704 int i;
4705 bool reenable_ips = false;
4706
4707 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004708 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004709 return;
4710
Imre Deak50360402015-01-16 00:55:16 -08004711 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004712 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004713 assert_dsi_pll_enabled(dev_priv);
4714 else
4715 assert_pll_enabled(dev_priv, pipe);
4716 }
4717
4718 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304719 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004720 palreg = LGC_PALETTE(pipe);
4721
4722 /* Workaround : Do not read or write the pipe palette/gamma data while
4723 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4724 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004725 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004726 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4727 GAMMA_MODE_MODE_SPLIT)) {
4728 hsw_disable_ips(intel_crtc);
4729 reenable_ips = true;
4730 }
4731
4732 for (i = 0; i < 256; i++) {
4733 I915_WRITE(palreg + 4 * i,
4734 (intel_crtc->lut_r[i] << 16) |
4735 (intel_crtc->lut_g[i] << 8) |
4736 intel_crtc->lut_b[i]);
4737 }
4738
4739 if (reenable_ips)
4740 hsw_enable_ips(intel_crtc);
4741}
4742
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004743static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004744{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004745 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004746 struct drm_device *dev = intel_crtc->base.dev;
4747 struct drm_i915_private *dev_priv = dev->dev_private;
4748
4749 mutex_lock(&dev->struct_mutex);
4750 dev_priv->mm.interruptible = false;
4751 (void) intel_overlay_switch_off(intel_crtc->overlay);
4752 dev_priv->mm.interruptible = true;
4753 mutex_unlock(&dev->struct_mutex);
4754 }
4755
4756 /* Let userspace switch the overlay on again. In most cases userspace
4757 * has to recompute where to put it anyway.
4758 */
4759}
4760
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004761/**
4762 * intel_post_enable_primary - Perform operations after enabling primary plane
4763 * @crtc: the CRTC whose primary plane was just enabled
4764 *
4765 * Performs potentially sleeping operations that must be done after the primary
4766 * plane is enabled, such as updating FBC and IPS. Note that this may be
4767 * called due to an explicit primary plane update, or due to an implicit
4768 * re-enable that is caused when a sprite plane is updated to no longer
4769 * completely hide the primary plane.
4770 */
4771static void
4772intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004773{
4774 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004775 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4777 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004778
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004779 /*
4780 * BDW signals flip done immediately if the plane
4781 * is disabled, even if the plane enable is already
4782 * armed to occur at the next vblank :(
4783 */
4784 if (IS_BROADWELL(dev))
4785 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004786
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004787 /*
4788 * FIXME IPS should be fine as long as one plane is
4789 * enabled, but in practice it seems to have problems
4790 * when going from primary only to sprite only and vice
4791 * versa.
4792 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004793 hsw_enable_ips(intel_crtc);
4794
4795 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004796 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004797 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004798
4799 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004800 * Gen2 reports pipe underruns whenever all planes are disabled.
4801 * So don't enable underrun reporting before at least some planes
4802 * are enabled.
4803 * FIXME: Need to fix the logic to work when we turn off all planes
4804 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004805 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004806 if (IS_GEN2(dev))
4807 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4808
4809 /* Underruns don't raise interrupts, so check manually. */
4810 if (HAS_GMCH_DISPLAY(dev))
4811 i9xx_check_fifo_underruns(dev_priv);
4812}
4813
4814/**
4815 * intel_pre_disable_primary - Perform operations before disabling primary plane
4816 * @crtc: the CRTC whose primary plane is to be disabled
4817 *
4818 * Performs potentially sleeping operations that must be done before the
4819 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4820 * be called due to an explicit primary plane update, or due to an implicit
4821 * disable that is caused when a sprite plane completely hides the primary
4822 * plane.
4823 */
4824static void
4825intel_pre_disable_primary(struct drm_crtc *crtc)
4826{
4827 struct drm_device *dev = crtc->dev;
4828 struct drm_i915_private *dev_priv = dev->dev_private;
4829 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4830 int pipe = intel_crtc->pipe;
4831
4832 /*
4833 * Gen2 reports pipe underruns whenever all planes are disabled.
4834 * So diasble underrun reporting before all the planes get disabled.
4835 * FIXME: Need to fix the logic to work when we turn off all planes
4836 * but leave the pipe running.
4837 */
4838 if (IS_GEN2(dev))
4839 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4840
4841 /*
4842 * Vblank time updates from the shadow to live plane control register
4843 * are blocked if the memory self-refresh mode is active at that
4844 * moment. So to make sure the plane gets truly disabled, disable
4845 * first the self-refresh mode. The self-refresh enable bit in turn
4846 * will be checked/applied by the HW only at the next frame start
4847 * event which is after the vblank start event, so we need to have a
4848 * wait-for-vblank between disabling the plane and the pipe.
4849 */
4850 if (HAS_GMCH_DISPLAY(dev))
4851 intel_set_memory_cxsr(dev_priv, false);
4852
4853 mutex_lock(&dev->struct_mutex);
4854 if (dev_priv->fbc.crtc == intel_crtc)
4855 intel_fbc_disable(dev);
4856 mutex_unlock(&dev->struct_mutex);
4857
4858 /*
4859 * FIXME IPS should be fine as long as one plane is
4860 * enabled, but in practice it seems to have problems
4861 * when going from primary only to sprite only and vice
4862 * versa.
4863 */
4864 hsw_disable_ips(intel_crtc);
4865}
4866
4867static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4868{
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004869 intel_enable_primary_hw_plane(crtc->primary, crtc);
4870 intel_enable_sprite_planes(crtc);
4871 intel_crtc_update_cursor(crtc, true);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004872
4873 intel_post_enable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004874}
4875
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004876static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004877{
4878 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004879 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004880 struct intel_plane *intel_plane;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004881 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004882
4883 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004884
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004885 intel_pre_disable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004886
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004887 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004888 for_each_intel_plane(dev, intel_plane) {
4889 if (intel_plane->pipe == pipe) {
4890 struct drm_crtc *from = intel_plane->base.crtc;
4891
4892 intel_plane->disable_plane(&intel_plane->base,
4893 from ?: crtc, true);
4894 }
4895 }
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004896
Daniel Vetterf99d7062014-06-19 16:01:59 +02004897 /*
4898 * FIXME: Once we grow proper nuclear flip support out of this we need
4899 * to compute the mask of flip planes precisely. For the time being
4900 * consider this a flip to a NULL plane.
4901 */
4902 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004903}
4904
Jesse Barnesf67a5592011-01-05 10:31:48 -08004905static void ironlake_crtc_enable(struct drm_crtc *crtc)
4906{
4907 struct drm_device *dev = crtc->dev;
4908 struct drm_i915_private *dev_priv = dev->dev_private;
4909 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004910 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004911 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004912
Matt Roper83d65732015-02-25 13:12:16 -08004913 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004914
Jesse Barnesf67a5592011-01-05 10:31:48 -08004915 if (intel_crtc->active)
4916 return;
4917
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004918 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004919 intel_prepare_shared_dpll(intel_crtc);
4920
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004921 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304922 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004923
4924 intel_set_pipe_timings(intel_crtc);
4925
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004926 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004927 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004928 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004929 }
4930
4931 ironlake_set_pipeconf(crtc);
4932
Jesse Barnesf67a5592011-01-05 10:31:48 -08004933 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004934
Daniel Vettera72e4c92014-09-30 10:56:47 +02004935 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4936 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004937
Daniel Vetterf6736a12013-06-05 13:34:30 +02004938 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004939 if (encoder->pre_enable)
4940 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004941
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004942 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004943 /* Note: FDI PLL enabling _must_ be done before we enable the
4944 * cpu pipes, hence this is separate from all the other fdi/pch
4945 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004946 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004947 } else {
4948 assert_fdi_tx_disabled(dev_priv, pipe);
4949 assert_fdi_rx_disabled(dev_priv, pipe);
4950 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004951
Jesse Barnesb074cec2013-04-25 12:55:02 -07004952 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004953
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004954 /*
4955 * On ILK+ LUT must be loaded before the pipe is running but with
4956 * clocks enabled
4957 */
4958 intel_crtc_load_lut(crtc);
4959
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004960 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004961 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004962
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004963 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004964 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004965
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004966 assert_vblank_disabled(crtc);
4967 drm_crtc_vblank_on(crtc);
4968
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004969 for_each_encoder_on_crtc(dev, crtc, encoder)
4970 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004971
4972 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004973 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004974}
4975
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004976/* IPS only exists on ULT machines and is tied to pipe A. */
4977static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4978{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004979 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004980}
4981
Paulo Zanonie4916942013-09-20 16:21:19 -03004982/*
4983 * This implements the workaround described in the "notes" section of the mode
4984 * set sequence documentation. When going from no pipes or single pipe to
4985 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4986 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4987 */
4988static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4989{
4990 struct drm_device *dev = crtc->base.dev;
4991 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4992
4993 /* We want to get the other_active_crtc only if there's only 1 other
4994 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004995 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004996 if (!crtc_it->active || crtc_it == crtc)
4997 continue;
4998
4999 if (other_active_crtc)
5000 return;
5001
5002 other_active_crtc = crtc_it;
5003 }
5004 if (!other_active_crtc)
5005 return;
5006
5007 intel_wait_for_vblank(dev, other_active_crtc->pipe);
5008 intel_wait_for_vblank(dev, other_active_crtc->pipe);
5009}
5010
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005011static void haswell_crtc_enable(struct drm_crtc *crtc)
5012{
5013 struct drm_device *dev = crtc->dev;
5014 struct drm_i915_private *dev_priv = dev->dev_private;
5015 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5016 struct intel_encoder *encoder;
5017 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005018
Matt Roper83d65732015-02-25 13:12:16 -08005019 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005020
5021 if (intel_crtc->active)
5022 return;
5023
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005024 if (intel_crtc_to_shared_dpll(intel_crtc))
5025 intel_enable_shared_dpll(intel_crtc);
5026
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005027 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305028 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005029
5030 intel_set_pipe_timings(intel_crtc);
5031
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005032 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
5033 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
5034 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005035 }
5036
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005037 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005038 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005039 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005040 }
5041
5042 haswell_set_pipeconf(crtc);
5043
5044 intel_set_pipe_csc(crtc);
5045
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005046 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005047
Daniel Vettera72e4c92014-09-30 10:56:47 +02005048 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005049 for_each_encoder_on_crtc(dev, crtc, encoder)
5050 if (encoder->pre_enable)
5051 encoder->pre_enable(encoder);
5052
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005053 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02005054 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5055 true);
Imre Deak4fe94672014-06-25 22:01:49 +03005056 dev_priv->display.fdi_link_train(crtc);
5057 }
5058
Paulo Zanoni1f544382012-10-24 11:32:00 -02005059 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005060
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005061 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005062 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005063 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005064 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005065 else
5066 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005067
5068 /*
5069 * On ILK+ LUT must be loaded before the pipe is running but with
5070 * clocks enabled
5071 */
5072 intel_crtc_load_lut(crtc);
5073
Paulo Zanoni1f544382012-10-24 11:32:00 -02005074 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00005075 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005076
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005077 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005078 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005079
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005080 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005081 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005082
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005083 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005084 intel_ddi_set_vc_payload_alloc(crtc, true);
5085
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005086 assert_vblank_disabled(crtc);
5087 drm_crtc_vblank_on(crtc);
5088
Jani Nikula8807e552013-08-30 19:40:32 +03005089 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005090 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005091 intel_opregion_notify_encoder(encoder, true);
5092 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005093
Paulo Zanonie4916942013-09-20 16:21:19 -03005094 /* If we change the relative order between pipe/planes enabling, we need
5095 * to change the workaround. */
5096 haswell_mode_set_planes_workaround(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005097}
5098
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005099static void ironlake_pfit_disable(struct intel_crtc *crtc)
5100{
5101 struct drm_device *dev = crtc->base.dev;
5102 struct drm_i915_private *dev_priv = dev->dev_private;
5103 int pipe = crtc->pipe;
5104
5105 /* To avoid upsetting the power well on haswell only disable the pfit if
5106 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005107 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005108 I915_WRITE(PF_CTL(pipe), 0);
5109 I915_WRITE(PF_WIN_POS(pipe), 0);
5110 I915_WRITE(PF_WIN_SZ(pipe), 0);
5111 }
5112}
5113
Jesse Barnes6be4a602010-09-10 10:26:01 -07005114static void ironlake_crtc_disable(struct drm_crtc *crtc)
5115{
5116 struct drm_device *dev = crtc->dev;
5117 struct drm_i915_private *dev_priv = dev->dev_private;
5118 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005119 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005120 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005121 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005122
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005123 if (!intel_crtc->active)
5124 return;
5125
Daniel Vetterea9d7582012-07-10 10:42:52 +02005126 for_each_encoder_on_crtc(dev, crtc, encoder)
5127 encoder->disable(encoder);
5128
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005129 drm_crtc_vblank_off(crtc);
5130 assert_vblank_disabled(crtc);
5131
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005132 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005133 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005134
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005135 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005136
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005137 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005138
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005139 for_each_encoder_on_crtc(dev, crtc, encoder)
5140 if (encoder->post_disable)
5141 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005142
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005143 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005144 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005145
Daniel Vetterd925c592013-06-05 13:34:04 +02005146 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005147
Daniel Vetterd925c592013-06-05 13:34:04 +02005148 if (HAS_PCH_CPT(dev)) {
5149 /* disable TRANS_DP_CTL */
5150 reg = TRANS_DP_CTL(pipe);
5151 temp = I915_READ(reg);
5152 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5153 TRANS_DP_PORT_SEL_MASK);
5154 temp |= TRANS_DP_PORT_SEL_NONE;
5155 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005156
Daniel Vetterd925c592013-06-05 13:34:04 +02005157 /* disable DPLL_SEL */
5158 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005159 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005160 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005161 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005162
5163 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005164 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005165
5166 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005167 }
5168
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005169 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005170 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005171
5172 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005173 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005174 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005175}
5176
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005177static void haswell_crtc_disable(struct drm_crtc *crtc)
5178{
5179 struct drm_device *dev = crtc->dev;
5180 struct drm_i915_private *dev_priv = dev->dev_private;
5181 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5182 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005183 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005184
5185 if (!intel_crtc->active)
5186 return;
5187
Jani Nikula8807e552013-08-30 19:40:32 +03005188 for_each_encoder_on_crtc(dev, crtc, encoder) {
5189 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005190 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005191 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005192
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005193 drm_crtc_vblank_off(crtc);
5194 assert_vblank_disabled(crtc);
5195
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005196 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005197 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5198 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005199 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005200
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005201 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005202 intel_ddi_set_vc_payload_alloc(crtc, false);
5203
Paulo Zanoniad80a812012-10-24 16:06:19 -02005204 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005205
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005206 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005207 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005208 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005209 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005210 else
5211 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005212
Paulo Zanoni1f544382012-10-24 11:32:00 -02005213 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005214
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005215 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005216 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005217 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005218 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005219
Imre Deak97b040a2014-06-25 22:01:50 +03005220 for_each_encoder_on_crtc(dev, crtc, encoder)
5221 if (encoder->post_disable)
5222 encoder->post_disable(encoder);
5223
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005224 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005225 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005226
5227 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005228 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005229 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005230
5231 if (intel_crtc_to_shared_dpll(intel_crtc))
5232 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005233}
5234
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005235static void ironlake_crtc_off(struct drm_crtc *crtc)
5236{
5237 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005238 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005239}
5240
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005241
Jesse Barnes2dd24552013-04-25 12:55:01 -07005242static void i9xx_pfit_enable(struct intel_crtc *crtc)
5243{
5244 struct drm_device *dev = crtc->base.dev;
5245 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005246 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005247
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005248 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005249 return;
5250
Daniel Vetterc0b03412013-05-28 12:05:54 +02005251 /*
5252 * The panel fitter should only be adjusted whilst the pipe is disabled,
5253 * according to register description and PRM.
5254 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005255 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5256 assert_pipe_disabled(dev_priv, crtc->pipe);
5257
Jesse Barnesb074cec2013-04-25 12:55:02 -07005258 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5259 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005260
5261 /* Border color in case we don't scale up to the full screen. Black by
5262 * default, change to something else for debugging. */
5263 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005264}
5265
Dave Airlied05410f2014-06-05 13:22:59 +10005266static enum intel_display_power_domain port_to_power_domain(enum port port)
5267{
5268 switch (port) {
5269 case PORT_A:
5270 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5271 case PORT_B:
5272 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5273 case PORT_C:
5274 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5275 case PORT_D:
5276 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5277 default:
5278 WARN_ON_ONCE(1);
5279 return POWER_DOMAIN_PORT_OTHER;
5280 }
5281}
5282
Imre Deak77d22dc2014-03-05 16:20:52 +02005283#define for_each_power_domain(domain, mask) \
5284 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5285 if ((1 << (domain)) & (mask))
5286
Imre Deak319be8a2014-03-04 19:22:57 +02005287enum intel_display_power_domain
5288intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005289{
Imre Deak319be8a2014-03-04 19:22:57 +02005290 struct drm_device *dev = intel_encoder->base.dev;
5291 struct intel_digital_port *intel_dig_port;
5292
5293 switch (intel_encoder->type) {
5294 case INTEL_OUTPUT_UNKNOWN:
5295 /* Only DDI platforms should ever use this output type */
5296 WARN_ON_ONCE(!HAS_DDI(dev));
5297 case INTEL_OUTPUT_DISPLAYPORT:
5298 case INTEL_OUTPUT_HDMI:
5299 case INTEL_OUTPUT_EDP:
5300 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005301 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005302 case INTEL_OUTPUT_DP_MST:
5303 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5304 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005305 case INTEL_OUTPUT_ANALOG:
5306 return POWER_DOMAIN_PORT_CRT;
5307 case INTEL_OUTPUT_DSI:
5308 return POWER_DOMAIN_PORT_DSI;
5309 default:
5310 return POWER_DOMAIN_PORT_OTHER;
5311 }
5312}
5313
5314static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5315{
5316 struct drm_device *dev = crtc->dev;
5317 struct intel_encoder *intel_encoder;
5318 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5319 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005320 unsigned long mask;
5321 enum transcoder transcoder;
5322
5323 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5324
5325 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5326 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005327 if (intel_crtc->config->pch_pfit.enabled ||
5328 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005329 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5330
Imre Deak319be8a2014-03-04 19:22:57 +02005331 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5332 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5333
Imre Deak77d22dc2014-03-05 16:20:52 +02005334 return mask;
5335}
5336
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005337static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005338{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005339 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005340 struct drm_i915_private *dev_priv = dev->dev_private;
5341 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5342 struct intel_crtc *crtc;
5343
5344 /*
5345 * First get all needed power domains, then put all unneeded, to avoid
5346 * any unnecessary toggling of the power wells.
5347 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005348 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005349 enum intel_display_power_domain domain;
5350
Matt Roper83d65732015-02-25 13:12:16 -08005351 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005352 continue;
5353
Imre Deak319be8a2014-03-04 19:22:57 +02005354 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005355
5356 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5357 intel_display_power_get(dev_priv, domain);
5358 }
5359
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005360 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005361 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005362
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005363 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005364 enum intel_display_power_domain domain;
5365
5366 for_each_power_domain(domain, crtc->enabled_power_domains)
5367 intel_display_power_put(dev_priv, domain);
5368
5369 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5370 }
5371
5372 intel_display_set_init_power(dev_priv, false);
5373}
5374
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305375void broxton_set_cdclk(struct drm_device *dev, int frequency)
5376{
5377 struct drm_i915_private *dev_priv = dev->dev_private;
5378 uint32_t divider;
5379 uint32_t ratio;
5380 uint32_t current_freq;
5381 int ret;
5382
5383 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5384 switch (frequency) {
5385 case 144000:
5386 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5387 ratio = BXT_DE_PLL_RATIO(60);
5388 break;
5389 case 288000:
5390 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5391 ratio = BXT_DE_PLL_RATIO(60);
5392 break;
5393 case 384000:
5394 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5395 ratio = BXT_DE_PLL_RATIO(60);
5396 break;
5397 case 576000:
5398 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5399 ratio = BXT_DE_PLL_RATIO(60);
5400 break;
5401 case 624000:
5402 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5403 ratio = BXT_DE_PLL_RATIO(65);
5404 break;
5405 case 19200:
5406 /*
5407 * Bypass frequency with DE PLL disabled. Init ratio, divider
5408 * to suppress GCC warning.
5409 */
5410 ratio = 0;
5411 divider = 0;
5412 break;
5413 default:
5414 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5415
5416 return;
5417 }
5418
5419 mutex_lock(&dev_priv->rps.hw_lock);
5420 /* Inform power controller of upcoming frequency change */
5421 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5422 0x80000000);
5423 mutex_unlock(&dev_priv->rps.hw_lock);
5424
5425 if (ret) {
5426 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5427 ret, frequency);
5428 return;
5429 }
5430
5431 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5432 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5433 current_freq = current_freq * 500 + 1000;
5434
5435 /*
5436 * DE PLL has to be disabled when
5437 * - setting to 19.2MHz (bypass, PLL isn't used)
5438 * - before setting to 624MHz (PLL needs toggling)
5439 * - before setting to any frequency from 624MHz (PLL needs toggling)
5440 */
5441 if (frequency == 19200 || frequency == 624000 ||
5442 current_freq == 624000) {
5443 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5444 /* Timeout 200us */
5445 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5446 1))
5447 DRM_ERROR("timout waiting for DE PLL unlock\n");
5448 }
5449
5450 if (frequency != 19200) {
5451 uint32_t val;
5452
5453 val = I915_READ(BXT_DE_PLL_CTL);
5454 val &= ~BXT_DE_PLL_RATIO_MASK;
5455 val |= ratio;
5456 I915_WRITE(BXT_DE_PLL_CTL, val);
5457
5458 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5459 /* Timeout 200us */
5460 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5461 DRM_ERROR("timeout waiting for DE PLL lock\n");
5462
5463 val = I915_READ(CDCLK_CTL);
5464 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5465 val |= divider;
5466 /*
5467 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5468 * enable otherwise.
5469 */
5470 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5471 if (frequency >= 500000)
5472 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5473
5474 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5475 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5476 val |= (frequency - 1000) / 500;
5477 I915_WRITE(CDCLK_CTL, val);
5478 }
5479
5480 mutex_lock(&dev_priv->rps.hw_lock);
5481 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5482 DIV_ROUND_UP(frequency, 25000));
5483 mutex_unlock(&dev_priv->rps.hw_lock);
5484
5485 if (ret) {
5486 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5487 ret, frequency);
5488 return;
5489 }
5490
5491 dev_priv->cdclk_freq = frequency;
5492}
5493
5494void broxton_init_cdclk(struct drm_device *dev)
5495{
5496 struct drm_i915_private *dev_priv = dev->dev_private;
5497 uint32_t val;
5498
5499 /*
5500 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5501 * or else the reset will hang because there is no PCH to respond.
5502 * Move the handshake programming to initialization sequence.
5503 * Previously was left up to BIOS.
5504 */
5505 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5506 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5507 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5508
5509 /* Enable PG1 for cdclk */
5510 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5511
5512 /* check if cd clock is enabled */
5513 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5514 DRM_DEBUG_KMS("Display already initialized\n");
5515 return;
5516 }
5517
5518 /*
5519 * FIXME:
5520 * - The initial CDCLK needs to be read from VBT.
5521 * Need to make this change after VBT has changes for BXT.
5522 * - check if setting the max (or any) cdclk freq is really necessary
5523 * here, it belongs to modeset time
5524 */
5525 broxton_set_cdclk(dev, 624000);
5526
5527 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005528 POSTING_READ(DBUF_CTL);
5529
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305530 udelay(10);
5531
5532 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5533 DRM_ERROR("DBuf power enable timeout!\n");
5534}
5535
5536void broxton_uninit_cdclk(struct drm_device *dev)
5537{
5538 struct drm_i915_private *dev_priv = dev->dev_private;
5539
5540 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005541 POSTING_READ(DBUF_CTL);
5542
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305543 udelay(10);
5544
5545 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5546 DRM_ERROR("DBuf power disable timeout!\n");
5547
5548 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5549 broxton_set_cdclk(dev, 19200);
5550
5551 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5552}
5553
Ville Syrjälädfcab172014-06-13 13:37:47 +03005554/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005555static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005556{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005557 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005558
Jesse Barnes586f49d2013-11-04 16:06:59 -08005559 /* Obtain SKU information */
5560 mutex_lock(&dev_priv->dpio_lock);
5561 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5562 CCK_FUSE_HPLL_FREQ_MASK;
5563 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005564
Ville Syrjälädfcab172014-06-13 13:37:47 +03005565 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005566}
5567
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005568static void vlv_update_cdclk(struct drm_device *dev)
5569{
5570 struct drm_i915_private *dev_priv = dev->dev_private;
5571
Vandana Kannan164dfd22014-11-24 13:37:41 +05305572 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03005573 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Vandana Kannan164dfd22014-11-24 13:37:41 +05305574 dev_priv->cdclk_freq);
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005575
5576 /*
5577 * Program the gmbus_freq based on the cdclk frequency.
5578 * BSpec erroneously claims we should aim for 4MHz, but
5579 * in fact 1MHz is the correct frequency.
5580 */
Vandana Kannan164dfd22014-11-24 13:37:41 +05305581 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005582}
5583
Jesse Barnes30a970c2013-11-04 13:48:12 -08005584/* Adjust CDclk dividers to allow high res or save power if possible */
5585static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5586{
5587 struct drm_i915_private *dev_priv = dev->dev_private;
5588 u32 val, cmd;
5589
Vandana Kannan164dfd22014-11-24 13:37:41 +05305590 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5591 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005592
Ville Syrjälädfcab172014-06-13 13:37:47 +03005593 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005594 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005595 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005596 cmd = 1;
5597 else
5598 cmd = 0;
5599
5600 mutex_lock(&dev_priv->rps.hw_lock);
5601 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5602 val &= ~DSPFREQGUAR_MASK;
5603 val |= (cmd << DSPFREQGUAR_SHIFT);
5604 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5605 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5606 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5607 50)) {
5608 DRM_ERROR("timed out waiting for CDclk change\n");
5609 }
5610 mutex_unlock(&dev_priv->rps.hw_lock);
5611
Ville Syrjälädfcab172014-06-13 13:37:47 +03005612 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005613 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005614
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005615 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005616
5617 mutex_lock(&dev_priv->dpio_lock);
5618 /* adjust cdclk divider */
5619 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005620 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005621 val |= divider;
5622 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005623
5624 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5625 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5626 50))
5627 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005628 mutex_unlock(&dev_priv->dpio_lock);
5629 }
5630
5631 mutex_lock(&dev_priv->dpio_lock);
5632 /* adjust self-refresh exit latency value */
5633 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5634 val &= ~0x7f;
5635
5636 /*
5637 * For high bandwidth configs, we set a higher latency in the bunit
5638 * so that the core display fetch happens in time to avoid underruns.
5639 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005640 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005641 val |= 4500 / 250; /* 4.5 usec */
5642 else
5643 val |= 3000 / 250; /* 3.0 usec */
5644 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5645 mutex_unlock(&dev_priv->dpio_lock);
5646
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005647 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005648}
5649
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005650static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5651{
5652 struct drm_i915_private *dev_priv = dev->dev_private;
5653 u32 val, cmd;
5654
Vandana Kannan164dfd22014-11-24 13:37:41 +05305655 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5656 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005657
5658 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005659 case 333333:
5660 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005661 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005662 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005663 break;
5664 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005665 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005666 return;
5667 }
5668
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005669 /*
5670 * Specs are full of misinformation, but testing on actual
5671 * hardware has shown that we just need to write the desired
5672 * CCK divider into the Punit register.
5673 */
5674 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5675
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005676 mutex_lock(&dev_priv->rps.hw_lock);
5677 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5678 val &= ~DSPFREQGUAR_MASK_CHV;
5679 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5680 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5681 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5682 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5683 50)) {
5684 DRM_ERROR("timed out waiting for CDclk change\n");
5685 }
5686 mutex_unlock(&dev_priv->rps.hw_lock);
5687
5688 vlv_update_cdclk(dev);
5689}
5690
Jesse Barnes30a970c2013-11-04 13:48:12 -08005691static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5692 int max_pixclk)
5693{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005694 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005695 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005696
Jesse Barnes30a970c2013-11-04 13:48:12 -08005697 /*
5698 * Really only a few cases to deal with, as only 4 CDclks are supported:
5699 * 200MHz
5700 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005701 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005702 * 400MHz (VLV only)
5703 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5704 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005705 *
5706 * We seem to get an unstable or solid color picture at 200MHz.
5707 * Not sure what's wrong. For now use 200MHz only when all pipes
5708 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005709 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005710 if (!IS_CHERRYVIEW(dev_priv) &&
5711 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005712 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005713 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005714 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005715 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005716 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005717 else
5718 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005719}
5720
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305721static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5722 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005723{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305724 /*
5725 * FIXME:
5726 * - remove the guardband, it's not needed on BXT
5727 * - set 19.2MHz bypass frequency if there are no active pipes
5728 */
5729 if (max_pixclk > 576000*9/10)
5730 return 624000;
5731 else if (max_pixclk > 384000*9/10)
5732 return 576000;
5733 else if (max_pixclk > 288000*9/10)
5734 return 384000;
5735 else if (max_pixclk > 144000*9/10)
5736 return 288000;
5737 else
5738 return 144000;
5739}
5740
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005741/* Compute the max pixel clock for new configuration. Uses atomic state if
5742 * that's non-NULL, look at current state otherwise. */
5743static int intel_mode_max_pixclk(struct drm_device *dev,
5744 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005745{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005746 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005747 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005748 int max_pixclk = 0;
5749
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005750 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005751 if (state)
5752 crtc_state =
5753 intel_atomic_get_crtc_state(state, intel_crtc);
5754 else
5755 crtc_state = intel_crtc->config;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005756 if (IS_ERR(crtc_state))
5757 return PTR_ERR(crtc_state);
5758
5759 if (!crtc_state->base.enable)
5760 continue;
5761
5762 max_pixclk = max(max_pixclk,
5763 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005764 }
5765
5766 return max_pixclk;
5767}
5768
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005769static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005770{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005771 struct drm_i915_private *dev_priv = to_i915(state->dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005772 struct drm_crtc *crtc;
5773 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005774 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005775 int cdclk, i;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005776
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005777 if (max_pixclk < 0)
5778 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005779
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305780 if (IS_VALLEYVIEW(dev_priv))
5781 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5782 else
5783 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5784
5785 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005786 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005787
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005788 /* add all active pipes to the state */
5789 for_each_crtc(state->dev, crtc) {
5790 if (!crtc->state->enable)
5791 continue;
5792
5793 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5794 if (IS_ERR(crtc_state))
5795 return PTR_ERR(crtc_state);
5796 }
5797
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005798 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005799 for_each_crtc_in_state(state, crtc, crtc_state, i)
5800 if (crtc_state->enable)
5801 crtc_state->mode_changed = true;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005802
5803 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005804}
5805
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005806static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5807{
5808 unsigned int credits, default_credits;
5809
5810 if (IS_CHERRYVIEW(dev_priv))
5811 default_credits = PFI_CREDIT(12);
5812 else
5813 default_credits = PFI_CREDIT(8);
5814
Vandana Kannan164dfd22014-11-24 13:37:41 +05305815 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005816 /* CHV suggested value is 31 or 63 */
5817 if (IS_CHERRYVIEW(dev_priv))
5818 credits = PFI_CREDIT_31;
5819 else
5820 credits = PFI_CREDIT(15);
5821 } else {
5822 credits = default_credits;
5823 }
5824
5825 /*
5826 * WA - write default credits before re-programming
5827 * FIXME: should we also set the resend bit here?
5828 */
5829 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5830 default_credits);
5831
5832 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5833 credits | PFI_CREDIT_RESEND);
5834
5835 /*
5836 * FIXME is this guaranteed to clear
5837 * immediately or should we poll for it?
5838 */
5839 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5840}
5841
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005842static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005843{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005844 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005845 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005846 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005847 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005848
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005849 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
5850 * never fail. */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005851 if (WARN_ON(max_pixclk < 0))
5852 return;
5853
5854 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005855
Vandana Kannan164dfd22014-11-24 13:37:41 +05305856 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005857 /*
5858 * FIXME: We can end up here with all power domains off, yet
5859 * with a CDCLK frequency other than the minimum. To account
5860 * for this take the PIPE-A power domain, which covers the HW
5861 * blocks needed for the following programming. This can be
5862 * removed once it's guaranteed that we get here either with
5863 * the minimum CDCLK set, or the required power domains
5864 * enabled.
5865 */
5866 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5867
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005868 if (IS_CHERRYVIEW(dev))
5869 cherryview_set_cdclk(dev, req_cdclk);
5870 else
5871 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005872
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005873 vlv_program_pfi_credits(dev_priv);
5874
Imre Deak738c05c2014-11-19 16:25:37 +02005875 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005876 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005877}
5878
Jesse Barnes89b667f2013-04-18 14:51:36 -07005879static void valleyview_crtc_enable(struct drm_crtc *crtc)
5880{
5881 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005882 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005883 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5884 struct intel_encoder *encoder;
5885 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005886 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005887
Matt Roper83d65732015-02-25 13:12:16 -08005888 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005889
5890 if (intel_crtc->active)
5891 return;
5892
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005893 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305894
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005895 if (!is_dsi) {
5896 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005897 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005898 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005899 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005900 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005901
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005902 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305903 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005904
5905 intel_set_pipe_timings(intel_crtc);
5906
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005907 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5908 struct drm_i915_private *dev_priv = dev->dev_private;
5909
5910 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5911 I915_WRITE(CHV_CANVAS(pipe), 0);
5912 }
5913
Daniel Vetter5b18e572014-04-24 23:55:06 +02005914 i9xx_set_pipeconf(intel_crtc);
5915
Jesse Barnes89b667f2013-04-18 14:51:36 -07005916 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005917
Daniel Vettera72e4c92014-09-30 10:56:47 +02005918 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005919
Jesse Barnes89b667f2013-04-18 14:51:36 -07005920 for_each_encoder_on_crtc(dev, crtc, encoder)
5921 if (encoder->pre_pll_enable)
5922 encoder->pre_pll_enable(encoder);
5923
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005924 if (!is_dsi) {
5925 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005926 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005927 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005928 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005929 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005930
5931 for_each_encoder_on_crtc(dev, crtc, encoder)
5932 if (encoder->pre_enable)
5933 encoder->pre_enable(encoder);
5934
Jesse Barnes2dd24552013-04-25 12:55:01 -07005935 i9xx_pfit_enable(intel_crtc);
5936
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005937 intel_crtc_load_lut(crtc);
5938
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005939 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005940 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005941
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005942 assert_vblank_disabled(crtc);
5943 drm_crtc_vblank_on(crtc);
5944
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005945 for_each_encoder_on_crtc(dev, crtc, encoder)
5946 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005947}
5948
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005949static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5950{
5951 struct drm_device *dev = crtc->base.dev;
5952 struct drm_i915_private *dev_priv = dev->dev_private;
5953
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005954 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5955 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005956}
5957
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005958static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005959{
5960 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005961 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005963 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005964 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005965
Matt Roper83d65732015-02-25 13:12:16 -08005966 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02005967
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005968 if (intel_crtc->active)
5969 return;
5970
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005971 i9xx_set_pll_dividers(intel_crtc);
5972
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005973 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305974 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005975
5976 intel_set_pipe_timings(intel_crtc);
5977
Daniel Vetter5b18e572014-04-24 23:55:06 +02005978 i9xx_set_pipeconf(intel_crtc);
5979
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005980 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005981
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005982 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005983 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005984
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005985 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005986 if (encoder->pre_enable)
5987 encoder->pre_enable(encoder);
5988
Daniel Vetterf6736a12013-06-05 13:34:30 +02005989 i9xx_enable_pll(intel_crtc);
5990
Jesse Barnes2dd24552013-04-25 12:55:01 -07005991 i9xx_pfit_enable(intel_crtc);
5992
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005993 intel_crtc_load_lut(crtc);
5994
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005995 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005996 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005997
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005998 assert_vblank_disabled(crtc);
5999 drm_crtc_vblank_on(crtc);
6000
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006001 for_each_encoder_on_crtc(dev, crtc, encoder)
6002 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006003}
6004
Daniel Vetter87476d62013-04-11 16:29:06 +02006005static void i9xx_pfit_disable(struct intel_crtc *crtc)
6006{
6007 struct drm_device *dev = crtc->base.dev;
6008 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006009
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006010 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006011 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006012
6013 assert_pipe_disabled(dev_priv, crtc->pipe);
6014
Daniel Vetter328d8e82013-05-08 10:36:31 +02006015 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6016 I915_READ(PFIT_CONTROL));
6017 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006018}
6019
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006020static void i9xx_crtc_disable(struct drm_crtc *crtc)
6021{
6022 struct drm_device *dev = crtc->dev;
6023 struct drm_i915_private *dev_priv = dev->dev_private;
6024 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006025 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006026 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006027
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006028 if (!intel_crtc->active)
6029 return;
6030
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006031 /*
6032 * On gen2 planes are double buffered but the pipe isn't, so we must
6033 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006034 * We also need to wait on all gmch platforms because of the
6035 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006036 */
Imre Deak564ed192014-06-13 14:54:21 +03006037 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006038
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006039 for_each_encoder_on_crtc(dev, crtc, encoder)
6040 encoder->disable(encoder);
6041
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006042 drm_crtc_vblank_off(crtc);
6043 assert_vblank_disabled(crtc);
6044
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006045 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006046
Daniel Vetter87476d62013-04-11 16:29:06 +02006047 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006048
Jesse Barnes89b667f2013-04-18 14:51:36 -07006049 for_each_encoder_on_crtc(dev, crtc, encoder)
6050 if (encoder->post_disable)
6051 encoder->post_disable(encoder);
6052
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006053 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006054 if (IS_CHERRYVIEW(dev))
6055 chv_disable_pll(dev_priv, pipe);
6056 else if (IS_VALLEYVIEW(dev))
6057 vlv_disable_pll(dev_priv, pipe);
6058 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006059 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006060 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006061
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006062 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006063 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006064
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006065 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006066 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006067
Daniel Vetterefa96242014-04-24 23:55:02 +02006068 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006069 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02006070 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006071}
6072
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006073static void i9xx_crtc_off(struct drm_crtc *crtc)
6074{
6075}
6076
Borun Fub04c5bd2014-07-12 10:02:27 +05306077/* Master function to enable/disable CRTC and corresponding power wells */
6078void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01006079{
Chris Wilsoncdd59982010-09-08 16:30:16 +01006080 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006081 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006082 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006083 enum intel_display_power_domain domain;
6084 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006085
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006086 if (enable) {
6087 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006088 domains = get_crtc_power_domains(crtc);
6089 for_each_power_domain(domain, domains)
6090 intel_display_power_get(dev_priv, domain);
6091 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006092
6093 dev_priv->display.crtc_enable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006094 intel_crtc_enable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006095 }
6096 } else {
6097 if (intel_crtc->active) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006098 intel_crtc_disable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006099 dev_priv->display.crtc_disable(crtc);
6100
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006101 domains = intel_crtc->enabled_power_domains;
6102 for_each_power_domain(domain, domains)
6103 intel_display_power_put(dev_priv, domain);
6104 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006105 }
6106 }
Borun Fub04c5bd2014-07-12 10:02:27 +05306107}
6108
6109/**
6110 * Sets the power management mode of the pipe and plane.
6111 */
6112void intel_crtc_update_dpms(struct drm_crtc *crtc)
6113{
6114 struct drm_device *dev = crtc->dev;
6115 struct intel_encoder *intel_encoder;
6116 bool enable = false;
6117
6118 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6119 enable |= intel_encoder->connectors_active;
6120
6121 intel_crtc_control(crtc, enable);
Ander Conselvan de Oliveira0f63cca2015-04-21 17:13:17 +03006122
6123 crtc->state->active = enable;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006124}
6125
Daniel Vetter976f8a22012-07-08 22:34:21 +02006126static void intel_crtc_disable(struct drm_crtc *crtc)
6127{
6128 struct drm_device *dev = crtc->dev;
6129 struct drm_connector *connector;
6130 struct drm_i915_private *dev_priv = dev->dev_private;
6131
6132 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08006133 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006134
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006135 intel_crtc_disable_planes(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006136 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006137 dev_priv->display.off(crtc);
6138
Matt Roper70a101f2015-04-08 18:56:53 -07006139 drm_plane_helper_disable(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006140
6141 /* Update computed state. */
6142 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6143 if (!connector->encoder || !connector->encoder->crtc)
6144 continue;
6145
6146 if (connector->encoder->crtc != crtc)
6147 continue;
6148
6149 connector->dpms = DRM_MODE_DPMS_OFF;
6150 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01006151 }
6152}
6153
Chris Wilsonea5b2132010-08-04 13:50:23 +01006154void intel_encoder_destroy(struct drm_encoder *encoder)
6155{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006156 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006157
Chris Wilsonea5b2132010-08-04 13:50:23 +01006158 drm_encoder_cleanup(encoder);
6159 kfree(intel_encoder);
6160}
6161
Damien Lespiau92373292013-08-08 22:28:57 +01006162/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006163 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6164 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006165static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006166{
6167 if (mode == DRM_MODE_DPMS_ON) {
6168 encoder->connectors_active = true;
6169
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006170 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006171 } else {
6172 encoder->connectors_active = false;
6173
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006174 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006175 }
6176}
6177
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006178/* Cross check the actual hw state with our own modeset state tracking (and it's
6179 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006180static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006181{
6182 if (connector->get_hw_state(connector)) {
6183 struct intel_encoder *encoder = connector->encoder;
6184 struct drm_crtc *crtc;
6185 bool encoder_enabled;
6186 enum pipe pipe;
6187
6188 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6189 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006190 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006191
Dave Airlie0e32b392014-05-02 14:02:48 +10006192 /* there is no real hw state for MST connectors */
6193 if (connector->mst_port)
6194 return;
6195
Rob Clarke2c719b2014-12-15 13:56:32 -05006196 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006197 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006198 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006199 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006200
Dave Airlie36cd7442014-05-02 13:44:18 +10006201 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006202 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006203 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006204
Dave Airlie36cd7442014-05-02 13:44:18 +10006205 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006206 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6207 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006208 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006209
Dave Airlie36cd7442014-05-02 13:44:18 +10006210 crtc = encoder->base.crtc;
6211
Matt Roper83d65732015-02-25 13:12:16 -08006212 I915_STATE_WARN(!crtc->state->enable,
6213 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006214 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6215 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006216 "encoder active on the wrong pipe\n");
6217 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006218 }
6219}
6220
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006221int intel_connector_init(struct intel_connector *connector)
6222{
6223 struct drm_connector_state *connector_state;
6224
6225 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6226 if (!connector_state)
6227 return -ENOMEM;
6228
6229 connector->base.state = connector_state;
6230 return 0;
6231}
6232
6233struct intel_connector *intel_connector_alloc(void)
6234{
6235 struct intel_connector *connector;
6236
6237 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6238 if (!connector)
6239 return NULL;
6240
6241 if (intel_connector_init(connector) < 0) {
6242 kfree(connector);
6243 return NULL;
6244 }
6245
6246 return connector;
6247}
6248
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006249/* Even simpler default implementation, if there's really no special case to
6250 * consider. */
6251void intel_connector_dpms(struct drm_connector *connector, int mode)
6252{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006253 /* All the simple cases only support two dpms states. */
6254 if (mode != DRM_MODE_DPMS_ON)
6255 mode = DRM_MODE_DPMS_OFF;
6256
6257 if (mode == connector->dpms)
6258 return;
6259
6260 connector->dpms = mode;
6261
6262 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01006263 if (connector->encoder)
6264 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006265
Daniel Vetterb9805142012-08-31 17:37:33 +02006266 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006267}
6268
Daniel Vetterf0947c32012-07-02 13:10:34 +02006269/* Simple connector->get_hw_state implementation for encoders that support only
6270 * one connector and no cloning and hence the encoder state determines the state
6271 * of the connector. */
6272bool intel_connector_get_hw_state(struct intel_connector *connector)
6273{
Daniel Vetter24929352012-07-02 20:28:59 +02006274 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006275 struct intel_encoder *encoder = connector->encoder;
6276
6277 return encoder->get_hw_state(encoder, &pipe);
6278}
6279
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006280static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006281{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006282 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6283 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006284
6285 return 0;
6286}
6287
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006288static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006289 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006290{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006291 struct drm_atomic_state *state = pipe_config->base.state;
6292 struct intel_crtc *other_crtc;
6293 struct intel_crtc_state *other_crtc_state;
6294
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006295 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6296 pipe_name(pipe), pipe_config->fdi_lanes);
6297 if (pipe_config->fdi_lanes > 4) {
6298 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6299 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006300 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006301 }
6302
Paulo Zanonibafb6552013-11-02 21:07:44 -07006303 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006304 if (pipe_config->fdi_lanes > 2) {
6305 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6306 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006307 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006308 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006309 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006310 }
6311 }
6312
6313 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006314 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006315
6316 /* Ivybridge 3 pipe is really complicated */
6317 switch (pipe) {
6318 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006319 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006320 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006321 if (pipe_config->fdi_lanes <= 2)
6322 return 0;
6323
6324 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6325 other_crtc_state =
6326 intel_atomic_get_crtc_state(state, other_crtc);
6327 if (IS_ERR(other_crtc_state))
6328 return PTR_ERR(other_crtc_state);
6329
6330 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006331 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6332 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006333 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006334 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006335 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006336 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006337 if (pipe_config->fdi_lanes > 2) {
6338 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6339 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006340 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006341 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006342
6343 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6344 other_crtc_state =
6345 intel_atomic_get_crtc_state(state, other_crtc);
6346 if (IS_ERR(other_crtc_state))
6347 return PTR_ERR(other_crtc_state);
6348
6349 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006350 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006351 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006352 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006353 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006354 default:
6355 BUG();
6356 }
6357}
6358
Daniel Vettere29c22c2013-02-21 00:00:16 +01006359#define RETRY 1
6360static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006361 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006362{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006363 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006364 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006365 int lane, link_bw, fdi_dotclock, ret;
6366 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006367
Daniel Vettere29c22c2013-02-21 00:00:16 +01006368retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006369 /* FDI is a binary signal running at ~2.7GHz, encoding
6370 * each output octet as 10 bits. The actual frequency
6371 * is stored as a divider into a 100MHz clock, and the
6372 * mode pixel clock is stored in units of 1KHz.
6373 * Hence the bw of each lane in terms of the mode signal
6374 * is:
6375 */
6376 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6377
Damien Lespiau241bfc32013-09-25 16:45:37 +01006378 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006379
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006380 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006381 pipe_config->pipe_bpp);
6382
6383 pipe_config->fdi_lanes = lane;
6384
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006385 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006386 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006387
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006388 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6389 intel_crtc->pipe, pipe_config);
6390 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006391 pipe_config->pipe_bpp -= 2*3;
6392 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6393 pipe_config->pipe_bpp);
6394 needs_recompute = true;
6395 pipe_config->bw_constrained = true;
6396
6397 goto retry;
6398 }
6399
6400 if (needs_recompute)
6401 return RETRY;
6402
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006403 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006404}
6405
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006406static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006407 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006408{
Jani Nikulad330a952014-01-21 11:24:25 +02006409 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03006410 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07006411 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006412}
6413
Daniel Vettera43f6e02013-06-07 23:10:32 +02006414static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006415 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006416{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006417 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006418 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006419 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006420 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006421
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006422 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006423 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006424 int clock_limit =
6425 dev_priv->display.get_display_clock_speed(dev);
6426
6427 /*
6428 * Enable pixel doubling when the dot clock
6429 * is > 90% of the (display) core speed.
6430 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006431 * GDG double wide on either pipe,
6432 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006433 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006434 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006435 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006436 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006437 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006438 }
6439
Damien Lespiau241bfc32013-09-25 16:45:37 +01006440 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006441 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006442 }
Chris Wilson89749352010-09-12 18:25:19 +01006443
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006444 /*
6445 * Pipe horizontal size must be even in:
6446 * - DVO ganged mode
6447 * - LVDS dual channel mode
6448 * - Double wide pipe
6449 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006450 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006451 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6452 pipe_config->pipe_src_w &= ~1;
6453
Damien Lespiau8693a822013-05-03 18:48:11 +01006454 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6455 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006456 */
6457 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6458 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006459 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006460
Damien Lespiauf5adf942013-06-24 18:29:34 +01006461 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006462 hsw_compute_ips_config(crtc, pipe_config);
6463
Daniel Vetter877d48d2013-04-19 11:24:43 +02006464 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006465 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006466
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006467 /* FIXME: remove below call once atomic mode set is place and all crtc
6468 * related checks called from atomic_crtc_check function */
6469 ret = 0;
6470 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6471 crtc, pipe_config->base.state);
6472 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6473
6474 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006475}
6476
Ville Syrjälä1652d192015-03-31 14:12:01 +03006477static int skylake_get_display_clock_speed(struct drm_device *dev)
6478{
6479 struct drm_i915_private *dev_priv = to_i915(dev);
6480 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6481 uint32_t cdctl = I915_READ(CDCLK_CTL);
6482 uint32_t linkrate;
6483
6484 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6485 WARN(1, "LCPLL1 not enabled\n");
6486 return 24000; /* 24MHz is the cd freq with NSSC ref */
6487 }
6488
6489 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6490 return 540000;
6491
6492 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006493 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006494
Damien Lespiau71cd8422015-04-30 16:39:17 +01006495 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6496 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006497 /* vco 8640 */
6498 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6499 case CDCLK_FREQ_450_432:
6500 return 432000;
6501 case CDCLK_FREQ_337_308:
6502 return 308570;
6503 case CDCLK_FREQ_675_617:
6504 return 617140;
6505 default:
6506 WARN(1, "Unknown cd freq selection\n");
6507 }
6508 } else {
6509 /* vco 8100 */
6510 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6511 case CDCLK_FREQ_450_432:
6512 return 450000;
6513 case CDCLK_FREQ_337_308:
6514 return 337500;
6515 case CDCLK_FREQ_675_617:
6516 return 675000;
6517 default:
6518 WARN(1, "Unknown cd freq selection\n");
6519 }
6520 }
6521
6522 /* error case, do as if DPLL0 isn't enabled */
6523 return 24000;
6524}
6525
6526static int broadwell_get_display_clock_speed(struct drm_device *dev)
6527{
6528 struct drm_i915_private *dev_priv = dev->dev_private;
6529 uint32_t lcpll = I915_READ(LCPLL_CTL);
6530 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6531
6532 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6533 return 800000;
6534 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6535 return 450000;
6536 else if (freq == LCPLL_CLK_FREQ_450)
6537 return 450000;
6538 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6539 return 540000;
6540 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6541 return 337500;
6542 else
6543 return 675000;
6544}
6545
6546static int haswell_get_display_clock_speed(struct drm_device *dev)
6547{
6548 struct drm_i915_private *dev_priv = dev->dev_private;
6549 uint32_t lcpll = I915_READ(LCPLL_CTL);
6550 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6551
6552 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6553 return 800000;
6554 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6555 return 450000;
6556 else if (freq == LCPLL_CLK_FREQ_450)
6557 return 450000;
6558 else if (IS_HSW_ULT(dev))
6559 return 337500;
6560 else
6561 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006562}
6563
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006564static int valleyview_get_display_clock_speed(struct drm_device *dev)
6565{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006566 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006567 u32 val;
6568 int divider;
6569
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006570 if (dev_priv->hpll_freq == 0)
6571 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6572
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006573 mutex_lock(&dev_priv->dpio_lock);
6574 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6575 mutex_unlock(&dev_priv->dpio_lock);
6576
6577 divider = val & DISPLAY_FREQUENCY_VALUES;
6578
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006579 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6580 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6581 "cdclk change in progress\n");
6582
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006583 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006584}
6585
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006586static int ilk_get_display_clock_speed(struct drm_device *dev)
6587{
6588 return 450000;
6589}
6590
Jesse Barnese70236a2009-09-21 10:42:27 -07006591static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006592{
Jesse Barnese70236a2009-09-21 10:42:27 -07006593 return 400000;
6594}
Jesse Barnes79e53942008-11-07 14:24:08 -08006595
Jesse Barnese70236a2009-09-21 10:42:27 -07006596static int i915_get_display_clock_speed(struct drm_device *dev)
6597{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006598 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006599}
Jesse Barnes79e53942008-11-07 14:24:08 -08006600
Jesse Barnese70236a2009-09-21 10:42:27 -07006601static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6602{
6603 return 200000;
6604}
Jesse Barnes79e53942008-11-07 14:24:08 -08006605
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006606static int pnv_get_display_clock_speed(struct drm_device *dev)
6607{
6608 u16 gcfgc = 0;
6609
6610 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6611
6612 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6613 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006614 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006615 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006616 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006617 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006618 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006619 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6620 return 200000;
6621 default:
6622 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6623 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006624 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006625 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006626 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006627 }
6628}
6629
Jesse Barnese70236a2009-09-21 10:42:27 -07006630static int i915gm_get_display_clock_speed(struct drm_device *dev)
6631{
6632 u16 gcfgc = 0;
6633
6634 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6635
6636 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006637 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006638 else {
6639 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6640 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006641 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006642 default:
6643 case GC_DISPLAY_CLOCK_190_200_MHZ:
6644 return 190000;
6645 }
6646 }
6647}
Jesse Barnes79e53942008-11-07 14:24:08 -08006648
Jesse Barnese70236a2009-09-21 10:42:27 -07006649static int i865_get_display_clock_speed(struct drm_device *dev)
6650{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006651 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006652}
6653
6654static int i855_get_display_clock_speed(struct drm_device *dev)
6655{
6656 u16 hpllcc = 0;
6657 /* Assume that the hardware is in the high speed state. This
6658 * should be the default.
6659 */
6660 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6661 case GC_CLOCK_133_200:
6662 case GC_CLOCK_100_200:
6663 return 200000;
6664 case GC_CLOCK_166_250:
6665 return 250000;
6666 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006667 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006668 }
6669
6670 /* Shouldn't happen */
6671 return 0;
6672}
6673
6674static int i830_get_display_clock_speed(struct drm_device *dev)
6675{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006676 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006677}
6678
Zhenyu Wang2c072452009-06-05 15:38:42 +08006679static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006680intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006681{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006682 while (*num > DATA_LINK_M_N_MASK ||
6683 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006684 *num >>= 1;
6685 *den >>= 1;
6686 }
6687}
6688
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006689static void compute_m_n(unsigned int m, unsigned int n,
6690 uint32_t *ret_m, uint32_t *ret_n)
6691{
6692 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6693 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6694 intel_reduce_m_n_ratio(ret_m, ret_n);
6695}
6696
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006697void
6698intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6699 int pixel_clock, int link_clock,
6700 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006701{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006702 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006703
6704 compute_m_n(bits_per_pixel * pixel_clock,
6705 link_clock * nlanes * 8,
6706 &m_n->gmch_m, &m_n->gmch_n);
6707
6708 compute_m_n(pixel_clock, link_clock,
6709 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006710}
6711
Chris Wilsona7615032011-01-12 17:04:08 +00006712static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6713{
Jani Nikulad330a952014-01-21 11:24:25 +02006714 if (i915.panel_use_ssc >= 0)
6715 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006716 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006717 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006718}
6719
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006720static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6721 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006722{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006723 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006724 struct drm_i915_private *dev_priv = dev->dev_private;
6725 int refclk;
6726
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006727 WARN_ON(!crtc_state->base.state);
6728
Imre Deak5ab7b0b2015-03-06 03:29:25 +02006729 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006730 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006731 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006732 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006733 refclk = dev_priv->vbt.lvds_ssc_freq;
6734 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006735 } else if (!IS_GEN2(dev)) {
6736 refclk = 96000;
6737 } else {
6738 refclk = 48000;
6739 }
6740
6741 return refclk;
6742}
6743
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006744static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006745{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006746 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006747}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006748
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006749static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6750{
6751 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006752}
6753
Daniel Vetterf47709a2013-03-28 10:42:02 +01006754static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006755 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08006756 intel_clock_t *reduced_clock)
6757{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006758 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006759 u32 fp, fp2 = 0;
6760
6761 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006762 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006763 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006764 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006765 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006766 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006767 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006768 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006769 }
6770
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006771 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006772
Daniel Vetterf47709a2013-03-28 10:42:02 +01006773 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006774 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006775 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006776 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006777 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006778 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006779 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006780 }
6781}
6782
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006783static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6784 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006785{
6786 u32 reg_val;
6787
6788 /*
6789 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6790 * and set it to a reasonable value instead.
6791 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006792 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006793 reg_val &= 0xffffff00;
6794 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006795 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006796
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006797 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006798 reg_val &= 0x8cffffff;
6799 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006800 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006801
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006802 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006803 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006804 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006805
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006806 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006807 reg_val &= 0x00ffffff;
6808 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006809 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006810}
6811
Daniel Vetterb5518422013-05-03 11:49:48 +02006812static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6813 struct intel_link_m_n *m_n)
6814{
6815 struct drm_device *dev = crtc->base.dev;
6816 struct drm_i915_private *dev_priv = dev->dev_private;
6817 int pipe = crtc->pipe;
6818
Daniel Vettere3b95f12013-05-03 11:49:49 +02006819 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6820 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6821 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6822 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006823}
6824
6825static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006826 struct intel_link_m_n *m_n,
6827 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006828{
6829 struct drm_device *dev = crtc->base.dev;
6830 struct drm_i915_private *dev_priv = dev->dev_private;
6831 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006832 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006833
6834 if (INTEL_INFO(dev)->gen >= 5) {
6835 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6836 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6837 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6838 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006839 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6840 * for gen < 8) and if DRRS is supported (to make sure the
6841 * registers are not unnecessarily accessed).
6842 */
Durgadoss R44395bf2015-02-13 15:33:02 +05306843 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006844 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006845 I915_WRITE(PIPE_DATA_M2(transcoder),
6846 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6847 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6848 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6849 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6850 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006851 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006852 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6853 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6854 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6855 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006856 }
6857}
6858
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306859void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006860{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306861 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6862
6863 if (m_n == M1_N1) {
6864 dp_m_n = &crtc->config->dp_m_n;
6865 dp_m2_n2 = &crtc->config->dp_m2_n2;
6866 } else if (m_n == M2_N2) {
6867
6868 /*
6869 * M2_N2 registers are not supported. Hence m2_n2 divider value
6870 * needs to be programmed into M1_N1.
6871 */
6872 dp_m_n = &crtc->config->dp_m2_n2;
6873 } else {
6874 DRM_ERROR("Unsupported divider value\n");
6875 return;
6876 }
6877
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006878 if (crtc->config->has_pch_encoder)
6879 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006880 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306881 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006882}
6883
Ville Syrjäläd288f652014-10-28 13:20:22 +02006884static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006885 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006886{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006887 u32 dpll, dpll_md;
6888
6889 /*
6890 * Enable DPIO clock input. We should never disable the reference
6891 * clock for pipe B, since VGA hotplug / manual detection depends
6892 * on it.
6893 */
6894 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6895 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6896 /* We should never disable this, set it here for state tracking */
6897 if (crtc->pipe == PIPE_B)
6898 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6899 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006900 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006901
Ville Syrjäläd288f652014-10-28 13:20:22 +02006902 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006903 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006904 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006905}
6906
Ville Syrjäläd288f652014-10-28 13:20:22 +02006907static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006908 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006909{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006910 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006911 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006912 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006913 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006914 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006915 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006916
Daniel Vetter09153002012-12-12 14:06:44 +01006917 mutex_lock(&dev_priv->dpio_lock);
6918
Ville Syrjäläd288f652014-10-28 13:20:22 +02006919 bestn = pipe_config->dpll.n;
6920 bestm1 = pipe_config->dpll.m1;
6921 bestm2 = pipe_config->dpll.m2;
6922 bestp1 = pipe_config->dpll.p1;
6923 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006924
Jesse Barnes89b667f2013-04-18 14:51:36 -07006925 /* See eDP HDMI DPIO driver vbios notes doc */
6926
6927 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006928 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006929 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006930
6931 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006932 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006933
6934 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006935 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006936 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006937 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006938
6939 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006940 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006941
6942 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006943 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6944 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6945 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006946 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006947
6948 /*
6949 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6950 * but we don't support that).
6951 * Note: don't use the DAC post divider as it seems unstable.
6952 */
6953 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006954 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006955
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006956 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006957 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006958
Jesse Barnes89b667f2013-04-18 14:51:36 -07006959 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006960 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006961 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6962 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006963 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03006964 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006965 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006966 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006967 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006968
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02006969 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006970 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006971 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006972 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006973 0x0df40000);
6974 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006975 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006976 0x0df70000);
6977 } else { /* HDMI or VGA */
6978 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006979 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006980 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006981 0x0df70000);
6982 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006983 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006984 0x0df40000);
6985 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006986
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006987 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006988 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006989 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6990 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006991 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006992 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006993
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006994 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01006995 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006996}
6997
Ville Syrjäläd288f652014-10-28 13:20:22 +02006998static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006999 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007000{
Ville Syrjäläd288f652014-10-28 13:20:22 +02007001 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007002 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7003 DPLL_VCO_ENABLE;
7004 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007005 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007006
Ville Syrjäläd288f652014-10-28 13:20:22 +02007007 pipe_config->dpll_hw_state.dpll_md =
7008 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007009}
7010
Ville Syrjäläd288f652014-10-28 13:20:22 +02007011static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007012 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007013{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007014 struct drm_device *dev = crtc->base.dev;
7015 struct drm_i915_private *dev_priv = dev->dev_private;
7016 int pipe = crtc->pipe;
7017 int dpll_reg = DPLL(crtc->pipe);
7018 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307019 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007020 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307021 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307022 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007023
Ville Syrjäläd288f652014-10-28 13:20:22 +02007024 bestn = pipe_config->dpll.n;
7025 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7026 bestm1 = pipe_config->dpll.m1;
7027 bestm2 = pipe_config->dpll.m2 >> 22;
7028 bestp1 = pipe_config->dpll.p1;
7029 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307030 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307031 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307032 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007033
7034 /*
7035 * Enable Refclk and SSC
7036 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007037 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007038 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007039
7040 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007041
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007042 /* p1 and p2 divider */
7043 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7044 5 << DPIO_CHV_S1_DIV_SHIFT |
7045 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7046 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7047 1 << DPIO_CHV_K_DIV_SHIFT);
7048
7049 /* Feedback post-divider - m2 */
7050 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7051
7052 /* Feedback refclk divider - n and m1 */
7053 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7054 DPIO_CHV_M1_DIV_BY_2 |
7055 1 << DPIO_CHV_N_DIV_SHIFT);
7056
7057 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307058 if (bestm2_frac)
7059 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007060
7061 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307062 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7063 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7064 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7065 if (bestm2_frac)
7066 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7067 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007068
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307069 /* Program digital lock detect threshold */
7070 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7071 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7072 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7073 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7074 if (!bestm2_frac)
7075 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7076 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7077
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007078 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307079 if (vco == 5400000) {
7080 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7081 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7082 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7083 tribuf_calcntr = 0x9;
7084 } else if (vco <= 6200000) {
7085 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7086 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7087 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7088 tribuf_calcntr = 0x9;
7089 } else if (vco <= 6480000) {
7090 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7091 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7092 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7093 tribuf_calcntr = 0x8;
7094 } else {
7095 /* Not supported. Apply the same limits as in the max case */
7096 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7097 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7098 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7099 tribuf_calcntr = 0;
7100 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007101 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7102
Ville Syrjälä968040b2015-03-11 22:52:08 +02007103 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307104 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7105 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7106 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7107
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007108 /* AFC Recal */
7109 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7110 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7111 DPIO_AFC_RECAL);
7112
7113 mutex_unlock(&dev_priv->dpio_lock);
7114}
7115
Ville Syrjäläd288f652014-10-28 13:20:22 +02007116/**
7117 * vlv_force_pll_on - forcibly enable just the PLL
7118 * @dev_priv: i915 private structure
7119 * @pipe: pipe PLL to enable
7120 * @dpll: PLL configuration
7121 *
7122 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7123 * in cases where we need the PLL enabled even when @pipe is not going to
7124 * be enabled.
7125 */
7126void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7127 const struct dpll *dpll)
7128{
7129 struct intel_crtc *crtc =
7130 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007131 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007132 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007133 .pixel_multiplier = 1,
7134 .dpll = *dpll,
7135 };
7136
7137 if (IS_CHERRYVIEW(dev)) {
7138 chv_update_pll(crtc, &pipe_config);
7139 chv_prepare_pll(crtc, &pipe_config);
7140 chv_enable_pll(crtc, &pipe_config);
7141 } else {
7142 vlv_update_pll(crtc, &pipe_config);
7143 vlv_prepare_pll(crtc, &pipe_config);
7144 vlv_enable_pll(crtc, &pipe_config);
7145 }
7146}
7147
7148/**
7149 * vlv_force_pll_off - forcibly disable just the PLL
7150 * @dev_priv: i915 private structure
7151 * @pipe: pipe PLL to disable
7152 *
7153 * Disable the PLL for @pipe. To be used in cases where we need
7154 * the PLL enabled even when @pipe is not going to be enabled.
7155 */
7156void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7157{
7158 if (IS_CHERRYVIEW(dev))
7159 chv_disable_pll(to_i915(dev), pipe);
7160 else
7161 vlv_disable_pll(to_i915(dev), pipe);
7162}
7163
Daniel Vetterf47709a2013-03-28 10:42:02 +01007164static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007165 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007166 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007167 int num_connectors)
7168{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007169 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007170 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007171 u32 dpll;
7172 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007173 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007174
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007175 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307176
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007177 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7178 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007179
7180 dpll = DPLL_VGA_MODE_DIS;
7181
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007182 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007183 dpll |= DPLLB_MODE_LVDS;
7184 else
7185 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007186
Daniel Vetteref1b4602013-06-01 17:17:04 +02007187 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007188 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007189 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007190 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007191
7192 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007193 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007194
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007195 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007196 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007197
7198 /* compute bitmask from p1 value */
7199 if (IS_PINEVIEW(dev))
7200 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7201 else {
7202 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7203 if (IS_G4X(dev) && reduced_clock)
7204 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7205 }
7206 switch (clock->p2) {
7207 case 5:
7208 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7209 break;
7210 case 7:
7211 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7212 break;
7213 case 10:
7214 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7215 break;
7216 case 14:
7217 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7218 break;
7219 }
7220 if (INTEL_INFO(dev)->gen >= 4)
7221 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7222
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007223 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007224 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007225 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007226 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7227 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7228 else
7229 dpll |= PLL_REF_INPUT_DREFCLK;
7230
7231 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007232 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007233
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007234 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007235 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007236 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007237 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007238 }
7239}
7240
Daniel Vetterf47709a2013-03-28 10:42:02 +01007241static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007242 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007243 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007244 int num_connectors)
7245{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007246 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007247 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007248 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007249 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007250
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007251 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307252
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007253 dpll = DPLL_VGA_MODE_DIS;
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 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7257 } else {
7258 if (clock->p1 == 2)
7259 dpll |= PLL_P1_DIVIDE_BY_TWO;
7260 else
7261 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7262 if (clock->p2 == 4)
7263 dpll |= PLL_P2_DIVIDE_BY_4;
7264 }
7265
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007266 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007267 dpll |= DPLL_DVO_2X_MODE;
7268
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007269 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007270 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7271 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7272 else
7273 dpll |= PLL_REF_INPUT_DREFCLK;
7274
7275 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007276 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007277}
7278
Daniel Vetter8a654f32013-06-01 17:16:22 +02007279static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007280{
7281 struct drm_device *dev = intel_crtc->base.dev;
7282 struct drm_i915_private *dev_priv = dev->dev_private;
7283 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007284 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007285 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007286 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007287 uint32_t crtc_vtotal, crtc_vblank_end;
7288 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007289
7290 /* We need to be careful not to changed the adjusted mode, for otherwise
7291 * the hw state checker will get angry at the mismatch. */
7292 crtc_vtotal = adjusted_mode->crtc_vtotal;
7293 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007294
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007295 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007296 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007297 crtc_vtotal -= 1;
7298 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007299
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007300 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007301 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7302 else
7303 vsyncshift = adjusted_mode->crtc_hsync_start -
7304 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007305 if (vsyncshift < 0)
7306 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007307 }
7308
7309 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007310 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007311
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007312 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007313 (adjusted_mode->crtc_hdisplay - 1) |
7314 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007315 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007316 (adjusted_mode->crtc_hblank_start - 1) |
7317 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007318 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007319 (adjusted_mode->crtc_hsync_start - 1) |
7320 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7321
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007322 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007323 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007324 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007325 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007326 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007327 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007328 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007329 (adjusted_mode->crtc_vsync_start - 1) |
7330 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7331
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007332 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7333 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7334 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7335 * bits. */
7336 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7337 (pipe == PIPE_B || pipe == PIPE_C))
7338 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7339
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007340 /* pipesrc controls the size that is scaled from, which should
7341 * always be the user's requested size.
7342 */
7343 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007344 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7345 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007346}
7347
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007348static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007349 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007350{
7351 struct drm_device *dev = crtc->base.dev;
7352 struct drm_i915_private *dev_priv = dev->dev_private;
7353 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7354 uint32_t tmp;
7355
7356 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007357 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7358 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007359 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007360 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7361 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007362 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007363 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7364 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007365
7366 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007367 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7368 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007369 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007370 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7371 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007372 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007373 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7374 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007375
7376 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007377 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7378 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7379 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007380 }
7381
7382 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007383 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7384 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7385
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007386 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7387 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007388}
7389
Daniel Vetterf6a83282014-02-11 15:28:57 -08007390void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007391 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007392{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007393 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7394 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7395 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7396 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007397
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007398 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7399 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7400 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7401 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007402
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007403 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007404
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007405 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7406 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007407}
7408
Daniel Vetter84b046f2013-02-19 18:48:54 +01007409static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7410{
7411 struct drm_device *dev = intel_crtc->base.dev;
7412 struct drm_i915_private *dev_priv = dev->dev_private;
7413 uint32_t pipeconf;
7414
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007415 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007416
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007417 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7418 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7419 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007420
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007421 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007422 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007423
Daniel Vetterff9ce462013-04-24 14:57:17 +02007424 /* only g4x and later have fancy bpc/dither controls */
7425 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007426 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007427 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007428 pipeconf |= PIPECONF_DITHER_EN |
7429 PIPECONF_DITHER_TYPE_SP;
7430
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007431 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007432 case 18:
7433 pipeconf |= PIPECONF_6BPC;
7434 break;
7435 case 24:
7436 pipeconf |= PIPECONF_8BPC;
7437 break;
7438 case 30:
7439 pipeconf |= PIPECONF_10BPC;
7440 break;
7441 default:
7442 /* Case prevented by intel_choose_pipe_bpp_dither. */
7443 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007444 }
7445 }
7446
7447 if (HAS_PIPE_CXSR(dev)) {
7448 if (intel_crtc->lowfreq_avail) {
7449 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7450 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7451 } else {
7452 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007453 }
7454 }
7455
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007456 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007457 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007458 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007459 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7460 else
7461 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7462 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007463 pipeconf |= PIPECONF_PROGRESSIVE;
7464
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007465 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007466 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007467
Daniel Vetter84b046f2013-02-19 18:48:54 +01007468 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7469 POSTING_READ(PIPECONF(intel_crtc->pipe));
7470}
7471
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007472static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7473 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007474{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007475 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007476 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007477 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007478 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007479 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007480 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007481 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007482 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007483 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007484 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007485 struct drm_connector_state *connector_state;
7486 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007487
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007488 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007489 if (connector_state->crtc != &crtc->base)
7490 continue;
7491
7492 encoder = to_intel_encoder(connector_state->best_encoder);
7493
Chris Wilson5eddb702010-09-11 13:48:45 +01007494 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007495 case INTEL_OUTPUT_LVDS:
7496 is_lvds = true;
7497 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007498 case INTEL_OUTPUT_DSI:
7499 is_dsi = true;
7500 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007501 default:
7502 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007503 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007504
Eric Anholtc751ce42010-03-25 11:48:48 -07007505 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007506 }
7507
Jani Nikulaf2335332013-09-13 11:03:09 +03007508 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007509 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007510
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007511 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007512 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007513
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007514 /*
7515 * Returns a set of divisors for the desired target clock with
7516 * the given refclk, or FALSE. The returned values represent
7517 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7518 * 2) / p1 / p2.
7519 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007520 limit = intel_limit(crtc_state, refclk);
7521 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007522 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007523 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007524 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007525 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7526 return -EINVAL;
7527 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007528
Jani Nikulaf2335332013-09-13 11:03:09 +03007529 if (is_lvds && dev_priv->lvds_downclock_avail) {
7530 /*
7531 * Ensure we match the reduced clock's P to the target
7532 * clock. If the clocks don't match, we can't switch
7533 * the display clock by using the FP0/FP1. In such case
7534 * we will disable the LVDS downclock feature.
7535 */
7536 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007537 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007538 dev_priv->lvds_downclock,
7539 refclk, &clock,
7540 &reduced_clock);
7541 }
7542 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007543 crtc_state->dpll.n = clock.n;
7544 crtc_state->dpll.m1 = clock.m1;
7545 crtc_state->dpll.m2 = clock.m2;
7546 crtc_state->dpll.p1 = clock.p1;
7547 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007548 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007549
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007550 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007551 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307552 has_reduced_clock ? &reduced_clock : NULL,
7553 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007554 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007555 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007556 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007557 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007558 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007559 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007560 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007561 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007562 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007563
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007564 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007565}
7566
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007567static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007568 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007569{
7570 struct drm_device *dev = crtc->base.dev;
7571 struct drm_i915_private *dev_priv = dev->dev_private;
7572 uint32_t tmp;
7573
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007574 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7575 return;
7576
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007577 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007578 if (!(tmp & PFIT_ENABLE))
7579 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007580
Daniel Vetter06922822013-07-11 13:35:40 +02007581 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007582 if (INTEL_INFO(dev)->gen < 4) {
7583 if (crtc->pipe != PIPE_B)
7584 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007585 } else {
7586 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7587 return;
7588 }
7589
Daniel Vetter06922822013-07-11 13:35:40 +02007590 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007591 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7592 if (INTEL_INFO(dev)->gen < 5)
7593 pipe_config->gmch_pfit.lvds_border_bits =
7594 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7595}
7596
Jesse Barnesacbec812013-09-20 11:29:32 -07007597static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007598 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007599{
7600 struct drm_device *dev = crtc->base.dev;
7601 struct drm_i915_private *dev_priv = dev->dev_private;
7602 int pipe = pipe_config->cpu_transcoder;
7603 intel_clock_t clock;
7604 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007605 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007606
Shobhit Kumarf573de52014-07-30 20:32:37 +05307607 /* In case of MIPI DPLL will not even be used */
7608 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7609 return;
7610
Jesse Barnesacbec812013-09-20 11:29:32 -07007611 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007612 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07007613 mutex_unlock(&dev_priv->dpio_lock);
7614
7615 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7616 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7617 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7618 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7619 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7620
Ville Syrjäläf6466282013-10-14 14:50:31 +03007621 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007622
Ville Syrjäläf6466282013-10-14 14:50:31 +03007623 /* clock.dot is the fast clock */
7624 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007625}
7626
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007627static void
7628i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7629 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007630{
7631 struct drm_device *dev = crtc->base.dev;
7632 struct drm_i915_private *dev_priv = dev->dev_private;
7633 u32 val, base, offset;
7634 int pipe = crtc->pipe, plane = crtc->plane;
7635 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007636 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007637 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007638 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007639
Damien Lespiau42a7b082015-02-05 19:35:13 +00007640 val = I915_READ(DSPCNTR(plane));
7641 if (!(val & DISPLAY_PLANE_ENABLE))
7642 return;
7643
Damien Lespiaud9806c92015-01-21 14:07:19 +00007644 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007645 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007646 DRM_DEBUG_KMS("failed to alloc fb\n");
7647 return;
7648 }
7649
Damien Lespiau1b842c82015-01-21 13:50:54 +00007650 fb = &intel_fb->base;
7651
Daniel Vetter18c52472015-02-10 17:16:09 +00007652 if (INTEL_INFO(dev)->gen >= 4) {
7653 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007654 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007655 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7656 }
7657 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007658
7659 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007660 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007661 fb->pixel_format = fourcc;
7662 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007663
7664 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007665 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007666 offset = I915_READ(DSPTILEOFF(plane));
7667 else
7668 offset = I915_READ(DSPLINOFF(plane));
7669 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7670 } else {
7671 base = I915_READ(DSPADDR(plane));
7672 }
7673 plane_config->base = base;
7674
7675 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007676 fb->width = ((val >> 16) & 0xfff) + 1;
7677 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007678
7679 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007680 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007681
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007682 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007683 fb->pixel_format,
7684 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007685
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007686 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007687
Damien Lespiau2844a922015-01-20 12:51:48 +00007688 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7689 pipe_name(pipe), plane, fb->width, fb->height,
7690 fb->bits_per_pixel, base, fb->pitches[0],
7691 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007692
Damien Lespiau2d140302015-02-05 17:22:18 +00007693 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007694}
7695
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007696static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007697 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007698{
7699 struct drm_device *dev = crtc->base.dev;
7700 struct drm_i915_private *dev_priv = dev->dev_private;
7701 int pipe = pipe_config->cpu_transcoder;
7702 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7703 intel_clock_t clock;
7704 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7705 int refclk = 100000;
7706
7707 mutex_lock(&dev_priv->dpio_lock);
7708 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7709 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7710 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7711 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7712 mutex_unlock(&dev_priv->dpio_lock);
7713
7714 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7715 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7716 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7717 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7718 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7719
7720 chv_clock(refclk, &clock);
7721
7722 /* clock.dot is the fast clock */
7723 pipe_config->port_clock = clock.dot / 5;
7724}
7725
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007726static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007727 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007728{
7729 struct drm_device *dev = crtc->base.dev;
7730 struct drm_i915_private *dev_priv = dev->dev_private;
7731 uint32_t tmp;
7732
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007733 if (!intel_display_power_is_enabled(dev_priv,
7734 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007735 return false;
7736
Daniel Vettere143a212013-07-04 12:01:15 +02007737 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007738 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007739
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007740 tmp = I915_READ(PIPECONF(crtc->pipe));
7741 if (!(tmp & PIPECONF_ENABLE))
7742 return false;
7743
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007744 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7745 switch (tmp & PIPECONF_BPC_MASK) {
7746 case PIPECONF_6BPC:
7747 pipe_config->pipe_bpp = 18;
7748 break;
7749 case PIPECONF_8BPC:
7750 pipe_config->pipe_bpp = 24;
7751 break;
7752 case PIPECONF_10BPC:
7753 pipe_config->pipe_bpp = 30;
7754 break;
7755 default:
7756 break;
7757 }
7758 }
7759
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007760 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7761 pipe_config->limited_color_range = true;
7762
Ville Syrjälä282740f2013-09-04 18:30:03 +03007763 if (INTEL_INFO(dev)->gen < 4)
7764 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7765
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007766 intel_get_pipe_timings(crtc, pipe_config);
7767
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007768 i9xx_get_pfit_config(crtc, pipe_config);
7769
Daniel Vetter6c49f242013-06-06 12:45:25 +02007770 if (INTEL_INFO(dev)->gen >= 4) {
7771 tmp = I915_READ(DPLL_MD(crtc->pipe));
7772 pipe_config->pixel_multiplier =
7773 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7774 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007775 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02007776 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7777 tmp = I915_READ(DPLL(crtc->pipe));
7778 pipe_config->pixel_multiplier =
7779 ((tmp & SDVO_MULTIPLIER_MASK)
7780 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7781 } else {
7782 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7783 * port and will be fixed up in the encoder->get_config
7784 * function. */
7785 pipe_config->pixel_multiplier = 1;
7786 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007787 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7788 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007789 /*
7790 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7791 * on 830. Filter it out here so that we don't
7792 * report errors due to that.
7793 */
7794 if (IS_I830(dev))
7795 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7796
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007797 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7798 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007799 } else {
7800 /* Mask out read-only status bits. */
7801 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7802 DPLL_PORTC_READY_MASK |
7803 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007804 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007805
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007806 if (IS_CHERRYVIEW(dev))
7807 chv_crtc_clock_get(crtc, pipe_config);
7808 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07007809 vlv_crtc_clock_get(crtc, pipe_config);
7810 else
7811 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007812
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007813 return true;
7814}
7815
Paulo Zanonidde86e22012-12-01 12:04:25 -02007816static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007817{
7818 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007819 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007820 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007821 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007822 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007823 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007824 bool has_ck505 = false;
7825 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007826
7827 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01007828 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007829 switch (encoder->type) {
7830 case INTEL_OUTPUT_LVDS:
7831 has_panel = true;
7832 has_lvds = true;
7833 break;
7834 case INTEL_OUTPUT_EDP:
7835 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03007836 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007837 has_cpu_edp = true;
7838 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007839 default:
7840 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007841 }
7842 }
7843
Keith Packard99eb6a02011-09-26 14:29:12 -07007844 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007845 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007846 can_ssc = has_ck505;
7847 } else {
7848 has_ck505 = false;
7849 can_ssc = true;
7850 }
7851
Imre Deak2de69052013-05-08 13:14:04 +03007852 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7853 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007854
7855 /* Ironlake: try to setup display ref clock before DPLL
7856 * enabling. This is only under driver's control after
7857 * PCH B stepping, previous chipset stepping should be
7858 * ignoring this setting.
7859 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007860 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007861
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007862 /* As we must carefully and slowly disable/enable each source in turn,
7863 * compute the final state we want first and check if we need to
7864 * make any changes at all.
7865 */
7866 final = val;
7867 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007868 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007869 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007870 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007871 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7872
7873 final &= ~DREF_SSC_SOURCE_MASK;
7874 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7875 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007876
Keith Packard199e5d72011-09-22 12:01:57 -07007877 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007878 final |= DREF_SSC_SOURCE_ENABLE;
7879
7880 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7881 final |= DREF_SSC1_ENABLE;
7882
7883 if (has_cpu_edp) {
7884 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7885 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7886 else
7887 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7888 } else
7889 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7890 } else {
7891 final |= DREF_SSC_SOURCE_DISABLE;
7892 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7893 }
7894
7895 if (final == val)
7896 return;
7897
7898 /* Always enable nonspread source */
7899 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7900
7901 if (has_ck505)
7902 val |= DREF_NONSPREAD_CK505_ENABLE;
7903 else
7904 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7905
7906 if (has_panel) {
7907 val &= ~DREF_SSC_SOURCE_MASK;
7908 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007909
Keith Packard199e5d72011-09-22 12:01:57 -07007910 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007911 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007912 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007913 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007914 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007915 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007916
7917 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007918 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007919 POSTING_READ(PCH_DREF_CONTROL);
7920 udelay(200);
7921
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007922 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007923
7924 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007925 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007926 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007927 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007928 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007929 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007930 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007931 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007932 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007933
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007934 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007935 POSTING_READ(PCH_DREF_CONTROL);
7936 udelay(200);
7937 } else {
7938 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7939
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007940 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007941
7942 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007943 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007944
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007945 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007946 POSTING_READ(PCH_DREF_CONTROL);
7947 udelay(200);
7948
7949 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007950 val &= ~DREF_SSC_SOURCE_MASK;
7951 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007952
7953 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007954 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007955
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007956 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007957 POSTING_READ(PCH_DREF_CONTROL);
7958 udelay(200);
7959 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007960
7961 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007962}
7963
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007964static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007965{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007966 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007967
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007968 tmp = I915_READ(SOUTH_CHICKEN2);
7969 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7970 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007971
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007972 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7973 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7974 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007975
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007976 tmp = I915_READ(SOUTH_CHICKEN2);
7977 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7978 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007979
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007980 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7981 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7982 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007983}
7984
7985/* WaMPhyProgramming:hsw */
7986static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7987{
7988 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007989
7990 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7991 tmp &= ~(0xFF << 24);
7992 tmp |= (0x12 << 24);
7993 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7994
Paulo Zanonidde86e22012-12-01 12:04:25 -02007995 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7996 tmp |= (1 << 11);
7997 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7998
7999 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8000 tmp |= (1 << 11);
8001 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8002
Paulo Zanonidde86e22012-12-01 12:04:25 -02008003 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8004 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8005 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8006
8007 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8008 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8009 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8010
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008011 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8012 tmp &= ~(7 << 13);
8013 tmp |= (5 << 13);
8014 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008015
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008016 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8017 tmp &= ~(7 << 13);
8018 tmp |= (5 << 13);
8019 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008020
8021 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8022 tmp &= ~0xFF;
8023 tmp |= 0x1C;
8024 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8025
8026 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8027 tmp &= ~0xFF;
8028 tmp |= 0x1C;
8029 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8030
8031 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8032 tmp &= ~(0xFF << 16);
8033 tmp |= (0x1C << 16);
8034 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8035
8036 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8037 tmp &= ~(0xFF << 16);
8038 tmp |= (0x1C << 16);
8039 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8040
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008041 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8042 tmp |= (1 << 27);
8043 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008044
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008045 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8046 tmp |= (1 << 27);
8047 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008048
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008049 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8050 tmp &= ~(0xF << 28);
8051 tmp |= (4 << 28);
8052 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008053
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008054 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8055 tmp &= ~(0xF << 28);
8056 tmp |= (4 << 28);
8057 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008058}
8059
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008060/* Implements 3 different sequences from BSpec chapter "Display iCLK
8061 * Programming" based on the parameters passed:
8062 * - Sequence to enable CLKOUT_DP
8063 * - Sequence to enable CLKOUT_DP without spread
8064 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8065 */
8066static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8067 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008068{
8069 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008070 uint32_t reg, tmp;
8071
8072 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8073 with_spread = true;
8074 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8075 with_fdi, "LP PCH doesn't have FDI\n"))
8076 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008077
8078 mutex_lock(&dev_priv->dpio_lock);
8079
8080 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8081 tmp &= ~SBI_SSCCTL_DISABLE;
8082 tmp |= SBI_SSCCTL_PATHALT;
8083 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8084
8085 udelay(24);
8086
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008087 if (with_spread) {
8088 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8089 tmp &= ~SBI_SSCCTL_PATHALT;
8090 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008091
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008092 if (with_fdi) {
8093 lpt_reset_fdi_mphy(dev_priv);
8094 lpt_program_fdi_mphy(dev_priv);
8095 }
8096 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008097
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008098 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8099 SBI_GEN0 : SBI_DBUFF0;
8100 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8101 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8102 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008103
8104 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008105}
8106
Paulo Zanoni47701c32013-07-23 11:19:25 -03008107/* Sequence to disable CLKOUT_DP */
8108static void lpt_disable_clkout_dp(struct drm_device *dev)
8109{
8110 struct drm_i915_private *dev_priv = dev->dev_private;
8111 uint32_t reg, tmp;
8112
8113 mutex_lock(&dev_priv->dpio_lock);
8114
8115 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8116 SBI_GEN0 : SBI_DBUFF0;
8117 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8118 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8119 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8120
8121 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8122 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8123 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8124 tmp |= SBI_SSCCTL_PATHALT;
8125 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8126 udelay(32);
8127 }
8128 tmp |= SBI_SSCCTL_DISABLE;
8129 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8130 }
8131
8132 mutex_unlock(&dev_priv->dpio_lock);
8133}
8134
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008135static void lpt_init_pch_refclk(struct drm_device *dev)
8136{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008137 struct intel_encoder *encoder;
8138 bool has_vga = false;
8139
Damien Lespiaub2784e12014-08-05 11:29:37 +01008140 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008141 switch (encoder->type) {
8142 case INTEL_OUTPUT_ANALOG:
8143 has_vga = true;
8144 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008145 default:
8146 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008147 }
8148 }
8149
Paulo Zanoni47701c32013-07-23 11:19:25 -03008150 if (has_vga)
8151 lpt_enable_clkout_dp(dev, true, true);
8152 else
8153 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008154}
8155
Paulo Zanonidde86e22012-12-01 12:04:25 -02008156/*
8157 * Initialize reference clocks when the driver loads
8158 */
8159void intel_init_pch_refclk(struct drm_device *dev)
8160{
8161 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8162 ironlake_init_pch_refclk(dev);
8163 else if (HAS_PCH_LPT(dev))
8164 lpt_init_pch_refclk(dev);
8165}
8166
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008167static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008168{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008169 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008170 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008171 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008172 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008173 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008174 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008175 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008176 bool is_lvds = false;
8177
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008178 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008179 if (connector_state->crtc != crtc_state->base.crtc)
8180 continue;
8181
8182 encoder = to_intel_encoder(connector_state->best_encoder);
8183
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008184 switch (encoder->type) {
8185 case INTEL_OUTPUT_LVDS:
8186 is_lvds = true;
8187 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008188 default:
8189 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008190 }
8191 num_connectors++;
8192 }
8193
8194 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008195 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008196 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008197 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008198 }
8199
8200 return 120000;
8201}
8202
Daniel Vetter6ff93602013-04-19 11:24:36 +02008203static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008204{
8205 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8206 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8207 int pipe = intel_crtc->pipe;
8208 uint32_t val;
8209
Daniel Vetter78114072013-06-13 00:54:57 +02008210 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008211
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008212 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008213 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008214 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008215 break;
8216 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008217 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008218 break;
8219 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008220 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008221 break;
8222 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008223 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008224 break;
8225 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008226 /* Case prevented by intel_choose_pipe_bpp_dither. */
8227 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008228 }
8229
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008230 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008231 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8232
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008233 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008234 val |= PIPECONF_INTERLACED_ILK;
8235 else
8236 val |= PIPECONF_PROGRESSIVE;
8237
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008238 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008239 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008240
Paulo Zanonic8203562012-09-12 10:06:29 -03008241 I915_WRITE(PIPECONF(pipe), val);
8242 POSTING_READ(PIPECONF(pipe));
8243}
8244
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008245/*
8246 * Set up the pipe CSC unit.
8247 *
8248 * Currently only full range RGB to limited range RGB conversion
8249 * is supported, but eventually this should handle various
8250 * RGB<->YCbCr scenarios as well.
8251 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008252static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008253{
8254 struct drm_device *dev = crtc->dev;
8255 struct drm_i915_private *dev_priv = dev->dev_private;
8256 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8257 int pipe = intel_crtc->pipe;
8258 uint16_t coeff = 0x7800; /* 1.0 */
8259
8260 /*
8261 * TODO: Check what kind of values actually come out of the pipe
8262 * with these coeff/postoff values and adjust to get the best
8263 * accuracy. Perhaps we even need to take the bpc value into
8264 * consideration.
8265 */
8266
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008267 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008268 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8269
8270 /*
8271 * GY/GU and RY/RU should be the other way around according
8272 * to BSpec, but reality doesn't agree. Just set them up in
8273 * a way that results in the correct picture.
8274 */
8275 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8276 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8277
8278 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8279 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8280
8281 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8282 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8283
8284 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8285 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8286 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8287
8288 if (INTEL_INFO(dev)->gen > 6) {
8289 uint16_t postoff = 0;
8290
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008291 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008292 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008293
8294 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8295 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8296 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8297
8298 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8299 } else {
8300 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8301
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008302 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008303 mode |= CSC_BLACK_SCREEN_OFFSET;
8304
8305 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8306 }
8307}
8308
Daniel Vetter6ff93602013-04-19 11:24:36 +02008309static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008310{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008311 struct drm_device *dev = crtc->dev;
8312 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008313 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008314 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008315 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008316 uint32_t val;
8317
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008318 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008319
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008320 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008321 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8322
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008323 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008324 val |= PIPECONF_INTERLACED_ILK;
8325 else
8326 val |= PIPECONF_PROGRESSIVE;
8327
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008328 I915_WRITE(PIPECONF(cpu_transcoder), val);
8329 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008330
8331 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8332 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008333
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05308334 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008335 val = 0;
8336
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008337 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008338 case 18:
8339 val |= PIPEMISC_DITHER_6_BPC;
8340 break;
8341 case 24:
8342 val |= PIPEMISC_DITHER_8_BPC;
8343 break;
8344 case 30:
8345 val |= PIPEMISC_DITHER_10_BPC;
8346 break;
8347 case 36:
8348 val |= PIPEMISC_DITHER_12_BPC;
8349 break;
8350 default:
8351 /* Case prevented by pipe_config_set_bpp. */
8352 BUG();
8353 }
8354
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008355 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008356 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8357
8358 I915_WRITE(PIPEMISC(pipe), val);
8359 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008360}
8361
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008362static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008363 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008364 intel_clock_t *clock,
8365 bool *has_reduced_clock,
8366 intel_clock_t *reduced_clock)
8367{
8368 struct drm_device *dev = crtc->dev;
8369 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008370 int refclk;
8371 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008372 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008373
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008374 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008375
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008376 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008377
8378 /*
8379 * Returns a set of divisors for the desired target clock with the given
8380 * refclk, or FALSE. The returned values represent the clock equation:
8381 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8382 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008383 limit = intel_limit(crtc_state, refclk);
8384 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008385 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008386 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008387 if (!ret)
8388 return false;
8389
8390 if (is_lvds && dev_priv->lvds_downclock_avail) {
8391 /*
8392 * Ensure we match the reduced clock's P to the target clock.
8393 * If the clocks don't match, we can't switch the display clock
8394 * by using the FP0/FP1. In such case we will disable the LVDS
8395 * downclock feature.
8396 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008397 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008398 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008399 dev_priv->lvds_downclock,
8400 refclk, clock,
8401 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008402 }
8403
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008404 return true;
8405}
8406
Paulo Zanonid4b19312012-11-29 11:29:32 -02008407int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8408{
8409 /*
8410 * Account for spread spectrum to avoid
8411 * oversubscribing the link. Max center spread
8412 * is 2.5%; use 5% for safety's sake.
8413 */
8414 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008415 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008416}
8417
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008418static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008419{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008420 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008421}
8422
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008423static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008424 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008425 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008426 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008427{
8428 struct drm_crtc *crtc = &intel_crtc->base;
8429 struct drm_device *dev = crtc->dev;
8430 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008431 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008432 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008433 struct drm_connector_state *connector_state;
8434 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008435 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008436 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008437 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008438
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008439 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008440 if (connector_state->crtc != crtc_state->base.crtc)
8441 continue;
8442
8443 encoder = to_intel_encoder(connector_state->best_encoder);
8444
8445 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008446 case INTEL_OUTPUT_LVDS:
8447 is_lvds = true;
8448 break;
8449 case INTEL_OUTPUT_SDVO:
8450 case INTEL_OUTPUT_HDMI:
8451 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008452 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008453 default:
8454 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008455 }
8456
8457 num_connectors++;
8458 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008459
Chris Wilsonc1858122010-12-03 21:35:48 +00008460 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008461 factor = 21;
8462 if (is_lvds) {
8463 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008464 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008465 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008466 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008467 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008468 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008469
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008470 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008471 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008472
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008473 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8474 *fp2 |= FP_CB_TUNE;
8475
Chris Wilson5eddb702010-09-11 13:48:45 +01008476 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008477
Eric Anholta07d6782011-03-30 13:01:08 -07008478 if (is_lvds)
8479 dpll |= DPLLB_MODE_LVDS;
8480 else
8481 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008482
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008483 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008484 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008485
8486 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008487 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008488 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008489 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008490
Eric Anholta07d6782011-03-30 13:01:08 -07008491 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008492 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008493 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008494 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008495
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008496 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008497 case 5:
8498 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8499 break;
8500 case 7:
8501 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8502 break;
8503 case 10:
8504 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8505 break;
8506 case 14:
8507 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8508 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008509 }
8510
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008511 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008512 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008513 else
8514 dpll |= PLL_REF_INPUT_DREFCLK;
8515
Daniel Vetter959e16d2013-06-05 13:34:21 +02008516 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008517}
8518
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008519static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8520 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008521{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008522 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008523 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008524 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008525 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008526 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008527 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008528
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008529 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008530
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008531 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8532 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8533
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008534 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008535 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008536 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008537 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8538 return -EINVAL;
8539 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008540 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008541 if (!crtc_state->clock_set) {
8542 crtc_state->dpll.n = clock.n;
8543 crtc_state->dpll.m1 = clock.m1;
8544 crtc_state->dpll.m2 = clock.m2;
8545 crtc_state->dpll.p1 = clock.p1;
8546 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008547 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008548
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008549 /* 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 +02008550 if (crtc_state->has_pch_encoder) {
8551 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008552 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008553 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008554
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008555 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008556 &fp, &reduced_clock,
8557 has_reduced_clock ? &fp2 : NULL);
8558
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008559 crtc_state->dpll_hw_state.dpll = dpll;
8560 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008561 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008562 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008563 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008564 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008565
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008566 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008567 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008568 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008569 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008570 return -EINVAL;
8571 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008572 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008573
Rodrigo Viviab585de2015-03-24 12:40:09 -07008574 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008575 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008576 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008577 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008578
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008579 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008580}
8581
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008582static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8583 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008584{
8585 struct drm_device *dev = crtc->base.dev;
8586 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008587 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008588
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008589 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8590 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8591 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8592 & ~TU_SIZE_MASK;
8593 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8594 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8595 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8596}
8597
8598static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8599 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008600 struct intel_link_m_n *m_n,
8601 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008602{
8603 struct drm_device *dev = crtc->base.dev;
8604 struct drm_i915_private *dev_priv = dev->dev_private;
8605 enum pipe pipe = crtc->pipe;
8606
8607 if (INTEL_INFO(dev)->gen >= 5) {
8608 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8609 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8610 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8611 & ~TU_SIZE_MASK;
8612 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8613 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8614 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008615 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8616 * gen < 8) and if DRRS is supported (to make sure the
8617 * registers are not unnecessarily read).
8618 */
8619 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008620 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008621 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8622 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8623 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8624 & ~TU_SIZE_MASK;
8625 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8626 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8627 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8628 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008629 } else {
8630 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8631 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8632 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8633 & ~TU_SIZE_MASK;
8634 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8635 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8636 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8637 }
8638}
8639
8640void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008641 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008642{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008643 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008644 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8645 else
8646 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008647 &pipe_config->dp_m_n,
8648 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008649}
8650
Daniel Vetter72419202013-04-04 13:28:53 +02008651static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008652 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008653{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008654 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008655 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008656}
8657
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008658static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008659 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008660{
8661 struct drm_device *dev = crtc->base.dev;
8662 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008663 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8664 uint32_t ps_ctrl = 0;
8665 int id = -1;
8666 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008667
Chandra Kondurua1b22782015-04-07 15:28:45 -07008668 /* find scaler attached to this pipe */
8669 for (i = 0; i < crtc->num_scalers; i++) {
8670 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8671 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8672 id = i;
8673 pipe_config->pch_pfit.enabled = true;
8674 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8675 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8676 break;
8677 }
8678 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008679
Chandra Kondurua1b22782015-04-07 15:28:45 -07008680 scaler_state->scaler_id = id;
8681 if (id >= 0) {
8682 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8683 } else {
8684 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008685 }
8686}
8687
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008688static void
8689skylake_get_initial_plane_config(struct intel_crtc *crtc,
8690 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008691{
8692 struct drm_device *dev = crtc->base.dev;
8693 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008694 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008695 int pipe = crtc->pipe;
8696 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008697 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008698 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008699 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008700
Damien Lespiaud9806c92015-01-21 14:07:19 +00008701 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008702 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008703 DRM_DEBUG_KMS("failed to alloc fb\n");
8704 return;
8705 }
8706
Damien Lespiau1b842c82015-01-21 13:50:54 +00008707 fb = &intel_fb->base;
8708
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008709 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008710 if (!(val & PLANE_CTL_ENABLE))
8711 goto error;
8712
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008713 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8714 fourcc = skl_format_to_fourcc(pixel_format,
8715 val & PLANE_CTL_ORDER_RGBX,
8716 val & PLANE_CTL_ALPHA_MASK);
8717 fb->pixel_format = fourcc;
8718 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8719
Damien Lespiau40f46282015-02-27 11:15:21 +00008720 tiling = val & PLANE_CTL_TILED_MASK;
8721 switch (tiling) {
8722 case PLANE_CTL_TILED_LINEAR:
8723 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8724 break;
8725 case PLANE_CTL_TILED_X:
8726 plane_config->tiling = I915_TILING_X;
8727 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8728 break;
8729 case PLANE_CTL_TILED_Y:
8730 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8731 break;
8732 case PLANE_CTL_TILED_YF:
8733 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8734 break;
8735 default:
8736 MISSING_CASE(tiling);
8737 goto error;
8738 }
8739
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008740 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8741 plane_config->base = base;
8742
8743 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8744
8745 val = I915_READ(PLANE_SIZE(pipe, 0));
8746 fb->height = ((val >> 16) & 0xfff) + 1;
8747 fb->width = ((val >> 0) & 0x1fff) + 1;
8748
8749 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00008750 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8751 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008752 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8753
8754 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008755 fb->pixel_format,
8756 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008757
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008758 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008759
8760 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8761 pipe_name(pipe), fb->width, fb->height,
8762 fb->bits_per_pixel, base, fb->pitches[0],
8763 plane_config->size);
8764
Damien Lespiau2d140302015-02-05 17:22:18 +00008765 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008766 return;
8767
8768error:
8769 kfree(fb);
8770}
8771
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008772static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008773 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008774{
8775 struct drm_device *dev = crtc->base.dev;
8776 struct drm_i915_private *dev_priv = dev->dev_private;
8777 uint32_t tmp;
8778
8779 tmp = I915_READ(PF_CTL(crtc->pipe));
8780
8781 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008782 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008783 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8784 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008785
8786 /* We currently do not free assignements of panel fitters on
8787 * ivb/hsw (since we don't use the higher upscaling modes which
8788 * differentiates them) so just WARN about this case for now. */
8789 if (IS_GEN7(dev)) {
8790 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8791 PF_PIPE_SEL_IVB(crtc->pipe));
8792 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008793 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008794}
8795
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008796static void
8797ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8798 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008799{
8800 struct drm_device *dev = crtc->base.dev;
8801 struct drm_i915_private *dev_priv = dev->dev_private;
8802 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008803 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008804 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008805 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008806 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008807 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008808
Damien Lespiau42a7b082015-02-05 19:35:13 +00008809 val = I915_READ(DSPCNTR(pipe));
8810 if (!(val & DISPLAY_PLANE_ENABLE))
8811 return;
8812
Damien Lespiaud9806c92015-01-21 14:07:19 +00008813 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008814 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008815 DRM_DEBUG_KMS("failed to alloc fb\n");
8816 return;
8817 }
8818
Damien Lespiau1b842c82015-01-21 13:50:54 +00008819 fb = &intel_fb->base;
8820
Daniel Vetter18c52472015-02-10 17:16:09 +00008821 if (INTEL_INFO(dev)->gen >= 4) {
8822 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008823 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008824 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8825 }
8826 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008827
8828 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008829 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008830 fb->pixel_format = fourcc;
8831 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008832
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008833 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008834 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008835 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008836 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00008837 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008838 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008839 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008840 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008841 }
8842 plane_config->base = base;
8843
8844 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008845 fb->width = ((val >> 16) & 0xfff) + 1;
8846 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008847
8848 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008849 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008850
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008851 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008852 fb->pixel_format,
8853 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008854
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008855 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008856
Damien Lespiau2844a922015-01-20 12:51:48 +00008857 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8858 pipe_name(pipe), fb->width, fb->height,
8859 fb->bits_per_pixel, base, fb->pitches[0],
8860 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008861
Damien Lespiau2d140302015-02-05 17:22:18 +00008862 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008863}
8864
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008865static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008866 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008867{
8868 struct drm_device *dev = crtc->base.dev;
8869 struct drm_i915_private *dev_priv = dev->dev_private;
8870 uint32_t tmp;
8871
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008872 if (!intel_display_power_is_enabled(dev_priv,
8873 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008874 return false;
8875
Daniel Vettere143a212013-07-04 12:01:15 +02008876 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008877 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008878
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008879 tmp = I915_READ(PIPECONF(crtc->pipe));
8880 if (!(tmp & PIPECONF_ENABLE))
8881 return false;
8882
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008883 switch (tmp & PIPECONF_BPC_MASK) {
8884 case PIPECONF_6BPC:
8885 pipe_config->pipe_bpp = 18;
8886 break;
8887 case PIPECONF_8BPC:
8888 pipe_config->pipe_bpp = 24;
8889 break;
8890 case PIPECONF_10BPC:
8891 pipe_config->pipe_bpp = 30;
8892 break;
8893 case PIPECONF_12BPC:
8894 pipe_config->pipe_bpp = 36;
8895 break;
8896 default:
8897 break;
8898 }
8899
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008900 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8901 pipe_config->limited_color_range = true;
8902
Daniel Vetterab9412b2013-05-03 11:49:46 +02008903 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008904 struct intel_shared_dpll *pll;
8905
Daniel Vetter88adfff2013-03-28 10:42:01 +01008906 pipe_config->has_pch_encoder = true;
8907
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008908 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8909 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8910 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008911
8912 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008913
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008914 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02008915 pipe_config->shared_dpll =
8916 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008917 } else {
8918 tmp = I915_READ(PCH_DPLL_SEL);
8919 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8920 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8921 else
8922 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8923 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008924
8925 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8926
8927 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8928 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008929
8930 tmp = pipe_config->dpll_hw_state.dpll;
8931 pipe_config->pixel_multiplier =
8932 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8933 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008934
8935 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008936 } else {
8937 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008938 }
8939
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008940 intel_get_pipe_timings(crtc, pipe_config);
8941
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008942 ironlake_get_pfit_config(crtc, pipe_config);
8943
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008944 return true;
8945}
8946
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008947static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8948{
8949 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008950 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008951
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008952 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008953 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008954 pipe_name(crtc->pipe));
8955
Rob Clarke2c719b2014-12-15 13:56:32 -05008956 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8957 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8958 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8959 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8960 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8961 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008962 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03008963 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05008964 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008965 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008966 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008967 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008968 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008969 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008970 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008971
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008972 /*
8973 * In theory we can still leave IRQs enabled, as long as only the HPD
8974 * interrupts remain enabled. We used to check for that, but since it's
8975 * gen-specific and since we only disable LCPLL after we fully disable
8976 * the interrupts, the check below should be enough.
8977 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008978 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008979}
8980
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008981static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8982{
8983 struct drm_device *dev = dev_priv->dev;
8984
8985 if (IS_HASWELL(dev))
8986 return I915_READ(D_COMP_HSW);
8987 else
8988 return I915_READ(D_COMP_BDW);
8989}
8990
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008991static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8992{
8993 struct drm_device *dev = dev_priv->dev;
8994
8995 if (IS_HASWELL(dev)) {
8996 mutex_lock(&dev_priv->rps.hw_lock);
8997 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8998 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03008999 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009000 mutex_unlock(&dev_priv->rps.hw_lock);
9001 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009002 I915_WRITE(D_COMP_BDW, val);
9003 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009004 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009005}
9006
9007/*
9008 * This function implements pieces of two sequences from BSpec:
9009 * - Sequence for display software to disable LCPLL
9010 * - Sequence for display software to allow package C8+
9011 * The steps implemented here are just the steps that actually touch the LCPLL
9012 * register. Callers should take care of disabling all the display engine
9013 * functions, doing the mode unset, fixing interrupts, etc.
9014 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009015static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9016 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009017{
9018 uint32_t val;
9019
9020 assert_can_disable_lcpll(dev_priv);
9021
9022 val = I915_READ(LCPLL_CTL);
9023
9024 if (switch_to_fclk) {
9025 val |= LCPLL_CD_SOURCE_FCLK;
9026 I915_WRITE(LCPLL_CTL, val);
9027
9028 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9029 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9030 DRM_ERROR("Switching to FCLK failed\n");
9031
9032 val = I915_READ(LCPLL_CTL);
9033 }
9034
9035 val |= LCPLL_PLL_DISABLE;
9036 I915_WRITE(LCPLL_CTL, val);
9037 POSTING_READ(LCPLL_CTL);
9038
9039 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9040 DRM_ERROR("LCPLL still locked\n");
9041
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009042 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009043 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009044 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009045 ndelay(100);
9046
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009047 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9048 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009049 DRM_ERROR("D_COMP RCOMP still in progress\n");
9050
9051 if (allow_power_down) {
9052 val = I915_READ(LCPLL_CTL);
9053 val |= LCPLL_POWER_DOWN_ALLOW;
9054 I915_WRITE(LCPLL_CTL, val);
9055 POSTING_READ(LCPLL_CTL);
9056 }
9057}
9058
9059/*
9060 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9061 * source.
9062 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009063static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009064{
9065 uint32_t val;
9066
9067 val = I915_READ(LCPLL_CTL);
9068
9069 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9070 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9071 return;
9072
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009073 /*
9074 * Make sure we're not on PC8 state before disabling PC8, otherwise
9075 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009076 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009077 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009078
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009079 if (val & LCPLL_POWER_DOWN_ALLOW) {
9080 val &= ~LCPLL_POWER_DOWN_ALLOW;
9081 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009082 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009083 }
9084
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009085 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009086 val |= D_COMP_COMP_FORCE;
9087 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009088 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009089
9090 val = I915_READ(LCPLL_CTL);
9091 val &= ~LCPLL_PLL_DISABLE;
9092 I915_WRITE(LCPLL_CTL, val);
9093
9094 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9095 DRM_ERROR("LCPLL not locked yet\n");
9096
9097 if (val & LCPLL_CD_SOURCE_FCLK) {
9098 val = I915_READ(LCPLL_CTL);
9099 val &= ~LCPLL_CD_SOURCE_FCLK;
9100 I915_WRITE(LCPLL_CTL, val);
9101
9102 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9103 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9104 DRM_ERROR("Switching back to LCPLL failed\n");
9105 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009106
Mika Kuoppala59bad942015-01-16 11:34:40 +02009107 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009108}
9109
Paulo Zanoni765dab672014-03-07 20:08:18 -03009110/*
9111 * Package states C8 and deeper are really deep PC states that can only be
9112 * reached when all the devices on the system allow it, so even if the graphics
9113 * device allows PC8+, it doesn't mean the system will actually get to these
9114 * states. Our driver only allows PC8+ when going into runtime PM.
9115 *
9116 * The requirements for PC8+ are that all the outputs are disabled, the power
9117 * well is disabled and most interrupts are disabled, and these are also
9118 * requirements for runtime PM. When these conditions are met, we manually do
9119 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9120 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9121 * hang the machine.
9122 *
9123 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9124 * the state of some registers, so when we come back from PC8+ we need to
9125 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9126 * need to take care of the registers kept by RC6. Notice that this happens even
9127 * if we don't put the device in PCI D3 state (which is what currently happens
9128 * because of the runtime PM support).
9129 *
9130 * For more, read "Display Sequences for Package C8" on the hardware
9131 * documentation.
9132 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009133void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009134{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009135 struct drm_device *dev = dev_priv->dev;
9136 uint32_t val;
9137
Paulo Zanonic67a4702013-08-19 13:18:09 -03009138 DRM_DEBUG_KMS("Enabling package C8+\n");
9139
Paulo Zanonic67a4702013-08-19 13:18:09 -03009140 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9141 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9142 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9143 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9144 }
9145
9146 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009147 hsw_disable_lcpll(dev_priv, true, true);
9148}
9149
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009150void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009151{
9152 struct drm_device *dev = dev_priv->dev;
9153 uint32_t val;
9154
Paulo Zanonic67a4702013-08-19 13:18:09 -03009155 DRM_DEBUG_KMS("Disabling package C8+\n");
9156
9157 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009158 lpt_init_pch_refclk(dev);
9159
9160 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9161 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9162 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9163 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9164 }
9165
9166 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009167}
9168
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009169static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309170{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009171 struct drm_device *dev = old_state->dev;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309172 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009173 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309174 int req_cdclk;
9175
9176 /* see the comment in valleyview_modeset_global_resources */
9177 if (WARN_ON(max_pixclk < 0))
9178 return;
9179
9180 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9181
9182 if (req_cdclk != dev_priv->cdclk_freq)
9183 broxton_set_cdclk(dev, req_cdclk);
9184}
9185
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009186static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9187 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009188{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009189 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009190 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009191
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009192 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009193
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009194 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009195}
9196
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309197static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9198 enum port port,
9199 struct intel_crtc_state *pipe_config)
9200{
9201 switch (port) {
9202 case PORT_A:
9203 pipe_config->ddi_pll_sel = SKL_DPLL0;
9204 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9205 break;
9206 case PORT_B:
9207 pipe_config->ddi_pll_sel = SKL_DPLL1;
9208 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9209 break;
9210 case PORT_C:
9211 pipe_config->ddi_pll_sel = SKL_DPLL2;
9212 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9213 break;
9214 default:
9215 DRM_ERROR("Incorrect port type\n");
9216 }
9217}
9218
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009219static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9220 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009221 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009222{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009223 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009224
9225 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9226 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9227
9228 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009229 case SKL_DPLL0:
9230 /*
9231 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9232 * of the shared DPLL framework and thus needs to be read out
9233 * separately
9234 */
9235 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9236 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9237 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009238 case SKL_DPLL1:
9239 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9240 break;
9241 case SKL_DPLL2:
9242 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9243 break;
9244 case SKL_DPLL3:
9245 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9246 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009247 }
9248}
9249
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009250static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9251 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009252 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009253{
9254 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9255
9256 switch (pipe_config->ddi_pll_sel) {
9257 case PORT_CLK_SEL_WRPLL1:
9258 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9259 break;
9260 case PORT_CLK_SEL_WRPLL2:
9261 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9262 break;
9263 }
9264}
9265
Daniel Vetter26804af2014-06-25 22:01:55 +03009266static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009267 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009268{
9269 struct drm_device *dev = crtc->base.dev;
9270 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009271 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009272 enum port port;
9273 uint32_t tmp;
9274
9275 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9276
9277 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9278
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009279 if (IS_SKYLAKE(dev))
9280 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309281 else if (IS_BROXTON(dev))
9282 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009283 else
9284 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009285
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009286 if (pipe_config->shared_dpll >= 0) {
9287 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9288
9289 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9290 &pipe_config->dpll_hw_state));
9291 }
9292
Daniel Vetter26804af2014-06-25 22:01:55 +03009293 /*
9294 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9295 * DDI E. So just check whether this pipe is wired to DDI E and whether
9296 * the PCH transcoder is on.
9297 */
Damien Lespiauca370452013-12-03 13:56:24 +00009298 if (INTEL_INFO(dev)->gen < 9 &&
9299 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009300 pipe_config->has_pch_encoder = true;
9301
9302 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9303 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9304 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9305
9306 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9307 }
9308}
9309
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009310static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009311 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009312{
9313 struct drm_device *dev = crtc->base.dev;
9314 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009315 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009316 uint32_t tmp;
9317
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009318 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009319 POWER_DOMAIN_PIPE(crtc->pipe)))
9320 return false;
9321
Daniel Vettere143a212013-07-04 12:01:15 +02009322 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009323 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9324
Daniel Vettereccb1402013-05-22 00:50:22 +02009325 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9326 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9327 enum pipe trans_edp_pipe;
9328 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9329 default:
9330 WARN(1, "unknown pipe linked to edp transcoder\n");
9331 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9332 case TRANS_DDI_EDP_INPUT_A_ON:
9333 trans_edp_pipe = PIPE_A;
9334 break;
9335 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9336 trans_edp_pipe = PIPE_B;
9337 break;
9338 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9339 trans_edp_pipe = PIPE_C;
9340 break;
9341 }
9342
9343 if (trans_edp_pipe == crtc->pipe)
9344 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9345 }
9346
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009347 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009348 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009349 return false;
9350
Daniel Vettereccb1402013-05-22 00:50:22 +02009351 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009352 if (!(tmp & PIPECONF_ENABLE))
9353 return false;
9354
Daniel Vetter26804af2014-06-25 22:01:55 +03009355 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009356
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009357 intel_get_pipe_timings(crtc, pipe_config);
9358
Chandra Kondurua1b22782015-04-07 15:28:45 -07009359 if (INTEL_INFO(dev)->gen >= 9) {
9360 skl_init_scalers(dev, crtc, pipe_config);
9361 }
9362
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009363 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009364
9365 if (INTEL_INFO(dev)->gen >= 9) {
9366 pipe_config->scaler_state.scaler_id = -1;
9367 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9368 }
9369
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009370 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009371 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009372 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009373 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009374 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009375 else
9376 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009377 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009378
Jesse Barnese59150d2014-01-07 13:30:45 -08009379 if (IS_HASWELL(dev))
9380 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9381 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009382
Clint Taylorebb69c92014-09-30 10:30:22 -07009383 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9384 pipe_config->pixel_multiplier =
9385 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9386 } else {
9387 pipe_config->pixel_multiplier = 1;
9388 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009389
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009390 return true;
9391}
9392
Chris Wilson560b85b2010-08-07 11:01:38 +01009393static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9394{
9395 struct drm_device *dev = crtc->dev;
9396 struct drm_i915_private *dev_priv = dev->dev_private;
9397 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009398 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009399
Ville Syrjälädc41c152014-08-13 11:57:05 +03009400 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009401 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9402 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009403 unsigned int stride = roundup_pow_of_two(width) * 4;
9404
9405 switch (stride) {
9406 default:
9407 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9408 width, stride);
9409 stride = 256;
9410 /* fallthrough */
9411 case 256:
9412 case 512:
9413 case 1024:
9414 case 2048:
9415 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009416 }
9417
Ville Syrjälädc41c152014-08-13 11:57:05 +03009418 cntl |= CURSOR_ENABLE |
9419 CURSOR_GAMMA_ENABLE |
9420 CURSOR_FORMAT_ARGB |
9421 CURSOR_STRIDE(stride);
9422
9423 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009424 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009425
Ville Syrjälädc41c152014-08-13 11:57:05 +03009426 if (intel_crtc->cursor_cntl != 0 &&
9427 (intel_crtc->cursor_base != base ||
9428 intel_crtc->cursor_size != size ||
9429 intel_crtc->cursor_cntl != cntl)) {
9430 /* On these chipsets we can only modify the base/size/stride
9431 * whilst the cursor is disabled.
9432 */
9433 I915_WRITE(_CURACNTR, 0);
9434 POSTING_READ(_CURACNTR);
9435 intel_crtc->cursor_cntl = 0;
9436 }
9437
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009438 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009439 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009440 intel_crtc->cursor_base = base;
9441 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009442
9443 if (intel_crtc->cursor_size != size) {
9444 I915_WRITE(CURSIZE, size);
9445 intel_crtc->cursor_size = size;
9446 }
9447
Chris Wilson4b0e3332014-05-30 16:35:26 +03009448 if (intel_crtc->cursor_cntl != cntl) {
9449 I915_WRITE(_CURACNTR, cntl);
9450 POSTING_READ(_CURACNTR);
9451 intel_crtc->cursor_cntl = cntl;
9452 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009453}
9454
9455static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9456{
9457 struct drm_device *dev = crtc->dev;
9458 struct drm_i915_private *dev_priv = dev->dev_private;
9459 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9460 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009461 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009462
Chris Wilson4b0e3332014-05-30 16:35:26 +03009463 cntl = 0;
9464 if (base) {
9465 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009466 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309467 case 64:
9468 cntl |= CURSOR_MODE_64_ARGB_AX;
9469 break;
9470 case 128:
9471 cntl |= CURSOR_MODE_128_ARGB_AX;
9472 break;
9473 case 256:
9474 cntl |= CURSOR_MODE_256_ARGB_AX;
9475 break;
9476 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009477 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309478 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009479 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009480 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009481
9482 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9483 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009484 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009485
Matt Roper8e7d6882015-01-21 16:35:41 -08009486 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009487 cntl |= CURSOR_ROTATE_180;
9488
Chris Wilson4b0e3332014-05-30 16:35:26 +03009489 if (intel_crtc->cursor_cntl != cntl) {
9490 I915_WRITE(CURCNTR(pipe), cntl);
9491 POSTING_READ(CURCNTR(pipe));
9492 intel_crtc->cursor_cntl = cntl;
9493 }
9494
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009495 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009496 I915_WRITE(CURBASE(pipe), base);
9497 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009498
9499 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009500}
9501
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009502/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009503static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9504 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009505{
9506 struct drm_device *dev = crtc->dev;
9507 struct drm_i915_private *dev_priv = dev->dev_private;
9508 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9509 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009510 int x = crtc->cursor_x;
9511 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009512 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009513
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009514 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009515 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009516
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009517 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009518 base = 0;
9519
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009520 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009521 base = 0;
9522
9523 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009524 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009525 base = 0;
9526
9527 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9528 x = -x;
9529 }
9530 pos |= x << CURSOR_X_SHIFT;
9531
9532 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009533 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009534 base = 0;
9535
9536 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9537 y = -y;
9538 }
9539 pos |= y << CURSOR_Y_SHIFT;
9540
Chris Wilson4b0e3332014-05-30 16:35:26 +03009541 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009542 return;
9543
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009544 I915_WRITE(CURPOS(pipe), pos);
9545
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009546 /* ILK+ do this automagically */
9547 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009548 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009549 base += (intel_crtc->base.cursor->state->crtc_h *
9550 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009551 }
9552
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009553 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009554 i845_update_cursor(crtc, base);
9555 else
9556 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009557}
9558
Ville Syrjälädc41c152014-08-13 11:57:05 +03009559static bool cursor_size_ok(struct drm_device *dev,
9560 uint32_t width, uint32_t height)
9561{
9562 if (width == 0 || height == 0)
9563 return false;
9564
9565 /*
9566 * 845g/865g are special in that they are only limited by
9567 * the width of their cursors, the height is arbitrary up to
9568 * the precision of the register. Everything else requires
9569 * square cursors, limited to a few power-of-two sizes.
9570 */
9571 if (IS_845G(dev) || IS_I865G(dev)) {
9572 if ((width & 63) != 0)
9573 return false;
9574
9575 if (width > (IS_845G(dev) ? 64 : 512))
9576 return false;
9577
9578 if (height > 1023)
9579 return false;
9580 } else {
9581 switch (width | height) {
9582 case 256:
9583 case 128:
9584 if (IS_GEN2(dev))
9585 return false;
9586 case 64:
9587 break;
9588 default:
9589 return false;
9590 }
9591 }
9592
9593 return true;
9594}
9595
Jesse Barnes79e53942008-11-07 14:24:08 -08009596static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01009597 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08009598{
James Simmons72034252010-08-03 01:33:19 +01009599 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08009600 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08009601
James Simmons72034252010-08-03 01:33:19 +01009602 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009603 intel_crtc->lut_r[i] = red[i] >> 8;
9604 intel_crtc->lut_g[i] = green[i] >> 8;
9605 intel_crtc->lut_b[i] = blue[i] >> 8;
9606 }
9607
9608 intel_crtc_load_lut(crtc);
9609}
9610
Jesse Barnes79e53942008-11-07 14:24:08 -08009611/* VESA 640x480x72Hz mode to set on the pipe */
9612static struct drm_display_mode load_detect_mode = {
9613 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9614 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9615};
9616
Daniel Vettera8bb6812014-02-10 18:00:39 +01009617struct drm_framebuffer *
9618__intel_framebuffer_create(struct drm_device *dev,
9619 struct drm_mode_fb_cmd2 *mode_cmd,
9620 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01009621{
9622 struct intel_framebuffer *intel_fb;
9623 int ret;
9624
9625 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9626 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009627 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01009628 return ERR_PTR(-ENOMEM);
9629 }
9630
9631 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009632 if (ret)
9633 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009634
9635 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009636err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009637 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009638 kfree(intel_fb);
9639
9640 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009641}
9642
Daniel Vetterb5ea6422014-03-02 21:18:00 +01009643static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01009644intel_framebuffer_create(struct drm_device *dev,
9645 struct drm_mode_fb_cmd2 *mode_cmd,
9646 struct drm_i915_gem_object *obj)
9647{
9648 struct drm_framebuffer *fb;
9649 int ret;
9650
9651 ret = i915_mutex_lock_interruptible(dev);
9652 if (ret)
9653 return ERR_PTR(ret);
9654 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9655 mutex_unlock(&dev->struct_mutex);
9656
9657 return fb;
9658}
9659
Chris Wilsond2dff872011-04-19 08:36:26 +01009660static u32
9661intel_framebuffer_pitch_for_width(int width, int bpp)
9662{
9663 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9664 return ALIGN(pitch, 64);
9665}
9666
9667static u32
9668intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9669{
9670 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009671 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009672}
9673
9674static struct drm_framebuffer *
9675intel_framebuffer_create_for_mode(struct drm_device *dev,
9676 struct drm_display_mode *mode,
9677 int depth, int bpp)
9678{
9679 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009680 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009681
9682 obj = i915_gem_alloc_object(dev,
9683 intel_framebuffer_size_for_mode(mode, bpp));
9684 if (obj == NULL)
9685 return ERR_PTR(-ENOMEM);
9686
9687 mode_cmd.width = mode->hdisplay;
9688 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009689 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9690 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009691 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009692
9693 return intel_framebuffer_create(dev, &mode_cmd, obj);
9694}
9695
9696static struct drm_framebuffer *
9697mode_fits_in_fbdev(struct drm_device *dev,
9698 struct drm_display_mode *mode)
9699{
Daniel Vetter4520f532013-10-09 09:18:51 +02009700#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01009701 struct drm_i915_private *dev_priv = dev->dev_private;
9702 struct drm_i915_gem_object *obj;
9703 struct drm_framebuffer *fb;
9704
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009705 if (!dev_priv->fbdev)
9706 return NULL;
9707
9708 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009709 return NULL;
9710
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009711 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009712 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009713
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009714 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009715 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9716 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01009717 return NULL;
9718
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009719 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009720 return NULL;
9721
9722 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009723#else
9724 return NULL;
9725#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009726}
9727
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009728static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9729 struct drm_crtc *crtc,
9730 struct drm_display_mode *mode,
9731 struct drm_framebuffer *fb,
9732 int x, int y)
9733{
9734 struct drm_plane_state *plane_state;
9735 int hdisplay, vdisplay;
9736 int ret;
9737
9738 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9739 if (IS_ERR(plane_state))
9740 return PTR_ERR(plane_state);
9741
9742 if (mode)
9743 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
9744 else
9745 hdisplay = vdisplay = 0;
9746
9747 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9748 if (ret)
9749 return ret;
9750 drm_atomic_set_fb_for_plane(plane_state, fb);
9751 plane_state->crtc_x = 0;
9752 plane_state->crtc_y = 0;
9753 plane_state->crtc_w = hdisplay;
9754 plane_state->crtc_h = vdisplay;
9755 plane_state->src_x = x << 16;
9756 plane_state->src_y = y << 16;
9757 plane_state->src_w = hdisplay << 16;
9758 plane_state->src_h = vdisplay << 16;
9759
9760 return 0;
9761}
9762
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009763bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01009764 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05009765 struct intel_load_detect_pipe *old,
9766 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009767{
9768 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009769 struct intel_encoder *intel_encoder =
9770 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009771 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009772 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009773 struct drm_crtc *crtc = NULL;
9774 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02009775 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009776 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009777 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009778 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009779 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009780 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009781
Chris Wilsond2dff872011-04-19 08:36:26 +01009782 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009783 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009784 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009785
Rob Clark51fd3712013-11-19 12:10:12 -05009786retry:
9787 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9788 if (ret)
9789 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009790
Jesse Barnes79e53942008-11-07 14:24:08 -08009791 /*
9792 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009793 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009794 * - if the connector already has an assigned crtc, use it (but make
9795 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009796 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009797 * - try to find the first unused crtc that can drive this connector,
9798 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009799 */
9800
9801 /* See if we already have a CRTC for this connector */
9802 if (encoder->crtc) {
9803 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009804
Rob Clark51fd3712013-11-19 12:10:12 -05009805 ret = drm_modeset_lock(&crtc->mutex, ctx);
9806 if (ret)
9807 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009808 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9809 if (ret)
9810 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01009811
Daniel Vetter24218aa2012-08-12 19:27:11 +02009812 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009813 old->load_detect_temp = false;
9814
9815 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009816 if (connector->dpms != DRM_MODE_DPMS_ON)
9817 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01009818
Chris Wilson71731882011-04-19 23:10:58 +01009819 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08009820 }
9821
9822 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009823 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009824 i++;
9825 if (!(encoder->possible_crtcs & (1 << i)))
9826 continue;
Matt Roper83d65732015-02-25 13:12:16 -08009827 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +03009828 continue;
9829 /* This can occur when applying the pipe A quirk on resume. */
9830 if (to_intel_crtc(possible_crtc)->new_enabled)
9831 continue;
9832
9833 crtc = possible_crtc;
9834 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009835 }
9836
9837 /*
9838 * If we didn't find an unused CRTC, don't use any.
9839 */
9840 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01009841 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05009842 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08009843 }
9844
Rob Clark51fd3712013-11-19 12:10:12 -05009845 ret = drm_modeset_lock(&crtc->mutex, ctx);
9846 if (ret)
9847 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009848 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9849 if (ret)
9850 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02009851 intel_encoder->new_crtc = to_intel_crtc(crtc);
9852 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009853
9854 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009855 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +02009856 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009857 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01009858 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08009859
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009860 state = drm_atomic_state_alloc(dev);
9861 if (!state)
9862 return false;
9863
9864 state->acquire_ctx = ctx;
9865
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009866 connector_state = drm_atomic_get_connector_state(state, connector);
9867 if (IS_ERR(connector_state)) {
9868 ret = PTR_ERR(connector_state);
9869 goto fail;
9870 }
9871
9872 connector_state->crtc = crtc;
9873 connector_state->best_encoder = &intel_encoder->base;
9874
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009875 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9876 if (IS_ERR(crtc_state)) {
9877 ret = PTR_ERR(crtc_state);
9878 goto fail;
9879 }
9880
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +02009881 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009882
Chris Wilson64927112011-04-20 07:25:26 +01009883 if (!mode)
9884 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08009885
Chris Wilsond2dff872011-04-19 08:36:26 +01009886 /* We need a framebuffer large enough to accommodate all accesses
9887 * that the plane may generate whilst we perform load detection.
9888 * We can not rely on the fbcon either being present (we get called
9889 * during its initialisation to detect all boot displays, or it may
9890 * not even exist) or that it is large enough to satisfy the
9891 * requested mode.
9892 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02009893 fb = mode_fits_in_fbdev(dev, mode);
9894 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009895 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009896 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9897 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009898 } else
9899 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009900 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009901 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009902 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009903 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009904
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009905 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
9906 if (ret)
9907 goto fail;
9908
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +03009909 drm_mode_copy(&crtc_state->base.mode, mode);
9910
9911 if (intel_set_mode(crtc, state)) {
Chris Wilson64927112011-04-20 07:25:26 +01009912 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01009913 if (old->release_fb)
9914 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009915 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009916 }
Daniel Vetter9128b042015-03-03 17:31:21 +01009917 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +01009918
Jesse Barnes79e53942008-11-07 14:24:08 -08009919 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009920 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01009921 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009922
9923 fail:
Matt Roper83d65732015-02-25 13:12:16 -08009924 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -05009925fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +03009926 drm_atomic_state_free(state);
9927 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009928
Rob Clark51fd3712013-11-19 12:10:12 -05009929 if (ret == -EDEADLK) {
9930 drm_modeset_backoff(ctx);
9931 goto retry;
9932 }
9933
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009934 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009935}
9936
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009937void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +02009938 struct intel_load_detect_pipe *old,
9939 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009940{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009941 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009942 struct intel_encoder *intel_encoder =
9943 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01009944 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01009945 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009946 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009947 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009948 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009949 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009950 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009951
Chris Wilsond2dff872011-04-19 08:36:26 +01009952 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009953 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009954 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009955
Chris Wilson8261b192011-04-19 23:18:09 +01009956 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009957 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009958 if (!state)
9959 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009960
9961 state->acquire_ctx = ctx;
9962
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009963 connector_state = drm_atomic_get_connector_state(state, connector);
9964 if (IS_ERR(connector_state))
9965 goto fail;
9966
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009967 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9968 if (IS_ERR(crtc_state))
9969 goto fail;
9970
Daniel Vetterfc303102012-07-09 10:40:58 +02009971 to_intel_connector(connector)->new_encoder = NULL;
9972 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009973 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009974
9975 connector_state->best_encoder = NULL;
9976 connector_state->crtc = NULL;
9977
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +02009978 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009979
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009980 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
9981 0, 0);
9982 if (ret)
9983 goto fail;
9984
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +03009985 ret = intel_set_mode(crtc, state);
9986 if (ret)
9987 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +01009988
Daniel Vetter36206362012-12-10 20:42:17 +01009989 if (old->release_fb) {
9990 drm_framebuffer_unregister_private(old->release_fb);
9991 drm_framebuffer_unreference(old->release_fb);
9992 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009993
Chris Wilson0622a532011-04-21 09:32:11 +01009994 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009995 }
9996
Eric Anholtc751ce42010-03-25 11:48:48 -07009997 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009998 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9999 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010000
10001 return;
10002fail:
10003 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10004 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010005}
10006
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010007static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010008 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010009{
10010 struct drm_i915_private *dev_priv = dev->dev_private;
10011 u32 dpll = pipe_config->dpll_hw_state.dpll;
10012
10013 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010014 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010015 else if (HAS_PCH_SPLIT(dev))
10016 return 120000;
10017 else if (!IS_GEN2(dev))
10018 return 96000;
10019 else
10020 return 48000;
10021}
10022
Jesse Barnes79e53942008-11-07 14:24:08 -080010023/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010024static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010025 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010026{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010027 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010028 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010029 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010030 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010031 u32 fp;
10032 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010033 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010034
10035 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010036 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010037 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010038 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010039
10040 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010041 if (IS_PINEVIEW(dev)) {
10042 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10043 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010044 } else {
10045 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10046 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10047 }
10048
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010049 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010050 if (IS_PINEVIEW(dev))
10051 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10052 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010053 else
10054 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010055 DPLL_FPA01_P1_POST_DIV_SHIFT);
10056
10057 switch (dpll & DPLL_MODE_MASK) {
10058 case DPLLB_MODE_DAC_SERIAL:
10059 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10060 5 : 10;
10061 break;
10062 case DPLLB_MODE_LVDS:
10063 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10064 7 : 14;
10065 break;
10066 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010067 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010068 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010069 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010070 }
10071
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010072 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010073 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010074 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010075 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010076 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010077 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010078 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010079
10080 if (is_lvds) {
10081 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10082 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010083
10084 if (lvds & LVDS_CLKB_POWER_UP)
10085 clock.p2 = 7;
10086 else
10087 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010088 } else {
10089 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10090 clock.p1 = 2;
10091 else {
10092 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10093 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10094 }
10095 if (dpll & PLL_P2_DIVIDE_BY_4)
10096 clock.p2 = 4;
10097 else
10098 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010099 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010100
10101 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010102 }
10103
Ville Syrjälä18442d02013-09-13 16:00:08 +030010104 /*
10105 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010106 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010107 * encoder's get_config() function.
10108 */
10109 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010110}
10111
Ville Syrjälä6878da02013-09-13 15:59:11 +030010112int intel_dotclock_calculate(int link_freq,
10113 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010114{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010115 /*
10116 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010117 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010118 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010119 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010120 *
10121 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010122 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010123 */
10124
Ville Syrjälä6878da02013-09-13 15:59:11 +030010125 if (!m_n->link_n)
10126 return 0;
10127
10128 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10129}
10130
Ville Syrjälä18442d02013-09-13 16:00:08 +030010131static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010132 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010133{
10134 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010135
10136 /* read out port_clock from the DPLL */
10137 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010138
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010139 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010140 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010141 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010142 * agree once we know their relationship in the encoder's
10143 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010144 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010145 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010146 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10147 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010148}
10149
10150/** Returns the currently programmed mode of the given pipe. */
10151struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10152 struct drm_crtc *crtc)
10153{
Jesse Barnes548f2452011-02-17 10:40:53 -080010154 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010155 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010156 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010157 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010158 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010159 int htot = I915_READ(HTOTAL(cpu_transcoder));
10160 int hsync = I915_READ(HSYNC(cpu_transcoder));
10161 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10162 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010163 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010164
10165 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10166 if (!mode)
10167 return NULL;
10168
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010169 /*
10170 * Construct a pipe_config sufficient for getting the clock info
10171 * back out of crtc_clock_get.
10172 *
10173 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10174 * to use a real value here instead.
10175 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010176 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010177 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010178 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10179 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10180 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010181 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10182
Ville Syrjälä773ae032013-09-23 17:48:20 +030010183 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010184 mode->hdisplay = (htot & 0xffff) + 1;
10185 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10186 mode->hsync_start = (hsync & 0xffff) + 1;
10187 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10188 mode->vdisplay = (vtot & 0xffff) + 1;
10189 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10190 mode->vsync_start = (vsync & 0xffff) + 1;
10191 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10192
10193 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010194
10195 return mode;
10196}
10197
Jesse Barnes652c3932009-08-17 13:31:43 -070010198static void intel_decrease_pllclock(struct drm_crtc *crtc)
10199{
10200 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010201 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010202 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010203
Sonika Jindalbaff2962014-07-22 11:16:35 +053010204 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010205 return;
10206
10207 if (!dev_priv->lvds_downclock_avail)
10208 return;
10209
10210 /*
10211 * Since this is called by a timer, we should never get here in
10212 * the manual case.
10213 */
10214 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010215 int pipe = intel_crtc->pipe;
10216 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010217 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010218
Zhao Yakui44d98a62009-10-09 11:39:40 +080010219 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010220
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010221 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010222
Chris Wilson074b5e12012-05-02 12:07:06 +010010223 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010224 dpll |= DISPLAY_RATE_SELECT_FPA1;
10225 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010226 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010227 dpll = I915_READ(dpll_reg);
10228 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010229 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010230 }
10231
10232}
10233
Chris Wilsonf047e392012-07-21 12:31:41 +010010234void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010235{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010236 struct drm_i915_private *dev_priv = dev->dev_private;
10237
Chris Wilsonf62a0072014-02-21 17:55:39 +000010238 if (dev_priv->mm.busy)
10239 return;
10240
Paulo Zanoni43694d62014-03-07 20:08:08 -030010241 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010242 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010243 if (INTEL_INFO(dev)->gen >= 6)
10244 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010245 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010246}
10247
10248void intel_mark_idle(struct drm_device *dev)
10249{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010250 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010251 struct drm_crtc *crtc;
10252
Chris Wilsonf62a0072014-02-21 17:55:39 +000010253 if (!dev_priv->mm.busy)
10254 return;
10255
10256 dev_priv->mm.busy = false;
10257
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010258 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010259 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010260 continue;
10261
10262 intel_decrease_pllclock(crtc);
10263 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010264
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010265 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010266 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010267
Paulo Zanoni43694d62014-03-07 20:08:08 -030010268 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010269}
10270
Jesse Barnes79e53942008-11-07 14:24:08 -080010271static void intel_crtc_destroy(struct drm_crtc *crtc)
10272{
10273 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010274 struct drm_device *dev = crtc->dev;
10275 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010276
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010277 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010278 work = intel_crtc->unpin_work;
10279 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010280 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010281
10282 if (work) {
10283 cancel_work_sync(&work->work);
10284 kfree(work);
10285 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010286
10287 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010288
Jesse Barnes79e53942008-11-07 14:24:08 -080010289 kfree(intel_crtc);
10290}
10291
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010292static void intel_unpin_work_fn(struct work_struct *__work)
10293{
10294 struct intel_unpin_work *work =
10295 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010296 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010297 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010298
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010299 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010300 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010301 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010302
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010303 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010304
10305 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010306 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010307 mutex_unlock(&dev->struct_mutex);
10308
Daniel Vetterf99d7062014-06-19 16:01:59 +020010309 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010310 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010311
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010312 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10313 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10314
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010315 kfree(work);
10316}
10317
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010318static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010319 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010320{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010321 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10322 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010323 unsigned long flags;
10324
10325 /* Ignore early vblank irqs */
10326 if (intel_crtc == NULL)
10327 return;
10328
Daniel Vetterf3260382014-09-15 14:55:23 +020010329 /*
10330 * This is called both by irq handlers and the reset code (to complete
10331 * lost pageflips) so needs the full irqsave spinlocks.
10332 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010333 spin_lock_irqsave(&dev->event_lock, flags);
10334 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010335
10336 /* Ensure we don't miss a work->pending update ... */
10337 smp_rmb();
10338
10339 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010340 spin_unlock_irqrestore(&dev->event_lock, flags);
10341 return;
10342 }
10343
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010344 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010345
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010346 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010347}
10348
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010349void intel_finish_page_flip(struct drm_device *dev, int pipe)
10350{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010351 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010352 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10353
Mario Kleiner49b14a52010-12-09 07:00:07 +010010354 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010355}
10356
10357void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10358{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010359 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010360 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10361
Mario Kleiner49b14a52010-12-09 07:00:07 +010010362 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010363}
10364
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010365/* Is 'a' after or equal to 'b'? */
10366static bool g4x_flip_count_after_eq(u32 a, u32 b)
10367{
10368 return !((a - b) & 0x80000000);
10369}
10370
10371static bool page_flip_finished(struct intel_crtc *crtc)
10372{
10373 struct drm_device *dev = crtc->base.dev;
10374 struct drm_i915_private *dev_priv = dev->dev_private;
10375
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010376 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10377 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10378 return true;
10379
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010380 /*
10381 * The relevant registers doen't exist on pre-ctg.
10382 * As the flip done interrupt doesn't trigger for mmio
10383 * flips on gmch platforms, a flip count check isn't
10384 * really needed there. But since ctg has the registers,
10385 * include it in the check anyway.
10386 */
10387 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10388 return true;
10389
10390 /*
10391 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10392 * used the same base address. In that case the mmio flip might
10393 * have completed, but the CS hasn't even executed the flip yet.
10394 *
10395 * A flip count check isn't enough as the CS might have updated
10396 * the base address just after start of vblank, but before we
10397 * managed to process the interrupt. This means we'd complete the
10398 * CS flip too soon.
10399 *
10400 * Combining both checks should get us a good enough result. It may
10401 * still happen that the CS flip has been executed, but has not
10402 * yet actually completed. But in case the base address is the same
10403 * anyway, we don't really care.
10404 */
10405 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10406 crtc->unpin_work->gtt_offset &&
10407 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10408 crtc->unpin_work->flip_count);
10409}
10410
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010411void intel_prepare_page_flip(struct drm_device *dev, int plane)
10412{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010413 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010414 struct intel_crtc *intel_crtc =
10415 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10416 unsigned long flags;
10417
Daniel Vetterf3260382014-09-15 14:55:23 +020010418
10419 /*
10420 * This is called both by irq handlers and the reset code (to complete
10421 * lost pageflips) so needs the full irqsave spinlocks.
10422 *
10423 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010424 * generate a page-flip completion irq, i.e. every modeset
10425 * is also accompanied by a spurious intel_prepare_page_flip().
10426 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010427 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010428 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010429 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010430 spin_unlock_irqrestore(&dev->event_lock, flags);
10431}
10432
Robin Schroereba905b2014-05-18 02:24:50 +020010433static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010434{
10435 /* Ensure that the work item is consistent when activating it ... */
10436 smp_wmb();
10437 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10438 /* and that it is marked active as soon as the irq could fire. */
10439 smp_wmb();
10440}
10441
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010442static int intel_gen2_queue_flip(struct drm_device *dev,
10443 struct drm_crtc *crtc,
10444 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010445 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010446 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010447 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010448{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010449 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010450 u32 flip_mask;
10451 int ret;
10452
Daniel Vetter6d90c952012-04-26 23:28:05 +020010453 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010454 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010455 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010456
10457 /* Can't queue multiple flips, so wait for the previous
10458 * one to finish before executing the next.
10459 */
10460 if (intel_crtc->plane)
10461 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10462 else
10463 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010464 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10465 intel_ring_emit(ring, MI_NOOP);
10466 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10467 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10468 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010469 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010470 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010471
10472 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010473 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010474 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010475}
10476
10477static int intel_gen3_queue_flip(struct drm_device *dev,
10478 struct drm_crtc *crtc,
10479 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010480 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010481 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010482 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010483{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010484 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010485 u32 flip_mask;
10486 int ret;
10487
Daniel Vetter6d90c952012-04-26 23:28:05 +020010488 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010489 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010490 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010491
10492 if (intel_crtc->plane)
10493 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10494 else
10495 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010496 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10497 intel_ring_emit(ring, MI_NOOP);
10498 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10499 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10500 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010501 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010502 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010503
Chris Wilsone7d841c2012-12-03 11:36:30 +000010504 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010505 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010506 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010507}
10508
10509static int intel_gen4_queue_flip(struct drm_device *dev,
10510 struct drm_crtc *crtc,
10511 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010512 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010513 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010514 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010515{
10516 struct drm_i915_private *dev_priv = dev->dev_private;
10517 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10518 uint32_t pf, pipesrc;
10519 int ret;
10520
Daniel Vetter6d90c952012-04-26 23:28:05 +020010521 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010522 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010523 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010524
10525 /* i965+ uses the linear or tiled offsets from the
10526 * Display Registers (which do not change across a page-flip)
10527 * so we need only reprogram the base address.
10528 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010529 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10530 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10531 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010532 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010533 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010534
10535 /* XXX Enabling the panel-fitter across page-flip is so far
10536 * untested on non-native modes, so ignore it for now.
10537 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10538 */
10539 pf = 0;
10540 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010541 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010542
10543 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010544 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010545 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010546}
10547
10548static int intel_gen6_queue_flip(struct drm_device *dev,
10549 struct drm_crtc *crtc,
10550 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010551 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010552 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010553 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010554{
10555 struct drm_i915_private *dev_priv = dev->dev_private;
10556 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10557 uint32_t pf, pipesrc;
10558 int ret;
10559
Daniel Vetter6d90c952012-04-26 23:28:05 +020010560 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010561 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010562 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010563
Daniel Vetter6d90c952012-04-26 23:28:05 +020010564 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10565 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10566 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010567 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010568
Chris Wilson99d9acd2012-04-17 20:37:00 +010010569 /* Contrary to the suggestions in the documentation,
10570 * "Enable Panel Fitter" does not seem to be required when page
10571 * flipping with a non-native mode, and worse causes a normal
10572 * modeset to fail.
10573 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10574 */
10575 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010576 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010577 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010578
10579 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010580 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010581 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010582}
10583
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010584static int intel_gen7_queue_flip(struct drm_device *dev,
10585 struct drm_crtc *crtc,
10586 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010587 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010588 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010589 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010590{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010591 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010592 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010593 int len, ret;
10594
Robin Schroereba905b2014-05-18 02:24:50 +020010595 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010596 case PLANE_A:
10597 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10598 break;
10599 case PLANE_B:
10600 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10601 break;
10602 case PLANE_C:
10603 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10604 break;
10605 default:
10606 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010607 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010608 }
10609
Chris Wilsonffe74d72013-08-26 20:58:12 +010010610 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010611 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010612 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010613 /*
10614 * On Gen 8, SRM is now taking an extra dword to accommodate
10615 * 48bits addresses, and we need a NOOP for the batch size to
10616 * stay even.
10617 */
10618 if (IS_GEN8(dev))
10619 len += 2;
10620 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010621
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010622 /*
10623 * BSpec MI_DISPLAY_FLIP for IVB:
10624 * "The full packet must be contained within the same cache line."
10625 *
10626 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10627 * cacheline, if we ever start emitting more commands before
10628 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10629 * then do the cacheline alignment, and finally emit the
10630 * MI_DISPLAY_FLIP.
10631 */
10632 ret = intel_ring_cacheline_align(ring);
10633 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010634 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010635
Chris Wilsonffe74d72013-08-26 20:58:12 +010010636 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010637 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010638 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010639
Chris Wilsonffe74d72013-08-26 20:58:12 +010010640 /* Unmask the flip-done completion message. Note that the bspec says that
10641 * we should do this for both the BCS and RCS, and that we must not unmask
10642 * more than one flip event at any time (or ensure that one flip message
10643 * can be sent by waiting for flip-done prior to queueing new flips).
10644 * Experimentation says that BCS works despite DERRMR masking all
10645 * flip-done completion events and that unmasking all planes at once
10646 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10647 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10648 */
10649 if (ring->id == RCS) {
10650 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10651 intel_ring_emit(ring, DERRMR);
10652 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10653 DERRMR_PIPEB_PRI_FLIP_DONE |
10654 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010010655 if (IS_GEN8(dev))
10656 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10657 MI_SRM_LRM_GLOBAL_GTT);
10658 else
10659 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10660 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010010661 intel_ring_emit(ring, DERRMR);
10662 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010010663 if (IS_GEN8(dev)) {
10664 intel_ring_emit(ring, 0);
10665 intel_ring_emit(ring, MI_NOOP);
10666 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010667 }
10668
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010669 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010670 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010671 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010672 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000010673
10674 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010675 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010676 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010677}
10678
Sourab Gupta84c33a62014-06-02 16:47:17 +053010679static bool use_mmio_flip(struct intel_engine_cs *ring,
10680 struct drm_i915_gem_object *obj)
10681{
10682 /*
10683 * This is not being used for older platforms, because
10684 * non-availability of flip done interrupt forces us to use
10685 * CS flips. Older platforms derive flip done using some clever
10686 * tricks involving the flip_pending status bits and vblank irqs.
10687 * So using MMIO flips there would disrupt this mechanism.
10688 */
10689
Chris Wilson8e09bf82014-07-08 10:40:30 +010010690 if (ring == NULL)
10691 return true;
10692
Sourab Gupta84c33a62014-06-02 16:47:17 +053010693 if (INTEL_INFO(ring->dev)->gen < 5)
10694 return false;
10695
10696 if (i915.use_mmio_flip < 0)
10697 return false;
10698 else if (i915.use_mmio_flip > 0)
10699 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010010700 else if (i915.enable_execlists)
10701 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010702 else
John Harrison41c52412014-11-24 18:49:43 +000010703 return ring != i915_gem_request_get_ring(obj->last_read_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010704}
10705
Damien Lespiauff944562014-11-20 14:58:16 +000010706static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10707{
10708 struct drm_device *dev = intel_crtc->base.dev;
10709 struct drm_i915_private *dev_priv = dev->dev_private;
10710 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000010711 const enum pipe pipe = intel_crtc->pipe;
10712 u32 ctl, stride;
10713
10714 ctl = I915_READ(PLANE_CTL(pipe, 0));
10715 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010716 switch (fb->modifier[0]) {
10717 case DRM_FORMAT_MOD_NONE:
10718 break;
10719 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000010720 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010721 break;
10722 case I915_FORMAT_MOD_Y_TILED:
10723 ctl |= PLANE_CTL_TILED_Y;
10724 break;
10725 case I915_FORMAT_MOD_Yf_TILED:
10726 ctl |= PLANE_CTL_TILED_YF;
10727 break;
10728 default:
10729 MISSING_CASE(fb->modifier[0]);
10730 }
Damien Lespiauff944562014-11-20 14:58:16 +000010731
10732 /*
10733 * The stride is either expressed as a multiple of 64 bytes chunks for
10734 * linear buffers or in number of tiles for tiled buffers.
10735 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010736 stride = fb->pitches[0] /
10737 intel_fb_stride_alignment(dev, fb->modifier[0],
10738 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000010739
10740 /*
10741 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10742 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10743 */
10744 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10745 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10746
10747 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10748 POSTING_READ(PLANE_SURF(pipe, 0));
10749}
10750
10751static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010752{
10753 struct drm_device *dev = intel_crtc->base.dev;
10754 struct drm_i915_private *dev_priv = dev->dev_private;
10755 struct intel_framebuffer *intel_fb =
10756 to_intel_framebuffer(intel_crtc->base.primary->fb);
10757 struct drm_i915_gem_object *obj = intel_fb->obj;
10758 u32 dspcntr;
10759 u32 reg;
10760
Sourab Gupta84c33a62014-06-02 16:47:17 +053010761 reg = DSPCNTR(intel_crtc->plane);
10762 dspcntr = I915_READ(reg);
10763
Damien Lespiauc5d97472014-10-25 00:11:11 +010010764 if (obj->tiling_mode != I915_TILING_NONE)
10765 dspcntr |= DISPPLANE_TILED;
10766 else
10767 dspcntr &= ~DISPPLANE_TILED;
10768
Sourab Gupta84c33a62014-06-02 16:47:17 +053010769 I915_WRITE(reg, dspcntr);
10770
10771 I915_WRITE(DSPSURF(intel_crtc->plane),
10772 intel_crtc->unpin_work->gtt_offset);
10773 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010774
Damien Lespiauff944562014-11-20 14:58:16 +000010775}
10776
10777/*
10778 * XXX: This is the temporary way to update the plane registers until we get
10779 * around to using the usual plane update functions for MMIO flips
10780 */
10781static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10782{
10783 struct drm_device *dev = intel_crtc->base.dev;
10784 bool atomic_update;
10785 u32 start_vbl_count;
10786
10787 intel_mark_page_flip_active(intel_crtc);
10788
10789 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10790
10791 if (INTEL_INFO(dev)->gen >= 9)
10792 skl_do_mmio_flip(intel_crtc);
10793 else
10794 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10795 ilk_do_mmio_flip(intel_crtc);
10796
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010797 if (atomic_update)
10798 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010799}
10800
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010801static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010802{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010803 struct intel_mmio_flip *mmio_flip =
10804 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010805
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010806 if (mmio_flip->rq)
10807 WARN_ON(__i915_wait_request(mmio_flip->rq,
10808 mmio_flip->crtc->reset_counter,
10809 false, NULL, NULL));
Sourab Gupta84c33a62014-06-02 16:47:17 +053010810
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010811 intel_do_mmio_flip(mmio_flip->crtc);
10812
10813 i915_gem_request_unreference__unlocked(mmio_flip->rq);
10814 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010815}
10816
10817static int intel_queue_mmio_flip(struct drm_device *dev,
10818 struct drm_crtc *crtc,
10819 struct drm_framebuffer *fb,
10820 struct drm_i915_gem_object *obj,
10821 struct intel_engine_cs *ring,
10822 uint32_t flags)
10823{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010824 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010825
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010826 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
10827 if (mmio_flip == NULL)
10828 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010829
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010830 mmio_flip->rq = i915_gem_request_reference(obj->last_write_req);
10831 mmio_flip->crtc = to_intel_crtc(crtc);
10832
10833 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
10834 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020010835
Sourab Gupta84c33a62014-06-02 16:47:17 +053010836 return 0;
10837}
10838
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010839static int intel_default_queue_flip(struct drm_device *dev,
10840 struct drm_crtc *crtc,
10841 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010842 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010843 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010844 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010845{
10846 return -ENODEV;
10847}
10848
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010849static bool __intel_pageflip_stall_check(struct drm_device *dev,
10850 struct drm_crtc *crtc)
10851{
10852 struct drm_i915_private *dev_priv = dev->dev_private;
10853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10854 struct intel_unpin_work *work = intel_crtc->unpin_work;
10855 u32 addr;
10856
10857 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10858 return true;
10859
10860 if (!work->enable_stall_check)
10861 return false;
10862
10863 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010010864 if (work->flip_queued_req &&
10865 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010866 return false;
10867
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010868 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010869 }
10870
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010871 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010872 return false;
10873
10874 /* Potential stall - if we see that the flip has happened,
10875 * assume a missed interrupt. */
10876 if (INTEL_INFO(dev)->gen >= 4)
10877 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10878 else
10879 addr = I915_READ(DSPADDR(intel_crtc->plane));
10880
10881 /* There is a potential issue here with a false positive after a flip
10882 * to the same address. We could address this by checking for a
10883 * non-incrementing frame counter.
10884 */
10885 return addr == work->gtt_offset;
10886}
10887
10888void intel_check_page_flip(struct drm_device *dev, int pipe)
10889{
10890 struct drm_i915_private *dev_priv = dev->dev_private;
10891 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10892 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010893 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020010894
Dave Gordon6c51d462015-03-06 15:34:26 +000010895 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010896
10897 if (crtc == NULL)
10898 return;
10899
Daniel Vetterf3260382014-09-15 14:55:23 +020010900 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010901 work = intel_crtc->unpin_work;
10902 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010903 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010010904 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010905 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010906 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010907 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010010908 if (work != NULL &&
10909 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10910 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020010911 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010912}
10913
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010914static int intel_crtc_page_flip(struct drm_crtc *crtc,
10915 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010916 struct drm_pending_vblank_event *event,
10917 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010918{
10919 struct drm_device *dev = crtc->dev;
10920 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070010921 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070010922 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010923 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080010924 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020010925 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010926 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010927 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010928 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010010929 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010930
Matt Roper2ff8fde2014-07-08 07:50:07 -070010931 /*
10932 * drm_mode_page_flip_ioctl() should already catch this, but double
10933 * check to be safe. In the future we may enable pageflipping from
10934 * a disabled primary plane.
10935 */
10936 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10937 return -EBUSY;
10938
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010939 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070010940 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010941 return -EINVAL;
10942
10943 /*
10944 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10945 * Note that pitch changes could also affect these register.
10946 */
10947 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070010948 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10949 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010950 return -EINVAL;
10951
Chris Wilsonf900db42014-02-20 09:26:13 +000010952 if (i915_terminally_wedged(&dev_priv->gpu_error))
10953 goto out_hang;
10954
Daniel Vetterb14c5672013-09-19 12:18:32 +020010955 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010956 if (work == NULL)
10957 return -ENOMEM;
10958
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010959 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010960 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010961 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010962 INIT_WORK(&work->work, intel_unpin_work_fn);
10963
Daniel Vetter87b6b102014-05-15 15:33:46 +020010964 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010965 if (ret)
10966 goto free_work;
10967
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010968 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010969 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010970 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010971 /* Before declaring the flip queue wedged, check if
10972 * the hardware completed the operation behind our backs.
10973 */
10974 if (__intel_pageflip_stall_check(dev, crtc)) {
10975 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10976 page_flip_completed(intel_crtc);
10977 } else {
10978 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010979 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010010980
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010981 drm_crtc_vblank_put(crtc);
10982 kfree(work);
10983 return -EBUSY;
10984 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010985 }
10986 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010987 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010988
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010989 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10990 flush_workqueue(dev_priv->wq);
10991
Jesse Barnes75dfca82010-02-10 15:09:44 -080010992 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010993 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010994 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010995
Matt Roperf4510a22014-04-01 15:22:40 -070010996 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010997 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080010998
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010999 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011000
Chris Wilson89ed88b2015-02-16 14:31:49 +000011001 ret = i915_mutex_lock_interruptible(dev);
11002 if (ret)
11003 goto cleanup;
11004
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011005 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011006 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011007
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011008 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011009 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011010
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011011 if (IS_VALLEYVIEW(dev)) {
11012 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011013 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011014 /* vlv: DISPLAY_FLIP fails to change tiling */
11015 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011016 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011017 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011018 } else if (INTEL_INFO(dev)->gen >= 7) {
John Harrison41c52412014-11-24 18:49:43 +000011019 ring = i915_gem_request_get_ring(obj->last_read_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011020 if (ring == NULL || ring->id != RCS)
11021 ring = &dev_priv->ring[BCS];
11022 } else {
11023 ring = &dev_priv->ring[RCS];
11024 }
11025
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011026 mmio_flip = use_mmio_flip(ring, obj);
11027
11028 /* When using CS flips, we want to emit semaphores between rings.
11029 * However, when using mmio flips we will create a task to do the
11030 * synchronisation, so all we want here is to pin the framebuffer
11031 * into the display plane and skip any waits.
11032 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011033 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011034 crtc->primary->state,
11035 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011036 if (ret)
11037 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011038
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011039 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11040 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011041
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011042 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011043 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11044 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011045 if (ret)
11046 goto cleanup_unpin;
11047
John Harrisonf06cc1b2014-11-24 18:49:37 +000011048 i915_gem_request_assign(&work->flip_queued_req,
11049 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011050 } else {
Chris Wilsond94b5032015-04-27 13:41:15 +010011051 if (obj->last_write_req) {
11052 ret = i915_gem_check_olr(obj->last_write_req);
11053 if (ret)
11054 goto cleanup_unpin;
11055 }
11056
Sourab Gupta84c33a62014-06-02 16:47:17 +053011057 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011058 page_flip_flags);
11059 if (ret)
11060 goto cleanup_unpin;
11061
John Harrisonf06cc1b2014-11-24 18:49:37 +000011062 i915_gem_request_assign(&work->flip_queued_req,
11063 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011064 }
11065
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011066 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011067 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011068
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011069 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020011070 INTEL_FRONTBUFFER_PRIMARY(pipe));
11071
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011072 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011073 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011074 mutex_unlock(&dev->struct_mutex);
11075
Jesse Barnese5510fa2010-07-01 16:48:37 -070011076 trace_i915_flip_request(intel_crtc->plane, obj);
11077
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011078 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011079
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011080cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011081 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011082cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011083 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011084 mutex_unlock(&dev->struct_mutex);
11085cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011086 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011087 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011088
Chris Wilson89ed88b2015-02-16 14:31:49 +000011089 drm_gem_object_unreference_unlocked(&obj->base);
11090 drm_framebuffer_unreference(work->old_fb);
11091
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011092 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011093 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011094 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011095
Daniel Vetter87b6b102014-05-15 15:33:46 +020011096 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011097free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011098 kfree(work);
11099
Chris Wilsonf900db42014-02-20 09:26:13 +000011100 if (ret == -EIO) {
11101out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080011102 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011103 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011104 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011105 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011106 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011107 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011108 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011109 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011110}
11111
Jani Nikula65b38e02015-04-13 11:26:56 +030011112static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011113 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11114 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011115 .atomic_begin = intel_begin_crtc_commit,
11116 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011117};
11118
Daniel Vetter9a935852012-07-05 22:34:27 +020011119/**
11120 * intel_modeset_update_staged_output_state
11121 *
11122 * Updates the staged output configuration state, e.g. after we've read out the
11123 * current hw state.
11124 */
11125static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11126{
Ville Syrjälä76688512014-01-10 11:28:06 +020011127 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011128 struct intel_encoder *encoder;
11129 struct intel_connector *connector;
11130
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011131 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011132 connector->new_encoder =
11133 to_intel_encoder(connector->base.encoder);
11134 }
11135
Damien Lespiaub2784e12014-08-05 11:29:37 +010011136 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011137 encoder->new_crtc =
11138 to_intel_crtc(encoder->base.crtc);
11139 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011140
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011141 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011142 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011143 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011144}
11145
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011146/* Transitional helper to copy current connector/encoder state to
11147 * connector->state. This is needed so that code that is partially
11148 * converted to atomic does the right thing.
11149 */
11150static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11151{
11152 struct intel_connector *connector;
11153
11154 for_each_intel_connector(dev, connector) {
11155 if (connector->base.encoder) {
11156 connector->base.state->best_encoder =
11157 connector->base.encoder;
11158 connector->base.state->crtc =
11159 connector->base.encoder->crtc;
11160 } else {
11161 connector->base.state->best_encoder = NULL;
11162 connector->base.state->crtc = NULL;
11163 }
11164 }
11165}
11166
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011167/* Fixup legacy state after an atomic state swap.
Daniel Vetter9a935852012-07-05 22:34:27 +020011168 */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011169static void intel_modeset_fixup_state(struct drm_atomic_state *state)
Daniel Vetter9a935852012-07-05 22:34:27 +020011170{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011171 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011172 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011173 struct intel_connector *connector;
Daniel Vetter9a935852012-07-05 22:34:27 +020011174
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011175 for_each_intel_connector(state->dev, connector) {
11176 connector->base.encoder = connector->base.state->best_encoder;
11177 if (connector->base.encoder)
11178 connector->base.encoder->crtc =
11179 connector->base.state->crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011180 }
11181
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011182 /* Update crtc of disabled encoders */
11183 for_each_intel_encoder(state->dev, encoder) {
11184 int num_connectors = 0;
11185
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011186 for_each_intel_connector(state->dev, connector)
11187 if (connector->base.encoder == &encoder->base)
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011188 num_connectors++;
11189
11190 if (num_connectors == 0)
11191 encoder->base.crtc = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020011192 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011193
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011194 for_each_intel_crtc(state->dev, crtc) {
11195 crtc->base.enabled = crtc->base.state->enable;
11196 crtc->config = to_intel_crtc_state(crtc->base.state);
Ville Syrjälä76688512014-01-10 11:28:06 +020011197 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011198
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011199 /* Copy the new configuration to the staged state, to keep the few
11200 * pieces of code that haven't been converted yet happy */
11201 intel_modeset_update_staged_output_state(state->dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020011202}
11203
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011204static void
Robin Schroereba905b2014-05-18 02:24:50 +020011205connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011206 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011207{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011208 int bpp = pipe_config->pipe_bpp;
11209
11210 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11211 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011212 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011213
11214 /* Don't use an invalid EDID bpc value */
11215 if (connector->base.display_info.bpc &&
11216 connector->base.display_info.bpc * 3 < bpp) {
11217 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11218 bpp, connector->base.display_info.bpc*3);
11219 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11220 }
11221
11222 /* Clamp bpp to 8 on screens without EDID 1.4 */
11223 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11224 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11225 bpp);
11226 pipe_config->pipe_bpp = 24;
11227 }
11228}
11229
11230static int
11231compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011232 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011233{
11234 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011235 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011236 struct drm_connector *connector;
11237 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011238 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011239
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011240 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011241 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011242 else if (INTEL_INFO(dev)->gen >= 5)
11243 bpp = 12*3;
11244 else
11245 bpp = 8*3;
11246
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011247
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011248 pipe_config->pipe_bpp = bpp;
11249
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011250 state = pipe_config->base.state;
11251
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011252 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011253 for_each_connector_in_state(state, connector, connector_state, i) {
11254 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011255 continue;
11256
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011257 connected_sink_compute_bpp(to_intel_connector(connector),
11258 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011259 }
11260
11261 return bpp;
11262}
11263
Daniel Vetter644db712013-09-19 14:53:58 +020011264static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11265{
11266 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11267 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011268 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011269 mode->crtc_hdisplay, mode->crtc_hsync_start,
11270 mode->crtc_hsync_end, mode->crtc_htotal,
11271 mode->crtc_vdisplay, mode->crtc_vsync_start,
11272 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11273}
11274
Daniel Vetterc0b03412013-05-28 12:05:54 +020011275static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011276 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011277 const char *context)
11278{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011279 struct drm_device *dev = crtc->base.dev;
11280 struct drm_plane *plane;
11281 struct intel_plane *intel_plane;
11282 struct intel_plane_state *state;
11283 struct drm_framebuffer *fb;
11284
11285 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11286 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011287
11288 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11289 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11290 pipe_config->pipe_bpp, pipe_config->dither);
11291 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11292 pipe_config->has_pch_encoder,
11293 pipe_config->fdi_lanes,
11294 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11295 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11296 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011297 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11298 pipe_config->has_dp_encoder,
11299 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11300 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11301 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011302
11303 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11304 pipe_config->has_dp_encoder,
11305 pipe_config->dp_m2_n2.gmch_m,
11306 pipe_config->dp_m2_n2.gmch_n,
11307 pipe_config->dp_m2_n2.link_m,
11308 pipe_config->dp_m2_n2.link_n,
11309 pipe_config->dp_m2_n2.tu);
11310
Daniel Vetter55072d12014-11-20 16:10:28 +010011311 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11312 pipe_config->has_audio,
11313 pipe_config->has_infoframe);
11314
Daniel Vetterc0b03412013-05-28 12:05:54 +020011315 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011316 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011317 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011318 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11319 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011320 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011321 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11322 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011323 DRM_DEBUG_KMS("num_scalers: %d\n", crtc->num_scalers);
11324 DRM_DEBUG_KMS("scaler_users: 0x%x\n", pipe_config->scaler_state.scaler_users);
11325 DRM_DEBUG_KMS("scaler id: %d\n", pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011326 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11327 pipe_config->gmch_pfit.control,
11328 pipe_config->gmch_pfit.pgm_ratios,
11329 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011330 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011331 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011332 pipe_config->pch_pfit.size,
11333 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011334 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011335 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011336
11337 DRM_DEBUG_KMS("planes on this crtc\n");
11338 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11339 intel_plane = to_intel_plane(plane);
11340 if (intel_plane->pipe != crtc->pipe)
11341 continue;
11342
11343 state = to_intel_plane_state(plane->state);
11344 fb = state->base.fb;
11345 if (!fb) {
11346 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11347 "disabled, scaler_id = %d\n",
11348 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11349 plane->base.id, intel_plane->pipe,
11350 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11351 drm_plane_index(plane), state->scaler_id);
11352 continue;
11353 }
11354
11355 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11356 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11357 plane->base.id, intel_plane->pipe,
11358 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11359 drm_plane_index(plane));
11360 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11361 fb->base.id, fb->width, fb->height, fb->pixel_format);
11362 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11363 state->scaler_id,
11364 state->src.x1 >> 16, state->src.y1 >> 16,
11365 drm_rect_width(&state->src) >> 16,
11366 drm_rect_height(&state->src) >> 16,
11367 state->dst.x1, state->dst.y1,
11368 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11369 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011370}
11371
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011372static bool encoders_cloneable(const struct intel_encoder *a,
11373 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011374{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011375 /* masks could be asymmetric, so check both ways */
11376 return a == b || (a->cloneable & (1 << b->type) &&
11377 b->cloneable & (1 << a->type));
11378}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011379
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011380static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11381 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011382 struct intel_encoder *encoder)
11383{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011384 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011385 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011386 struct drm_connector_state *connector_state;
11387 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011388
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011389 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011390 if (connector_state->crtc != &crtc->base)
11391 continue;
11392
11393 source_encoder =
11394 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011395 if (!encoders_cloneable(encoder, source_encoder))
11396 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011397 }
11398
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011399 return true;
11400}
11401
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011402static bool check_encoder_cloning(struct drm_atomic_state *state,
11403 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011404{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011405 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011406 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011407 struct drm_connector_state *connector_state;
11408 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011409
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011410 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011411 if (connector_state->crtc != &crtc->base)
11412 continue;
11413
11414 encoder = to_intel_encoder(connector_state->best_encoder);
11415 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011416 return false;
11417 }
11418
11419 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011420}
11421
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011422static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011423{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011424 struct drm_device *dev = state->dev;
11425 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011426 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011427 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011428 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011429 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011430
11431 /*
11432 * Walk the connector list instead of the encoder
11433 * list to detect the problem on ddi platforms
11434 * where there's just one encoder per digital port.
11435 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011436 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011437 if (!connector_state->best_encoder)
11438 continue;
11439
11440 encoder = to_intel_encoder(connector_state->best_encoder);
11441
11442 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011443
11444 switch (encoder->type) {
11445 unsigned int port_mask;
11446 case INTEL_OUTPUT_UNKNOWN:
11447 if (WARN_ON(!HAS_DDI(dev)))
11448 break;
11449 case INTEL_OUTPUT_DISPLAYPORT:
11450 case INTEL_OUTPUT_HDMI:
11451 case INTEL_OUTPUT_EDP:
11452 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11453
11454 /* the same port mustn't appear more than once */
11455 if (used_ports & port_mask)
11456 return false;
11457
11458 used_ports |= port_mask;
11459 default:
11460 break;
11461 }
11462 }
11463
11464 return true;
11465}
11466
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011467static void
11468clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11469{
11470 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011471 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011472 struct intel_dpll_hw_state dpll_hw_state;
11473 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011474
Chandra Konduru663a3642015-04-07 15:28:41 -070011475 /* Clear only the intel specific part of the crtc state excluding scalers */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011476 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070011477 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011478 shared_dpll = crtc_state->shared_dpll;
11479 dpll_hw_state = crtc_state->dpll_hw_state;
11480
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011481 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011482
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011483 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011484 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011485 crtc_state->shared_dpll = shared_dpll;
11486 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011487}
11488
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011489static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011490intel_modeset_pipe_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011491 struct drm_atomic_state *state,
11492 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020011493{
Daniel Vetter7758a112012-07-08 19:40:39 +020011494 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011495 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011496 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011497 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011498 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011499 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011500
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011501 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011502 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011503 return -EINVAL;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011504 }
11505
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011506 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011507 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011508 return -EINVAL;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011509 }
11510
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011511 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011512
Daniel Vettere143a212013-07-04 12:01:15 +020011513 pipe_config->cpu_transcoder =
11514 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011515
Imre Deak2960bc92013-07-30 13:36:32 +030011516 /*
11517 * Sanitize sync polarity flags based on requested ones. If neither
11518 * positive or negative polarity is requested, treat this as meaning
11519 * negative polarity.
11520 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011521 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011522 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011523 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011524
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011525 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011526 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011527 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011528
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011529 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11530 * plane pixel format and any sink constraints into account. Returns the
11531 * source plane bpp so that dithering can be selected on mismatches
11532 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011533 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11534 pipe_config);
11535 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011536 goto fail;
11537
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011538 /*
11539 * Determine the real pipe dimensions. Note that stereo modes can
11540 * increase the actual pipe size due to the frame doubling and
11541 * insertion of additional space for blanks between the frame. This
11542 * is stored in the crtc timings. We use the requested mode to do this
11543 * computation to clearly distinguish it from the adjusted mode, which
11544 * can be changed by the connectors in the below retry loop.
11545 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011546 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011547 &pipe_config->pipe_src_w,
11548 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011549
Daniel Vettere29c22c2013-02-21 00:00:16 +010011550encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011551 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011552 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011553 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011554
Daniel Vetter135c81b2013-07-21 21:37:09 +020011555 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011556 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11557 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011558
Daniel Vetter7758a112012-07-08 19:40:39 +020011559 /* Pass our mode to the connectors and the CRTC to give them a chance to
11560 * adjust it according to limitations or connector properties, and also
11561 * a chance to reject the mode entirely.
11562 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011563 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011564 if (connector_state->crtc != crtc)
11565 continue;
11566
11567 encoder = to_intel_encoder(connector_state->best_encoder);
11568
Daniel Vetterefea6e82013-07-21 21:36:59 +020011569 if (!(encoder->compute_config(encoder, pipe_config))) {
11570 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011571 goto fail;
11572 }
11573 }
11574
Daniel Vetterff9a6752013-06-01 17:16:21 +020011575 /* Set default port clock if not overwritten by the encoder. Needs to be
11576 * done afterwards in case the encoder adjusts the mode. */
11577 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011578 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011579 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011580
Daniel Vettera43f6e02013-06-07 23:10:32 +020011581 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011582 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011583 DRM_DEBUG_KMS("CRTC fixup failed\n");
11584 goto fail;
11585 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011586
11587 if (ret == RETRY) {
11588 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11589 ret = -EINVAL;
11590 goto fail;
11591 }
11592
11593 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11594 retry = false;
11595 goto encoder_retry;
11596 }
11597
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011598 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011599 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011600 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011601
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011602 return 0;
Daniel Vetter7758a112012-07-08 19:40:39 +020011603fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011604 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020011605}
11606
Daniel Vetterea9d7582012-07-10 10:42:52 +020011607static bool intel_crtc_in_use(struct drm_crtc *crtc)
11608{
11609 struct drm_encoder *encoder;
11610 struct drm_device *dev = crtc->dev;
11611
11612 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11613 if (encoder->crtc == crtc)
11614 return true;
11615
11616 return false;
11617}
11618
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011619static bool
11620needs_modeset(struct drm_crtc_state *state)
Daniel Vetterea9d7582012-07-10 10:42:52 +020011621{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011622 return state->mode_changed || state->active_changed;
11623}
11624
11625static void
11626intel_modeset_update_state(struct drm_atomic_state *state)
11627{
11628 struct drm_device *dev = state->dev;
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011629 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011630 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011631 struct drm_crtc *crtc;
11632 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011633 struct drm_connector *connector;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011634 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011635
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011636 intel_shared_dpll_commit(dev_priv);
11637
Damien Lespiaub2784e12014-08-05 11:29:37 +010011638 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020011639 if (!intel_encoder->base.crtc)
11640 continue;
11641
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011642 for_each_crtc_in_state(state, crtc, crtc_state, i)
11643 if (crtc == intel_encoder->base.crtc)
11644 break;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011645
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011646 if (crtc != intel_encoder->base.crtc)
11647 continue;
11648
11649 if (crtc_state->enable && needs_modeset(crtc_state))
Daniel Vetterea9d7582012-07-10 10:42:52 +020011650 intel_encoder->connectors_active = false;
11651 }
11652
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011653 drm_atomic_helper_swap_state(state->dev, state);
11654 intel_modeset_fixup_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011655
Ville Syrjälä76688512014-01-10 11:28:06 +020011656 /* Double check state. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011657 for_each_crtc(dev, crtc) {
11658 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Daniel Vetterea9d7582012-07-10 10:42:52 +020011659 }
11660
11661 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11662 if (!connector->encoder || !connector->encoder->crtc)
11663 continue;
11664
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011665 for_each_crtc_in_state(state, crtc, crtc_state, i)
11666 if (crtc == connector->encoder->crtc)
11667 break;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011668
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011669 if (crtc != connector->encoder->crtc)
11670 continue;
11671
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011672 if (crtc->state->enable && needs_modeset(crtc->state)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020011673 struct drm_property *dpms_property =
11674 dev->mode_config.dpms_property;
11675
Daniel Vetterea9d7582012-07-10 10:42:52 +020011676 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050011677 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020011678 dpms_property,
11679 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011680
11681 intel_encoder = to_intel_encoder(connector->encoder);
11682 intel_encoder->connectors_active = true;
11683 }
11684 }
11685
11686}
11687
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011688static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011689{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011690 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011691
11692 if (clock1 == clock2)
11693 return true;
11694
11695 if (!clock1 || !clock2)
11696 return false;
11697
11698 diff = abs(clock1 - clock2);
11699
11700 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11701 return true;
11702
11703 return false;
11704}
11705
Daniel Vetter25c5b262012-07-08 22:08:04 +020011706#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11707 list_for_each_entry((intel_crtc), \
11708 &(dev)->mode_config.crtc_list, \
11709 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020011710 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020011711
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011712static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011713intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011714 struct intel_crtc_state *current_config,
11715 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011716{
Daniel Vetter66e985c2013-06-05 13:34:20 +020011717#define PIPE_CONF_CHECK_X(name) \
11718 if (current_config->name != pipe_config->name) { \
11719 DRM_ERROR("mismatch in " #name " " \
11720 "(expected 0x%08x, found 0x%08x)\n", \
11721 current_config->name, \
11722 pipe_config->name); \
11723 return false; \
11724 }
11725
Daniel Vetter08a24032013-04-19 11:25:34 +020011726#define PIPE_CONF_CHECK_I(name) \
11727 if (current_config->name != pipe_config->name) { \
11728 DRM_ERROR("mismatch in " #name " " \
11729 "(expected %i, found %i)\n", \
11730 current_config->name, \
11731 pipe_config->name); \
11732 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011733 }
11734
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011735/* This is required for BDW+ where there is only one set of registers for
11736 * switching between high and low RR.
11737 * This macro can be used whenever a comparison has to be made between one
11738 * hw state and multiple sw state variables.
11739 */
11740#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11741 if ((current_config->name != pipe_config->name) && \
11742 (current_config->alt_name != pipe_config->name)) { \
11743 DRM_ERROR("mismatch in " #name " " \
11744 "(expected %i or %i, found %i)\n", \
11745 current_config->name, \
11746 current_config->alt_name, \
11747 pipe_config->name); \
11748 return false; \
11749 }
11750
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011751#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11752 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070011753 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011754 "(expected %i, found %i)\n", \
11755 current_config->name & (mask), \
11756 pipe_config->name & (mask)); \
11757 return false; \
11758 }
11759
Ville Syrjälä5e550652013-09-06 23:29:07 +030011760#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11761 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11762 DRM_ERROR("mismatch in " #name " " \
11763 "(expected %i, found %i)\n", \
11764 current_config->name, \
11765 pipe_config->name); \
11766 return false; \
11767 }
11768
Daniel Vetterbb760062013-06-06 14:55:52 +020011769#define PIPE_CONF_QUIRK(quirk) \
11770 ((current_config->quirks | pipe_config->quirks) & (quirk))
11771
Daniel Vettereccb1402013-05-22 00:50:22 +020011772 PIPE_CONF_CHECK_I(cpu_transcoder);
11773
Daniel Vetter08a24032013-04-19 11:25:34 +020011774 PIPE_CONF_CHECK_I(has_pch_encoder);
11775 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020011776 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11777 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11778 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11779 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11780 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020011781
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011782 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011783
11784 if (INTEL_INFO(dev)->gen < 8) {
11785 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11786 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11787 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11788 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11789 PIPE_CONF_CHECK_I(dp_m_n.tu);
11790
11791 if (current_config->has_drrs) {
11792 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11793 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11794 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11795 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11796 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11797 }
11798 } else {
11799 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11800 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11801 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11802 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11803 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11804 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011805
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011806 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11807 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11808 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11809 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11810 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11811 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011812
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011813 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11814 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11815 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11816 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11817 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11818 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011819
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011820 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020011821 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020011822 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11823 IS_VALLEYVIEW(dev))
11824 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080011825 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011826
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011827 PIPE_CONF_CHECK_I(has_audio);
11828
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011829 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011830 DRM_MODE_FLAG_INTERLACE);
11831
Daniel Vetterbb760062013-06-06 14:55:52 +020011832 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011833 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011834 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011835 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011836 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011837 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011838 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011839 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011840 DRM_MODE_FLAG_NVSYNC);
11841 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011842
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011843 PIPE_CONF_CHECK_I(pipe_src_w);
11844 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011845
Daniel Vetter99535992014-04-13 12:00:33 +020011846 /*
11847 * FIXME: BIOS likes to set up a cloned config with lvds+external
11848 * screen. Since we don't yet re-compute the pipe config when moving
11849 * just the lvds port away to another pipe the sw tracking won't match.
11850 *
11851 * Proper atomic modesets with recomputed global state will fix this.
11852 * Until then just don't check gmch state for inherited modes.
11853 */
11854 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11855 PIPE_CONF_CHECK_I(gmch_pfit.control);
11856 /* pfit ratios are autocomputed by the hw on gen4+ */
11857 if (INTEL_INFO(dev)->gen < 4)
11858 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11859 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11860 }
11861
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011862 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11863 if (current_config->pch_pfit.enabled) {
11864 PIPE_CONF_CHECK_I(pch_pfit.pos);
11865 PIPE_CONF_CHECK_I(pch_pfit.size);
11866 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011867
Chandra Kondurua1b22782015-04-07 15:28:45 -070011868 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
11869
Jesse Barnese59150d2014-01-07 13:30:45 -080011870 /* BDW+ don't expose a synchronous way to read the state */
11871 if (IS_HASWELL(dev))
11872 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011873
Ville Syrjälä282740f2013-09-04 18:30:03 +030011874 PIPE_CONF_CHECK_I(double_wide);
11875
Daniel Vetter26804af2014-06-25 22:01:55 +030011876 PIPE_CONF_CHECK_X(ddi_pll_sel);
11877
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011878 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011879 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011880 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011881 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11882 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011883 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011884 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11885 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11886 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011887
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011888 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11889 PIPE_CONF_CHECK_I(pipe_bpp);
11890
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011891 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011892 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011893
Daniel Vetter66e985c2013-06-05 13:34:20 +020011894#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011895#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011896#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011897#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011898#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011899#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011900
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011901 return true;
11902}
11903
Damien Lespiau08db6652014-11-04 17:06:52 +000011904static void check_wm_state(struct drm_device *dev)
11905{
11906 struct drm_i915_private *dev_priv = dev->dev_private;
11907 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11908 struct intel_crtc *intel_crtc;
11909 int plane;
11910
11911 if (INTEL_INFO(dev)->gen < 9)
11912 return;
11913
11914 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11915 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11916
11917 for_each_intel_crtc(dev, intel_crtc) {
11918 struct skl_ddb_entry *hw_entry, *sw_entry;
11919 const enum pipe pipe = intel_crtc->pipe;
11920
11921 if (!intel_crtc->active)
11922 continue;
11923
11924 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000011925 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000011926 hw_entry = &hw_ddb.plane[pipe][plane];
11927 sw_entry = &sw_ddb->plane[pipe][plane];
11928
11929 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11930 continue;
11931
11932 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11933 "(expected (%u,%u), found (%u,%u))\n",
11934 pipe_name(pipe), plane + 1,
11935 sw_entry->start, sw_entry->end,
11936 hw_entry->start, hw_entry->end);
11937 }
11938
11939 /* cursor */
11940 hw_entry = &hw_ddb.cursor[pipe];
11941 sw_entry = &sw_ddb->cursor[pipe];
11942
11943 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11944 continue;
11945
11946 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11947 "(expected (%u,%u), found (%u,%u))\n",
11948 pipe_name(pipe),
11949 sw_entry->start, sw_entry->end,
11950 hw_entry->start, hw_entry->end);
11951 }
11952}
11953
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011954static void
11955check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011956{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011957 struct intel_connector *connector;
11958
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011959 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011960 /* This also checks the encoder/connector hw state with the
11961 * ->get_hw_state callbacks. */
11962 intel_connector_check_state(connector);
11963
Rob Clarke2c719b2014-12-15 13:56:32 -050011964 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011965 "connector's staged encoder doesn't match current encoder\n");
11966 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011967}
11968
11969static void
11970check_encoder_state(struct drm_device *dev)
11971{
11972 struct intel_encoder *encoder;
11973 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011974
Damien Lespiaub2784e12014-08-05 11:29:37 +010011975 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011976 bool enabled = false;
11977 bool active = false;
11978 enum pipe pipe, tracked_pipe;
11979
11980 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11981 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011982 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011983
Rob Clarke2c719b2014-12-15 13:56:32 -050011984 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011985 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011986 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011987 "encoder's active_connectors set, but no crtc\n");
11988
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011989 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011990 if (connector->base.encoder != &encoder->base)
11991 continue;
11992 enabled = true;
11993 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11994 active = true;
11995 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011996 /*
11997 * for MST connectors if we unplug the connector is gone
11998 * away but the encoder is still connected to a crtc
11999 * until a modeset happens in response to the hotplug.
12000 */
12001 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12002 continue;
12003
Rob Clarke2c719b2014-12-15 13:56:32 -050012004 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012005 "encoder's enabled state mismatch "
12006 "(expected %i, found %i)\n",
12007 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012008 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012009 "active encoder with no crtc\n");
12010
Rob Clarke2c719b2014-12-15 13:56:32 -050012011 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012012 "encoder's computed active state doesn't match tracked active state "
12013 "(expected %i, found %i)\n", active, encoder->connectors_active);
12014
12015 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012016 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012017 "encoder's hw state doesn't match sw tracking "
12018 "(expected %i, found %i)\n",
12019 encoder->connectors_active, active);
12020
12021 if (!encoder->base.crtc)
12022 continue;
12023
12024 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012025 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012026 "active encoder's pipe doesn't match"
12027 "(expected %i, found %i)\n",
12028 tracked_pipe, pipe);
12029
12030 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012031}
12032
12033static void
12034check_crtc_state(struct drm_device *dev)
12035{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012036 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012037 struct intel_crtc *crtc;
12038 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012039 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012040
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012041 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012042 bool enabled = false;
12043 bool active = false;
12044
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012045 memset(&pipe_config, 0, sizeof(pipe_config));
12046
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012047 DRM_DEBUG_KMS("[CRTC:%d]\n",
12048 crtc->base.base.id);
12049
Matt Roper83d65732015-02-25 13:12:16 -080012050 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012051 "active crtc, but not enabled in sw tracking\n");
12052
Damien Lespiaub2784e12014-08-05 11:29:37 +010012053 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012054 if (encoder->base.crtc != &crtc->base)
12055 continue;
12056 enabled = true;
12057 if (encoder->connectors_active)
12058 active = true;
12059 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012060
Rob Clarke2c719b2014-12-15 13:56:32 -050012061 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012062 "crtc's computed active state doesn't match tracked active state "
12063 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012064 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012065 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012066 "(expected %i, found %i)\n", enabled,
12067 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012068
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012069 active = dev_priv->display.get_pipe_config(crtc,
12070 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012071
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012072 /* hw state is inconsistent with the pipe quirk */
12073 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12074 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012075 active = crtc->active;
12076
Damien Lespiaub2784e12014-08-05 11:29:37 +010012077 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012078 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012079 if (encoder->base.crtc != &crtc->base)
12080 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012081 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012082 encoder->get_config(encoder, &pipe_config);
12083 }
12084
Rob Clarke2c719b2014-12-15 13:56:32 -050012085 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012086 "crtc active state doesn't match with hw state "
12087 "(expected %i, found %i)\n", crtc->active, active);
12088
Daniel Vetterc0b03412013-05-28 12:05:54 +020012089 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012090 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012091 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012092 intel_dump_pipe_config(crtc, &pipe_config,
12093 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012094 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012095 "[sw state]");
12096 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012097 }
12098}
12099
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012100static void
12101check_shared_dpll_state(struct drm_device *dev)
12102{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012103 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012104 struct intel_crtc *crtc;
12105 struct intel_dpll_hw_state dpll_hw_state;
12106 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012107
12108 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12109 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12110 int enabled_crtcs = 0, active_crtcs = 0;
12111 bool active;
12112
12113 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12114
12115 DRM_DEBUG_KMS("%s\n", pll->name);
12116
12117 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12118
Rob Clarke2c719b2014-12-15 13:56:32 -050012119 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012120 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012121 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012122 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012123 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012124 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012125 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012126 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012127 "pll on state mismatch (expected %i, found %i)\n",
12128 pll->on, active);
12129
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012130 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012131 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012132 enabled_crtcs++;
12133 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12134 active_crtcs++;
12135 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012136 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012137 "pll active crtcs mismatch (expected %i, found %i)\n",
12138 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012139 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012140 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012141 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012142
Rob Clarke2c719b2014-12-15 13:56:32 -050012143 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012144 sizeof(dpll_hw_state)),
12145 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012146 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012147}
12148
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012149void
12150intel_modeset_check_state(struct drm_device *dev)
12151{
Damien Lespiau08db6652014-11-04 17:06:52 +000012152 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012153 check_connector_state(dev);
12154 check_encoder_state(dev);
12155 check_crtc_state(dev);
12156 check_shared_dpll_state(dev);
12157}
12158
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012159void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012160 int dotclock)
12161{
12162 /*
12163 * FDI already provided one idea for the dotclock.
12164 * Yell if the encoder disagrees.
12165 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012166 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012167 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012168 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012169}
12170
Ville Syrjälä80715b22014-05-15 20:23:23 +030012171static void update_scanline_offset(struct intel_crtc *crtc)
12172{
12173 struct drm_device *dev = crtc->base.dev;
12174
12175 /*
12176 * The scanline counter increments at the leading edge of hsync.
12177 *
12178 * On most platforms it starts counting from vtotal-1 on the
12179 * first active line. That means the scanline counter value is
12180 * always one less than what we would expect. Ie. just after
12181 * start of vblank, which also occurs at start of hsync (on the
12182 * last active line), the scanline counter will read vblank_start-1.
12183 *
12184 * On gen2 the scanline counter starts counting from 1 instead
12185 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12186 * to keep the value positive), instead of adding one.
12187 *
12188 * On HSW+ the behaviour of the scanline counter depends on the output
12189 * type. For DP ports it behaves like most other platforms, but on HDMI
12190 * there's an extra 1 line difference. So we need to add two instead of
12191 * one to the value.
12192 */
12193 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012194 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012195 int vtotal;
12196
12197 vtotal = mode->crtc_vtotal;
12198 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12199 vtotal /= 2;
12200
12201 crtc->scanline_offset = vtotal - 1;
12202 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012203 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012204 crtc->scanline_offset = 2;
12205 } else
12206 crtc->scanline_offset = 1;
12207}
12208
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012209static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012210intel_modeset_compute_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012211 struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012212{
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012213 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012214 int ret = 0;
12215
12216 ret = drm_atomic_add_affected_connectors(state, crtc);
12217 if (ret)
12218 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012219
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012220 ret = drm_atomic_helper_check_modeset(state->dev, state);
12221 if (ret)
12222 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012223
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012224 /*
12225 * Note this needs changes when we start tracking multiple modes
12226 * and crtcs. At that point we'll need to compute the whole config
12227 * (i.e. one pipe_config for each crtc) rather than just the one
12228 * for this crtc.
12229 */
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012230 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
12231 if (IS_ERR(pipe_config))
12232 return pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012233
Ander Conselvan de Oliveira4fed33f2015-04-21 17:13:03 +030012234 if (!pipe_config->base.enable)
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012235 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012236
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012237 ret = intel_modeset_pipe_config(crtc, state, pipe_config);
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012238 if (ret)
12239 return ERR_PTR(ret);
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012240
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012241 /* Check things that can only be changed through modeset */
12242 if (pipe_config->has_audio !=
12243 to_intel_crtc(crtc)->config->has_audio)
12244 pipe_config->base.mode_changed = true;
12245
12246 /*
12247 * Note we have an issue here with infoframes: current code
12248 * only updates them on the full mode set path per hw
12249 * requirements. So here we should be checking for any
12250 * required changes and forcing a mode set.
12251 */
12252
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012253 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,"[modeset]");
12254
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012255 ret = drm_atomic_helper_check_planes(state->dev, state);
12256 if (ret)
12257 return ERR_PTR(ret);
12258
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012259 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012260}
12261
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012262static int __intel_set_mode_setup_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012263{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012264 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012265 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012266 unsigned clear_pipes = 0;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012267 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012268 struct intel_crtc_state *intel_crtc_state;
12269 struct drm_crtc *crtc;
12270 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012271 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012272 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012273
12274 if (!dev_priv->display.crtc_compute_clock)
12275 return 0;
12276
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012277 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12278 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012279 intel_crtc_state = to_intel_crtc_state(crtc_state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012280
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012281 if (needs_modeset(crtc_state)) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012282 clear_pipes |= 1 << intel_crtc->pipe;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012283 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
12284 memset(&intel_crtc_state->dpll_hw_state, 0,
12285 sizeof(intel_crtc_state->dpll_hw_state));
12286 }
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012287 }
12288
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012289 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12290 if (ret)
12291 goto done;
12292
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012293 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12294 if (!needs_modeset(crtc_state) || !crtc_state->enable)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012295 continue;
12296
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012297 intel_crtc = to_intel_crtc(crtc);
12298 intel_crtc_state = to_intel_crtc_state(crtc_state);
12299
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012300 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012301 intel_crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012302 if (ret) {
12303 intel_shared_dpll_abort_config(dev_priv);
12304 goto done;
12305 }
12306 }
12307
12308done:
12309 return ret;
12310}
12311
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012312/* Code that should eventually be part of atomic_check() */
12313static int __intel_set_mode_checks(struct drm_atomic_state *state)
12314{
12315 struct drm_device *dev = state->dev;
12316 int ret;
12317
12318 /*
12319 * See if the config requires any additional preparation, e.g.
12320 * to adjust global state with pipes off. We need to do this
12321 * here so we can get the modeset_pipe updated config for the new
12322 * mode set on this crtc. For other crtcs we need to use the
12323 * adjusted_mode bits in the crtc directly.
12324 */
12325 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
12326 ret = valleyview_modeset_global_pipes(state);
12327 if (ret)
12328 return ret;
12329 }
12330
12331 ret = __intel_set_mode_setup_plls(state);
12332 if (ret)
12333 return ret;
12334
12335 return 0;
12336}
12337
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012338static int __intel_set_mode(struct drm_crtc *modeset_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012339 struct intel_crtc_state *pipe_config)
Daniel Vettera6778b32012-07-02 09:56:42 +020012340{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012341 struct drm_device *dev = modeset_crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012342 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012343 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012344 struct drm_crtc *crtc;
12345 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012346 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012347 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020012348
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012349 ret = __intel_set_mode_checks(state);
12350 if (ret < 0)
12351 return ret;
12352
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012353 ret = drm_atomic_helper_prepare_planes(dev, state);
12354 if (ret)
12355 return ret;
12356
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012357 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12358 if (!needs_modeset(crtc_state))
12359 continue;
Daniel Vetter460da9162013-03-27 00:44:51 +010012360
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012361 if (!crtc_state->enable) {
12362 intel_crtc_disable(crtc);
12363 } else if (crtc->state->enable) {
12364 intel_crtc_disable_planes(crtc);
12365 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030012366 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012367 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012368
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012369 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12370 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012371 *
12372 * Note we'll need to fix this up when we start tracking multiple
12373 * pipes; here we assume a single modeset_pipe and only track the
12374 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012375 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012376 if (pipe_config->base.enable && needs_modeset(&pipe_config->base)) {
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012377 modeset_crtc->mode = pipe_config->base.mode;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020012378
12379 /*
12380 * Calculate and store various constants which
12381 * are later needed by vblank and swap-completion
12382 * timestamping. They are derived from true hwmode.
12383 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012384 drm_calc_timestamping_constants(modeset_crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012385 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012386 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012387
Daniel Vetterea9d7582012-07-10 10:42:52 +020012388 /* Only after disabling all output pipelines that will be changed can we
12389 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012390 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012391
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030012392 /* The state has been swaped above, so state actually contains the
12393 * old state now. */
12394
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012395 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020012396
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012397 drm_atomic_helper_commit_planes(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012398
12399 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012400 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030012401 if (!needs_modeset(crtc->state) || !crtc->state->enable)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012402 continue;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012403
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012404 update_scanline_offset(to_intel_crtc(crtc));
12405
12406 dev_priv->display.crtc_enable(crtc);
12407 intel_crtc_enable_planes(crtc);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012408 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012409
Daniel Vettera6778b32012-07-02 09:56:42 +020012410 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012411
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012412 drm_atomic_helper_cleanup_planes(dev, state);
12413
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012414 drm_atomic_state_free(state);
12415
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030012416 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020012417}
12418
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012419static int intel_set_mode_with_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012420 struct intel_crtc_state *pipe_config)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012421{
12422 int ret;
12423
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012424 ret = __intel_set_mode(crtc, pipe_config);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012425
12426 if (ret == 0)
12427 intel_modeset_check_state(crtc->dev);
12428
12429 return ret;
12430}
12431
Damien Lespiaue7457a92013-08-08 22:28:59 +010012432static int intel_set_mode(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012433 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020012434{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012435 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012436 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020012437
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012438 pipe_config = intel_modeset_compute_config(crtc, state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012439 if (IS_ERR(pipe_config)) {
12440 ret = PTR_ERR(pipe_config);
12441 goto out;
12442 }
Daniel Vetterf30da182013-04-11 20:22:50 +020012443
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012444 ret = intel_set_mode_with_config(crtc, pipe_config);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012445 if (ret)
12446 goto out;
12447
12448out:
12449 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020012450}
12451
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012452void intel_crtc_restore_mode(struct drm_crtc *crtc)
12453{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012454 struct drm_device *dev = crtc->dev;
12455 struct drm_atomic_state *state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012456 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012457 struct intel_encoder *encoder;
12458 struct intel_connector *connector;
12459 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012460 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012461 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012462
12463 state = drm_atomic_state_alloc(dev);
12464 if (!state) {
12465 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12466 crtc->base.id);
12467 return;
12468 }
12469
12470 state->acquire_ctx = dev->mode_config.acquire_ctx;
12471
12472 /* The force restore path in the HW readout code relies on the staged
12473 * config still keeping the user requested config while the actual
12474 * state has been overwritten by the configuration read from HW. We
12475 * need to copy the staged config to the atomic state, otherwise the
12476 * mode set will just reapply the state the HW is already in. */
12477 for_each_intel_encoder(dev, encoder) {
12478 if (&encoder->new_crtc->base != crtc)
12479 continue;
12480
12481 for_each_intel_connector(dev, connector) {
12482 if (connector->new_encoder != encoder)
12483 continue;
12484
12485 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12486 if (IS_ERR(connector_state)) {
12487 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12488 connector->base.base.id,
12489 connector->base.name,
12490 PTR_ERR(connector_state));
12491 continue;
12492 }
12493
12494 connector_state->crtc = crtc;
12495 connector_state->best_encoder = &encoder->base;
12496 }
12497 }
12498
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012499 for_each_intel_crtc(dev, intel_crtc) {
12500 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
12501 continue;
12502
12503 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
12504 if (IS_ERR(crtc_state)) {
12505 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
12506 intel_crtc->base.base.id,
12507 PTR_ERR(crtc_state));
12508 continue;
12509 }
12510
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020012511 crtc_state->base.active = crtc_state->base.enable =
12512 intel_crtc->new_enabled;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012513
12514 if (&intel_crtc->base == crtc)
12515 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012516 }
12517
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030012518 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
12519 crtc->primary->fb, crtc->x, crtc->y);
12520
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012521 ret = intel_set_mode(crtc, state);
12522 if (ret)
12523 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012524}
12525
Daniel Vetter25c5b262012-07-08 22:08:04 +020012526#undef for_each_intel_crtc_masked
12527
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012528static bool intel_connector_in_mode_set(struct intel_connector *connector,
12529 struct drm_mode_set *set)
12530{
12531 int ro;
12532
12533 for (ro = 0; ro < set->num_connectors; ro++)
12534 if (set->connectors[ro] == &connector->base)
12535 return true;
12536
12537 return false;
12538}
12539
Daniel Vetter2e431052012-07-04 22:42:15 +020012540static int
Daniel Vetter9a935852012-07-05 22:34:27 +020012541intel_modeset_stage_output_state(struct drm_device *dev,
12542 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012543 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020012544{
Daniel Vetter9a935852012-07-05 22:34:27 +020012545 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012546 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012547 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012548 struct drm_crtc *crtc;
12549 struct drm_crtc_state *crtc_state;
12550 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020012551
Damien Lespiau9abdda72013-02-13 13:29:23 +000012552 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020012553 * of connectors. For paranoia, double-check this. */
12554 WARN_ON(!set->fb && (set->num_connectors != 0));
12555 WARN_ON(set->fb && (set->num_connectors == 0));
12556
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012557 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012558 bool in_mode_set = intel_connector_in_mode_set(connector, set);
12559
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012560 if (!in_mode_set && connector->base.state->crtc != set->crtc)
12561 continue;
12562
12563 connector_state =
12564 drm_atomic_get_connector_state(state, &connector->base);
12565 if (IS_ERR(connector_state))
12566 return PTR_ERR(connector_state);
12567
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012568 if (in_mode_set) {
12569 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012570 connector_state->best_encoder =
12571 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020012572 }
12573
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012574 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012575 continue;
12576
Daniel Vetter9a935852012-07-05 22:34:27 +020012577 /* If we disable the crtc, disable all its connectors. Also, if
12578 * the connector is on the changing crtc but not on the new
12579 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012580 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012581 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020012582
12583 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12584 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012585 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020012586 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012587 }
12588 /* connector->new_encoder is now updated for all connectors. */
12589
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012590 for_each_connector_in_state(state, drm_connector, connector_state, i) {
12591 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020012592
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012593 if (!connector_state->best_encoder) {
12594 ret = drm_atomic_set_crtc_for_connector(connector_state,
12595 NULL);
12596 if (ret)
12597 return ret;
12598
Daniel Vetter50f56112012-07-02 09:35:43 +020012599 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012600 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012601
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012602 if (intel_connector_in_mode_set(connector, set)) {
12603 struct drm_crtc *crtc = connector->base.state->crtc;
12604
12605 /* If this connector was in a previous crtc, add it
12606 * to the state. We might need to disable it. */
12607 if (crtc) {
12608 crtc_state =
12609 drm_atomic_get_crtc_state(state, crtc);
12610 if (IS_ERR(crtc_state))
12611 return PTR_ERR(crtc_state);
12612 }
12613
12614 ret = drm_atomic_set_crtc_for_connector(connector_state,
12615 set->crtc);
12616 if (ret)
12617 return ret;
12618 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012619
12620 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012621 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
12622 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012623 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012624 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012625
Daniel Vetter9a935852012-07-05 22:34:27 +020012626 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12627 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012628 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012629 connector_state->crtc->base.id);
12630
12631 if (connector_state->best_encoder != &connector->encoder->base)
12632 connector->encoder =
12633 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020012634 }
12635
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012636 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020012637 bool has_connectors;
12638
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012639 ret = drm_atomic_add_affected_connectors(state, crtc);
12640 if (ret)
12641 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012642
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020012643 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
12644 if (has_connectors != crtc_state->enable)
12645 crtc_state->enable =
12646 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020012647 }
12648
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012649 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
12650 set->fb, set->x, set->y);
12651 if (ret)
12652 return ret;
12653
12654 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
12655 if (IS_ERR(crtc_state))
12656 return PTR_ERR(crtc_state);
12657
12658 if (set->mode)
12659 drm_mode_copy(&crtc_state->mode, set->mode);
12660
12661 if (set->num_connectors)
12662 crtc_state->active = true;
12663
Daniel Vetter2e431052012-07-04 22:42:15 +020012664 return 0;
12665}
12666
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012667static bool primary_plane_visible(struct drm_crtc *crtc)
12668{
12669 struct intel_plane_state *plane_state =
12670 to_intel_plane_state(crtc->primary->state);
12671
12672 return plane_state->visible;
12673}
12674
Daniel Vetter2e431052012-07-04 22:42:15 +020012675static int intel_crtc_set_config(struct drm_mode_set *set)
12676{
12677 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012678 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012679 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012680 bool primary_plane_was_visible;
Daniel Vetter2e431052012-07-04 22:42:15 +020012681 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020012682
Daniel Vetter8d3e3752012-07-05 16:09:09 +020012683 BUG_ON(!set);
12684 BUG_ON(!set->crtc);
12685 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020012686
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010012687 /* Enforce sane interface api - has been abused by the fb helper. */
12688 BUG_ON(!set->mode && set->fb);
12689 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020012690
Daniel Vetter2e431052012-07-04 22:42:15 +020012691 if (set->fb) {
12692 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12693 set->crtc->base.id, set->fb->base.id,
12694 (int)set->num_connectors, set->x, set->y);
12695 } else {
12696 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020012697 }
12698
12699 dev = set->crtc->dev;
12700
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012701 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012702 if (!state)
12703 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012704
12705 state->acquire_ctx = dev->mode_config.acquire_ctx;
12706
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030012707 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020012708 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012709 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020012710
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012711 pipe_config = intel_modeset_compute_config(set->crtc, state);
Jesse Barnes20664592014-11-05 14:26:09 -080012712 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080012713 ret = PTR_ERR(pipe_config);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012714 goto out;
Jesse Barnes20664592014-11-05 14:26:09 -080012715 }
Jesse Barnes50f52752014-11-07 13:11:00 -080012716
Jesse Barnes1f9954d2014-11-05 14:26:10 -080012717 intel_update_pipe_size(to_intel_crtc(set->crtc));
12718
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012719 primary_plane_was_visible = primary_plane_visible(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012720
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012721 ret = intel_set_mode_with_config(set->crtc, pipe_config);
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012722
12723 if (ret == 0 &&
12724 pipe_config->base.enable &&
12725 pipe_config->base.planes_changed &&
12726 !needs_modeset(&pipe_config->base)) {
12727 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012728
12729 /*
12730 * We need to make sure the primary plane is re-enabled if it
12731 * has previously been turned off.
12732 */
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012733 if (ret == 0 && !primary_plane_was_visible &&
12734 primary_plane_visible(set->crtc)) {
Matt Roper3b150f02014-05-29 08:06:53 -070012735 WARN_ON(!intel_crtc->active);
Maarten Lankhorst87d43002015-04-21 17:12:54 +030012736 intel_post_enable_primary(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012737 }
12738
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012739 /*
12740 * In the fastboot case this may be our only check of the
12741 * state after boot. It would be better to only do it on
12742 * the first update, but we don't have a nice way of doing that
12743 * (and really, set_config isn't used much for high freq page
12744 * flipping, so increasing its cost here shouldn't be a big
12745 * deal).
12746 */
Jani Nikulad330a952014-01-21 11:24:25 +020012747 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012748 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020012749 }
12750
Chris Wilson2d05eae2013-05-03 17:36:25 +010012751 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020012752 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12753 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010012754 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012755
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012756out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012757 if (ret)
12758 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020012759 return ret;
12760}
12761
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012762static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012763 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020012764 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012765 .destroy = intel_crtc_destroy,
12766 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080012767 .atomic_duplicate_state = intel_crtc_duplicate_state,
12768 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012769};
12770
Daniel Vetter53589012013-06-05 13:34:16 +020012771static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
12772 struct intel_shared_dpll *pll,
12773 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012774{
Daniel Vetter53589012013-06-05 13:34:16 +020012775 uint32_t val;
12776
Daniel Vetterf458ebb2014-09-30 10:56:39 +020012777 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030012778 return false;
12779
Daniel Vetter53589012013-06-05 13:34:16 +020012780 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020012781 hw_state->dpll = val;
12782 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
12783 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020012784
12785 return val & DPLL_VCO_ENABLE;
12786}
12787
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012788static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
12789 struct intel_shared_dpll *pll)
12790{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012791 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
12792 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012793}
12794
Daniel Vettere7b903d2013-06-05 13:34:14 +020012795static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
12796 struct intel_shared_dpll *pll)
12797{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012798 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020012799 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020012800
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012801 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012802
12803 /* Wait for the clocks to stabilize. */
12804 POSTING_READ(PCH_DPLL(pll->id));
12805 udelay(150);
12806
12807 /* The pixel multiplier can only be updated once the
12808 * DPLL is enabled and the clocks are stable.
12809 *
12810 * So write it again.
12811 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012812 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012813 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012814 udelay(200);
12815}
12816
12817static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
12818 struct intel_shared_dpll *pll)
12819{
12820 struct drm_device *dev = dev_priv->dev;
12821 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020012822
12823 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012824 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020012825 if (intel_crtc_to_shared_dpll(crtc) == pll)
12826 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
12827 }
12828
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012829 I915_WRITE(PCH_DPLL(pll->id), 0);
12830 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012831 udelay(200);
12832}
12833
Daniel Vetter46edb022013-06-05 13:34:12 +020012834static char *ibx_pch_dpll_names[] = {
12835 "PCH DPLL A",
12836 "PCH DPLL B",
12837};
12838
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012839static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012840{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012841 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012842 int i;
12843
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012844 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012845
Daniel Vettere72f9fb2013-06-05 13:34:06 +020012846 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020012847 dev_priv->shared_dplls[i].id = i;
12848 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012849 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020012850 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
12851 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020012852 dev_priv->shared_dplls[i].get_hw_state =
12853 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012854 }
12855}
12856
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012857static void intel_shared_dpll_init(struct drm_device *dev)
12858{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012859 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012860
Daniel Vetter9cd86932014-06-25 22:01:57 +030012861 if (HAS_DDI(dev))
12862 intel_ddi_pll_init(dev);
12863 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012864 ibx_pch_dpll_init(dev);
12865 else
12866 dev_priv->num_shared_dpll = 0;
12867
12868 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012869}
12870
Matt Roper6beb8c232014-12-01 15:40:14 -080012871/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000012872 * intel_wm_need_update - Check whether watermarks need updating
12873 * @plane: drm plane
12874 * @state: new plane state
12875 *
12876 * Check current plane state versus the new one to determine whether
12877 * watermarks need to be recalculated.
12878 *
12879 * Returns true or false.
12880 */
12881bool intel_wm_need_update(struct drm_plane *plane,
12882 struct drm_plane_state *state)
12883{
12884 /* Update watermarks on tiling changes. */
12885 if (!plane->state->fb || !state->fb ||
12886 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
12887 plane->state->rotation != state->rotation)
12888 return true;
12889
12890 return false;
12891}
12892
12893/**
Matt Roper6beb8c232014-12-01 15:40:14 -080012894 * intel_prepare_plane_fb - Prepare fb for usage on plane
12895 * @plane: drm plane to prepare for
12896 * @fb: framebuffer to prepare for presentation
12897 *
12898 * Prepares a framebuffer for usage on a display plane. Generally this
12899 * involves pinning the underlying object and updating the frontbuffer tracking
12900 * bits. Some older platforms need special physical address handling for
12901 * cursor planes.
12902 *
12903 * Returns 0 on success, negative error code on failure.
12904 */
12905int
12906intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000012907 struct drm_framebuffer *fb,
12908 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070012909{
12910 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080012911 struct intel_plane *intel_plane = to_intel_plane(plane);
12912 enum pipe pipe = intel_plane->pipe;
12913 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12914 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
12915 unsigned frontbuffer_bits = 0;
12916 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070012917
Matt Roperea2c67b2014-12-23 10:41:52 -080012918 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070012919 return 0;
12920
Matt Roper6beb8c232014-12-01 15:40:14 -080012921 switch (plane->type) {
12922 case DRM_PLANE_TYPE_PRIMARY:
12923 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
12924 break;
12925 case DRM_PLANE_TYPE_CURSOR:
12926 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
12927 break;
12928 case DRM_PLANE_TYPE_OVERLAY:
12929 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
12930 break;
12931 }
Matt Roper465c1202014-05-29 08:06:54 -070012932
Matt Roper4c345742014-07-09 16:22:10 -070012933 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070012934
Matt Roper6beb8c232014-12-01 15:40:14 -080012935 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
12936 INTEL_INFO(dev)->cursor_needs_physical) {
12937 int align = IS_I830(dev) ? 16 * 1024 : 256;
12938 ret = i915_gem_object_attach_phys(obj, align);
12939 if (ret)
12940 DRM_DEBUG_KMS("failed to attach phys object\n");
12941 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000012942 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080012943 }
12944
12945 if (ret == 0)
12946 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
12947
12948 mutex_unlock(&dev->struct_mutex);
12949
12950 return ret;
12951}
12952
Matt Roper38f3ce32014-12-02 07:45:25 -080012953/**
12954 * intel_cleanup_plane_fb - Cleans up an fb after plane use
12955 * @plane: drm plane to clean up for
12956 * @fb: old framebuffer that was on plane
12957 *
12958 * Cleans up a framebuffer that has just been removed from a plane.
12959 */
12960void
12961intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000012962 struct drm_framebuffer *fb,
12963 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080012964{
12965 struct drm_device *dev = plane->dev;
12966 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12967
12968 if (WARN_ON(!obj))
12969 return;
12970
12971 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
12972 !INTEL_INFO(dev)->cursor_needs_physical) {
12973 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000012974 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080012975 mutex_unlock(&dev->struct_mutex);
12976 }
Matt Roper465c1202014-05-29 08:06:54 -070012977}
12978
Chandra Konduru6156a452015-04-27 13:48:39 -070012979int
12980skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
12981{
12982 int max_scale;
12983 struct drm_device *dev;
12984 struct drm_i915_private *dev_priv;
12985 int crtc_clock, cdclk;
12986
12987 if (!intel_crtc || !crtc_state)
12988 return DRM_PLANE_HELPER_NO_SCALING;
12989
12990 dev = intel_crtc->base.dev;
12991 dev_priv = dev->dev_private;
12992 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
12993 cdclk = dev_priv->display.get_display_clock_speed(dev);
12994
12995 if (!crtc_clock || !cdclk)
12996 return DRM_PLANE_HELPER_NO_SCALING;
12997
12998 /*
12999 * skl max scale is lower of:
13000 * close to 3 but not 3, -1 is for that purpose
13001 * or
13002 * cdclk/crtc_clock
13003 */
13004 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13005
13006 return max_scale;
13007}
13008
Matt Roper465c1202014-05-29 08:06:54 -070013009static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013010intel_check_primary_plane(struct drm_plane *plane,
13011 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013012{
Matt Roper32b7eee2014-12-24 07:59:06 -080013013 struct drm_device *dev = plane->dev;
13014 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013015 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013016 struct intel_crtc *intel_crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013017 struct intel_crtc_state *crtc_state;
Matt Roper2b875c22014-12-01 15:40:13 -080013018 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013019 struct drm_rect *dest = &state->dst;
13020 struct drm_rect *src = &state->src;
13021 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013022 bool can_position = false;
Chandra Konduru6156a452015-04-27 13:48:39 -070013023 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13024 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013025 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013026
Matt Roperea2c67b2014-12-23 10:41:52 -080013027 crtc = crtc ? crtc : plane->crtc;
13028 intel_crtc = to_intel_crtc(crtc);
Chandra Konduru6156a452015-04-27 13:48:39 -070013029 crtc_state = state->base.state ?
13030 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
Matt Roperea2c67b2014-12-23 10:41:52 -080013031
Chandra Konduru6156a452015-04-27 13:48:39 -070013032 if (INTEL_INFO(dev)->gen >= 9) {
13033 min_scale = 1;
13034 max_scale = skl_max_scale(intel_crtc, crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013035 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013036 }
Sonika Jindald8106362015-04-10 14:37:28 +053013037
Matt Roperc59cb172014-12-01 15:40:16 -080013038 ret = drm_plane_helper_check_update(plane, crtc, fb,
13039 src, dest, clip,
Chandra Konduru6156a452015-04-27 13:48:39 -070013040 min_scale,
13041 max_scale,
Sonika Jindald8106362015-04-10 14:37:28 +053013042 can_position, true,
13043 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013044 if (ret)
13045 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013046
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013047 if (intel_crtc->active) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013048 struct intel_plane_state *old_state =
13049 to_intel_plane_state(plane->state);
13050
Matt Roper32b7eee2014-12-24 07:59:06 -080013051 intel_crtc->atomic.wait_for_flips = true;
13052
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013053 /*
13054 * FBC does not work on some platforms for rotated
13055 * planes, so disable it when rotation is not 0 and
13056 * update it when rotation is set back to 0.
13057 *
13058 * FIXME: This is redundant with the fbc update done in
13059 * the primary plane enable function except that that
13060 * one is done too late. We eventually need to unify
13061 * this.
13062 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013063 if (state->visible &&
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013064 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013065 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013066 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013067 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013068 }
13069
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013070 if (state->visible && !old_state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013071 /*
13072 * BDW signals flip done immediately if the plane
13073 * is disabled, even if the plane enable is already
13074 * armed to occur at the next vblank :(
13075 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013076 if (IS_BROADWELL(dev))
Matt Roper32b7eee2014-12-24 07:59:06 -080013077 intel_crtc->atomic.wait_vblank = true;
13078 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013079
Matt Roper32b7eee2014-12-24 07:59:06 -080013080 intel_crtc->atomic.fb_bits |=
13081 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13082
13083 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013084
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013085 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013086 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013087 }
13088
Chandra Konduru6156a452015-04-27 13:48:39 -070013089 if (INTEL_INFO(dev)->gen >= 9) {
13090 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13091 to_intel_plane(plane), state, 0);
13092 if (ret)
13093 return ret;
13094 }
13095
Matt Roperc59cb172014-12-01 15:40:16 -080013096 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013097}
13098
Sonika Jindal48404c12014-08-22 14:06:04 +053013099static void
13100intel_commit_primary_plane(struct drm_plane *plane,
13101 struct intel_plane_state *state)
13102{
Matt Roper2b875c22014-12-01 15:40:13 -080013103 struct drm_crtc *crtc = state->base.crtc;
13104 struct drm_framebuffer *fb = state->base.fb;
13105 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013106 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013107 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013108 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013109
Matt Roperea2c67b2014-12-23 10:41:52 -080013110 crtc = crtc ? crtc : plane->crtc;
13111 intel_crtc = to_intel_crtc(crtc);
13112
Matt Ropercf4c7c12014-12-04 10:27:42 -080013113 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013114 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013115 crtc->y = src->y1 >> 16;
13116
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013117 if (intel_crtc->active) {
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013118 if (state->visible)
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013119 /* FIXME: kill this fastboot hack */
13120 intel_update_pipe_size(intel_crtc);
13121
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013122 dev_priv->display.update_primary_plane(crtc, plane->fb,
13123 crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013124 }
13125}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013126
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013127static void
13128intel_disable_primary_plane(struct drm_plane *plane,
13129 struct drm_crtc *crtc,
13130 bool force)
13131{
13132 struct drm_device *dev = plane->dev;
13133 struct drm_i915_private *dev_priv = dev->dev_private;
13134
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013135 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13136}
13137
Matt Roper32b7eee2014-12-24 07:59:06 -080013138static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13139{
13140 struct drm_device *dev = crtc->dev;
13141 struct drm_i915_private *dev_priv = dev->dev_private;
13142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013143 struct intel_plane *intel_plane;
13144 struct drm_plane *p;
13145 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013146
Matt Roperea2c67b2014-12-23 10:41:52 -080013147 /* Track fb's for any planes being disabled */
13148 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13149 intel_plane = to_intel_plane(p);
13150
13151 if (intel_crtc->atomic.disabled_planes &
13152 (1 << drm_plane_index(p))) {
13153 switch (p->type) {
13154 case DRM_PLANE_TYPE_PRIMARY:
13155 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13156 break;
13157 case DRM_PLANE_TYPE_CURSOR:
13158 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13159 break;
13160 case DRM_PLANE_TYPE_OVERLAY:
13161 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13162 break;
13163 }
13164
13165 mutex_lock(&dev->struct_mutex);
13166 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13167 mutex_unlock(&dev->struct_mutex);
13168 }
13169 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013170
Matt Roper32b7eee2014-12-24 07:59:06 -080013171 if (intel_crtc->atomic.wait_for_flips)
13172 intel_crtc_wait_for_pending_flips(crtc);
13173
13174 if (intel_crtc->atomic.disable_fbc)
13175 intel_fbc_disable(dev);
13176
13177 if (intel_crtc->atomic.pre_disable_primary)
13178 intel_pre_disable_primary(crtc);
13179
13180 if (intel_crtc->atomic.update_wm)
13181 intel_update_watermarks(crtc);
13182
13183 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013184
13185 /* Perform vblank evasion around commit operation */
13186 if (intel_crtc->active)
13187 intel_crtc->atomic.evade =
13188 intel_pipe_update_start(intel_crtc,
13189 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013190}
13191
13192static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13193{
13194 struct drm_device *dev = crtc->dev;
13195 struct drm_i915_private *dev_priv = dev->dev_private;
13196 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13197 struct drm_plane *p;
13198
Matt Roperc34c9ee2014-12-23 10:41:50 -080013199 if (intel_crtc->atomic.evade)
13200 intel_pipe_update_end(intel_crtc,
13201 intel_crtc->atomic.start_vbl_count);
13202
Matt Roper32b7eee2014-12-24 07:59:06 -080013203 intel_runtime_pm_put(dev_priv);
13204
13205 if (intel_crtc->atomic.wait_vblank)
13206 intel_wait_for_vblank(dev, intel_crtc->pipe);
13207
13208 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13209
13210 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013211 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013212 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013213 mutex_unlock(&dev->struct_mutex);
13214 }
Matt Roper465c1202014-05-29 08:06:54 -070013215
Matt Roper32b7eee2014-12-24 07:59:06 -080013216 if (intel_crtc->atomic.post_enable_primary)
13217 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013218
Matt Roper32b7eee2014-12-24 07:59:06 -080013219 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13220 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13221 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13222 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013223
Matt Roper32b7eee2014-12-24 07:59:06 -080013224 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013225}
13226
Matt Ropercf4c7c12014-12-04 10:27:42 -080013227/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013228 * intel_plane_destroy - destroy a plane
13229 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013230 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013231 * Common destruction function for all types of planes (primary, cursor,
13232 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013233 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013234void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013235{
13236 struct intel_plane *intel_plane = to_intel_plane(plane);
13237 drm_plane_cleanup(plane);
13238 kfree(intel_plane);
13239}
13240
Matt Roper65a3fea2015-01-21 16:35:42 -080013241const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013242 .update_plane = drm_atomic_helper_update_plane,
13243 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013244 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013245 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013246 .atomic_get_property = intel_plane_atomic_get_property,
13247 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013248 .atomic_duplicate_state = intel_plane_duplicate_state,
13249 .atomic_destroy_state = intel_plane_destroy_state,
13250
Matt Roper465c1202014-05-29 08:06:54 -070013251};
13252
13253static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13254 int pipe)
13255{
13256 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013257 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013258 const uint32_t *intel_primary_formats;
13259 int num_formats;
13260
13261 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13262 if (primary == NULL)
13263 return NULL;
13264
Matt Roper8e7d6882015-01-21 16:35:41 -080013265 state = intel_create_plane_state(&primary->base);
13266 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013267 kfree(primary);
13268 return NULL;
13269 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013270 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013271
Matt Roper465c1202014-05-29 08:06:54 -070013272 primary->can_scale = false;
13273 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013274 if (INTEL_INFO(dev)->gen >= 9) {
13275 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013276 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013277 }
Matt Roper465c1202014-05-29 08:06:54 -070013278 primary->pipe = pipe;
13279 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013280 primary->check_plane = intel_check_primary_plane;
13281 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013282 primary->disable_plane = intel_disable_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013283 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013284 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13285 primary->plane = !pipe;
13286
13287 if (INTEL_INFO(dev)->gen <= 3) {
13288 intel_primary_formats = intel_primary_formats_gen2;
13289 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
13290 } else {
13291 intel_primary_formats = intel_primary_formats_gen4;
13292 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
13293 }
13294
13295 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013296 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013297 intel_primary_formats, num_formats,
13298 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013299
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013300 if (INTEL_INFO(dev)->gen >= 4)
13301 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013302
Matt Roperea2c67b2014-12-23 10:41:52 -080013303 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13304
Matt Roper465c1202014-05-29 08:06:54 -070013305 return &primary->base;
13306}
13307
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013308void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13309{
13310 if (!dev->mode_config.rotation_property) {
13311 unsigned long flags = BIT(DRM_ROTATE_0) |
13312 BIT(DRM_ROTATE_180);
13313
13314 if (INTEL_INFO(dev)->gen >= 9)
13315 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13316
13317 dev->mode_config.rotation_property =
13318 drm_mode_create_rotation_property(dev, flags);
13319 }
13320 if (dev->mode_config.rotation_property)
13321 drm_object_attach_property(&plane->base.base,
13322 dev->mode_config.rotation_property,
13323 plane->base.state->rotation);
13324}
13325
Matt Roper3d7d6512014-06-10 08:28:13 -070013326static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013327intel_check_cursor_plane(struct drm_plane *plane,
13328 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013329{
Matt Roper2b875c22014-12-01 15:40:13 -080013330 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013331 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013332 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013333 struct drm_rect *dest = &state->dst;
13334 struct drm_rect *src = &state->src;
13335 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013336 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013337 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013338 unsigned stride;
13339 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013340
Matt Roperea2c67b2014-12-23 10:41:52 -080013341 crtc = crtc ? crtc : plane->crtc;
13342 intel_crtc = to_intel_crtc(crtc);
13343
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013344 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013345 src, dest, clip,
13346 DRM_PLANE_HELPER_NO_SCALING,
13347 DRM_PLANE_HELPER_NO_SCALING,
13348 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013349 if (ret)
13350 return ret;
13351
13352
13353 /* if we want to turn off the cursor ignore width and height */
13354 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013355 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013356
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013357 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013358 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13359 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13360 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013361 return -EINVAL;
13362 }
13363
Matt Roperea2c67b2014-12-23 10:41:52 -080013364 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13365 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013366 DRM_DEBUG_KMS("buffer is too small\n");
13367 return -ENOMEM;
13368 }
13369
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013370 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013371 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13372 ret = -EINVAL;
13373 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013374
Matt Roper32b7eee2014-12-24 07:59:06 -080013375finish:
13376 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013377 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013378 intel_crtc->atomic.update_wm = true;
13379
13380 intel_crtc->atomic.fb_bits |=
13381 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13382 }
13383
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013384 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013385}
13386
Matt Roperf4a2cf22014-12-01 15:40:12 -080013387static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013388intel_disable_cursor_plane(struct drm_plane *plane,
13389 struct drm_crtc *crtc,
13390 bool force)
13391{
13392 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13393
13394 if (!force) {
13395 plane->fb = NULL;
13396 intel_crtc->cursor_bo = NULL;
13397 intel_crtc->cursor_addr = 0;
13398 }
13399
13400 intel_crtc_update_cursor(crtc, false);
13401}
13402
13403static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013404intel_commit_cursor_plane(struct drm_plane *plane,
13405 struct intel_plane_state *state)
13406{
Matt Roper2b875c22014-12-01 15:40:13 -080013407 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013408 struct drm_device *dev = plane->dev;
13409 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013410 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013411 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013412
Matt Roperea2c67b2014-12-23 10:41:52 -080013413 crtc = crtc ? crtc : plane->crtc;
13414 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013415
Matt Roperea2c67b2014-12-23 10:41:52 -080013416 plane->fb = state->base.fb;
13417 crtc->cursor_x = state->base.crtc_x;
13418 crtc->cursor_y = state->base.crtc_y;
13419
Gustavo Padovana912f122014-12-01 15:40:10 -080013420 if (intel_crtc->cursor_bo == obj)
13421 goto update;
13422
Matt Roperf4a2cf22014-12-01 15:40:12 -080013423 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013424 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013425 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013426 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013427 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013428 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013429
Gustavo Padovana912f122014-12-01 15:40:10 -080013430 intel_crtc->cursor_addr = addr;
13431 intel_crtc->cursor_bo = obj;
13432update:
Gustavo Padovana912f122014-12-01 15:40:10 -080013433
Matt Roper32b7eee2014-12-24 07:59:06 -080013434 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013435 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013436}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013437
Matt Roper3d7d6512014-06-10 08:28:13 -070013438static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13439 int pipe)
13440{
13441 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013442 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013443
13444 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13445 if (cursor == NULL)
13446 return NULL;
13447
Matt Roper8e7d6882015-01-21 16:35:41 -080013448 state = intel_create_plane_state(&cursor->base);
13449 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013450 kfree(cursor);
13451 return NULL;
13452 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013453 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013454
Matt Roper3d7d6512014-06-10 08:28:13 -070013455 cursor->can_scale = false;
13456 cursor->max_downscale = 1;
13457 cursor->pipe = pipe;
13458 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013459 cursor->check_plane = intel_check_cursor_plane;
13460 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013461 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013462
13463 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013464 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013465 intel_cursor_formats,
13466 ARRAY_SIZE(intel_cursor_formats),
13467 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013468
13469 if (INTEL_INFO(dev)->gen >= 4) {
13470 if (!dev->mode_config.rotation_property)
13471 dev->mode_config.rotation_property =
13472 drm_mode_create_rotation_property(dev,
13473 BIT(DRM_ROTATE_0) |
13474 BIT(DRM_ROTATE_180));
13475 if (dev->mode_config.rotation_property)
13476 drm_object_attach_property(&cursor->base.base,
13477 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013478 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013479 }
13480
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013481 if (INTEL_INFO(dev)->gen >=9)
13482 state->scaler_id = -1;
13483
Matt Roperea2c67b2014-12-23 10:41:52 -080013484 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13485
Matt Roper3d7d6512014-06-10 08:28:13 -070013486 return &cursor->base;
13487}
13488
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013489static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13490 struct intel_crtc_state *crtc_state)
13491{
13492 int i;
13493 struct intel_scaler *intel_scaler;
13494 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13495
13496 for (i = 0; i < intel_crtc->num_scalers; i++) {
13497 intel_scaler = &scaler_state->scalers[i];
13498 intel_scaler->in_use = 0;
13499 intel_scaler->id = i;
13500
13501 intel_scaler->mode = PS_SCALER_MODE_DYN;
13502 }
13503
13504 scaler_state->scaler_id = -1;
13505}
13506
Hannes Ederb358d0a2008-12-18 21:18:47 +010013507static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013508{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013509 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013510 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013511 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013512 struct drm_plane *primary = NULL;
13513 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013514 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013515
Daniel Vetter955382f2013-09-19 14:05:45 +020013516 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013517 if (intel_crtc == NULL)
13518 return;
13519
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013520 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13521 if (!crtc_state)
13522 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030013523 intel_crtc->config = crtc_state;
13524 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080013525 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013526
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013527 /* initialize shared scalers */
13528 if (INTEL_INFO(dev)->gen >= 9) {
13529 if (pipe == PIPE_C)
13530 intel_crtc->num_scalers = 1;
13531 else
13532 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13533
13534 skl_init_scalers(dev, intel_crtc, crtc_state);
13535 }
13536
Matt Roper465c1202014-05-29 08:06:54 -070013537 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013538 if (!primary)
13539 goto fail;
13540
13541 cursor = intel_cursor_plane_create(dev, pipe);
13542 if (!cursor)
13543 goto fail;
13544
Matt Roper465c1202014-05-29 08:06:54 -070013545 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013546 cursor, &intel_crtc_funcs);
13547 if (ret)
13548 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013549
13550 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013551 for (i = 0; i < 256; i++) {
13552 intel_crtc->lut_r[i] = i;
13553 intel_crtc->lut_g[i] = i;
13554 intel_crtc->lut_b[i] = i;
13555 }
13556
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013557 /*
13558 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013559 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013560 */
Jesse Barnes80824002009-09-10 15:28:06 -070013561 intel_crtc->pipe = pipe;
13562 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013563 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013564 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013565 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013566 }
13567
Chris Wilson4b0e3332014-05-30 16:35:26 +030013568 intel_crtc->cursor_base = ~0;
13569 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013570 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013571
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013572 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13573 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13574 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13575 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13576
Jesse Barnes79e53942008-11-07 14:24:08 -080013577 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013578
13579 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013580 return;
13581
13582fail:
13583 if (primary)
13584 drm_plane_cleanup(primary);
13585 if (cursor)
13586 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013587 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013588 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013589}
13590
Jesse Barnes752aa882013-10-31 18:55:49 +020013591enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13592{
13593 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013594 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013595
Rob Clark51fd3712013-11-19 12:10:12 -050013596 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013597
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013598 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013599 return INVALID_PIPE;
13600
13601 return to_intel_crtc(encoder->crtc)->pipe;
13602}
13603
Carl Worth08d7b3d2009-04-29 14:43:54 -070013604int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013605 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013606{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013607 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013608 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013609 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013610
Rob Clark7707e652014-07-17 23:30:04 -040013611 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013612
Rob Clark7707e652014-07-17 23:30:04 -040013613 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013614 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013615 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013616 }
13617
Rob Clark7707e652014-07-17 23:30:04 -040013618 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013619 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013620
Daniel Vetterc05422d2009-08-11 16:05:30 +020013621 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013622}
13623
Daniel Vetter66a92782012-07-12 20:08:18 +020013624static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013625{
Daniel Vetter66a92782012-07-12 20:08:18 +020013626 struct drm_device *dev = encoder->base.dev;
13627 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013628 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013629 int entry = 0;
13630
Damien Lespiaub2784e12014-08-05 11:29:37 +010013631 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013632 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013633 index_mask |= (1 << entry);
13634
Jesse Barnes79e53942008-11-07 14:24:08 -080013635 entry++;
13636 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013637
Jesse Barnes79e53942008-11-07 14:24:08 -080013638 return index_mask;
13639}
13640
Chris Wilson4d302442010-12-14 19:21:29 +000013641static bool has_edp_a(struct drm_device *dev)
13642{
13643 struct drm_i915_private *dev_priv = dev->dev_private;
13644
13645 if (!IS_MOBILE(dev))
13646 return false;
13647
13648 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13649 return false;
13650
Damien Lespiaue3589902014-02-07 19:12:50 +000013651 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013652 return false;
13653
13654 return true;
13655}
13656
Jesse Barnes84b4e042014-06-25 08:24:29 -070013657static bool intel_crt_present(struct drm_device *dev)
13658{
13659 struct drm_i915_private *dev_priv = dev->dev_private;
13660
Damien Lespiau884497e2013-12-03 13:56:23 +000013661 if (INTEL_INFO(dev)->gen >= 9)
13662 return false;
13663
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013664 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013665 return false;
13666
13667 if (IS_CHERRYVIEW(dev))
13668 return false;
13669
13670 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13671 return false;
13672
13673 return true;
13674}
13675
Jesse Barnes79e53942008-11-07 14:24:08 -080013676static void intel_setup_outputs(struct drm_device *dev)
13677{
Eric Anholt725e30a2009-01-22 13:01:02 -080013678 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013679 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013680 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013681
Daniel Vetterc9093352013-06-06 22:22:47 +020013682 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013683
Jesse Barnes84b4e042014-06-25 08:24:29 -070013684 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013685 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013686
Vandana Kannanc776eb22014-08-19 12:05:01 +053013687 if (IS_BROXTON(dev)) {
13688 /*
13689 * FIXME: Broxton doesn't support port detection via the
13690 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13691 * detect the ports.
13692 */
13693 intel_ddi_init(dev, PORT_A);
13694 intel_ddi_init(dev, PORT_B);
13695 intel_ddi_init(dev, PORT_C);
13696 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013697 int found;
13698
Jesse Barnesde31fac2015-03-06 15:53:32 -080013699 /*
13700 * Haswell uses DDI functions to detect digital outputs.
13701 * On SKL pre-D0 the strap isn't connected, so we assume
13702 * it's there.
13703 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013704 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013705 /* WaIgnoreDDIAStrap: skl */
13706 if (found ||
13707 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013708 intel_ddi_init(dev, PORT_A);
13709
13710 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13711 * register */
13712 found = I915_READ(SFUSE_STRAP);
13713
13714 if (found & SFUSE_STRAP_DDIB_DETECTED)
13715 intel_ddi_init(dev, PORT_B);
13716 if (found & SFUSE_STRAP_DDIC_DETECTED)
13717 intel_ddi_init(dev, PORT_C);
13718 if (found & SFUSE_STRAP_DDID_DETECTED)
13719 intel_ddi_init(dev, PORT_D);
13720 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013721 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013722 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013723
13724 if (has_edp_a(dev))
13725 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013726
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013727 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013728 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013729 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013730 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013731 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013732 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013733 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013734 }
13735
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013736 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013737 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013738
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013739 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013740 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013741
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013742 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013743 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013744
Daniel Vetter270b3042012-10-27 15:52:05 +020013745 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013746 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013747 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013748 /*
13749 * The DP_DETECTED bit is the latched state of the DDC
13750 * SDA pin at boot. However since eDP doesn't require DDC
13751 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13752 * eDP ports may have been muxed to an alternate function.
13753 * Thus we can't rely on the DP_DETECTED bit alone to detect
13754 * eDP ports. Consult the VBT as well as DP_DETECTED to
13755 * detect eDP ports.
13756 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013757 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13758 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013759 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13760 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013761 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13762 intel_dp_is_edp(dev, PORT_B))
13763 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013764
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013765 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13766 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070013767 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13768 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013769 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13770 intel_dp_is_edp(dev, PORT_C))
13771 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053013772
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013773 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013774 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013775 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
13776 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013777 /* eDP not supported on port D, so don't check VBT */
13778 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
13779 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013780 }
13781
Jani Nikula3cfca972013-08-27 15:12:26 +030013782 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080013783 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013784 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080013785
Paulo Zanonie2debe92013-02-18 19:00:27 -030013786 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013787 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013788 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013789 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
13790 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013791 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013792 }
Ma Ling27185ae2009-08-24 13:50:23 +080013793
Imre Deake7281ea2013-05-08 13:14:08 +030013794 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013795 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080013796 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013797
13798 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013799
Paulo Zanonie2debe92013-02-18 19:00:27 -030013800 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013801 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013802 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013803 }
Ma Ling27185ae2009-08-24 13:50:23 +080013804
Paulo Zanonie2debe92013-02-18 19:00:27 -030013805 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013806
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013807 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
13808 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013809 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013810 }
Imre Deake7281ea2013-05-08 13:14:08 +030013811 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013812 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080013813 }
Ma Ling27185ae2009-08-24 13:50:23 +080013814
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013815 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030013816 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013817 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070013818 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013819 intel_dvo_init(dev);
13820
Zhenyu Wang103a1962009-11-27 11:44:36 +080013821 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013822 intel_tv_init(dev);
13823
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080013824 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070013825
Damien Lespiaub2784e12014-08-05 11:29:37 +010013826 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010013827 encoder->base.possible_crtcs = encoder->crtc_mask;
13828 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020013829 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080013830 }
Chris Wilson47356eb2011-01-11 17:06:04 +000013831
Paulo Zanonidde86e22012-12-01 12:04:25 -020013832 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020013833
13834 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013835}
13836
13837static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
13838{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013839 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080013840 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080013841
Daniel Vetteref2d6332014-02-10 18:00:38 +010013842 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013843 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010013844 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013845 drm_gem_object_unreference(&intel_fb->obj->base);
13846 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013847 kfree(intel_fb);
13848}
13849
13850static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000013851 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080013852 unsigned int *handle)
13853{
13854 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000013855 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080013856
Chris Wilson05394f32010-11-08 19:18:58 +000013857 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080013858}
13859
13860static const struct drm_framebuffer_funcs intel_fb_funcs = {
13861 .destroy = intel_user_framebuffer_destroy,
13862 .create_handle = intel_user_framebuffer_create_handle,
13863};
13864
Damien Lespiaub3218032015-02-27 11:15:18 +000013865static
13866u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
13867 uint32_t pixel_format)
13868{
13869 u32 gen = INTEL_INFO(dev)->gen;
13870
13871 if (gen >= 9) {
13872 /* "The stride in bytes must not exceed the of the size of 8K
13873 * pixels and 32K bytes."
13874 */
13875 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
13876 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
13877 return 32*1024;
13878 } else if (gen >= 4) {
13879 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13880 return 16*1024;
13881 else
13882 return 32*1024;
13883 } else if (gen >= 3) {
13884 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13885 return 8*1024;
13886 else
13887 return 16*1024;
13888 } else {
13889 /* XXX DSPC is limited to 4k tiled */
13890 return 8*1024;
13891 }
13892}
13893
Daniel Vetterb5ea6422014-03-02 21:18:00 +010013894static int intel_framebuffer_init(struct drm_device *dev,
13895 struct intel_framebuffer *intel_fb,
13896 struct drm_mode_fb_cmd2 *mode_cmd,
13897 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080013898{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000013899 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080013900 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000013901 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080013902
Daniel Vetterdd4916c2013-10-09 21:23:51 +020013903 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
13904
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013905 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
13906 /* Enforce that fb modifier and tiling mode match, but only for
13907 * X-tiled. This is needed for FBC. */
13908 if (!!(obj->tiling_mode == I915_TILING_X) !=
13909 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
13910 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
13911 return -EINVAL;
13912 }
13913 } else {
13914 if (obj->tiling_mode == I915_TILING_X)
13915 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
13916 else if (obj->tiling_mode == I915_TILING_Y) {
13917 DRM_DEBUG("No Y tiling for legacy addfb\n");
13918 return -EINVAL;
13919 }
13920 }
13921
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000013922 /* Passed in modifier sanity checking. */
13923 switch (mode_cmd->modifier[0]) {
13924 case I915_FORMAT_MOD_Y_TILED:
13925 case I915_FORMAT_MOD_Yf_TILED:
13926 if (INTEL_INFO(dev)->gen < 9) {
13927 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
13928 mode_cmd->modifier[0]);
13929 return -EINVAL;
13930 }
13931 case DRM_FORMAT_MOD_NONE:
13932 case I915_FORMAT_MOD_X_TILED:
13933 break;
13934 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070013935 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
13936 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010013937 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013938 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013939
Damien Lespiaub3218032015-02-27 11:15:18 +000013940 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
13941 mode_cmd->pixel_format);
13942 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
13943 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
13944 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010013945 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013946 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013947
Damien Lespiaub3218032015-02-27 11:15:18 +000013948 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
13949 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010013950 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000013951 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
13952 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013953 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010013954 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013955 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013956 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013957
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013958 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013959 mode_cmd->pitches[0] != obj->stride) {
13960 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
13961 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013962 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013963 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013964
Ville Syrjälä57779d02012-10-31 17:50:14 +020013965 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013966 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020013967 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020013968 case DRM_FORMAT_RGB565:
13969 case DRM_FORMAT_XRGB8888:
13970 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020013971 break;
13972 case DRM_FORMAT_XRGB1555:
13973 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013974 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013975 DRM_DEBUG("unsupported pixel format: %s\n",
13976 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013977 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013978 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020013979 break;
13980 case DRM_FORMAT_XBGR8888:
13981 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020013982 case DRM_FORMAT_XRGB2101010:
13983 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020013984 case DRM_FORMAT_XBGR2101010:
13985 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013986 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013987 DRM_DEBUG("unsupported pixel format: %s\n",
13988 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013989 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013990 }
Jesse Barnesb5626742011-06-24 12:19:27 -070013991 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020013992 case DRM_FORMAT_YUYV:
13993 case DRM_FORMAT_UYVY:
13994 case DRM_FORMAT_YVYU:
13995 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013996 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013997 DRM_DEBUG("unsupported pixel format: %s\n",
13998 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013999 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014000 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014001 break;
14002 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014003 DRM_DEBUG("unsupported pixel format: %s\n",
14004 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014005 return -EINVAL;
14006 }
14007
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014008 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14009 if (mode_cmd->offsets[0] != 0)
14010 return -EINVAL;
14011
Damien Lespiauec2c9812015-01-20 12:51:45 +000014012 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014013 mode_cmd->pixel_format,
14014 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014015 /* FIXME drm helper for size checks (especially planar formats)? */
14016 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14017 return -EINVAL;
14018
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014019 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14020 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014021 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014022
Jesse Barnes79e53942008-11-07 14:24:08 -080014023 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14024 if (ret) {
14025 DRM_ERROR("framebuffer init failed %d\n", ret);
14026 return ret;
14027 }
14028
Jesse Barnes79e53942008-11-07 14:24:08 -080014029 return 0;
14030}
14031
Jesse Barnes79e53942008-11-07 14:24:08 -080014032static struct drm_framebuffer *
14033intel_user_framebuffer_create(struct drm_device *dev,
14034 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014035 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014036{
Chris Wilson05394f32010-11-08 19:18:58 +000014037 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014038
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014039 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14040 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014041 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014042 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014043
Chris Wilsond2dff872011-04-19 08:36:26 +010014044 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014045}
14046
Daniel Vetter4520f532013-10-09 09:18:51 +020014047#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014048static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014049{
14050}
14051#endif
14052
Jesse Barnes79e53942008-11-07 14:24:08 -080014053static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014054 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014055 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014056 .atomic_check = intel_atomic_check,
14057 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080014058};
14059
Jesse Barnese70236a2009-09-21 10:42:27 -070014060/* Set up chip specific display functions */
14061static void intel_init_display(struct drm_device *dev)
14062{
14063 struct drm_i915_private *dev_priv = dev->dev_private;
14064
Daniel Vetteree9300b2013-06-03 22:40:22 +020014065 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14066 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014067 else if (IS_CHERRYVIEW(dev))
14068 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014069 else if (IS_VALLEYVIEW(dev))
14070 dev_priv->display.find_dpll = vlv_find_best_dpll;
14071 else if (IS_PINEVIEW(dev))
14072 dev_priv->display.find_dpll = pnv_find_best_dpll;
14073 else
14074 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14075
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014076 if (INTEL_INFO(dev)->gen >= 9) {
14077 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014078 dev_priv->display.get_initial_plane_config =
14079 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014080 dev_priv->display.crtc_compute_clock =
14081 haswell_crtc_compute_clock;
14082 dev_priv->display.crtc_enable = haswell_crtc_enable;
14083 dev_priv->display.crtc_disable = haswell_crtc_disable;
14084 dev_priv->display.off = ironlake_crtc_off;
14085 dev_priv->display.update_primary_plane =
14086 skylake_update_primary_plane;
14087 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014088 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014089 dev_priv->display.get_initial_plane_config =
14090 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014091 dev_priv->display.crtc_compute_clock =
14092 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014093 dev_priv->display.crtc_enable = haswell_crtc_enable;
14094 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030014095 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014096 dev_priv->display.update_primary_plane =
14097 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014098 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014099 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014100 dev_priv->display.get_initial_plane_config =
14101 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014102 dev_priv->display.crtc_compute_clock =
14103 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014104 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14105 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014106 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014107 dev_priv->display.update_primary_plane =
14108 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014109 } else if (IS_VALLEYVIEW(dev)) {
14110 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014111 dev_priv->display.get_initial_plane_config =
14112 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014113 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014114 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14115 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14116 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014117 dev_priv->display.update_primary_plane =
14118 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014119 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014120 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014121 dev_priv->display.get_initial_plane_config =
14122 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014123 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014124 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14125 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014126 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014127 dev_priv->display.update_primary_plane =
14128 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014129 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014130
Jesse Barnese70236a2009-09-21 10:42:27 -070014131 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014132 if (IS_SKYLAKE(dev))
14133 dev_priv->display.get_display_clock_speed =
14134 skylake_get_display_clock_speed;
14135 else if (IS_BROADWELL(dev))
14136 dev_priv->display.get_display_clock_speed =
14137 broadwell_get_display_clock_speed;
14138 else if (IS_HASWELL(dev))
14139 dev_priv->display.get_display_clock_speed =
14140 haswell_get_display_clock_speed;
14141 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014142 dev_priv->display.get_display_clock_speed =
14143 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014144 else if (IS_GEN5(dev))
14145 dev_priv->display.get_display_clock_speed =
14146 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014147 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14148 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070014149 dev_priv->display.get_display_clock_speed =
14150 i945_get_display_clock_speed;
14151 else if (IS_I915G(dev))
14152 dev_priv->display.get_display_clock_speed =
14153 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014154 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014155 dev_priv->display.get_display_clock_speed =
14156 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014157 else if (IS_PINEVIEW(dev))
14158 dev_priv->display.get_display_clock_speed =
14159 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014160 else if (IS_I915GM(dev))
14161 dev_priv->display.get_display_clock_speed =
14162 i915gm_get_display_clock_speed;
14163 else if (IS_I865G(dev))
14164 dev_priv->display.get_display_clock_speed =
14165 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014166 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014167 dev_priv->display.get_display_clock_speed =
14168 i855_get_display_clock_speed;
14169 else /* 852, 830 */
14170 dev_priv->display.get_display_clock_speed =
14171 i830_get_display_clock_speed;
14172
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014173 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014174 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014175 } else if (IS_GEN6(dev)) {
14176 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014177 } else if (IS_IVYBRIDGE(dev)) {
14178 /* FIXME: detect B0+ stepping and use auto training */
14179 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014180 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014181 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014182 } else if (IS_VALLEYVIEW(dev)) {
14183 dev_priv->display.modeset_global_resources =
14184 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014185 } else if (IS_BROXTON(dev)) {
14186 dev_priv->display.modeset_global_resources =
14187 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014188 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014189
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014190 switch (INTEL_INFO(dev)->gen) {
14191 case 2:
14192 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14193 break;
14194
14195 case 3:
14196 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14197 break;
14198
14199 case 4:
14200 case 5:
14201 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14202 break;
14203
14204 case 6:
14205 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14206 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014207 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014208 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014209 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14210 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014211 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014212 /* Drop through - unsupported since execlist only. */
14213 default:
14214 /* Default just returns -ENODEV to indicate unsupported */
14215 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014216 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014217
14218 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014219
14220 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014221}
14222
Jesse Barnesb690e962010-07-19 13:53:12 -070014223/*
14224 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14225 * resume, or other times. This quirk makes sure that's the case for
14226 * affected systems.
14227 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014228static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014229{
14230 struct drm_i915_private *dev_priv = dev->dev_private;
14231
14232 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014233 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014234}
14235
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014236static void quirk_pipeb_force(struct drm_device *dev)
14237{
14238 struct drm_i915_private *dev_priv = dev->dev_private;
14239
14240 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14241 DRM_INFO("applying pipe b force quirk\n");
14242}
14243
Keith Packard435793d2011-07-12 14:56:22 -070014244/*
14245 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14246 */
14247static void quirk_ssc_force_disable(struct drm_device *dev)
14248{
14249 struct drm_i915_private *dev_priv = dev->dev_private;
14250 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014251 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014252}
14253
Carsten Emde4dca20e2012-03-15 15:56:26 +010014254/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014255 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14256 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014257 */
14258static void quirk_invert_brightness(struct drm_device *dev)
14259{
14260 struct drm_i915_private *dev_priv = dev->dev_private;
14261 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014262 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014263}
14264
Scot Doyle9c72cc62014-07-03 23:27:50 +000014265/* Some VBT's incorrectly indicate no backlight is present */
14266static void quirk_backlight_present(struct drm_device *dev)
14267{
14268 struct drm_i915_private *dev_priv = dev->dev_private;
14269 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14270 DRM_INFO("applying backlight present quirk\n");
14271}
14272
Jesse Barnesb690e962010-07-19 13:53:12 -070014273struct intel_quirk {
14274 int device;
14275 int subsystem_vendor;
14276 int subsystem_device;
14277 void (*hook)(struct drm_device *dev);
14278};
14279
Egbert Eich5f85f172012-10-14 15:46:38 +020014280/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14281struct intel_dmi_quirk {
14282 void (*hook)(struct drm_device *dev);
14283 const struct dmi_system_id (*dmi_id_list)[];
14284};
14285
14286static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14287{
14288 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14289 return 1;
14290}
14291
14292static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14293 {
14294 .dmi_id_list = &(const struct dmi_system_id[]) {
14295 {
14296 .callback = intel_dmi_reverse_brightness,
14297 .ident = "NCR Corporation",
14298 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14299 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14300 },
14301 },
14302 { } /* terminating entry */
14303 },
14304 .hook = quirk_invert_brightness,
14305 },
14306};
14307
Ben Widawskyc43b5632012-04-16 14:07:40 -070014308static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014309 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14310 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14311
Jesse Barnesb690e962010-07-19 13:53:12 -070014312 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14313 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14314
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014315 /* 830 needs to leave pipe A & dpll A up */
14316 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14317
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014318 /* 830 needs to leave pipe B & dpll B up */
14319 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14320
Keith Packard435793d2011-07-12 14:56:22 -070014321 /* Lenovo U160 cannot use SSC on LVDS */
14322 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014323
14324 /* Sony Vaio Y cannot use SSC on LVDS */
14325 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014326
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014327 /* Acer Aspire 5734Z must invert backlight brightness */
14328 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14329
14330 /* Acer/eMachines G725 */
14331 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14332
14333 /* Acer/eMachines e725 */
14334 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14335
14336 /* Acer/Packard Bell NCL20 */
14337 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14338
14339 /* Acer Aspire 4736Z */
14340 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014341
14342 /* Acer Aspire 5336 */
14343 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014344
14345 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14346 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014347
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014348 /* Acer C720 Chromebook (Core i3 4005U) */
14349 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14350
jens steinb2a96012014-10-28 20:25:53 +010014351 /* Apple Macbook 2,1 (Core 2 T7400) */
14352 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14353
Scot Doyled4967d82014-07-03 23:27:52 +000014354 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14355 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014356
14357 /* HP Chromebook 14 (Celeron 2955U) */
14358 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014359
14360 /* Dell Chromebook 11 */
14361 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014362};
14363
14364static void intel_init_quirks(struct drm_device *dev)
14365{
14366 struct pci_dev *d = dev->pdev;
14367 int i;
14368
14369 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14370 struct intel_quirk *q = &intel_quirks[i];
14371
14372 if (d->device == q->device &&
14373 (d->subsystem_vendor == q->subsystem_vendor ||
14374 q->subsystem_vendor == PCI_ANY_ID) &&
14375 (d->subsystem_device == q->subsystem_device ||
14376 q->subsystem_device == PCI_ANY_ID))
14377 q->hook(dev);
14378 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014379 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14380 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14381 intel_dmi_quirks[i].hook(dev);
14382 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014383}
14384
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014385/* Disable the VGA plane that we never use */
14386static void i915_disable_vga(struct drm_device *dev)
14387{
14388 struct drm_i915_private *dev_priv = dev->dev_private;
14389 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014390 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014391
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014392 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014393 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014394 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014395 sr1 = inb(VGA_SR_DATA);
14396 outb(sr1 | 1<<5, VGA_SR_DATA);
14397 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14398 udelay(300);
14399
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014400 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014401 POSTING_READ(vga_reg);
14402}
14403
Daniel Vetterf8175862012-04-10 15:50:11 +020014404void intel_modeset_init_hw(struct drm_device *dev)
14405{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014406 intel_prepare_ddi(dev);
14407
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030014408 if (IS_VALLEYVIEW(dev))
14409 vlv_update_cdclk(dev);
14410
Daniel Vetterf8175862012-04-10 15:50:11 +020014411 intel_init_clock_gating(dev);
14412
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014413 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014414}
14415
Jesse Barnes79e53942008-11-07 14:24:08 -080014416void intel_modeset_init(struct drm_device *dev)
14417{
Jesse Barnes652c3932009-08-17 13:31:43 -070014418 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014419 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014420 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014421 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014422
14423 drm_mode_config_init(dev);
14424
14425 dev->mode_config.min_width = 0;
14426 dev->mode_config.min_height = 0;
14427
Dave Airlie019d96c2011-09-29 16:20:42 +010014428 dev->mode_config.preferred_depth = 24;
14429 dev->mode_config.prefer_shadow = 1;
14430
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014431 dev->mode_config.allow_fb_modifiers = true;
14432
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014433 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014434
Jesse Barnesb690e962010-07-19 13:53:12 -070014435 intel_init_quirks(dev);
14436
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014437 intel_init_pm(dev);
14438
Ben Widawskye3c74752013-04-05 13:12:39 -070014439 if (INTEL_INFO(dev)->num_pipes == 0)
14440 return;
14441
Jesse Barnese70236a2009-09-21 10:42:27 -070014442 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014443 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014444
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014445 if (IS_GEN2(dev)) {
14446 dev->mode_config.max_width = 2048;
14447 dev->mode_config.max_height = 2048;
14448 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014449 dev->mode_config.max_width = 4096;
14450 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014451 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014452 dev->mode_config.max_width = 8192;
14453 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014454 }
Damien Lespiau068be562014-03-28 14:17:49 +000014455
Ville Syrjälädc41c152014-08-13 11:57:05 +030014456 if (IS_845G(dev) || IS_I865G(dev)) {
14457 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14458 dev->mode_config.cursor_height = 1023;
14459 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014460 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14461 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14462 } else {
14463 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14464 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14465 }
14466
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014467 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014468
Zhao Yakui28c97732009-10-09 11:39:41 +080014469 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014470 INTEL_INFO(dev)->num_pipes,
14471 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014472
Damien Lespiau055e3932014-08-18 13:49:10 +010014473 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014474 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014475 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014476 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014477 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014478 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014479 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014480 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014481 }
14482
Jesse Barnesf42bb702013-12-16 16:34:23 -080014483 intel_init_dpio(dev);
14484
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014485 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014486
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014487 /* Just disable it once at startup */
14488 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014489 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014490
14491 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014492 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014493
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014494 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014495 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014496 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014497
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014498 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080014499 if (!crtc->active)
14500 continue;
14501
Jesse Barnes46f297f2014-03-07 08:57:48 -080014502 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014503 * Note that reserving the BIOS fb up front prevents us
14504 * from stuffing other stolen allocations like the ring
14505 * on top. This prevents some ugliness at boot time, and
14506 * can even allow for smooth boot transitions if the BIOS
14507 * fb is large enough for the active pipe configuration.
14508 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014509 if (dev_priv->display.get_initial_plane_config) {
14510 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080014511 &crtc->plane_config);
14512 /*
14513 * If the fb is shared between multiple heads, we'll
14514 * just get the first one.
14515 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010014516 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014517 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080014518 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014519}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014520
Daniel Vetter7fad7982012-07-04 17:51:47 +020014521static void intel_enable_pipe_a(struct drm_device *dev)
14522{
14523 struct intel_connector *connector;
14524 struct drm_connector *crt = NULL;
14525 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014526 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014527
14528 /* We can't just switch on the pipe A, we need to set things up with a
14529 * proper mode and output configuration. As a gross hack, enable pipe A
14530 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014531 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014532 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14533 crt = &connector->base;
14534 break;
14535 }
14536 }
14537
14538 if (!crt)
14539 return;
14540
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014541 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014542 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014543}
14544
Daniel Vetterfa555832012-10-10 23:14:00 +020014545static bool
14546intel_check_plane_mapping(struct intel_crtc *crtc)
14547{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014548 struct drm_device *dev = crtc->base.dev;
14549 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014550 u32 reg, val;
14551
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014552 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014553 return true;
14554
14555 reg = DSPCNTR(!crtc->plane);
14556 val = I915_READ(reg);
14557
14558 if ((val & DISPLAY_PLANE_ENABLE) &&
14559 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14560 return false;
14561
14562 return true;
14563}
14564
Daniel Vetter24929352012-07-02 20:28:59 +020014565static void intel_sanitize_crtc(struct intel_crtc *crtc)
14566{
14567 struct drm_device *dev = crtc->base.dev;
14568 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014569 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014570
Daniel Vetter24929352012-07-02 20:28:59 +020014571 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014572 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014573 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14574
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014575 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014576 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014577 if (crtc->active) {
14578 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014579 drm_crtc_vblank_on(&crtc->base);
14580 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014581
Daniel Vetter24929352012-07-02 20:28:59 +020014582 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014583 * disable the crtc (and hence change the state) if it is wrong. Note
14584 * that gen4+ has a fixed plane -> pipe mapping. */
14585 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014586 struct intel_connector *connector;
14587 bool plane;
14588
Daniel Vetter24929352012-07-02 20:28:59 +020014589 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14590 crtc->base.base.id);
14591
14592 /* Pipe has the wrong plane attached and the plane is active.
14593 * Temporarily change the plane mapping and disable everything
14594 * ... */
14595 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014596 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014597 crtc->plane = !plane;
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030014598 intel_crtc_disable_planes(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020014599 dev_priv->display.crtc_disable(&crtc->base);
14600 crtc->plane = plane;
14601
14602 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014603 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014604 if (connector->encoder->base.crtc != &crtc->base)
14605 continue;
14606
Egbert Eich7f1950f2014-04-25 10:56:22 +020014607 connector->base.dpms = DRM_MODE_DPMS_OFF;
14608 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014609 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014610 /* multiple connectors may have the same encoder:
14611 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014612 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020014613 if (connector->encoder->base.crtc == &crtc->base) {
14614 connector->encoder->base.crtc = NULL;
14615 connector->encoder->connectors_active = false;
14616 }
Daniel Vetter24929352012-07-02 20:28:59 +020014617
14618 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080014619 crtc->base.state->enable = false;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014620 crtc->base.state->active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014621 crtc->base.enabled = false;
14622 }
Daniel Vetter24929352012-07-02 20:28:59 +020014623
Daniel Vetter7fad7982012-07-04 17:51:47 +020014624 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14625 crtc->pipe == PIPE_A && !crtc->active) {
14626 /* BIOS forgot to enable pipe A, this mostly happens after
14627 * resume. Force-enable the pipe to fix this, the update_dpms
14628 * call below we restore the pipe to the right state, but leave
14629 * the required bits on. */
14630 intel_enable_pipe_a(dev);
14631 }
14632
Daniel Vetter24929352012-07-02 20:28:59 +020014633 /* Adjust the state of the output pipe according to whether we
14634 * have active connectors/encoders. */
14635 intel_crtc_update_dpms(&crtc->base);
14636
Matt Roper83d65732015-02-25 13:12:16 -080014637 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020014638 struct intel_encoder *encoder;
14639
14640 /* This can happen either due to bugs in the get_hw_state
14641 * functions or because the pipe is force-enabled due to the
14642 * pipe A quirk. */
14643 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14644 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014645 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014646 crtc->active ? "enabled" : "disabled");
14647
Matt Roper83d65732015-02-25 13:12:16 -080014648 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014649 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014650 crtc->base.enabled = crtc->active;
14651
14652 /* Because we only establish the connector -> encoder ->
14653 * crtc links if something is active, this means the
14654 * crtc is now deactivated. Break the links. connector
14655 * -> encoder links are only establish when things are
14656 * actually up, hence no need to break them. */
14657 WARN_ON(crtc->active);
14658
14659 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14660 WARN_ON(encoder->connectors_active);
14661 encoder->base.crtc = NULL;
14662 }
14663 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014664
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014665 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014666 /*
14667 * We start out with underrun reporting disabled to avoid races.
14668 * For correct bookkeeping mark this on active crtcs.
14669 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014670 * Also on gmch platforms we dont have any hardware bits to
14671 * disable the underrun reporting. Which means we need to start
14672 * out with underrun reporting disabled also on inactive pipes,
14673 * since otherwise we'll complain about the garbage we read when
14674 * e.g. coming up after runtime pm.
14675 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014676 * No protection against concurrent access is required - at
14677 * worst a fifo underrun happens which also sets this to false.
14678 */
14679 crtc->cpu_fifo_underrun_disabled = true;
14680 crtc->pch_fifo_underrun_disabled = true;
14681 }
Daniel Vetter24929352012-07-02 20:28:59 +020014682}
14683
14684static void intel_sanitize_encoder(struct intel_encoder *encoder)
14685{
14686 struct intel_connector *connector;
14687 struct drm_device *dev = encoder->base.dev;
14688
14689 /* We need to check both for a crtc link (meaning that the
14690 * encoder is active and trying to read from a pipe) and the
14691 * pipe itself being active. */
14692 bool has_active_crtc = encoder->base.crtc &&
14693 to_intel_crtc(encoder->base.crtc)->active;
14694
14695 if (encoder->connectors_active && !has_active_crtc) {
14696 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14697 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014698 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014699
14700 /* Connector is active, but has no active pipe. This is
14701 * fallout from our resume register restoring. Disable
14702 * the encoder manually again. */
14703 if (encoder->base.crtc) {
14704 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14705 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014706 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014707 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014708 if (encoder->post_disable)
14709 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014710 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014711 encoder->base.crtc = NULL;
14712 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014713
14714 /* Inconsistent output/port/pipe state happens presumably due to
14715 * a bug in one of the get_hw_state functions. Or someplace else
14716 * in our code, like the register restore mess on resume. Clamp
14717 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014718 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014719 if (connector->encoder != encoder)
14720 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014721 connector->base.dpms = DRM_MODE_DPMS_OFF;
14722 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014723 }
14724 }
14725 /* Enabled encoders without active connectors will be fixed in
14726 * the crtc fixup. */
14727}
14728
Imre Deak04098752014-02-18 00:02:16 +020014729void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014730{
14731 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014732 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014733
Imre Deak04098752014-02-18 00:02:16 +020014734 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14735 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14736 i915_disable_vga(dev);
14737 }
14738}
14739
14740void i915_redisable_vga(struct drm_device *dev)
14741{
14742 struct drm_i915_private *dev_priv = dev->dev_private;
14743
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014744 /* This function can be called both from intel_modeset_setup_hw_state or
14745 * at a very early point in our resume sequence, where the power well
14746 * structures are not yet restored. Since this function is at a very
14747 * paranoid "someone might have enabled VGA while we were not looking"
14748 * level, just check if the power well is enabled instead of trying to
14749 * follow the "don't touch the power well if we don't need it" policy
14750 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014751 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014752 return;
14753
Imre Deak04098752014-02-18 00:02:16 +020014754 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014755}
14756
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014757static bool primary_get_hw_state(struct intel_crtc *crtc)
14758{
14759 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14760
14761 if (!crtc->active)
14762 return false;
14763
14764 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
14765}
14766
Daniel Vetter30e984d2013-06-05 13:34:17 +020014767static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020014768{
14769 struct drm_i915_private *dev_priv = dev->dev_private;
14770 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020014771 struct intel_crtc *crtc;
14772 struct intel_encoder *encoder;
14773 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020014774 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020014775
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014776 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014777 struct drm_plane *primary = crtc->base.primary;
14778 struct intel_plane_state *plane_state;
14779
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014780 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020014781
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014782 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020014783
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014784 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014785 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014786
Matt Roper83d65732015-02-25 13:12:16 -080014787 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014788 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014789 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014790
14791 plane_state = to_intel_plane_state(primary->state);
14792 plane_state->visible = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020014793
14794 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
14795 crtc->base.base.id,
14796 crtc->active ? "enabled" : "disabled");
14797 }
14798
Daniel Vetter53589012013-06-05 13:34:16 +020014799 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14800 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14801
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014802 pll->on = pll->get_hw_state(dev_priv, pll,
14803 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020014804 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014805 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014806 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014807 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020014808 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014809 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014810 }
Daniel Vetter53589012013-06-05 13:34:16 +020014811 }
Daniel Vetter53589012013-06-05 13:34:16 +020014812
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014813 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014814 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014815
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014816 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014817 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020014818 }
14819
Damien Lespiaub2784e12014-08-05 11:29:37 +010014820 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014821 pipe = 0;
14822
14823 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070014824 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14825 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014826 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014827 } else {
14828 encoder->base.crtc = NULL;
14829 }
14830
14831 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014832 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020014833 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014834 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014835 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014836 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020014837 }
14838
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014839 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014840 if (connector->get_hw_state(connector)) {
14841 connector->base.dpms = DRM_MODE_DPMS_ON;
14842 connector->encoder->connectors_active = true;
14843 connector->base.encoder = &connector->encoder->base;
14844 } else {
14845 connector->base.dpms = DRM_MODE_DPMS_OFF;
14846 connector->base.encoder = NULL;
14847 }
14848 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
14849 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030014850 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014851 connector->base.encoder ? "enabled" : "disabled");
14852 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020014853}
14854
14855/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
14856 * and i915 state tracking structures. */
14857void intel_modeset_setup_hw_state(struct drm_device *dev,
14858 bool force_restore)
14859{
14860 struct drm_i915_private *dev_priv = dev->dev_private;
14861 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014862 struct intel_crtc *crtc;
14863 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020014864 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014865
14866 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020014867
Jesse Barnesbabea612013-06-26 18:57:38 +030014868 /*
14869 * Now that we have the config, copy it to each CRTC struct
14870 * Note that this could go away if we move to using crtc_config
14871 * checking everywhere.
14872 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014873 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020014874 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014875 intel_mode_from_pipe_config(&crtc->base.mode,
14876 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030014877 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
14878 crtc->base.base.id);
14879 drm_mode_debug_printmodeline(&crtc->base.mode);
14880 }
14881 }
14882
Daniel Vetter24929352012-07-02 20:28:59 +020014883 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010014884 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014885 intel_sanitize_encoder(encoder);
14886 }
14887
Damien Lespiau055e3932014-08-18 13:49:10 +010014888 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020014889 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14890 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014891 intel_dump_pipe_config(crtc, crtc->config,
14892 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020014893 }
Daniel Vetter9a935852012-07-05 22:34:27 +020014894
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020014895 intel_modeset_update_connector_atomic_state(dev);
14896
Daniel Vetter35c95372013-07-17 06:55:04 +020014897 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14898 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14899
14900 if (!pll->on || pll->active)
14901 continue;
14902
14903 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
14904
14905 pll->disable(dev_priv, pll);
14906 pll->on = false;
14907 }
14908
Pradeep Bhat30789992014-11-04 17:06:45 +000014909 if (IS_GEN9(dev))
14910 skl_wm_get_hw_state(dev);
14911 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030014912 ilk_wm_get_hw_state(dev);
14913
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010014914 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030014915 i915_redisable_vga(dev);
14916
Daniel Vetterf30da182013-04-11 20:22:50 +020014917 /*
14918 * We need to use raw interfaces for restoring state to avoid
14919 * checking (bogus) intermediate states.
14920 */
Damien Lespiau055e3932014-08-18 13:49:10 +010014921 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070014922 struct drm_crtc *crtc =
14923 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020014924
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014925 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010014926 }
14927 } else {
14928 intel_modeset_update_staged_output_state(dev);
14929 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020014930
14931 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010014932}
14933
14934void intel_modeset_gem_init(struct drm_device *dev)
14935{
Jesse Barnes92122782014-10-09 12:57:42 -070014936 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080014937 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070014938 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010014939 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080014940
Imre Deakae484342014-03-31 15:10:44 +030014941 mutex_lock(&dev->struct_mutex);
14942 intel_init_gt_powersave(dev);
14943 mutex_unlock(&dev->struct_mutex);
14944
Jesse Barnes92122782014-10-09 12:57:42 -070014945 /*
14946 * There may be no VBT; and if the BIOS enabled SSC we can
14947 * just keep using it to avoid unnecessary flicker. Whereas if the
14948 * BIOS isn't using it, don't assume it will work even if the VBT
14949 * indicates as much.
14950 */
14951 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
14952 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14953 DREF_SSC1_ENABLE);
14954
Chris Wilson1833b132012-05-09 11:56:28 +010014955 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020014956
14957 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080014958
14959 /*
14960 * Make sure any fbs we allocated at startup are properly
14961 * pinned & fenced. When we do the allocation it's too early
14962 * for this.
14963 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010014964 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070014965 obj = intel_fb_obj(c->primary->fb);
14966 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080014967 continue;
14968
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010014969 mutex_lock(&dev->struct_mutex);
14970 ret = intel_pin_and_fence_fb_obj(c->primary,
14971 c->primary->fb,
14972 c->primary->state,
14973 NULL);
14974 mutex_unlock(&dev->struct_mutex);
14975 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080014976 DRM_ERROR("failed to pin boot fb on pipe %d\n",
14977 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100014978 drm_framebuffer_unreference(c->primary->fb);
14979 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080014980 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080014981 }
14982 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020014983
14984 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014985}
14986
Imre Deak4932e2c2014-02-11 17:12:48 +020014987void intel_connector_unregister(struct intel_connector *intel_connector)
14988{
14989 struct drm_connector *connector = &intel_connector->base;
14990
14991 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010014992 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020014993}
14994
Jesse Barnes79e53942008-11-07 14:24:08 -080014995void intel_modeset_cleanup(struct drm_device *dev)
14996{
Jesse Barnes652c3932009-08-17 13:31:43 -070014997 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030014998 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070014999
Imre Deak2eb52522014-11-19 15:30:05 +020015000 intel_disable_gt_powersave(dev);
15001
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015002 intel_backlight_unregister(dev);
15003
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015004 /*
15005 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015006 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015007 * experience fancy races otherwise.
15008 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015009 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015010
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015011 /*
15012 * Due to the hpd irq storm handling the hotplug work can re-arm the
15013 * poll handlers. Hence disable polling after hpd handling is shut down.
15014 */
Keith Packardf87ea762010-10-03 19:36:26 -070015015 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015016
Jesse Barnes652c3932009-08-17 13:31:43 -070015017 mutex_lock(&dev->struct_mutex);
15018
Jesse Barnes723bfd72010-10-07 16:01:13 -070015019 intel_unregister_dsm_handler();
15020
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015021 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015022
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015023 mutex_unlock(&dev->struct_mutex);
15024
Chris Wilson1630fe72011-07-08 12:22:42 +010015025 /* flush any delayed tasks or pending work */
15026 flush_scheduled_work();
15027
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015028 /* destroy the backlight and sysfs files before encoders/connectors */
15029 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015030 struct intel_connector *intel_connector;
15031
15032 intel_connector = to_intel_connector(connector);
15033 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015034 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015035
Jesse Barnes79e53942008-11-07 14:24:08 -080015036 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015037
15038 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015039
15040 mutex_lock(&dev->struct_mutex);
15041 intel_cleanup_gt_powersave(dev);
15042 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015043}
15044
Dave Airlie28d52042009-09-21 14:33:58 +100015045/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015046 * Return which encoder is currently attached for connector.
15047 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015048struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015049{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015050 return &intel_attached_encoder(connector)->base;
15051}
Jesse Barnes79e53942008-11-07 14:24:08 -080015052
Chris Wilsondf0e9242010-09-09 16:20:55 +010015053void intel_connector_attach_encoder(struct intel_connector *connector,
15054 struct intel_encoder *encoder)
15055{
15056 connector->encoder = encoder;
15057 drm_mode_connector_attach_encoder(&connector->base,
15058 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015059}
Dave Airlie28d52042009-09-21 14:33:58 +100015060
15061/*
15062 * set vga decode state - true == enable VGA decode
15063 */
15064int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15065{
15066 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015067 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015068 u16 gmch_ctrl;
15069
Chris Wilson75fa0412014-02-07 18:37:02 -020015070 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15071 DRM_ERROR("failed to read control word\n");
15072 return -EIO;
15073 }
15074
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015075 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15076 return 0;
15077
Dave Airlie28d52042009-09-21 14:33:58 +100015078 if (state)
15079 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15080 else
15081 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015082
15083 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15084 DRM_ERROR("failed to write control word\n");
15085 return -EIO;
15086 }
15087
Dave Airlie28d52042009-09-21 14:33:58 +100015088 return 0;
15089}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015090
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015091struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015092
15093 u32 power_well_driver;
15094
Chris Wilson63b66e52013-08-08 15:12:06 +020015095 int num_transcoders;
15096
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015097 struct intel_cursor_error_state {
15098 u32 control;
15099 u32 position;
15100 u32 base;
15101 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015102 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015103
15104 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015105 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015106 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015107 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015108 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015109
15110 struct intel_plane_error_state {
15111 u32 control;
15112 u32 stride;
15113 u32 size;
15114 u32 pos;
15115 u32 addr;
15116 u32 surface;
15117 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015118 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015119
15120 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015121 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015122 enum transcoder cpu_transcoder;
15123
15124 u32 conf;
15125
15126 u32 htotal;
15127 u32 hblank;
15128 u32 hsync;
15129 u32 vtotal;
15130 u32 vblank;
15131 u32 vsync;
15132 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015133};
15134
15135struct intel_display_error_state *
15136intel_display_capture_error_state(struct drm_device *dev)
15137{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015138 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015139 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015140 int transcoders[] = {
15141 TRANSCODER_A,
15142 TRANSCODER_B,
15143 TRANSCODER_C,
15144 TRANSCODER_EDP,
15145 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015146 int i;
15147
Chris Wilson63b66e52013-08-08 15:12:06 +020015148 if (INTEL_INFO(dev)->num_pipes == 0)
15149 return NULL;
15150
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015151 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015152 if (error == NULL)
15153 return NULL;
15154
Imre Deak190be112013-11-25 17:15:31 +020015155 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015156 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15157
Damien Lespiau055e3932014-08-18 13:49:10 +010015158 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015159 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015160 __intel_display_power_is_enabled(dev_priv,
15161 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015162 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015163 continue;
15164
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015165 error->cursor[i].control = I915_READ(CURCNTR(i));
15166 error->cursor[i].position = I915_READ(CURPOS(i));
15167 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015168
15169 error->plane[i].control = I915_READ(DSPCNTR(i));
15170 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015171 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015172 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015173 error->plane[i].pos = I915_READ(DSPPOS(i));
15174 }
Paulo Zanonica291362013-03-06 20:03:14 -030015175 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15176 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015177 if (INTEL_INFO(dev)->gen >= 4) {
15178 error->plane[i].surface = I915_READ(DSPSURF(i));
15179 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15180 }
15181
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015182 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015183
Sonika Jindal3abfce72014-07-21 15:23:43 +053015184 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015185 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015186 }
15187
15188 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15189 if (HAS_DDI(dev_priv->dev))
15190 error->num_transcoders++; /* Account for eDP. */
15191
15192 for (i = 0; i < error->num_transcoders; i++) {
15193 enum transcoder cpu_transcoder = transcoders[i];
15194
Imre Deakddf9c532013-11-27 22:02:02 +020015195 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015196 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015197 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015198 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015199 continue;
15200
Chris Wilson63b66e52013-08-08 15:12:06 +020015201 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15202
15203 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15204 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15205 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15206 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15207 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15208 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15209 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015210 }
15211
15212 return error;
15213}
15214
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015215#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15216
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015217void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015218intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015219 struct drm_device *dev,
15220 struct intel_display_error_state *error)
15221{
Damien Lespiau055e3932014-08-18 13:49:10 +010015222 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015223 int i;
15224
Chris Wilson63b66e52013-08-08 15:12:06 +020015225 if (!error)
15226 return;
15227
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015228 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015229 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015230 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015231 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015232 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015233 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015234 err_printf(m, " Power: %s\n",
15235 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015236 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015237 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015238
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015239 err_printf(m, "Plane [%d]:\n", i);
15240 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15241 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015242 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015243 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15244 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015245 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015246 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015247 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015248 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015249 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15250 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015251 }
15252
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015253 err_printf(m, "Cursor [%d]:\n", i);
15254 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15255 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15256 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015257 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015258
15259 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015260 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015261 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015262 err_printf(m, " Power: %s\n",
15263 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015264 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15265 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15266 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15267 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15268 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15269 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15270 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15271 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015272}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015273
15274void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15275{
15276 struct intel_crtc *crtc;
15277
15278 for_each_intel_crtc(dev, crtc) {
15279 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015280
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015281 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015282
15283 work = crtc->unpin_work;
15284
15285 if (work && work->event &&
15286 work->event->base.file_priv == file) {
15287 kfree(work->event);
15288 work->event = NULL;
15289 }
15290
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015291 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015292 }
15293}