blob: 3b9ce89d2a98ae80d0d7908654545c313185a0b4 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080040#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080041#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010042#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070044#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080046#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080047
Matt Roper465c1202014-05-29 08:06:54 -070048/* Primary plane formats supported by all gen */
49#define COMMON_PRIMARY_FORMATS \
50 DRM_FORMAT_C8, \
51 DRM_FORMAT_RGB565, \
52 DRM_FORMAT_XRGB8888, \
53 DRM_FORMAT_ARGB8888
54
55/* Primary plane formats for gen <= 3 */
56static const uint32_t intel_primary_formats_gen2[] = {
57 COMMON_PRIMARY_FORMATS,
58 DRM_FORMAT_XRGB1555,
59 DRM_FORMAT_ARGB1555,
60};
61
62/* Primary plane formats for gen >= 4 */
63static const uint32_t intel_primary_formats_gen4[] = {
64 COMMON_PRIMARY_FORMATS, \
65 DRM_FORMAT_XBGR8888,
66 DRM_FORMAT_ABGR8888,
67 DRM_FORMAT_XRGB2101010,
68 DRM_FORMAT_ARGB2101010,
69 DRM_FORMAT_XBGR2101010,
70 DRM_FORMAT_ABGR2101010,
71};
72
Matt Roper3d7d6512014-06-10 08:28:13 -070073/* Cursor formats */
74static const uint32_t intel_cursor_formats[] = {
75 DRM_FORMAT_ARGB8888,
76};
77
Chris Wilson6b383a72010-09-13 13:54:26 +010078static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080079
Jesse Barnesf1f644d2013-06-27 00:39:25 +030080static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020081 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030082static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020083 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030084
Damien Lespiaue7457a92013-08-08 22:28:59 +010085static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
86 int x, int y, struct drm_framebuffer *old_fb);
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);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100105
Dave Airlie0e32b392014-05-02 14:02:48 +1000106static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
107{
108 if (!connector->mst_port)
109 return connector->encoder;
110 else
111 return &connector->mst_port->mst_encoders[pipe]->base;
112}
113
Jesse Barnes79e53942008-11-07 14:24:08 -0800114typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400115 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800116} intel_range_t;
117
118typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400119 int dot_limit;
120 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800121} intel_p2_t;
122
Ma Lingd4906092009-03-18 20:13:27 +0800123typedef struct intel_limit intel_limit_t;
124struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400125 intel_range_t dot, vco, n, m, m1, m2, p, p1;
126 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800127};
Jesse Barnes79e53942008-11-07 14:24:08 -0800128
Daniel Vetterd2acd212012-10-20 20:57:43 +0200129int
130intel_pch_rawclk(struct drm_device *dev)
131{
132 struct drm_i915_private *dev_priv = dev->dev_private;
133
134 WARN_ON(!HAS_PCH_SPLIT(dev));
135
136 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
137}
138
Chris Wilson021357a2010-09-07 20:54:59 +0100139static inline u32 /* units of 100MHz */
140intel_fdi_link_freq(struct drm_device *dev)
141{
Chris Wilson8b99e682010-10-13 09:59:17 +0100142 if (IS_GEN5(dev)) {
143 struct drm_i915_private *dev_priv = dev->dev_private;
144 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
145 } else
146 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100147}
148
Daniel Vetter5d536e22013-07-06 12:52:06 +0200149static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400150 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200151 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200152 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400153 .m = { .min = 96, .max = 140 },
154 .m1 = { .min = 18, .max = 26 },
155 .m2 = { .min = 6, .max = 16 },
156 .p = { .min = 4, .max = 128 },
157 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700158 .p2 = { .dot_limit = 165000,
159 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700160};
161
Daniel Vetter5d536e22013-07-06 12:52:06 +0200162static const intel_limit_t intel_limits_i8xx_dvo = {
163 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200164 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200165 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200166 .m = { .min = 96, .max = 140 },
167 .m1 = { .min = 18, .max = 26 },
168 .m2 = { .min = 6, .max = 16 },
169 .p = { .min = 4, .max = 128 },
170 .p1 = { .min = 2, .max = 33 },
171 .p2 = { .dot_limit = 165000,
172 .p2_slow = 4, .p2_fast = 4 },
173};
174
Keith Packarde4b36692009-06-05 19:22:17 -0700175static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400176 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200177 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200178 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400179 .m = { .min = 96, .max = 140 },
180 .m1 = { .min = 18, .max = 26 },
181 .m2 = { .min = 6, .max = 16 },
182 .p = { .min = 4, .max = 128 },
183 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700184 .p2 = { .dot_limit = 165000,
185 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700186};
Eric Anholt273e27c2011-03-30 13:01:10 -0700187
Keith Packarde4b36692009-06-05 19:22:17 -0700188static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400189 .dot = { .min = 20000, .max = 400000 },
190 .vco = { .min = 1400000, .max = 2800000 },
191 .n = { .min = 1, .max = 6 },
192 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100193 .m1 = { .min = 8, .max = 18 },
194 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400195 .p = { .min = 5, .max = 80 },
196 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700197 .p2 = { .dot_limit = 200000,
198 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700199};
200
201static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400202 .dot = { .min = 20000, .max = 400000 },
203 .vco = { .min = 1400000, .max = 2800000 },
204 .n = { .min = 1, .max = 6 },
205 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100206 .m1 = { .min = 8, .max = 18 },
207 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400208 .p = { .min = 7, .max = 98 },
209 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700210 .p2 = { .dot_limit = 112000,
211 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700212};
213
Eric Anholt273e27c2011-03-30 13:01:10 -0700214
Keith Packarde4b36692009-06-05 19:22:17 -0700215static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700216 .dot = { .min = 25000, .max = 270000 },
217 .vco = { .min = 1750000, .max = 3500000},
218 .n = { .min = 1, .max = 4 },
219 .m = { .min = 104, .max = 138 },
220 .m1 = { .min = 17, .max = 23 },
221 .m2 = { .min = 5, .max = 11 },
222 .p = { .min = 10, .max = 30 },
223 .p1 = { .min = 1, .max = 3},
224 .p2 = { .dot_limit = 270000,
225 .p2_slow = 10,
226 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800227 },
Keith Packarde4b36692009-06-05 19:22:17 -0700228};
229
230static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700231 .dot = { .min = 22000, .max = 400000 },
232 .vco = { .min = 1750000, .max = 3500000},
233 .n = { .min = 1, .max = 4 },
234 .m = { .min = 104, .max = 138 },
235 .m1 = { .min = 16, .max = 23 },
236 .m2 = { .min = 5, .max = 11 },
237 .p = { .min = 5, .max = 80 },
238 .p1 = { .min = 1, .max = 8},
239 .p2 = { .dot_limit = 165000,
240 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700241};
242
243static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700244 .dot = { .min = 20000, .max = 115000 },
245 .vco = { .min = 1750000, .max = 3500000 },
246 .n = { .min = 1, .max = 3 },
247 .m = { .min = 104, .max = 138 },
248 .m1 = { .min = 17, .max = 23 },
249 .m2 = { .min = 5, .max = 11 },
250 .p = { .min = 28, .max = 112 },
251 .p1 = { .min = 2, .max = 8 },
252 .p2 = { .dot_limit = 0,
253 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800254 },
Keith Packarde4b36692009-06-05 19:22:17 -0700255};
256
257static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700258 .dot = { .min = 80000, .max = 224000 },
259 .vco = { .min = 1750000, .max = 3500000 },
260 .n = { .min = 1, .max = 3 },
261 .m = { .min = 104, .max = 138 },
262 .m1 = { .min = 17, .max = 23 },
263 .m2 = { .min = 5, .max = 11 },
264 .p = { .min = 14, .max = 42 },
265 .p1 = { .min = 2, .max = 6 },
266 .p2 = { .dot_limit = 0,
267 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800268 },
Keith Packarde4b36692009-06-05 19:22:17 -0700269};
270
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500271static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400272 .dot = { .min = 20000, .max = 400000},
273 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700274 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400275 .n = { .min = 3, .max = 6 },
276 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700277 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400278 .m1 = { .min = 0, .max = 0 },
279 .m2 = { .min = 0, .max = 254 },
280 .p = { .min = 5, .max = 80 },
281 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700282 .p2 = { .dot_limit = 200000,
283 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700284};
285
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500286static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400287 .dot = { .min = 20000, .max = 400000 },
288 .vco = { .min = 1700000, .max = 3500000 },
289 .n = { .min = 3, .max = 6 },
290 .m = { .min = 2, .max = 256 },
291 .m1 = { .min = 0, .max = 0 },
292 .m2 = { .min = 0, .max = 254 },
293 .p = { .min = 7, .max = 112 },
294 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700295 .p2 = { .dot_limit = 112000,
296 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700297};
298
Eric Anholt273e27c2011-03-30 13:01:10 -0700299/* Ironlake / Sandybridge
300 *
301 * We calculate clock using (register_value + 2) for N/M1/M2, so here
302 * the range value for them is (actual_value - 2).
303 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800304static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700305 .dot = { .min = 25000, .max = 350000 },
306 .vco = { .min = 1760000, .max = 3510000 },
307 .n = { .min = 1, .max = 5 },
308 .m = { .min = 79, .max = 127 },
309 .m1 = { .min = 12, .max = 22 },
310 .m2 = { .min = 5, .max = 9 },
311 .p = { .min = 5, .max = 80 },
312 .p1 = { .min = 1, .max = 8 },
313 .p2 = { .dot_limit = 225000,
314 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700315};
316
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800317static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700318 .dot = { .min = 25000, .max = 350000 },
319 .vco = { .min = 1760000, .max = 3510000 },
320 .n = { .min = 1, .max = 3 },
321 .m = { .min = 79, .max = 118 },
322 .m1 = { .min = 12, .max = 22 },
323 .m2 = { .min = 5, .max = 9 },
324 .p = { .min = 28, .max = 112 },
325 .p1 = { .min = 2, .max = 8 },
326 .p2 = { .dot_limit = 225000,
327 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800328};
329
330static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700331 .dot = { .min = 25000, .max = 350000 },
332 .vco = { .min = 1760000, .max = 3510000 },
333 .n = { .min = 1, .max = 3 },
334 .m = { .min = 79, .max = 127 },
335 .m1 = { .min = 12, .max = 22 },
336 .m2 = { .min = 5, .max = 9 },
337 .p = { .min = 14, .max = 56 },
338 .p1 = { .min = 2, .max = 8 },
339 .p2 = { .dot_limit = 225000,
340 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800341};
342
Eric Anholt273e27c2011-03-30 13:01:10 -0700343/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800344static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700345 .dot = { .min = 25000, .max = 350000 },
346 .vco = { .min = 1760000, .max = 3510000 },
347 .n = { .min = 1, .max = 2 },
348 .m = { .min = 79, .max = 126 },
349 .m1 = { .min = 12, .max = 22 },
350 .m2 = { .min = 5, .max = 9 },
351 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400352 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700353 .p2 = { .dot_limit = 225000,
354 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800355};
356
357static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700358 .dot = { .min = 25000, .max = 350000 },
359 .vco = { .min = 1760000, .max = 3510000 },
360 .n = { .min = 1, .max = 3 },
361 .m = { .min = 79, .max = 126 },
362 .m1 = { .min = 12, .max = 22 },
363 .m2 = { .min = 5, .max = 9 },
364 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400365 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700366 .p2 = { .dot_limit = 225000,
367 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800368};
369
Ville Syrjälädc730512013-09-24 21:26:30 +0300370static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300371 /*
372 * These are the data rate limits (measured in fast clocks)
373 * since those are the strictest limits we have. The fast
374 * clock and actual rate limits are more relaxed, so checking
375 * them would make no difference.
376 */
377 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200378 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700379 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700380 .m1 = { .min = 2, .max = 3 },
381 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300382 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300383 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700384};
385
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300386static const intel_limit_t intel_limits_chv = {
387 /*
388 * These are the data rate limits (measured in fast clocks)
389 * since those are the strictest limits we have. The fast
390 * clock and actual rate limits are more relaxed, so checking
391 * them would make no difference.
392 */
393 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200394 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300395 .n = { .min = 1, .max = 1 },
396 .m1 = { .min = 2, .max = 2 },
397 .m2 = { .min = 24 << 22, .max = 175 << 22 },
398 .p1 = { .min = 2, .max = 4 },
399 .p2 = { .p2_slow = 1, .p2_fast = 14 },
400};
401
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300402static void vlv_clock(int refclk, intel_clock_t *clock)
403{
404 clock->m = clock->m1 * clock->m2;
405 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200406 if (WARN_ON(clock->n == 0 || clock->p == 0))
407 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300408 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
409 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300410}
411
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300412/**
413 * Returns whether any output on the specified pipe is of the specified type
414 */
Damien Lespiau40935612014-10-29 11:16:59 +0000415bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300416{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300417 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300418 struct intel_encoder *encoder;
419
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300420 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300421 if (encoder->type == type)
422 return true;
423
424 return false;
425}
426
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200427/**
428 * Returns whether any output on the specified pipe will have the specified
429 * type after a staged modeset is complete, i.e., the same as
430 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
431 * encoder->crtc.
432 */
433static bool intel_pipe_will_have_type(struct intel_crtc *crtc, int type)
434{
435 struct drm_device *dev = crtc->base.dev;
436 struct intel_encoder *encoder;
437
438 for_each_intel_encoder(dev, encoder)
439 if (encoder->new_crtc == crtc && encoder->type == type)
440 return true;
441
442 return false;
443}
444
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300445static const intel_limit_t *intel_ironlake_limit(struct intel_crtc *crtc,
Chris Wilson1b894b52010-12-14 20:04:54 +0000446 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800447{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300448 struct drm_device *dev = crtc->base.dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800449 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800450
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200451 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100452 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000453 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800454 limit = &intel_limits_ironlake_dual_lvds_100m;
455 else
456 limit = &intel_limits_ironlake_dual_lvds;
457 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000458 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800459 limit = &intel_limits_ironlake_single_lvds_100m;
460 else
461 limit = &intel_limits_ironlake_single_lvds;
462 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200463 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800464 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800465
466 return limit;
467}
468
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300469static const intel_limit_t *intel_g4x_limit(struct intel_crtc *crtc)
Ma Ling044c7c42009-03-18 20:13:23 +0800470{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300471 struct drm_device *dev = crtc->base.dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800472 const intel_limit_t *limit;
473
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200474 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100475 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700476 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800477 else
Keith Packarde4b36692009-06-05 19:22:17 -0700478 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200479 } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI) ||
480 intel_pipe_will_have_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700481 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200482 } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700483 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800484 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700485 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800486
487 return limit;
488}
489
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300490static const intel_limit_t *intel_limit(struct intel_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800491{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300492 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800493 const intel_limit_t *limit;
494
Eric Anholtbad720f2009-10-22 16:11:14 -0700495 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000496 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800497 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800498 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500499 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200500 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500501 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800502 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500503 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300504 } else if (IS_CHERRYVIEW(dev)) {
505 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700506 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300507 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100508 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200509 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100510 limit = &intel_limits_i9xx_lvds;
511 else
512 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800513 } else {
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200514 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700515 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200516 else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700517 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200518 else
519 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800520 }
521 return limit;
522}
523
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500524/* m1 is reserved as 0 in Pineview, n is a ring counter */
525static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800526{
Shaohua Li21778322009-02-23 15:19:16 +0800527 clock->m = clock->m2 + 2;
528 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200529 if (WARN_ON(clock->n == 0 || clock->p == 0))
530 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300531 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
532 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800533}
534
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200535static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
536{
537 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
538}
539
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200540static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800541{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200542 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800543 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200544 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
545 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300546 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
547 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800548}
549
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300550static void chv_clock(int refclk, intel_clock_t *clock)
551{
552 clock->m = clock->m1 * clock->m2;
553 clock->p = clock->p1 * clock->p2;
554 if (WARN_ON(clock->n == 0 || clock->p == 0))
555 return;
556 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
557 clock->n << 22);
558 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
559}
560
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800561#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800562/**
563 * Returns whether the given set of divisors are valid for a given refclk with
564 * the given connectors.
565 */
566
Chris Wilson1b894b52010-12-14 20:04:54 +0000567static bool intel_PLL_is_valid(struct drm_device *dev,
568 const intel_limit_t *limit,
569 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800570{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300571 if (clock->n < limit->n.min || limit->n.max < clock->n)
572 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800573 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400574 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800575 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400576 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800577 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400578 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300579
580 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
581 if (clock->m1 <= clock->m2)
582 INTELPllInvalid("m1 <= m2\n");
583
584 if (!IS_VALLEYVIEW(dev)) {
585 if (clock->p < limit->p.min || limit->p.max < clock->p)
586 INTELPllInvalid("p out of range\n");
587 if (clock->m < limit->m.min || limit->m.max < clock->m)
588 INTELPllInvalid("m out of range\n");
589 }
590
Jesse Barnes79e53942008-11-07 14:24:08 -0800591 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400592 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800593 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
594 * connector, etc., rather than just a single range.
595 */
596 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400597 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800598
599 return true;
600}
601
Ma Lingd4906092009-03-18 20:13:27 +0800602static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300603i9xx_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800604 int target, int refclk, intel_clock_t *match_clock,
605 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800606{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300607 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800608 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800609 int err = target;
610
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200611 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100613 * For LVDS just rely on its current settings for dual-channel.
614 * We haven't figured out how to reliably set up different
615 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800616 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100617 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800618 clock.p2 = limit->p2.p2_fast;
619 else
620 clock.p2 = limit->p2.p2_slow;
621 } else {
622 if (target < limit->p2.dot_limit)
623 clock.p2 = limit->p2.p2_slow;
624 else
625 clock.p2 = limit->p2.p2_fast;
626 }
627
Akshay Joshi0206e352011-08-16 15:34:10 -0400628 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800629
Zhao Yakui42158662009-11-20 11:24:18 +0800630 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
631 clock.m1++) {
632 for (clock.m2 = limit->m2.min;
633 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200634 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800635 break;
636 for (clock.n = limit->n.min;
637 clock.n <= limit->n.max; clock.n++) {
638 for (clock.p1 = limit->p1.min;
639 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800640 int this_err;
641
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200642 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000643 if (!intel_PLL_is_valid(dev, limit,
644 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800645 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800646 if (match_clock &&
647 clock.p != match_clock->p)
648 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800649
650 this_err = abs(clock.dot - target);
651 if (this_err < err) {
652 *best_clock = clock;
653 err = this_err;
654 }
655 }
656 }
657 }
658 }
659
660 return (err != target);
661}
662
Ma Lingd4906092009-03-18 20:13:27 +0800663static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300664pnv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200665 int target, int refclk, intel_clock_t *match_clock,
666 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200667{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300668 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200669 intel_clock_t clock;
670 int err = target;
671
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200672 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200673 /*
674 * For LVDS just rely on its current settings for dual-channel.
675 * We haven't figured out how to reliably set up different
676 * single/dual channel state, if we even can.
677 */
678 if (intel_is_dual_link_lvds(dev))
679 clock.p2 = limit->p2.p2_fast;
680 else
681 clock.p2 = limit->p2.p2_slow;
682 } else {
683 if (target < limit->p2.dot_limit)
684 clock.p2 = limit->p2.p2_slow;
685 else
686 clock.p2 = limit->p2.p2_fast;
687 }
688
689 memset(best_clock, 0, sizeof(*best_clock));
690
691 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
692 clock.m1++) {
693 for (clock.m2 = limit->m2.min;
694 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200695 for (clock.n = limit->n.min;
696 clock.n <= limit->n.max; clock.n++) {
697 for (clock.p1 = limit->p1.min;
698 clock.p1 <= limit->p1.max; clock.p1++) {
699 int this_err;
700
701 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800702 if (!intel_PLL_is_valid(dev, limit,
703 &clock))
704 continue;
705 if (match_clock &&
706 clock.p != match_clock->p)
707 continue;
708
709 this_err = abs(clock.dot - target);
710 if (this_err < err) {
711 *best_clock = clock;
712 err = this_err;
713 }
714 }
715 }
716 }
717 }
718
719 return (err != target);
720}
721
Ma Lingd4906092009-03-18 20:13:27 +0800722static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300723g4x_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200724 int target, int refclk, intel_clock_t *match_clock,
725 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800726{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300727 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800728 intel_clock_t clock;
729 int max_n;
730 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400731 /* approximately equals target * 0.00585 */
732 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800733 found = false;
734
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200735 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100736 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800737 clock.p2 = limit->p2.p2_fast;
738 else
739 clock.p2 = limit->p2.p2_slow;
740 } else {
741 if (target < limit->p2.dot_limit)
742 clock.p2 = limit->p2.p2_slow;
743 else
744 clock.p2 = limit->p2.p2_fast;
745 }
746
747 memset(best_clock, 0, sizeof(*best_clock));
748 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200749 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800750 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200751 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800752 for (clock.m1 = limit->m1.max;
753 clock.m1 >= limit->m1.min; clock.m1--) {
754 for (clock.m2 = limit->m2.max;
755 clock.m2 >= limit->m2.min; clock.m2--) {
756 for (clock.p1 = limit->p1.max;
757 clock.p1 >= limit->p1.min; clock.p1--) {
758 int this_err;
759
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200760 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000761 if (!intel_PLL_is_valid(dev, limit,
762 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800763 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000764
765 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800766 if (this_err < err_most) {
767 *best_clock = clock;
768 err_most = this_err;
769 max_n = clock.n;
770 found = true;
771 }
772 }
773 }
774 }
775 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800776 return found;
777}
Ma Lingd4906092009-03-18 20:13:27 +0800778
Imre Deakd5dd62b2015-03-17 11:40:03 +0200779/*
780 * Check if the calculated PLL configuration is more optimal compared to the
781 * best configuration and error found so far. Return the calculated error.
782 */
783static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
784 const intel_clock_t *calculated_clock,
785 const intel_clock_t *best_clock,
786 unsigned int best_error_ppm,
787 unsigned int *error_ppm)
788{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200789 /*
790 * For CHV ignore the error and consider only the P value.
791 * Prefer a bigger P value based on HW requirements.
792 */
793 if (IS_CHERRYVIEW(dev)) {
794 *error_ppm = 0;
795
796 return calculated_clock->p > best_clock->p;
797 }
798
Imre Deak24be4e42015-03-17 11:40:04 +0200799 if (WARN_ON_ONCE(!target_freq))
800 return false;
801
Imre Deakd5dd62b2015-03-17 11:40:03 +0200802 *error_ppm = div_u64(1000000ULL *
803 abs(target_freq - calculated_clock->dot),
804 target_freq);
805 /*
806 * Prefer a better P value over a better (smaller) error if the error
807 * is small. Ensure this preference for future configurations too by
808 * setting the error to 0.
809 */
810 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
811 *error_ppm = 0;
812
813 return true;
814 }
815
816 return *error_ppm + 10 < best_error_ppm;
817}
818
Zhenyu Wang2c072452009-06-05 15:38:42 +0800819static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300820vlv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200821 int target, int refclk, intel_clock_t *match_clock,
822 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700823{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300824 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300825 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300826 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300827 /* min update 19.2 MHz */
828 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300829 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700830
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300831 target *= 5; /* fast clock */
832
833 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700834
835 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300836 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300837 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300838 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300839 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300840 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700841 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300842 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200843 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300844
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300845 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
846 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300847
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300848 vlv_clock(refclk, &clock);
849
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300850 if (!intel_PLL_is_valid(dev, limit,
851 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300852 continue;
853
Imre Deakd5dd62b2015-03-17 11:40:03 +0200854 if (!vlv_PLL_is_optimal(dev, target,
855 &clock,
856 best_clock,
857 bestppm, &ppm))
858 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300859
Imre Deakd5dd62b2015-03-17 11:40:03 +0200860 *best_clock = clock;
861 bestppm = ppm;
862 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700863 }
864 }
865 }
866 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700867
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300868 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700869}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700870
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300871static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300872chv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300873 int target, int refclk, intel_clock_t *match_clock,
874 intel_clock_t *best_clock)
875{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300876 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200877 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300878 intel_clock_t clock;
879 uint64_t m2;
880 int found = false;
881
882 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200883 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300884
885 /*
886 * Based on hardware doc, the n always set to 1, and m1 always
887 * set to 2. If requires to support 200Mhz refclk, we need to
888 * revisit this because n may not 1 anymore.
889 */
890 clock.n = 1, clock.m1 = 2;
891 target *= 5; /* fast clock */
892
893 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
894 for (clock.p2 = limit->p2.p2_fast;
895 clock.p2 >= limit->p2.p2_slow;
896 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200897 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300898
899 clock.p = clock.p1 * clock.p2;
900
901 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
902 clock.n) << 22, refclk * clock.m1);
903
904 if (m2 > INT_MAX/clock.m1)
905 continue;
906
907 clock.m2 = m2;
908
909 chv_clock(refclk, &clock);
910
911 if (!intel_PLL_is_valid(dev, limit, &clock))
912 continue;
913
Imre Deak9ca3ba02015-03-17 11:40:05 +0200914 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
915 best_error_ppm, &error_ppm))
916 continue;
917
918 *best_clock = clock;
919 best_error_ppm = error_ppm;
920 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300921 }
922 }
923
924 return found;
925}
926
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300927bool intel_crtc_active(struct drm_crtc *crtc)
928{
929 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
930
931 /* Be paranoid as we can arrive here with only partial
932 * state retrieved from the hardware during setup.
933 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100934 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300935 * as Haswell has gained clock readout/fastboot support.
936 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000937 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300938 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700939 *
940 * FIXME: The intel_crtc->active here should be switched to
941 * crtc->state->active once we have proper CRTC states wired up
942 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300943 */
Matt Roperc3d1f432015-03-09 10:19:23 -0700944 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200945 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300946}
947
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200948enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
949 enum pipe pipe)
950{
951 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
952 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
953
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200954 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200955}
956
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300957static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
958{
959 struct drm_i915_private *dev_priv = dev->dev_private;
960 u32 reg = PIPEDSL(pipe);
961 u32 line1, line2;
962 u32 line_mask;
963
964 if (IS_GEN2(dev))
965 line_mask = DSL_LINEMASK_GEN2;
966 else
967 line_mask = DSL_LINEMASK_GEN3;
968
969 line1 = I915_READ(reg) & line_mask;
970 mdelay(5);
971 line2 = I915_READ(reg) & line_mask;
972
973 return line1 == line2;
974}
975
Keith Packardab7ad7f2010-10-03 00:33:06 -0700976/*
977 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300978 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700979 *
980 * After disabling a pipe, we can't wait for vblank in the usual way,
981 * spinning on the vblank interrupt status bit, since we won't actually
982 * see an interrupt when the pipe is disabled.
983 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700984 * On Gen4 and above:
985 * wait for the pipe register state bit to turn off
986 *
987 * Otherwise:
988 * wait for the display line value to settle (it usually
989 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100990 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700991 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300992static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700993{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300994 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700995 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200996 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300997 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700998
Keith Packardab7ad7f2010-10-03 00:33:06 -0700999 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001000 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001001
Keith Packardab7ad7f2010-10-03 00:33:06 -07001002 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001003 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1004 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001005 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001006 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001007 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001008 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001009 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001010 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001011}
1012
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001013/*
1014 * ibx_digital_port_connected - is the specified port connected?
1015 * @dev_priv: i915 private structure
1016 * @port: the port to test
1017 *
1018 * Returns true if @port is connected, false otherwise.
1019 */
1020bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1021 struct intel_digital_port *port)
1022{
1023 u32 bit;
1024
Damien Lespiauc36346e2012-12-13 16:09:03 +00001025 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001026 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001027 case PORT_B:
1028 bit = SDE_PORTB_HOTPLUG;
1029 break;
1030 case PORT_C:
1031 bit = SDE_PORTC_HOTPLUG;
1032 break;
1033 case PORT_D:
1034 bit = SDE_PORTD_HOTPLUG;
1035 break;
1036 default:
1037 return true;
1038 }
1039 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001040 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001041 case PORT_B:
1042 bit = SDE_PORTB_HOTPLUG_CPT;
1043 break;
1044 case PORT_C:
1045 bit = SDE_PORTC_HOTPLUG_CPT;
1046 break;
1047 case PORT_D:
1048 bit = SDE_PORTD_HOTPLUG_CPT;
1049 break;
1050 default:
1051 return true;
1052 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001053 }
1054
1055 return I915_READ(SDEISR) & bit;
1056}
1057
Jesse Barnesb24e7172011-01-04 15:09:30 -08001058static const char *state_string(bool enabled)
1059{
1060 return enabled ? "on" : "off";
1061}
1062
1063/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001064void assert_pll(struct drm_i915_private *dev_priv,
1065 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001066{
1067 int reg;
1068 u32 val;
1069 bool cur_state;
1070
1071 reg = DPLL(pipe);
1072 val = I915_READ(reg);
1073 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001074 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001075 "PLL state assertion failure (expected %s, current %s)\n",
1076 state_string(state), state_string(cur_state));
1077}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001078
Jani Nikula23538ef2013-08-27 15:12:22 +03001079/* XXX: the dsi pll is shared between MIPI DSI ports */
1080static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1081{
1082 u32 val;
1083 bool cur_state;
1084
1085 mutex_lock(&dev_priv->dpio_lock);
1086 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1087 mutex_unlock(&dev_priv->dpio_lock);
1088
1089 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001090 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001091 "DSI PLL state assertion failure (expected %s, current %s)\n",
1092 state_string(state), state_string(cur_state));
1093}
1094#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1095#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1096
Daniel Vetter55607e82013-06-16 21:42:39 +02001097struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001098intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001099{
Daniel Vettere2b78262013-06-07 23:10:03 +02001100 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1101
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001102 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001103 return NULL;
1104
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001105 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001106}
1107
Jesse Barnesb24e7172011-01-04 15:09:30 -08001108/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001109void assert_shared_dpll(struct drm_i915_private *dev_priv,
1110 struct intel_shared_dpll *pll,
1111 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001112{
Jesse Barnes040484a2011-01-03 12:14:26 -08001113 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001114 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001115
Chris Wilson92b27b02012-05-20 18:10:50 +01001116 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001117 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001118 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001119
Daniel Vetter53589012013-06-05 13:34:16 +02001120 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001121 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001122 "%s assertion failure (expected %s, current %s)\n",
1123 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001124}
Jesse Barnes040484a2011-01-03 12:14:26 -08001125
1126static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1127 enum pipe pipe, bool state)
1128{
1129 int reg;
1130 u32 val;
1131 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001132 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1133 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001134
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001135 if (HAS_DDI(dev_priv->dev)) {
1136 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001137 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001138 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001139 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001140 } else {
1141 reg = FDI_TX_CTL(pipe);
1142 val = I915_READ(reg);
1143 cur_state = !!(val & FDI_TX_ENABLE);
1144 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001145 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001146 "FDI TX state assertion failure (expected %s, current %s)\n",
1147 state_string(state), state_string(cur_state));
1148}
1149#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1150#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1151
1152static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1153 enum pipe pipe, bool state)
1154{
1155 int reg;
1156 u32 val;
1157 bool cur_state;
1158
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001159 reg = FDI_RX_CTL(pipe);
1160 val = I915_READ(reg);
1161 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001162 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001163 "FDI RX state assertion failure (expected %s, current %s)\n",
1164 state_string(state), state_string(cur_state));
1165}
1166#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1167#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1168
1169static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1170 enum pipe pipe)
1171{
1172 int reg;
1173 u32 val;
1174
1175 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001176 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001177 return;
1178
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001179 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001180 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001181 return;
1182
Jesse Barnes040484a2011-01-03 12:14:26 -08001183 reg = FDI_TX_CTL(pipe);
1184 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001185 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 -08001186}
1187
Daniel Vetter55607e82013-06-16 21:42:39 +02001188void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1189 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001190{
1191 int reg;
1192 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001193 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001194
1195 reg = FDI_RX_CTL(pipe);
1196 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001197 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001198 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001199 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1200 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001201}
1202
Daniel Vetterb680c372014-09-19 18:27:27 +02001203void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1204 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001205{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001206 struct drm_device *dev = dev_priv->dev;
1207 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001208 u32 val;
1209 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001210 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001211
Jani Nikulabedd4db2014-08-22 15:04:13 +03001212 if (WARN_ON(HAS_DDI(dev)))
1213 return;
1214
1215 if (HAS_PCH_SPLIT(dev)) {
1216 u32 port_sel;
1217
Jesse Barnesea0760c2011-01-04 15:09:32 -08001218 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001219 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1220
1221 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1222 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1223 panel_pipe = PIPE_B;
1224 /* XXX: else fix for eDP */
1225 } else if (IS_VALLEYVIEW(dev)) {
1226 /* presumably write lock depends on pipe, not port select */
1227 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1228 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001229 } else {
1230 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001231 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1232 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001233 }
1234
1235 val = I915_READ(pp_reg);
1236 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001237 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001238 locked = false;
1239
Rob Clarke2c719b2014-12-15 13:56:32 -05001240 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001241 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001242 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001243}
1244
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001245static void assert_cursor(struct drm_i915_private *dev_priv,
1246 enum pipe pipe, bool state)
1247{
1248 struct drm_device *dev = dev_priv->dev;
1249 bool cur_state;
1250
Paulo Zanonid9d82082014-02-27 16:30:56 -03001251 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001252 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001253 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001254 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001255
Rob Clarke2c719b2014-12-15 13:56:32 -05001256 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001257 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1258 pipe_name(pipe), state_string(state), state_string(cur_state));
1259}
1260#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1261#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1262
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001263void assert_pipe(struct drm_i915_private *dev_priv,
1264 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001265{
1266 int reg;
1267 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001268 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001269 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1270 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001271
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001272 /* if we need the pipe quirk it must be always on */
1273 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1274 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001275 state = true;
1276
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001277 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001278 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001279 cur_state = false;
1280 } else {
1281 reg = PIPECONF(cpu_transcoder);
1282 val = I915_READ(reg);
1283 cur_state = !!(val & PIPECONF_ENABLE);
1284 }
1285
Rob Clarke2c719b2014-12-15 13:56:32 -05001286 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001287 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001288 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001289}
1290
Chris Wilson931872f2012-01-16 23:01:13 +00001291static void assert_plane(struct drm_i915_private *dev_priv,
1292 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001293{
1294 int reg;
1295 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001296 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001297
1298 reg = DSPCNTR(plane);
1299 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001300 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001301 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001302 "plane %c assertion failure (expected %s, current %s)\n",
1303 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001304}
1305
Chris Wilson931872f2012-01-16 23:01:13 +00001306#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1307#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1308
Jesse Barnesb24e7172011-01-04 15:09:30 -08001309static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1310 enum pipe pipe)
1311{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001312 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001313 int reg, i;
1314 u32 val;
1315 int cur_pipe;
1316
Ville Syrjälä653e1022013-06-04 13:49:05 +03001317 /* Primary planes are fixed to pipes on gen4+ */
1318 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001319 reg = DSPCNTR(pipe);
1320 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001321 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001322 "plane %c assertion failure, should be disabled but not\n",
1323 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001324 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001325 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001326
Jesse Barnesb24e7172011-01-04 15:09:30 -08001327 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001328 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001329 reg = DSPCNTR(i);
1330 val = I915_READ(reg);
1331 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1332 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001333 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001334 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1335 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001336 }
1337}
1338
Jesse Barnes19332d72013-03-28 09:55:38 -07001339static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1340 enum pipe pipe)
1341{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001342 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001343 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001344 u32 val;
1345
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001346 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001347 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001348 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001349 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001350 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1351 sprite, pipe_name(pipe));
1352 }
1353 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001354 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001355 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001356 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001357 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001358 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001359 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001360 }
1361 } else if (INTEL_INFO(dev)->gen >= 7) {
1362 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001363 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001364 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001365 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001366 plane_name(pipe), pipe_name(pipe));
1367 } else if (INTEL_INFO(dev)->gen >= 5) {
1368 reg = DVSCNTR(pipe);
1369 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001370 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001371 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1372 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001373 }
1374}
1375
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001376static void assert_vblank_disabled(struct drm_crtc *crtc)
1377{
Rob Clarke2c719b2014-12-15 13:56:32 -05001378 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001379 drm_crtc_vblank_put(crtc);
1380}
1381
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001382static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001383{
1384 u32 val;
1385 bool enabled;
1386
Rob Clarke2c719b2014-12-15 13:56:32 -05001387 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001388
Jesse Barnes92f25842011-01-04 15:09:34 -08001389 val = I915_READ(PCH_DREF_CONTROL);
1390 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1391 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001392 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001393}
1394
Daniel Vetterab9412b2013-05-03 11:49:46 +02001395static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1396 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001397{
1398 int reg;
1399 u32 val;
1400 bool enabled;
1401
Daniel Vetterab9412b2013-05-03 11:49:46 +02001402 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001403 val = I915_READ(reg);
1404 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001405 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001406 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1407 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001408}
1409
Keith Packard4e634382011-08-06 10:39:45 -07001410static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1411 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001412{
1413 if ((val & DP_PORT_EN) == 0)
1414 return false;
1415
1416 if (HAS_PCH_CPT(dev_priv->dev)) {
1417 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1418 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1419 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1420 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001421 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1422 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1423 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001424 } else {
1425 if ((val & DP_PIPE_MASK) != (pipe << 30))
1426 return false;
1427 }
1428 return true;
1429}
1430
Keith Packard1519b992011-08-06 10:35:34 -07001431static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1432 enum pipe pipe, u32 val)
1433{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001434 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001435 return false;
1436
1437 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001438 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001439 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001440 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1441 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1442 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001443 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001444 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001445 return false;
1446 }
1447 return true;
1448}
1449
1450static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1451 enum pipe pipe, u32 val)
1452{
1453 if ((val & LVDS_PORT_EN) == 0)
1454 return false;
1455
1456 if (HAS_PCH_CPT(dev_priv->dev)) {
1457 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1458 return false;
1459 } else {
1460 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1461 return false;
1462 }
1463 return true;
1464}
1465
1466static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1467 enum pipe pipe, u32 val)
1468{
1469 if ((val & ADPA_DAC_ENABLE) == 0)
1470 return false;
1471 if (HAS_PCH_CPT(dev_priv->dev)) {
1472 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1473 return false;
1474 } else {
1475 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1476 return false;
1477 }
1478 return true;
1479}
1480
Jesse Barnes291906f2011-02-02 12:28:03 -08001481static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001482 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001483{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001484 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001485 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001486 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001487 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001488
Rob Clarke2c719b2014-12-15 13:56:32 -05001489 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001490 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001491 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001492}
1493
1494static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1495 enum pipe pipe, int reg)
1496{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001497 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001498 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001499 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001500 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001501
Rob Clarke2c719b2014-12-15 13:56:32 -05001502 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001503 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001504 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001505}
1506
1507static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1508 enum pipe pipe)
1509{
1510 int reg;
1511 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001512
Keith Packardf0575e92011-07-25 22:12:43 -07001513 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1514 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1515 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001516
1517 reg = PCH_ADPA;
1518 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001519 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001520 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001521 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001522
1523 reg = PCH_LVDS;
1524 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001525 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001526 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001527 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001528
Paulo Zanonie2debe92013-02-18 19:00:27 -03001529 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1530 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1531 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001532}
1533
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001534static void intel_init_dpio(struct drm_device *dev)
1535{
1536 struct drm_i915_private *dev_priv = dev->dev_private;
1537
1538 if (!IS_VALLEYVIEW(dev))
1539 return;
1540
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001541 /*
1542 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1543 * CHV x1 PHY (DP/HDMI D)
1544 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1545 */
1546 if (IS_CHERRYVIEW(dev)) {
1547 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1548 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1549 } else {
1550 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1551 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001552}
1553
Ville Syrjäläd288f652014-10-28 13:20:22 +02001554static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001555 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001556{
Daniel Vetter426115c2013-07-11 22:13:42 +02001557 struct drm_device *dev = crtc->base.dev;
1558 struct drm_i915_private *dev_priv = dev->dev_private;
1559 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001560 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001561
Daniel Vetter426115c2013-07-11 22:13:42 +02001562 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001563
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001564 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001565 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1566
1567 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001568 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001569 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001570
Daniel Vetter426115c2013-07-11 22:13:42 +02001571 I915_WRITE(reg, dpll);
1572 POSTING_READ(reg);
1573 udelay(150);
1574
1575 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1576 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1577
Ville Syrjäläd288f652014-10-28 13:20:22 +02001578 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001579 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001580
1581 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001582 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001583 POSTING_READ(reg);
1584 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001585 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001586 POSTING_READ(reg);
1587 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001588 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001589 POSTING_READ(reg);
1590 udelay(150); /* wait for warmup */
1591}
1592
Ville Syrjäläd288f652014-10-28 13:20:22 +02001593static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001594 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001595{
1596 struct drm_device *dev = crtc->base.dev;
1597 struct drm_i915_private *dev_priv = dev->dev_private;
1598 int pipe = crtc->pipe;
1599 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001600 u32 tmp;
1601
1602 assert_pipe_disabled(dev_priv, crtc->pipe);
1603
1604 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1605
1606 mutex_lock(&dev_priv->dpio_lock);
1607
1608 /* Enable back the 10bit clock to display controller */
1609 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1610 tmp |= DPIO_DCLKP_EN;
1611 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1612
1613 /*
1614 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1615 */
1616 udelay(1);
1617
1618 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001619 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001620
1621 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001622 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001623 DRM_ERROR("PLL %d failed to lock\n", pipe);
1624
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001625 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001626 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001627 POSTING_READ(DPLL_MD(pipe));
1628
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001629 mutex_unlock(&dev_priv->dpio_lock);
1630}
1631
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001632static int intel_num_dvo_pipes(struct drm_device *dev)
1633{
1634 struct intel_crtc *crtc;
1635 int count = 0;
1636
1637 for_each_intel_crtc(dev, crtc)
1638 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001639 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001640
1641 return count;
1642}
1643
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001644static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001645{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001646 struct drm_device *dev = crtc->base.dev;
1647 struct drm_i915_private *dev_priv = dev->dev_private;
1648 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001649 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001650
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001651 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001652
1653 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001654 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001655
1656 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001657 if (IS_MOBILE(dev) && !IS_I830(dev))
1658 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001659
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001660 /* Enable DVO 2x clock on both PLLs if necessary */
1661 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1662 /*
1663 * It appears to be important that we don't enable this
1664 * for the current pipe before otherwise configuring the
1665 * PLL. No idea how this should be handled if multiple
1666 * DVO outputs are enabled simultaneosly.
1667 */
1668 dpll |= DPLL_DVO_2X_MODE;
1669 I915_WRITE(DPLL(!crtc->pipe),
1670 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1671 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001672
1673 /* Wait for the clocks to stabilize. */
1674 POSTING_READ(reg);
1675 udelay(150);
1676
1677 if (INTEL_INFO(dev)->gen >= 4) {
1678 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001679 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001680 } else {
1681 /* The pixel multiplier can only be updated once the
1682 * DPLL is enabled and the clocks are stable.
1683 *
1684 * So write it again.
1685 */
1686 I915_WRITE(reg, dpll);
1687 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001688
1689 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001690 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001691 POSTING_READ(reg);
1692 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001693 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001694 POSTING_READ(reg);
1695 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001696 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001697 POSTING_READ(reg);
1698 udelay(150); /* wait for warmup */
1699}
1700
1701/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001702 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001703 * @dev_priv: i915 private structure
1704 * @pipe: pipe PLL to disable
1705 *
1706 * Disable the PLL for @pipe, making sure the pipe is off first.
1707 *
1708 * Note! This is for pre-ILK only.
1709 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001710static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001711{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001712 struct drm_device *dev = crtc->base.dev;
1713 struct drm_i915_private *dev_priv = dev->dev_private;
1714 enum pipe pipe = crtc->pipe;
1715
1716 /* Disable DVO 2x clock on both PLLs if necessary */
1717 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001718 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001719 intel_num_dvo_pipes(dev) == 1) {
1720 I915_WRITE(DPLL(PIPE_B),
1721 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1722 I915_WRITE(DPLL(PIPE_A),
1723 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1724 }
1725
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001726 /* Don't disable pipe or pipe PLLs if needed */
1727 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1728 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001729 return;
1730
1731 /* Make sure the pipe isn't still relying on us */
1732 assert_pipe_disabled(dev_priv, pipe);
1733
Daniel Vetter50b44a42013-06-05 13:34:33 +02001734 I915_WRITE(DPLL(pipe), 0);
1735 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001736}
1737
Jesse Barnesf6071162013-10-01 10:41:38 -07001738static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1739{
1740 u32 val = 0;
1741
1742 /* Make sure the pipe isn't still relying on us */
1743 assert_pipe_disabled(dev_priv, pipe);
1744
Imre Deake5cbfbf2014-01-09 17:08:16 +02001745 /*
1746 * Leave integrated clock source and reference clock enabled for pipe B.
1747 * The latter is needed for VGA hotplug / manual detection.
1748 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001749 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001750 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001751 I915_WRITE(DPLL(pipe), val);
1752 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001753
1754}
1755
1756static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1757{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001758 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001759 u32 val;
1760
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001761 /* Make sure the pipe isn't still relying on us */
1762 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001763
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001764 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001765 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001766 if (pipe != PIPE_A)
1767 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1768 I915_WRITE(DPLL(pipe), val);
1769 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001770
1771 mutex_lock(&dev_priv->dpio_lock);
1772
1773 /* Disable 10bit clock to display controller */
1774 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1775 val &= ~DPIO_DCLKP_EN;
1776 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1777
Ville Syrjälä61407f62014-05-27 16:32:55 +03001778 /* disable left/right clock distribution */
1779 if (pipe != PIPE_B) {
1780 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1781 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1782 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1783 } else {
1784 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1785 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1786 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1787 }
1788
Ville Syrjäläd7520482014-04-09 13:28:59 +03001789 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001790}
1791
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001792void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1793 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001794{
1795 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001796 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001797
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001798 switch (dport->port) {
1799 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001800 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001801 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001802 break;
1803 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001804 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001805 dpll_reg = DPLL(0);
1806 break;
1807 case PORT_D:
1808 port_mask = DPLL_PORTD_READY_MASK;
1809 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001810 break;
1811 default:
1812 BUG();
1813 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001814
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001815 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
Jesse Barnes89b667f2013-04-18 14:51:36 -07001816 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001817 port_name(dport->port), I915_READ(dpll_reg));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001818}
1819
Daniel Vetterb14b1052014-04-24 23:55:13 +02001820static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1821{
1822 struct drm_device *dev = crtc->base.dev;
1823 struct drm_i915_private *dev_priv = dev->dev_private;
1824 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1825
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001826 if (WARN_ON(pll == NULL))
1827 return;
1828
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001829 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001830 if (pll->active == 0) {
1831 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1832 WARN_ON(pll->on);
1833 assert_shared_dpll_disabled(dev_priv, pll);
1834
1835 pll->mode_set(dev_priv, pll);
1836 }
1837}
1838
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001839/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001840 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001841 * @dev_priv: i915 private structure
1842 * @pipe: pipe PLL to enable
1843 *
1844 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1845 * drives the transcoder clock.
1846 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001847static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001848{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001849 struct drm_device *dev = crtc->base.dev;
1850 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001851 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001852
Daniel Vetter87a875b2013-06-05 13:34:19 +02001853 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001854 return;
1855
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001856 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001857 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001858
Damien Lespiau74dd6922014-07-29 18:06:17 +01001859 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001860 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001861 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001862
Daniel Vettercdbd2312013-06-05 13:34:03 +02001863 if (pll->active++) {
1864 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001865 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001866 return;
1867 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001868 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001869
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001870 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1871
Daniel Vetter46edb022013-06-05 13:34:12 +02001872 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001873 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001874 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001875}
1876
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001877static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001878{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001879 struct drm_device *dev = crtc->base.dev;
1880 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001881 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001882
Jesse Barnes92f25842011-01-04 15:09:34 -08001883 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001884 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001885 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001886 return;
1887
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001888 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001889 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001890
Daniel Vetter46edb022013-06-05 13:34:12 +02001891 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1892 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001893 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001894
Chris Wilson48da64a2012-05-13 20:16:12 +01001895 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001896 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001897 return;
1898 }
1899
Daniel Vettere9d69442013-06-05 13:34:15 +02001900 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001901 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001902 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001903 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001904
Daniel Vetter46edb022013-06-05 13:34:12 +02001905 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001906 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001907 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001908
1909 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001910}
1911
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001912static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1913 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001914{
Daniel Vetter23670b322012-11-01 09:15:30 +01001915 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001916 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001917 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001918 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001919
1920 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001921 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001922
1923 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001924 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001925 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001926
1927 /* FDI must be feeding us bits for PCH ports */
1928 assert_fdi_tx_enabled(dev_priv, pipe);
1929 assert_fdi_rx_enabled(dev_priv, pipe);
1930
Daniel Vetter23670b322012-11-01 09:15:30 +01001931 if (HAS_PCH_CPT(dev)) {
1932 /* Workaround: Set the timing override bit before enabling the
1933 * pch transcoder. */
1934 reg = TRANS_CHICKEN2(pipe);
1935 val = I915_READ(reg);
1936 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1937 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001938 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001939
Daniel Vetterab9412b2013-05-03 11:49:46 +02001940 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001941 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001942 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001943
1944 if (HAS_PCH_IBX(dev_priv->dev)) {
1945 /*
1946 * make the BPC in transcoder be consistent with
1947 * that in pipeconf reg.
1948 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001949 val &= ~PIPECONF_BPC_MASK;
1950 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001951 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001952
1953 val &= ~TRANS_INTERLACE_MASK;
1954 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001955 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001956 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001957 val |= TRANS_LEGACY_INTERLACED_ILK;
1958 else
1959 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001960 else
1961 val |= TRANS_PROGRESSIVE;
1962
Jesse Barnes040484a2011-01-03 12:14:26 -08001963 I915_WRITE(reg, val | TRANS_ENABLE);
1964 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001965 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001966}
1967
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001968static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001969 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001970{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001971 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001972
1973 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001974 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001975
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001976 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001977 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001978 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001979
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001980 /* Workaround: set timing override bit. */
1981 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001982 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001983 I915_WRITE(_TRANSA_CHICKEN2, val);
1984
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001985 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001986 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001987
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001988 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1989 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001990 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001991 else
1992 val |= TRANS_PROGRESSIVE;
1993
Daniel Vetterab9412b2013-05-03 11:49:46 +02001994 I915_WRITE(LPT_TRANSCONF, val);
1995 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001996 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001997}
1998
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001999static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2000 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002001{
Daniel Vetter23670b322012-11-01 09:15:30 +01002002 struct drm_device *dev = dev_priv->dev;
2003 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002004
2005 /* FDI relies on the transcoder */
2006 assert_fdi_tx_disabled(dev_priv, pipe);
2007 assert_fdi_rx_disabled(dev_priv, pipe);
2008
Jesse Barnes291906f2011-02-02 12:28:03 -08002009 /* Ports must be off as well */
2010 assert_pch_ports_disabled(dev_priv, pipe);
2011
Daniel Vetterab9412b2013-05-03 11:49:46 +02002012 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002013 val = I915_READ(reg);
2014 val &= ~TRANS_ENABLE;
2015 I915_WRITE(reg, val);
2016 /* wait for PCH transcoder off, transcoder state */
2017 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002018 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002019
2020 if (!HAS_PCH_IBX(dev)) {
2021 /* Workaround: Clear the timing override chicken bit again. */
2022 reg = TRANS_CHICKEN2(pipe);
2023 val = I915_READ(reg);
2024 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2025 I915_WRITE(reg, val);
2026 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002027}
2028
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002029static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002030{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002031 u32 val;
2032
Daniel Vetterab9412b2013-05-03 11:49:46 +02002033 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002034 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002035 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002036 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002037 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002038 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002039
2040 /* Workaround: clear timing override bit. */
2041 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002042 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002043 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002044}
2045
2046/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002047 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002048 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002049 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002050 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002051 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002052 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002053static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002054{
Paulo Zanoni03722642014-01-17 13:51:09 -02002055 struct drm_device *dev = crtc->base.dev;
2056 struct drm_i915_private *dev_priv = dev->dev_private;
2057 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002058 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2059 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002060 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002061 int reg;
2062 u32 val;
2063
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002064 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002065 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002066 assert_sprites_disabled(dev_priv, pipe);
2067
Paulo Zanoni681e5812012-12-06 11:12:38 -02002068 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002069 pch_transcoder = TRANSCODER_A;
2070 else
2071 pch_transcoder = pipe;
2072
Jesse Barnesb24e7172011-01-04 15:09:30 -08002073 /*
2074 * A pipe without a PLL won't actually be able to drive bits from
2075 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2076 * need the check.
2077 */
2078 if (!HAS_PCH_SPLIT(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002079 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002080 assert_dsi_pll_enabled(dev_priv);
2081 else
2082 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002083 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002084 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002085 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002086 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002087 assert_fdi_tx_pll_enabled(dev_priv,
2088 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002089 }
2090 /* FIXME: assert CPU port conditions for SNB+ */
2091 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002092
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002093 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002094 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002095 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002096 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2097 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002098 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002099 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002100
2101 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002102 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002103}
2104
2105/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002106 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002107 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002108 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002109 * Disable the pipe of @crtc, making sure that various hardware
2110 * specific requirements are met, if applicable, e.g. plane
2111 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002112 *
2113 * Will wait until the pipe has shut down before returning.
2114 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002115static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002116{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002117 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002118 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002119 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002120 int reg;
2121 u32 val;
2122
2123 /*
2124 * Make sure planes won't keep trying to pump pixels to us,
2125 * or we might hang the display.
2126 */
2127 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002128 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002129 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002130
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002131 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002132 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002133 if ((val & PIPECONF_ENABLE) == 0)
2134 return;
2135
Ville Syrjälä67adc642014-08-15 01:21:57 +03002136 /*
2137 * Double wide has implications for planes
2138 * so best keep it disabled when not needed.
2139 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002140 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002141 val &= ~PIPECONF_DOUBLE_WIDE;
2142
2143 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002144 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2145 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002146 val &= ~PIPECONF_ENABLE;
2147
2148 I915_WRITE(reg, val);
2149 if ((val & PIPECONF_ENABLE) == 0)
2150 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002151}
2152
Keith Packardd74362c2011-07-28 14:47:14 -07002153/*
2154 * Plane regs are double buffered, going from enabled->disabled needs a
2155 * trigger in order to latch. The display address reg provides this.
2156 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002157void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2158 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002159{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002160 struct drm_device *dev = dev_priv->dev;
2161 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002162
2163 I915_WRITE(reg, I915_READ(reg));
2164 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002165}
2166
Jesse Barnesb24e7172011-01-04 15:09:30 -08002167/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002168 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002169 * @plane: plane to be enabled
2170 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002171 *
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002172 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002173 */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002174static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2175 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002176{
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002177 struct drm_device *dev = plane->dev;
2178 struct drm_i915_private *dev_priv = dev->dev_private;
2179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002180
2181 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002182 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002183
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002184 if (intel_crtc->primary_enabled)
2185 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002186
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002187 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002188
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002189 dev_priv->display.update_primary_plane(crtc, plane->fb,
2190 crtc->x, crtc->y);
Ville Syrjälä33c3b0d2014-06-24 13:59:28 +03002191
2192 /*
2193 * BDW signals flip done immediately if the plane
2194 * is disabled, even if the plane enable is already
2195 * armed to occur at the next vblank :(
2196 */
2197 if (IS_BROADWELL(dev))
2198 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002199}
2200
Jesse Barnesb24e7172011-01-04 15:09:30 -08002201/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002202 * intel_disable_primary_hw_plane - disable the primary hardware plane
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002203 * @plane: plane to be disabled
2204 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002205 *
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002206 * Disable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002207 */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002208static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2209 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002210{
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002211 struct drm_device *dev = plane->dev;
2212 struct drm_i915_private *dev_priv = dev->dev_private;
2213 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2214
Matt Roper32b7eee2014-12-24 07:59:06 -08002215 if (WARN_ON(!intel_crtc->active))
2216 return;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002217
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002218 if (!intel_crtc->primary_enabled)
2219 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002220
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002221 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002222
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002223 dev_priv->display.update_primary_plane(crtc, plane->fb,
2224 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002225}
2226
Chris Wilson693db182013-03-05 14:52:39 +00002227static bool need_vtd_wa(struct drm_device *dev)
2228{
2229#ifdef CONFIG_INTEL_IOMMU
2230 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2231 return true;
2232#endif
2233 return false;
2234}
2235
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002236static unsigned int
2237intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2238 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002239{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002240 unsigned int tile_height;
2241 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002242
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002243 switch (fb_format_modifier) {
2244 case DRM_FORMAT_MOD_NONE:
2245 tile_height = 1;
2246 break;
2247 case I915_FORMAT_MOD_X_TILED:
2248 tile_height = IS_GEN2(dev) ? 16 : 8;
2249 break;
2250 case I915_FORMAT_MOD_Y_TILED:
2251 tile_height = 32;
2252 break;
2253 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002254 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2255 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002256 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002257 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002258 tile_height = 64;
2259 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002260 case 2:
2261 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002262 tile_height = 32;
2263 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002264 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002265 tile_height = 16;
2266 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002267 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002268 WARN_ONCE(1,
2269 "128-bit pixels are not supported for display!");
2270 tile_height = 16;
2271 break;
2272 }
2273 break;
2274 default:
2275 MISSING_CASE(fb_format_modifier);
2276 tile_height = 1;
2277 break;
2278 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002279
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002280 return tile_height;
2281}
2282
2283unsigned int
2284intel_fb_align_height(struct drm_device *dev, unsigned int height,
2285 uint32_t pixel_format, uint64_t fb_format_modifier)
2286{
2287 return ALIGN(height, intel_tile_height(dev, pixel_format,
2288 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002289}
2290
Chris Wilson127bd2a2010-07-23 23:32:05 +01002291int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002292intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2293 struct drm_framebuffer *fb,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002294 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002295{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002296 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002297 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002298 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002299 u32 alignment;
2300 int ret;
2301
Matt Roperebcdd392014-07-09 16:22:11 -07002302 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2303
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002304 switch (fb->modifier[0]) {
2305 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002306 if (INTEL_INFO(dev)->gen >= 9)
2307 alignment = 256 * 1024;
2308 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002309 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002310 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002311 alignment = 4 * 1024;
2312 else
2313 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002314 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002315 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002316 if (INTEL_INFO(dev)->gen >= 9)
2317 alignment = 256 * 1024;
2318 else {
2319 /* pin() will align the object as required by fence */
2320 alignment = 0;
2321 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002322 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002323 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002324 case I915_FORMAT_MOD_Yf_TILED:
2325 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2326 "Y tiling bo slipped through, driver bug!\n"))
2327 return -EINVAL;
2328 alignment = 1 * 1024 * 1024;
2329 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002330 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002331 MISSING_CASE(fb->modifier[0]);
2332 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002333 }
2334
Chris Wilson693db182013-03-05 14:52:39 +00002335 /* Note that the w/a also requires 64 PTE of padding following the
2336 * bo. We currently fill all unused PTE with the shadow page and so
2337 * we should always have valid PTE following the scanout preventing
2338 * the VT-d warning.
2339 */
2340 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2341 alignment = 256 * 1024;
2342
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002343 /*
2344 * Global gtt pte registers are special registers which actually forward
2345 * writes to a chunk of system memory. Which means that there is no risk
2346 * that the register values disappear as soon as we call
2347 * intel_runtime_pm_put(), so it is correct to wrap only the
2348 * pin/unpin/fence and not more.
2349 */
2350 intel_runtime_pm_get(dev_priv);
2351
Chris Wilsonce453d82011-02-21 14:43:56 +00002352 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002353 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002354 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002355 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002356
2357 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2358 * fence, whereas 965+ only requires a fence if using
2359 * framebuffer compression. For simplicity, we always install
2360 * a fence as the cost is not that onerous.
2361 */
Chris Wilson06d98132012-04-17 15:31:24 +01002362 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002363 if (ret)
2364 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002365
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002366 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002367
Chris Wilsonce453d82011-02-21 14:43:56 +00002368 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002369 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002370 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002371
2372err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01002373 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002374err_interruptible:
2375 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002376 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002377 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002378}
2379
Damien Lespiauf63bdb52015-02-10 19:32:24 +00002380static void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002381{
Matt Roperebcdd392014-07-09 16:22:11 -07002382 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2383
Chris Wilson1690e1e2011-12-14 13:57:08 +01002384 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01002385 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002386}
2387
Daniel Vetterc2c75132012-07-05 12:17:30 +02002388/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2389 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002390unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2391 unsigned int tiling_mode,
2392 unsigned int cpp,
2393 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002394{
Chris Wilsonbc752862013-02-21 20:04:31 +00002395 if (tiling_mode != I915_TILING_NONE) {
2396 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002397
Chris Wilsonbc752862013-02-21 20:04:31 +00002398 tile_rows = *y / 8;
2399 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002400
Chris Wilsonbc752862013-02-21 20:04:31 +00002401 tiles = *x / (512/cpp);
2402 *x %= 512/cpp;
2403
2404 return tile_rows * pitch * 8 + tiles * 4096;
2405 } else {
2406 unsigned int offset;
2407
2408 offset = *y * pitch + *x * cpp;
2409 *y = 0;
2410 *x = (offset & 4095) / cpp;
2411 return offset & -4096;
2412 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002413}
2414
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002415static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002416{
2417 switch (format) {
2418 case DISPPLANE_8BPP:
2419 return DRM_FORMAT_C8;
2420 case DISPPLANE_BGRX555:
2421 return DRM_FORMAT_XRGB1555;
2422 case DISPPLANE_BGRX565:
2423 return DRM_FORMAT_RGB565;
2424 default:
2425 case DISPPLANE_BGRX888:
2426 return DRM_FORMAT_XRGB8888;
2427 case DISPPLANE_RGBX888:
2428 return DRM_FORMAT_XBGR8888;
2429 case DISPPLANE_BGRX101010:
2430 return DRM_FORMAT_XRGB2101010;
2431 case DISPPLANE_RGBX101010:
2432 return DRM_FORMAT_XBGR2101010;
2433 }
2434}
2435
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002436static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2437{
2438 switch (format) {
2439 case PLANE_CTL_FORMAT_RGB_565:
2440 return DRM_FORMAT_RGB565;
2441 default:
2442 case PLANE_CTL_FORMAT_XRGB_8888:
2443 if (rgb_order) {
2444 if (alpha)
2445 return DRM_FORMAT_ABGR8888;
2446 else
2447 return DRM_FORMAT_XBGR8888;
2448 } else {
2449 if (alpha)
2450 return DRM_FORMAT_ARGB8888;
2451 else
2452 return DRM_FORMAT_XRGB8888;
2453 }
2454 case PLANE_CTL_FORMAT_XRGB_2101010:
2455 if (rgb_order)
2456 return DRM_FORMAT_XBGR2101010;
2457 else
2458 return DRM_FORMAT_XRGB2101010;
2459 }
2460}
2461
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002462static bool
2463intel_alloc_plane_obj(struct intel_crtc *crtc,
2464 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002465{
2466 struct drm_device *dev = crtc->base.dev;
2467 struct drm_i915_gem_object *obj = NULL;
2468 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002469 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002470 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2471 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2472 PAGE_SIZE);
2473
2474 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002475
Chris Wilsonff2652e2014-03-10 08:07:02 +00002476 if (plane_config->size == 0)
2477 return false;
2478
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002479 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2480 base_aligned,
2481 base_aligned,
2482 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002483 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002484 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002485
Damien Lespiau49af4492015-01-20 12:51:44 +00002486 obj->tiling_mode = plane_config->tiling;
2487 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002488 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002489
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002490 mode_cmd.pixel_format = fb->pixel_format;
2491 mode_cmd.width = fb->width;
2492 mode_cmd.height = fb->height;
2493 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002494 mode_cmd.modifier[0] = fb->modifier[0];
2495 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002496
2497 mutex_lock(&dev->struct_mutex);
2498
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002499 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002500 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002501 DRM_DEBUG_KMS("intel fb init failed\n");
2502 goto out_unref_obj;
2503 }
2504
Daniel Vettera071fa02014-06-18 23:28:09 +02002505 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002506 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002507
2508 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2509 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002510
2511out_unref_obj:
2512 drm_gem_object_unreference(&obj->base);
2513 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002514 return false;
2515}
2516
Matt Roperafd65eb2015-02-03 13:10:04 -08002517/* Update plane->state->fb to match plane->fb after driver-internal updates */
2518static void
2519update_state_fb(struct drm_plane *plane)
2520{
2521 if (plane->fb == plane->state->fb)
2522 return;
2523
2524 if (plane->state->fb)
2525 drm_framebuffer_unreference(plane->state->fb);
2526 plane->state->fb = plane->fb;
2527 if (plane->state->fb)
2528 drm_framebuffer_reference(plane->state->fb);
2529}
2530
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002531static void
2532intel_find_plane_obj(struct intel_crtc *intel_crtc,
2533 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002534{
2535 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002536 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002537 struct drm_crtc *c;
2538 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002539 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002540
Damien Lespiau2d140302015-02-05 17:22:18 +00002541 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002542 return;
2543
Damien Lespiauf55548b2015-02-05 18:30:20 +00002544 if (intel_alloc_plane_obj(intel_crtc, plane_config)) {
Damien Lespiaufb9981a2015-02-05 19:24:25 +00002545 struct drm_plane *primary = intel_crtc->base.primary;
2546
2547 primary->fb = &plane_config->fb->base;
2548 primary->state->crtc = &intel_crtc->base;
2549 update_state_fb(primary);
2550
Jesse Barnes484b41d2014-03-07 08:57:55 -08002551 return;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002552 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002553
Damien Lespiau2d140302015-02-05 17:22:18 +00002554 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002555
2556 /*
2557 * Failed to alloc the obj, check to see if we should share
2558 * an fb with another CRTC instead
2559 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002560 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002561 i = to_intel_crtc(c);
2562
2563 if (c == &intel_crtc->base)
2564 continue;
2565
Matt Roper2ff8fde2014-07-08 07:50:07 -07002566 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002567 continue;
2568
Matt Roper2ff8fde2014-07-08 07:50:07 -07002569 obj = intel_fb_obj(c->primary->fb);
2570 if (obj == NULL)
2571 continue;
2572
2573 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Damien Lespiaufb9981a2015-02-05 19:24:25 +00002574 struct drm_plane *primary = intel_crtc->base.primary;
2575
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002576 if (obj->tiling_mode != I915_TILING_NONE)
2577 dev_priv->preserve_bios_swizzle = true;
2578
Dave Airlie66e514c2014-04-03 07:51:54 +10002579 drm_framebuffer_reference(c->primary->fb);
Damien Lespiaufb9981a2015-02-05 19:24:25 +00002580 primary->fb = c->primary->fb;
2581 primary->state->crtc = &intel_crtc->base;
Damien Lespiau5ba76c42015-02-05 17:22:15 +00002582 update_state_fb(intel_crtc->base.primary);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002583 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002584 break;
2585 }
2586 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002587}
2588
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002589static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2590 struct drm_framebuffer *fb,
2591 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002592{
2593 struct drm_device *dev = crtc->dev;
2594 struct drm_i915_private *dev_priv = dev->dev_private;
2595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002596 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002597 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002598 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002599 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002600 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302601 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002602
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002603 if (!intel_crtc->primary_enabled) {
2604 I915_WRITE(reg, 0);
2605 if (INTEL_INFO(dev)->gen >= 4)
2606 I915_WRITE(DSPSURF(plane), 0);
2607 else
2608 I915_WRITE(DSPADDR(plane), 0);
2609 POSTING_READ(reg);
2610 return;
2611 }
2612
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002613 obj = intel_fb_obj(fb);
2614 if (WARN_ON(obj == NULL))
2615 return;
2616
2617 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2618
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002619 dspcntr = DISPPLANE_GAMMA_ENABLE;
2620
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002621 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002622
2623 if (INTEL_INFO(dev)->gen < 4) {
2624 if (intel_crtc->pipe == PIPE_B)
2625 dspcntr |= DISPPLANE_SEL_PIPE_B;
2626
2627 /* pipesrc and dspsize control the size that is scaled from,
2628 * which should always be the user's requested size.
2629 */
2630 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002631 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2632 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002633 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002634 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2635 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002636 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2637 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002638 I915_WRITE(PRIMPOS(plane), 0);
2639 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002640 }
2641
Ville Syrjälä57779d02012-10-31 17:50:14 +02002642 switch (fb->pixel_format) {
2643 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002644 dspcntr |= DISPPLANE_8BPP;
2645 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002646 case DRM_FORMAT_XRGB1555:
2647 case DRM_FORMAT_ARGB1555:
2648 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002649 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002650 case DRM_FORMAT_RGB565:
2651 dspcntr |= DISPPLANE_BGRX565;
2652 break;
2653 case DRM_FORMAT_XRGB8888:
2654 case DRM_FORMAT_ARGB8888:
2655 dspcntr |= DISPPLANE_BGRX888;
2656 break;
2657 case DRM_FORMAT_XBGR8888:
2658 case DRM_FORMAT_ABGR8888:
2659 dspcntr |= DISPPLANE_RGBX888;
2660 break;
2661 case DRM_FORMAT_XRGB2101010:
2662 case DRM_FORMAT_ARGB2101010:
2663 dspcntr |= DISPPLANE_BGRX101010;
2664 break;
2665 case DRM_FORMAT_XBGR2101010:
2666 case DRM_FORMAT_ABGR2101010:
2667 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002668 break;
2669 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002670 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002671 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002672
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002673 if (INTEL_INFO(dev)->gen >= 4 &&
2674 obj->tiling_mode != I915_TILING_NONE)
2675 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002676
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002677 if (IS_G4X(dev))
2678 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2679
Ville Syrjäläb98971272014-08-27 16:51:22 +03002680 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002681
Daniel Vetterc2c75132012-07-05 12:17:30 +02002682 if (INTEL_INFO(dev)->gen >= 4) {
2683 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002684 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002685 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002686 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002687 linear_offset -= intel_crtc->dspaddr_offset;
2688 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002689 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002690 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002691
Matt Roper8e7d6882015-01-21 16:35:41 -08002692 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302693 dspcntr |= DISPPLANE_ROTATE_180;
2694
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002695 x += (intel_crtc->config->pipe_src_w - 1);
2696 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302697
2698 /* Finding the last pixel of the last line of the display
2699 data and adding to linear_offset*/
2700 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002701 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2702 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302703 }
2704
2705 I915_WRITE(reg, dspcntr);
2706
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002707 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002708 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002709 I915_WRITE(DSPSURF(plane),
2710 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002711 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002712 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002713 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002714 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002715 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002716}
2717
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002718static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2719 struct drm_framebuffer *fb,
2720 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002721{
2722 struct drm_device *dev = crtc->dev;
2723 struct drm_i915_private *dev_priv = dev->dev_private;
2724 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002725 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002726 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002727 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002728 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002729 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302730 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002731
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002732 if (!intel_crtc->primary_enabled) {
2733 I915_WRITE(reg, 0);
2734 I915_WRITE(DSPSURF(plane), 0);
2735 POSTING_READ(reg);
2736 return;
2737 }
2738
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002739 obj = intel_fb_obj(fb);
2740 if (WARN_ON(obj == NULL))
2741 return;
2742
2743 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2744
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002745 dspcntr = DISPPLANE_GAMMA_ENABLE;
2746
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002747 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002748
2749 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2750 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2751
Ville Syrjälä57779d02012-10-31 17:50:14 +02002752 switch (fb->pixel_format) {
2753 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002754 dspcntr |= DISPPLANE_8BPP;
2755 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002756 case DRM_FORMAT_RGB565:
2757 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002758 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002759 case DRM_FORMAT_XRGB8888:
2760 case DRM_FORMAT_ARGB8888:
2761 dspcntr |= DISPPLANE_BGRX888;
2762 break;
2763 case DRM_FORMAT_XBGR8888:
2764 case DRM_FORMAT_ABGR8888:
2765 dspcntr |= DISPPLANE_RGBX888;
2766 break;
2767 case DRM_FORMAT_XRGB2101010:
2768 case DRM_FORMAT_ARGB2101010:
2769 dspcntr |= DISPPLANE_BGRX101010;
2770 break;
2771 case DRM_FORMAT_XBGR2101010:
2772 case DRM_FORMAT_ABGR2101010:
2773 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002774 break;
2775 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002776 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002777 }
2778
2779 if (obj->tiling_mode != I915_TILING_NONE)
2780 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002781
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002782 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002783 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002784
Ville Syrjäläb98971272014-08-27 16:51:22 +03002785 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002786 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002787 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002788 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002789 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002790 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002791 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302792 dspcntr |= DISPPLANE_ROTATE_180;
2793
2794 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002795 x += (intel_crtc->config->pipe_src_w - 1);
2796 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302797
2798 /* Finding the last pixel of the last line of the display
2799 data and adding to linear_offset*/
2800 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002801 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2802 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302803 }
2804 }
2805
2806 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002807
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002808 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002809 I915_WRITE(DSPSURF(plane),
2810 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002811 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002812 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2813 } else {
2814 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2815 I915_WRITE(DSPLINOFF(plane), linear_offset);
2816 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002817 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002818}
2819
Damien Lespiaub3218032015-02-27 11:15:18 +00002820u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2821 uint32_t pixel_format)
2822{
2823 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2824
2825 /*
2826 * The stride is either expressed as a multiple of 64 bytes
2827 * chunks for linear buffers or in number of tiles for tiled
2828 * buffers.
2829 */
2830 switch (fb_modifier) {
2831 case DRM_FORMAT_MOD_NONE:
2832 return 64;
2833 case I915_FORMAT_MOD_X_TILED:
2834 if (INTEL_INFO(dev)->gen == 2)
2835 return 128;
2836 return 512;
2837 case I915_FORMAT_MOD_Y_TILED:
2838 /* No need to check for old gens and Y tiling since this is
2839 * about the display engine and those will be blocked before
2840 * we get here.
2841 */
2842 return 128;
2843 case I915_FORMAT_MOD_Yf_TILED:
2844 if (bits_per_pixel == 8)
2845 return 64;
2846 else
2847 return 128;
2848 default:
2849 MISSING_CASE(fb_modifier);
2850 return 64;
2851 }
2852}
2853
Damien Lespiau70d21f02013-07-03 21:06:04 +01002854static void skylake_update_primary_plane(struct drm_crtc *crtc,
2855 struct drm_framebuffer *fb,
2856 int x, int y)
2857{
2858 struct drm_device *dev = crtc->dev;
2859 struct drm_i915_private *dev_priv = dev->dev_private;
2860 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002861 struct drm_i915_gem_object *obj;
2862 int pipe = intel_crtc->pipe;
Damien Lespiaub3218032015-02-27 11:15:18 +00002863 u32 plane_ctl, stride_div;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002864
2865 if (!intel_crtc->primary_enabled) {
2866 I915_WRITE(PLANE_CTL(pipe, 0), 0);
2867 I915_WRITE(PLANE_SURF(pipe, 0), 0);
2868 POSTING_READ(PLANE_CTL(pipe, 0));
2869 return;
2870 }
2871
2872 plane_ctl = PLANE_CTL_ENABLE |
2873 PLANE_CTL_PIPE_GAMMA_ENABLE |
2874 PLANE_CTL_PIPE_CSC_ENABLE;
2875
2876 switch (fb->pixel_format) {
2877 case DRM_FORMAT_RGB565:
2878 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
2879 break;
2880 case DRM_FORMAT_XRGB8888:
2881 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2882 break;
Jani Nikulaf75fb422015-02-10 13:15:49 +02002883 case DRM_FORMAT_ARGB8888:
2884 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2885 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2886 break;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002887 case DRM_FORMAT_XBGR8888:
2888 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2889 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2890 break;
Jani Nikulaf75fb422015-02-10 13:15:49 +02002891 case DRM_FORMAT_ABGR8888:
2892 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2893 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2894 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2895 break;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002896 case DRM_FORMAT_XRGB2101010:
2897 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2898 break;
2899 case DRM_FORMAT_XBGR2101010:
2900 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2901 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2902 break;
2903 default:
2904 BUG();
2905 }
2906
Daniel Vetter30af77c2015-02-10 17:16:11 +00002907 switch (fb->modifier[0]) {
2908 case DRM_FORMAT_MOD_NONE:
Damien Lespiau70d21f02013-07-03 21:06:04 +01002909 break;
Daniel Vetter30af77c2015-02-10 17:16:11 +00002910 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau70d21f02013-07-03 21:06:04 +01002911 plane_ctl |= PLANE_CTL_TILED_X;
Damien Lespiaub3218032015-02-27 11:15:18 +00002912 break;
2913 case I915_FORMAT_MOD_Y_TILED:
2914 plane_ctl |= PLANE_CTL_TILED_Y;
2915 break;
2916 case I915_FORMAT_MOD_Yf_TILED:
2917 plane_ctl |= PLANE_CTL_TILED_YF;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002918 break;
2919 default:
Damien Lespiaub3218032015-02-27 11:15:18 +00002920 MISSING_CASE(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002921 }
2922
2923 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Matt Roper8e7d6882015-01-21 16:35:41 -08002924 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180))
Sonika Jindal1447dde2014-10-04 10:53:31 +01002925 plane_ctl |= PLANE_CTL_ROTATE_180;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002926
Damien Lespiaub3218032015-02-27 11:15:18 +00002927 obj = intel_fb_obj(fb);
2928 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
2929 fb->pixel_format);
2930
Damien Lespiau70d21f02013-07-03 21:06:04 +01002931 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
2932
Damien Lespiau70d21f02013-07-03 21:06:04 +01002933 I915_WRITE(PLANE_POS(pipe, 0), 0);
2934 I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
2935 I915_WRITE(PLANE_SIZE(pipe, 0),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002936 (intel_crtc->config->pipe_src_h - 1) << 16 |
2937 (intel_crtc->config->pipe_src_w - 1));
Damien Lespiaub3218032015-02-27 11:15:18 +00002938 I915_WRITE(PLANE_STRIDE(pipe, 0), fb->pitches[0] / stride_div);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002939 I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
2940
2941 POSTING_READ(PLANE_SURF(pipe, 0));
2942}
2943
Jesse Barnes17638cd2011-06-24 12:19:23 -07002944/* Assume fb object is pinned & idle & fenced and just update base pointers */
2945static int
2946intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2947 int x, int y, enum mode_set_atomic state)
2948{
2949 struct drm_device *dev = crtc->dev;
2950 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002951
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002952 if (dev_priv->display.disable_fbc)
2953 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07002954
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002955 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2956
2957 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07002958}
2959
Ville Syrjälä75147472014-11-24 18:28:11 +02002960static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02002961{
Ville Syrjälä96a02912013-02-18 19:08:49 +02002962 struct drm_crtc *crtc;
2963
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002964 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002965 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2966 enum plane plane = intel_crtc->plane;
2967
2968 intel_prepare_page_flip(dev, plane);
2969 intel_finish_page_flip_plane(dev, plane);
2970 }
Ville Syrjälä75147472014-11-24 18:28:11 +02002971}
2972
2973static void intel_update_primary_planes(struct drm_device *dev)
2974{
2975 struct drm_i915_private *dev_priv = dev->dev_private;
2976 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02002977
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002978 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002979 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2980
Rob Clark51fd3712013-11-19 12:10:12 -05002981 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002982 /*
2983 * FIXME: Once we have proper support for primary planes (and
2984 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10002985 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002986 */
Matt Roperf4510a22014-04-01 15:22:40 -07002987 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07002988 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10002989 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07002990 crtc->x,
2991 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05002992 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02002993 }
2994}
2995
Ville Syrjälä75147472014-11-24 18:28:11 +02002996void intel_prepare_reset(struct drm_device *dev)
2997{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02002998 struct drm_i915_private *dev_priv = to_i915(dev);
2999 struct intel_crtc *crtc;
3000
Ville Syrjälä75147472014-11-24 18:28:11 +02003001 /* no reset support for gen2 */
3002 if (IS_GEN2(dev))
3003 return;
3004
3005 /* reset doesn't touch the display */
3006 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3007 return;
3008
3009 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003010
3011 /*
3012 * Disabling the crtcs gracefully seems nicer. Also the
3013 * g33 docs say we should at least disable all the planes.
3014 */
3015 for_each_intel_crtc(dev, crtc) {
3016 if (crtc->active)
3017 dev_priv->display.crtc_disable(&crtc->base);
3018 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003019}
3020
3021void intel_finish_reset(struct drm_device *dev)
3022{
3023 struct drm_i915_private *dev_priv = to_i915(dev);
3024
3025 /*
3026 * Flips in the rings will be nuked by the reset,
3027 * so complete all pending flips so that user space
3028 * will get its events and not get stuck.
3029 */
3030 intel_complete_page_flips(dev);
3031
3032 /* no reset support for gen2 */
3033 if (IS_GEN2(dev))
3034 return;
3035
3036 /* reset doesn't touch the display */
3037 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3038 /*
3039 * Flips in the rings have been nuked by the reset,
3040 * so update the base address of all primary
3041 * planes to the the last fb to make sure we're
3042 * showing the correct fb after a reset.
3043 */
3044 intel_update_primary_planes(dev);
3045 return;
3046 }
3047
3048 /*
3049 * The display has been reset as well,
3050 * so need a full re-initialization.
3051 */
3052 intel_runtime_pm_disable_interrupts(dev_priv);
3053 intel_runtime_pm_enable_interrupts(dev_priv);
3054
3055 intel_modeset_init_hw(dev);
3056
3057 spin_lock_irq(&dev_priv->irq_lock);
3058 if (dev_priv->display.hpd_irq_setup)
3059 dev_priv->display.hpd_irq_setup(dev);
3060 spin_unlock_irq(&dev_priv->irq_lock);
3061
3062 intel_modeset_setup_hw_state(dev, true);
3063
3064 intel_hpd_init(dev_priv);
3065
3066 drm_modeset_unlock_all(dev);
3067}
3068
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003069static int
Chris Wilson14667a42012-04-03 17:58:35 +01003070intel_finish_fb(struct drm_framebuffer *old_fb)
3071{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003072 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01003073 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3074 bool was_interruptible = dev_priv->mm.interruptible;
3075 int ret;
3076
Chris Wilson14667a42012-04-03 17:58:35 +01003077 /* Big Hammer, we also need to ensure that any pending
3078 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3079 * current scanout is retired before unpinning the old
3080 * framebuffer.
3081 *
3082 * This should only fail upon a hung GPU, in which case we
3083 * can safely continue.
3084 */
3085 dev_priv->mm.interruptible = false;
3086 ret = i915_gem_object_finish_gpu(obj);
3087 dev_priv->mm.interruptible = was_interruptible;
3088
3089 return ret;
3090}
3091
Chris Wilson7d5e3792014-03-04 13:15:08 +00003092static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3093{
3094 struct drm_device *dev = crtc->dev;
3095 struct drm_i915_private *dev_priv = dev->dev_private;
3096 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003097 bool pending;
3098
3099 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3100 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3101 return false;
3102
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003103 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003104 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003105 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003106
3107 return pending;
3108}
3109
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003110static void intel_update_pipe_size(struct intel_crtc *crtc)
3111{
3112 struct drm_device *dev = crtc->base.dev;
3113 struct drm_i915_private *dev_priv = dev->dev_private;
3114 const struct drm_display_mode *adjusted_mode;
3115
3116 if (!i915.fastboot)
3117 return;
3118
3119 /*
3120 * Update pipe size and adjust fitter if needed: the reason for this is
3121 * that in compute_mode_changes we check the native mode (not the pfit
3122 * mode) to see if we can flip rather than do a full mode set. In the
3123 * fastboot case, we'll flip, but if we don't update the pipesrc and
3124 * pfit state, we'll end up with a big fb scanned out into the wrong
3125 * sized surface.
3126 *
3127 * To fix this properly, we need to hoist the checks up into
3128 * compute_mode_changes (or above), check the actual pfit state and
3129 * whether the platform allows pfit disable with pipe active, and only
3130 * then update the pipesrc and pfit state, even on the flip path.
3131 */
3132
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003133 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003134
3135 I915_WRITE(PIPESRC(crtc->pipe),
3136 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3137 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003138 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003139 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3140 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003141 I915_WRITE(PF_CTL(crtc->pipe), 0);
3142 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3143 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3144 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003145 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3146 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003147}
3148
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003149static void intel_fdi_normal_train(struct drm_crtc *crtc)
3150{
3151 struct drm_device *dev = crtc->dev;
3152 struct drm_i915_private *dev_priv = dev->dev_private;
3153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3154 int pipe = intel_crtc->pipe;
3155 u32 reg, temp;
3156
3157 /* enable normal train */
3158 reg = FDI_TX_CTL(pipe);
3159 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003160 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003161 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3162 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003163 } else {
3164 temp &= ~FDI_LINK_TRAIN_NONE;
3165 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003166 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003167 I915_WRITE(reg, temp);
3168
3169 reg = FDI_RX_CTL(pipe);
3170 temp = I915_READ(reg);
3171 if (HAS_PCH_CPT(dev)) {
3172 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3173 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3174 } else {
3175 temp &= ~FDI_LINK_TRAIN_NONE;
3176 temp |= FDI_LINK_TRAIN_NONE;
3177 }
3178 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3179
3180 /* wait one idle pattern time */
3181 POSTING_READ(reg);
3182 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003183
3184 /* IVB wants error correction enabled */
3185 if (IS_IVYBRIDGE(dev))
3186 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3187 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003188}
3189
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003190/* The FDI link training functions for ILK/Ibexpeak. */
3191static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3192{
3193 struct drm_device *dev = crtc->dev;
3194 struct drm_i915_private *dev_priv = dev->dev_private;
3195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3196 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003197 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003198
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003199 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003200 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003201
Adam Jacksone1a44742010-06-25 15:32:14 -04003202 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3203 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003204 reg = FDI_RX_IMR(pipe);
3205 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003206 temp &= ~FDI_RX_SYMBOL_LOCK;
3207 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003208 I915_WRITE(reg, temp);
3209 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003210 udelay(150);
3211
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003212 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003213 reg = FDI_TX_CTL(pipe);
3214 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003215 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003216 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003217 temp &= ~FDI_LINK_TRAIN_NONE;
3218 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003219 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003220
Chris Wilson5eddb702010-09-11 13:48:45 +01003221 reg = FDI_RX_CTL(pipe);
3222 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003223 temp &= ~FDI_LINK_TRAIN_NONE;
3224 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003225 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3226
3227 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003228 udelay(150);
3229
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003230 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003231 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3232 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3233 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003234
Chris Wilson5eddb702010-09-11 13:48:45 +01003235 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003236 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003237 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003238 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3239
3240 if ((temp & FDI_RX_BIT_LOCK)) {
3241 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003242 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003243 break;
3244 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003245 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003246 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003247 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003248
3249 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003250 reg = FDI_TX_CTL(pipe);
3251 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003252 temp &= ~FDI_LINK_TRAIN_NONE;
3253 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003254 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003255
Chris Wilson5eddb702010-09-11 13:48:45 +01003256 reg = FDI_RX_CTL(pipe);
3257 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003258 temp &= ~FDI_LINK_TRAIN_NONE;
3259 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003260 I915_WRITE(reg, temp);
3261
3262 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003263 udelay(150);
3264
Chris Wilson5eddb702010-09-11 13:48:45 +01003265 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003266 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003267 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003268 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3269
3270 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003271 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003272 DRM_DEBUG_KMS("FDI train 2 done.\n");
3273 break;
3274 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003275 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003276 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003277 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003278
3279 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003280
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003281}
3282
Akshay Joshi0206e352011-08-16 15:34:10 -04003283static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003284 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3285 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3286 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3287 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3288};
3289
3290/* The FDI link training functions for SNB/Cougarpoint. */
3291static void gen6_fdi_link_train(struct drm_crtc *crtc)
3292{
3293 struct drm_device *dev = crtc->dev;
3294 struct drm_i915_private *dev_priv = dev->dev_private;
3295 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3296 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003297 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003298
Adam Jacksone1a44742010-06-25 15:32:14 -04003299 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3300 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003301 reg = FDI_RX_IMR(pipe);
3302 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003303 temp &= ~FDI_RX_SYMBOL_LOCK;
3304 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003305 I915_WRITE(reg, temp);
3306
3307 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003308 udelay(150);
3309
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003310 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003311 reg = FDI_TX_CTL(pipe);
3312 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003313 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003314 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003315 temp &= ~FDI_LINK_TRAIN_NONE;
3316 temp |= FDI_LINK_TRAIN_PATTERN_1;
3317 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3318 /* SNB-B */
3319 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003320 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003321
Daniel Vetterd74cf322012-10-26 10:58:13 +02003322 I915_WRITE(FDI_RX_MISC(pipe),
3323 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3324
Chris Wilson5eddb702010-09-11 13:48:45 +01003325 reg = FDI_RX_CTL(pipe);
3326 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003327 if (HAS_PCH_CPT(dev)) {
3328 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3329 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3330 } else {
3331 temp &= ~FDI_LINK_TRAIN_NONE;
3332 temp |= FDI_LINK_TRAIN_PATTERN_1;
3333 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003334 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3335
3336 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003337 udelay(150);
3338
Akshay Joshi0206e352011-08-16 15:34:10 -04003339 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003340 reg = FDI_TX_CTL(pipe);
3341 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003342 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3343 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003344 I915_WRITE(reg, temp);
3345
3346 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003347 udelay(500);
3348
Sean Paulfa37d392012-03-02 12:53:39 -05003349 for (retry = 0; retry < 5; retry++) {
3350 reg = FDI_RX_IIR(pipe);
3351 temp = I915_READ(reg);
3352 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3353 if (temp & FDI_RX_BIT_LOCK) {
3354 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3355 DRM_DEBUG_KMS("FDI train 1 done.\n");
3356 break;
3357 }
3358 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003359 }
Sean Paulfa37d392012-03-02 12:53:39 -05003360 if (retry < 5)
3361 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003362 }
3363 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003364 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003365
3366 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003367 reg = FDI_TX_CTL(pipe);
3368 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003369 temp &= ~FDI_LINK_TRAIN_NONE;
3370 temp |= FDI_LINK_TRAIN_PATTERN_2;
3371 if (IS_GEN6(dev)) {
3372 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3373 /* SNB-B */
3374 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3375 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003376 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003377
Chris Wilson5eddb702010-09-11 13:48:45 +01003378 reg = FDI_RX_CTL(pipe);
3379 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003380 if (HAS_PCH_CPT(dev)) {
3381 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3382 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3383 } else {
3384 temp &= ~FDI_LINK_TRAIN_NONE;
3385 temp |= FDI_LINK_TRAIN_PATTERN_2;
3386 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003387 I915_WRITE(reg, temp);
3388
3389 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003390 udelay(150);
3391
Akshay Joshi0206e352011-08-16 15:34:10 -04003392 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003393 reg = FDI_TX_CTL(pipe);
3394 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003395 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3396 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003397 I915_WRITE(reg, temp);
3398
3399 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003400 udelay(500);
3401
Sean Paulfa37d392012-03-02 12:53:39 -05003402 for (retry = 0; retry < 5; retry++) {
3403 reg = FDI_RX_IIR(pipe);
3404 temp = I915_READ(reg);
3405 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3406 if (temp & FDI_RX_SYMBOL_LOCK) {
3407 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3408 DRM_DEBUG_KMS("FDI train 2 done.\n");
3409 break;
3410 }
3411 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003412 }
Sean Paulfa37d392012-03-02 12:53:39 -05003413 if (retry < 5)
3414 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003415 }
3416 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003417 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003418
3419 DRM_DEBUG_KMS("FDI train done.\n");
3420}
3421
Jesse Barnes357555c2011-04-28 15:09:55 -07003422/* Manual link training for Ivy Bridge A0 parts */
3423static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3424{
3425 struct drm_device *dev = crtc->dev;
3426 struct drm_i915_private *dev_priv = dev->dev_private;
3427 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3428 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003429 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003430
3431 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3432 for train result */
3433 reg = FDI_RX_IMR(pipe);
3434 temp = I915_READ(reg);
3435 temp &= ~FDI_RX_SYMBOL_LOCK;
3436 temp &= ~FDI_RX_BIT_LOCK;
3437 I915_WRITE(reg, temp);
3438
3439 POSTING_READ(reg);
3440 udelay(150);
3441
Daniel Vetter01a415f2012-10-27 15:58:40 +02003442 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3443 I915_READ(FDI_RX_IIR(pipe)));
3444
Jesse Barnes139ccd32013-08-19 11:04:55 -07003445 /* Try each vswing and preemphasis setting twice before moving on */
3446 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3447 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003448 reg = FDI_TX_CTL(pipe);
3449 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003450 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3451 temp &= ~FDI_TX_ENABLE;
3452 I915_WRITE(reg, temp);
3453
3454 reg = FDI_RX_CTL(pipe);
3455 temp = I915_READ(reg);
3456 temp &= ~FDI_LINK_TRAIN_AUTO;
3457 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3458 temp &= ~FDI_RX_ENABLE;
3459 I915_WRITE(reg, temp);
3460
3461 /* enable CPU FDI TX and PCH FDI RX */
3462 reg = FDI_TX_CTL(pipe);
3463 temp = I915_READ(reg);
3464 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003465 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003466 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003467 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003468 temp |= snb_b_fdi_train_param[j/2];
3469 temp |= FDI_COMPOSITE_SYNC;
3470 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3471
3472 I915_WRITE(FDI_RX_MISC(pipe),
3473 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3474
3475 reg = FDI_RX_CTL(pipe);
3476 temp = I915_READ(reg);
3477 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3478 temp |= FDI_COMPOSITE_SYNC;
3479 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3480
3481 POSTING_READ(reg);
3482 udelay(1); /* should be 0.5us */
3483
3484 for (i = 0; i < 4; i++) {
3485 reg = FDI_RX_IIR(pipe);
3486 temp = I915_READ(reg);
3487 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3488
3489 if (temp & FDI_RX_BIT_LOCK ||
3490 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3491 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3492 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3493 i);
3494 break;
3495 }
3496 udelay(1); /* should be 0.5us */
3497 }
3498 if (i == 4) {
3499 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3500 continue;
3501 }
3502
3503 /* Train 2 */
3504 reg = FDI_TX_CTL(pipe);
3505 temp = I915_READ(reg);
3506 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3507 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3508 I915_WRITE(reg, temp);
3509
3510 reg = FDI_RX_CTL(pipe);
3511 temp = I915_READ(reg);
3512 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3513 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003514 I915_WRITE(reg, temp);
3515
3516 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003517 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003518
Jesse Barnes139ccd32013-08-19 11:04:55 -07003519 for (i = 0; i < 4; i++) {
3520 reg = FDI_RX_IIR(pipe);
3521 temp = I915_READ(reg);
3522 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003523
Jesse Barnes139ccd32013-08-19 11:04:55 -07003524 if (temp & FDI_RX_SYMBOL_LOCK ||
3525 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3526 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3527 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3528 i);
3529 goto train_done;
3530 }
3531 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003532 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003533 if (i == 4)
3534 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003535 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003536
Jesse Barnes139ccd32013-08-19 11:04:55 -07003537train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003538 DRM_DEBUG_KMS("FDI train done.\n");
3539}
3540
Daniel Vetter88cefb62012-08-12 19:27:14 +02003541static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003542{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003543 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003544 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003545 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003546 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003547
Jesse Barnesc64e3112010-09-10 11:27:03 -07003548
Jesse Barnes0e23b992010-09-10 11:10:00 -07003549 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003550 reg = FDI_RX_CTL(pipe);
3551 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003552 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003553 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003554 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003555 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3556
3557 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003558 udelay(200);
3559
3560 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003561 temp = I915_READ(reg);
3562 I915_WRITE(reg, temp | FDI_PCDCLK);
3563
3564 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003565 udelay(200);
3566
Paulo Zanoni20749732012-11-23 15:30:38 -02003567 /* Enable CPU FDI TX PLL, always on for Ironlake */
3568 reg = FDI_TX_CTL(pipe);
3569 temp = I915_READ(reg);
3570 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3571 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003572
Paulo Zanoni20749732012-11-23 15:30:38 -02003573 POSTING_READ(reg);
3574 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003575 }
3576}
3577
Daniel Vetter88cefb62012-08-12 19:27:14 +02003578static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3579{
3580 struct drm_device *dev = intel_crtc->base.dev;
3581 struct drm_i915_private *dev_priv = dev->dev_private;
3582 int pipe = intel_crtc->pipe;
3583 u32 reg, temp;
3584
3585 /* Switch from PCDclk to Rawclk */
3586 reg = FDI_RX_CTL(pipe);
3587 temp = I915_READ(reg);
3588 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3589
3590 /* Disable CPU FDI TX PLL */
3591 reg = FDI_TX_CTL(pipe);
3592 temp = I915_READ(reg);
3593 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3594
3595 POSTING_READ(reg);
3596 udelay(100);
3597
3598 reg = FDI_RX_CTL(pipe);
3599 temp = I915_READ(reg);
3600 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3601
3602 /* Wait for the clocks to turn off. */
3603 POSTING_READ(reg);
3604 udelay(100);
3605}
3606
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003607static void ironlake_fdi_disable(struct drm_crtc *crtc)
3608{
3609 struct drm_device *dev = crtc->dev;
3610 struct drm_i915_private *dev_priv = dev->dev_private;
3611 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3612 int pipe = intel_crtc->pipe;
3613 u32 reg, temp;
3614
3615 /* disable CPU FDI tx and PCH FDI rx */
3616 reg = FDI_TX_CTL(pipe);
3617 temp = I915_READ(reg);
3618 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3619 POSTING_READ(reg);
3620
3621 reg = FDI_RX_CTL(pipe);
3622 temp = I915_READ(reg);
3623 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003624 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003625 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3626
3627 POSTING_READ(reg);
3628 udelay(100);
3629
3630 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003631 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003632 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003633
3634 /* still set train pattern 1 */
3635 reg = FDI_TX_CTL(pipe);
3636 temp = I915_READ(reg);
3637 temp &= ~FDI_LINK_TRAIN_NONE;
3638 temp |= FDI_LINK_TRAIN_PATTERN_1;
3639 I915_WRITE(reg, temp);
3640
3641 reg = FDI_RX_CTL(pipe);
3642 temp = I915_READ(reg);
3643 if (HAS_PCH_CPT(dev)) {
3644 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3645 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3646 } else {
3647 temp &= ~FDI_LINK_TRAIN_NONE;
3648 temp |= FDI_LINK_TRAIN_PATTERN_1;
3649 }
3650 /* BPC in FDI rx is consistent with that in PIPECONF */
3651 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003652 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003653 I915_WRITE(reg, temp);
3654
3655 POSTING_READ(reg);
3656 udelay(100);
3657}
3658
Chris Wilson5dce5b932014-01-20 10:17:36 +00003659bool intel_has_pending_fb_unpin(struct drm_device *dev)
3660{
3661 struct intel_crtc *crtc;
3662
3663 /* Note that we don't need to be called with mode_config.lock here
3664 * as our list of CRTC objects is static for the lifetime of the
3665 * device and so cannot disappear as we iterate. Similarly, we can
3666 * happily treat the predicates as racy, atomic checks as userspace
3667 * cannot claim and pin a new fb without at least acquring the
3668 * struct_mutex and so serialising with us.
3669 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003670 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003671 if (atomic_read(&crtc->unpin_work_count) == 0)
3672 continue;
3673
3674 if (crtc->unpin_work)
3675 intel_wait_for_vblank(dev, crtc->pipe);
3676
3677 return true;
3678 }
3679
3680 return false;
3681}
3682
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003683static void page_flip_completed(struct intel_crtc *intel_crtc)
3684{
3685 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3686 struct intel_unpin_work *work = intel_crtc->unpin_work;
3687
3688 /* ensure that the unpin work is consistent wrt ->pending. */
3689 smp_rmb();
3690 intel_crtc->unpin_work = NULL;
3691
3692 if (work->event)
3693 drm_send_vblank_event(intel_crtc->base.dev,
3694 intel_crtc->pipe,
3695 work->event);
3696
3697 drm_crtc_vblank_put(&intel_crtc->base);
3698
3699 wake_up_all(&dev_priv->pending_flip_queue);
3700 queue_work(dev_priv->wq, &work->work);
3701
3702 trace_i915_flip_complete(intel_crtc->plane,
3703 work->pending_flip_obj);
3704}
3705
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003706void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003707{
Chris Wilson0f911282012-04-17 10:05:38 +01003708 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003709 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003710
Daniel Vetter2c10d572012-12-20 21:24:07 +01003711 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003712 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3713 !intel_crtc_has_pending_flip(crtc),
3714 60*HZ) == 0)) {
3715 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003716
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003717 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003718 if (intel_crtc->unpin_work) {
3719 WARN_ONCE(1, "Removing stuck page flip\n");
3720 page_flip_completed(intel_crtc);
3721 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003722 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003723 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003724
Chris Wilson975d5682014-08-20 13:13:34 +01003725 if (crtc->primary->fb) {
3726 mutex_lock(&dev->struct_mutex);
3727 intel_finish_fb(crtc->primary->fb);
3728 mutex_unlock(&dev->struct_mutex);
3729 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003730}
3731
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003732/* Program iCLKIP clock to the desired frequency */
3733static void lpt_program_iclkip(struct drm_crtc *crtc)
3734{
3735 struct drm_device *dev = crtc->dev;
3736 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003737 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003738 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3739 u32 temp;
3740
Daniel Vetter09153002012-12-12 14:06:44 +01003741 mutex_lock(&dev_priv->dpio_lock);
3742
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003743 /* It is necessary to ungate the pixclk gate prior to programming
3744 * the divisors, and gate it back when it is done.
3745 */
3746 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3747
3748 /* Disable SSCCTL */
3749 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003750 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3751 SBI_SSCCTL_DISABLE,
3752 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003753
3754 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003755 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003756 auxdiv = 1;
3757 divsel = 0x41;
3758 phaseinc = 0x20;
3759 } else {
3760 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003761 * but the adjusted_mode->crtc_clock in in KHz. To get the
3762 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003763 * convert the virtual clock precision to KHz here for higher
3764 * precision.
3765 */
3766 u32 iclk_virtual_root_freq = 172800 * 1000;
3767 u32 iclk_pi_range = 64;
3768 u32 desired_divisor, msb_divisor_value, pi_value;
3769
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003770 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003771 msb_divisor_value = desired_divisor / iclk_pi_range;
3772 pi_value = desired_divisor % iclk_pi_range;
3773
3774 auxdiv = 0;
3775 divsel = msb_divisor_value - 2;
3776 phaseinc = pi_value;
3777 }
3778
3779 /* This should not happen with any sane values */
3780 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3781 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3782 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3783 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3784
3785 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 +03003786 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003787 auxdiv,
3788 divsel,
3789 phasedir,
3790 phaseinc);
3791
3792 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003793 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003794 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3795 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3796 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3797 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3798 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3799 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003800 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003801
3802 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003803 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003804 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3805 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003806 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003807
3808 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003809 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003810 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003811 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003812
3813 /* Wait for initialization time */
3814 udelay(24);
3815
3816 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003817
3818 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003819}
3820
Daniel Vetter275f01b22013-05-03 11:49:47 +02003821static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3822 enum pipe pch_transcoder)
3823{
3824 struct drm_device *dev = crtc->base.dev;
3825 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003826 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003827
3828 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3829 I915_READ(HTOTAL(cpu_transcoder)));
3830 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3831 I915_READ(HBLANK(cpu_transcoder)));
3832 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3833 I915_READ(HSYNC(cpu_transcoder)));
3834
3835 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3836 I915_READ(VTOTAL(cpu_transcoder)));
3837 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3838 I915_READ(VBLANK(cpu_transcoder)));
3839 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3840 I915_READ(VSYNC(cpu_transcoder)));
3841 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3842 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3843}
3844
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003845static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003846{
3847 struct drm_i915_private *dev_priv = dev->dev_private;
3848 uint32_t temp;
3849
3850 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003851 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003852 return;
3853
3854 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3855 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3856
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003857 temp &= ~FDI_BC_BIFURCATION_SELECT;
3858 if (enable)
3859 temp |= FDI_BC_BIFURCATION_SELECT;
3860
3861 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003862 I915_WRITE(SOUTH_CHICKEN1, temp);
3863 POSTING_READ(SOUTH_CHICKEN1);
3864}
3865
3866static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3867{
3868 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003869
3870 switch (intel_crtc->pipe) {
3871 case PIPE_A:
3872 break;
3873 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003874 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003875 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003876 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003877 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003878
3879 break;
3880 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003881 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003882
3883 break;
3884 default:
3885 BUG();
3886 }
3887}
3888
Jesse Barnesf67a5592011-01-05 10:31:48 -08003889/*
3890 * Enable PCH resources required for PCH ports:
3891 * - PCH PLLs
3892 * - FDI training & RX/TX
3893 * - update transcoder timings
3894 * - DP transcoding bits
3895 * - transcoder
3896 */
3897static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003898{
3899 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003900 struct drm_i915_private *dev_priv = dev->dev_private;
3901 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3902 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003903 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003904
Daniel Vetterab9412b2013-05-03 11:49:46 +02003905 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003906
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003907 if (IS_IVYBRIDGE(dev))
3908 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3909
Daniel Vettercd986ab2012-10-26 10:58:12 +02003910 /* Write the TU size bits before fdi link training, so that error
3911 * detection works. */
3912 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3913 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3914
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003915 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003916 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003917
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003918 /* We need to program the right clock selection before writing the pixel
3919 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003920 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003921 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003922
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003923 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003924 temp |= TRANS_DPLL_ENABLE(pipe);
3925 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003926 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003927 temp |= sel;
3928 else
3929 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003930 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003931 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003932
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003933 /* XXX: pch pll's can be enabled any time before we enable the PCH
3934 * transcoder, and we actually should do this to not upset any PCH
3935 * transcoder that already use the clock when we share it.
3936 *
3937 * Note that enable_shared_dpll tries to do the right thing, but
3938 * get_shared_dpll unconditionally resets the pll - we need that to have
3939 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02003940 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003941
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003942 /* set transcoder timing, panel must allow it */
3943 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003944 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003945
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003946 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003947
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003948 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003949 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003950 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003951 reg = TRANS_DP_CTL(pipe);
3952 temp = I915_READ(reg);
3953 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003954 TRANS_DP_SYNC_MASK |
3955 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003956 temp |= (TRANS_DP_OUTPUT_ENABLE |
3957 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003958 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003959
3960 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003961 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003962 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003963 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003964
3965 switch (intel_trans_dp_port_sel(crtc)) {
3966 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003967 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003968 break;
3969 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003970 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003971 break;
3972 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003973 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003974 break;
3975 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003976 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003977 }
3978
Chris Wilson5eddb702010-09-11 13:48:45 +01003979 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003980 }
3981
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003982 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003983}
3984
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003985static void lpt_pch_enable(struct drm_crtc *crtc)
3986{
3987 struct drm_device *dev = crtc->dev;
3988 struct drm_i915_private *dev_priv = dev->dev_private;
3989 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003990 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003991
Daniel Vetterab9412b2013-05-03 11:49:46 +02003992 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003993
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003994 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003995
Paulo Zanoni0540e482012-10-31 18:12:40 -02003996 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003997 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003998
Paulo Zanoni937bb612012-10-31 18:12:47 -02003999 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004000}
4001
Daniel Vetter716c2e52014-06-25 22:02:02 +03004002void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004003{
Daniel Vettere2b78262013-06-07 23:10:03 +02004004 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004005
4006 if (pll == NULL)
4007 return;
4008
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004009 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004010 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004011 return;
4012 }
4013
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004014 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4015 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004016 WARN_ON(pll->on);
4017 WARN_ON(pll->active);
4018 }
4019
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004020 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004021}
4022
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004023struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4024 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004025{
Daniel Vettere2b78262013-06-07 23:10:03 +02004026 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004027 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004028 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004029
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004030 if (HAS_PCH_IBX(dev_priv->dev)) {
4031 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004032 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004033 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004034
Daniel Vetter46edb022013-06-05 13:34:12 +02004035 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4036 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004037
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004038 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004039
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004040 goto found;
4041 }
4042
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004043 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4044 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004045
4046 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004047 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004048 continue;
4049
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004050 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004051 &pll->new_config->hw_state,
4052 sizeof(pll->new_config->hw_state)) == 0) {
4053 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004054 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004055 pll->new_config->crtc_mask,
4056 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004057 goto found;
4058 }
4059 }
4060
4061 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004062 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4063 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004064 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004065 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4066 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004067 goto found;
4068 }
4069 }
4070
4071 return NULL;
4072
4073found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004074 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004075 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004076
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004077 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004078 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4079 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004080
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004081 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004082
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004083 return pll;
4084}
4085
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004086/**
4087 * intel_shared_dpll_start_config - start a new PLL staged config
4088 * @dev_priv: DRM device
4089 * @clear_pipes: mask of pipes that will have their PLLs freed
4090 *
4091 * Starts a new PLL staged config, copying the current config but
4092 * releasing the references of pipes specified in clear_pipes.
4093 */
4094static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4095 unsigned clear_pipes)
4096{
4097 struct intel_shared_dpll *pll;
4098 enum intel_dpll_id i;
4099
4100 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4101 pll = &dev_priv->shared_dplls[i];
4102
4103 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4104 GFP_KERNEL);
4105 if (!pll->new_config)
4106 goto cleanup;
4107
4108 pll->new_config->crtc_mask &= ~clear_pipes;
4109 }
4110
4111 return 0;
4112
4113cleanup:
4114 while (--i >= 0) {
4115 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004116 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004117 pll->new_config = NULL;
4118 }
4119
4120 return -ENOMEM;
4121}
4122
4123static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4124{
4125 struct intel_shared_dpll *pll;
4126 enum intel_dpll_id i;
4127
4128 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4129 pll = &dev_priv->shared_dplls[i];
4130
4131 WARN_ON(pll->new_config == &pll->config);
4132
4133 pll->config = *pll->new_config;
4134 kfree(pll->new_config);
4135 pll->new_config = NULL;
4136 }
4137}
4138
4139static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4140{
4141 struct intel_shared_dpll *pll;
4142 enum intel_dpll_id i;
4143
4144 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4145 pll = &dev_priv->shared_dplls[i];
4146
4147 WARN_ON(pll->new_config == &pll->config);
4148
4149 kfree(pll->new_config);
4150 pll->new_config = NULL;
4151 }
4152}
4153
Daniel Vettera1520312013-05-03 11:49:50 +02004154static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004155{
4156 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004157 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004158 u32 temp;
4159
4160 temp = I915_READ(dslreg);
4161 udelay(500);
4162 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004163 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004164 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004165 }
4166}
4167
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004168static void skylake_pfit_enable(struct intel_crtc *crtc)
4169{
4170 struct drm_device *dev = crtc->base.dev;
4171 struct drm_i915_private *dev_priv = dev->dev_private;
4172 int pipe = crtc->pipe;
4173
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004174 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004175 I915_WRITE(PS_CTL(pipe), PS_ENABLE);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004176 I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4177 I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004178 }
4179}
4180
Jesse Barnesb074cec2013-04-25 12:55:02 -07004181static void ironlake_pfit_enable(struct intel_crtc *crtc)
4182{
4183 struct drm_device *dev = crtc->base.dev;
4184 struct drm_i915_private *dev_priv = dev->dev_private;
4185 int pipe = crtc->pipe;
4186
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004187 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004188 /* Force use of hard-coded filter coefficients
4189 * as some pre-programmed values are broken,
4190 * e.g. x201.
4191 */
4192 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4193 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4194 PF_PIPE_SEL_IVB(pipe));
4195 else
4196 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004197 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4198 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004199 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004200}
4201
Matt Roper4a3b8762014-12-23 10:41:51 -08004202static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004203{
4204 struct drm_device *dev = crtc->dev;
4205 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004206 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004207 struct intel_plane *intel_plane;
4208
Matt Roperaf2b6532014-04-01 15:22:32 -07004209 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4210 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004211 if (intel_plane->pipe == pipe)
4212 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004213 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004214}
4215
Matt Roper0d703d42015-03-04 10:49:04 -08004216/*
4217 * Disable a plane internally without actually modifying the plane's state.
4218 * This will allow us to easily restore the plane later by just reprogramming
4219 * its state.
4220 */
4221static void disable_plane_internal(struct drm_plane *plane)
4222{
4223 struct intel_plane *intel_plane = to_intel_plane(plane);
4224 struct drm_plane_state *state =
4225 plane->funcs->atomic_duplicate_state(plane);
4226 struct intel_plane_state *intel_state = to_intel_plane_state(state);
4227
4228 intel_state->visible = false;
4229 intel_plane->commit_plane(plane, intel_state);
4230
4231 intel_plane_destroy_state(plane, state);
4232}
4233
Matt Roper4a3b8762014-12-23 10:41:51 -08004234static void intel_disable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004235{
4236 struct drm_device *dev = crtc->dev;
4237 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004238 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004239 struct intel_plane *intel_plane;
4240
Matt Roperaf2b6532014-04-01 15:22:32 -07004241 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4242 intel_plane = to_intel_plane(plane);
Matt Roper0d703d42015-03-04 10:49:04 -08004243 if (plane->fb && intel_plane->pipe == pipe)
4244 disable_plane_internal(plane);
Matt Roperaf2b6532014-04-01 15:22:32 -07004245 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004246}
4247
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004248void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004249{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004250 struct drm_device *dev = crtc->base.dev;
4251 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004252
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004253 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004254 return;
4255
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004256 /* We can only enable IPS after we enable a plane and wait for a vblank */
4257 intel_wait_for_vblank(dev, crtc->pipe);
4258
Paulo Zanonid77e4532013-09-24 13:52:55 -03004259 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004260 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004261 mutex_lock(&dev_priv->rps.hw_lock);
4262 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4263 mutex_unlock(&dev_priv->rps.hw_lock);
4264 /* Quoting Art Runyan: "its not safe to expect any particular
4265 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004266 * mailbox." Moreover, the mailbox may return a bogus state,
4267 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004268 */
4269 } else {
4270 I915_WRITE(IPS_CTL, IPS_ENABLE);
4271 /* The bit only becomes 1 in the next vblank, so this wait here
4272 * is essentially intel_wait_for_vblank. If we don't have this
4273 * and don't wait for vblanks until the end of crtc_enable, then
4274 * the HW state readout code will complain that the expected
4275 * IPS_CTL value is not the one we read. */
4276 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4277 DRM_ERROR("Timed out waiting for IPS enable\n");
4278 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004279}
4280
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004281void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004282{
4283 struct drm_device *dev = crtc->base.dev;
4284 struct drm_i915_private *dev_priv = dev->dev_private;
4285
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004286 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004287 return;
4288
4289 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004290 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004291 mutex_lock(&dev_priv->rps.hw_lock);
4292 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4293 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004294 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4295 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4296 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004297 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004298 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004299 POSTING_READ(IPS_CTL);
4300 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004301
4302 /* We need to wait for a vblank before we can disable the plane. */
4303 intel_wait_for_vblank(dev, crtc->pipe);
4304}
4305
4306/** Loads the palette/gamma unit for the CRTC with the prepared values */
4307static void intel_crtc_load_lut(struct drm_crtc *crtc)
4308{
4309 struct drm_device *dev = crtc->dev;
4310 struct drm_i915_private *dev_priv = dev->dev_private;
4311 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4312 enum pipe pipe = intel_crtc->pipe;
4313 int palreg = PALETTE(pipe);
4314 int i;
4315 bool reenable_ips = false;
4316
4317 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004318 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004319 return;
4320
4321 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004322 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004323 assert_dsi_pll_enabled(dev_priv);
4324 else
4325 assert_pll_enabled(dev_priv, pipe);
4326 }
4327
4328 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304329 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004330 palreg = LGC_PALETTE(pipe);
4331
4332 /* Workaround : Do not read or write the pipe palette/gamma data while
4333 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4334 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004335 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004336 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4337 GAMMA_MODE_MODE_SPLIT)) {
4338 hsw_disable_ips(intel_crtc);
4339 reenable_ips = true;
4340 }
4341
4342 for (i = 0; i < 256; i++) {
4343 I915_WRITE(palreg + 4 * i,
4344 (intel_crtc->lut_r[i] << 16) |
4345 (intel_crtc->lut_g[i] << 8) |
4346 intel_crtc->lut_b[i]);
4347 }
4348
4349 if (reenable_ips)
4350 hsw_enable_ips(intel_crtc);
4351}
4352
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004353static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4354{
4355 if (!enable && intel_crtc->overlay) {
4356 struct drm_device *dev = intel_crtc->base.dev;
4357 struct drm_i915_private *dev_priv = dev->dev_private;
4358
4359 mutex_lock(&dev->struct_mutex);
4360 dev_priv->mm.interruptible = false;
4361 (void) intel_overlay_switch_off(intel_crtc->overlay);
4362 dev_priv->mm.interruptible = true;
4363 mutex_unlock(&dev->struct_mutex);
4364 }
4365
4366 /* Let userspace switch the overlay on again. In most cases userspace
4367 * has to recompute where to put it anyway.
4368 */
4369}
4370
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004371static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004372{
4373 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004374 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4375 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004376
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03004377 intel_enable_primary_hw_plane(crtc->primary, crtc);
Matt Roper4a3b8762014-12-23 10:41:51 -08004378 intel_enable_sprite_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004379 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004380 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004381
4382 hsw_enable_ips(intel_crtc);
4383
4384 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004385 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004386 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004387
4388 /*
4389 * FIXME: Once we grow proper nuclear flip support out of this we need
4390 * to compute the mask of flip planes precisely. For the time being
4391 * consider this a flip from a NULL plane.
4392 */
4393 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004394}
4395
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004396static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004397{
4398 struct drm_device *dev = crtc->dev;
4399 struct drm_i915_private *dev_priv = dev->dev_private;
4400 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4401 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004402
4403 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004404
Paulo Zanonie35fef22015-02-09 14:46:29 -02004405 if (dev_priv->fbc.crtc == intel_crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004406 intel_fbc_disable(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004407
4408 hsw_disable_ips(intel_crtc);
4409
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004410 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004411 intel_crtc_update_cursor(crtc, false);
Matt Roper4a3b8762014-12-23 10:41:51 -08004412 intel_disable_sprite_planes(crtc);
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03004413 intel_disable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004414
Daniel Vetterf99d7062014-06-19 16:01:59 +02004415 /*
4416 * FIXME: Once we grow proper nuclear flip support out of this we need
4417 * to compute the mask of flip planes precisely. For the time being
4418 * consider this a flip to a NULL plane.
4419 */
4420 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004421}
4422
Jesse Barnesf67a5592011-01-05 10:31:48 -08004423static void ironlake_crtc_enable(struct drm_crtc *crtc)
4424{
4425 struct drm_device *dev = crtc->dev;
4426 struct drm_i915_private *dev_priv = dev->dev_private;
4427 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004428 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004429 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004430
Matt Roper83d65732015-02-25 13:12:16 -08004431 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004432
Jesse Barnesf67a5592011-01-05 10:31:48 -08004433 if (intel_crtc->active)
4434 return;
4435
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004436 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004437 intel_prepare_shared_dpll(intel_crtc);
4438
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004439 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304440 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004441
4442 intel_set_pipe_timings(intel_crtc);
4443
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004444 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004445 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004446 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004447 }
4448
4449 ironlake_set_pipeconf(crtc);
4450
Jesse Barnesf67a5592011-01-05 10:31:48 -08004451 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004452
Daniel Vettera72e4c92014-09-30 10:56:47 +02004453 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4454 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004455
Daniel Vetterf6736a12013-06-05 13:34:30 +02004456 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004457 if (encoder->pre_enable)
4458 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004459
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004460 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004461 /* Note: FDI PLL enabling _must_ be done before we enable the
4462 * cpu pipes, hence this is separate from all the other fdi/pch
4463 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004464 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004465 } else {
4466 assert_fdi_tx_disabled(dev_priv, pipe);
4467 assert_fdi_rx_disabled(dev_priv, pipe);
4468 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004469
Jesse Barnesb074cec2013-04-25 12:55:02 -07004470 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004471
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004472 /*
4473 * On ILK+ LUT must be loaded before the pipe is running but with
4474 * clocks enabled
4475 */
4476 intel_crtc_load_lut(crtc);
4477
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004478 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004479 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004480
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004481 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004482 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004483
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004484 assert_vblank_disabled(crtc);
4485 drm_crtc_vblank_on(crtc);
4486
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004487 for_each_encoder_on_crtc(dev, crtc, encoder)
4488 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004489
4490 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004491 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004492
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004493 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004494}
4495
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004496/* IPS only exists on ULT machines and is tied to pipe A. */
4497static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4498{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004499 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004500}
4501
Paulo Zanonie4916942013-09-20 16:21:19 -03004502/*
4503 * This implements the workaround described in the "notes" section of the mode
4504 * set sequence documentation. When going from no pipes or single pipe to
4505 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4506 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4507 */
4508static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4509{
4510 struct drm_device *dev = crtc->base.dev;
4511 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4512
4513 /* We want to get the other_active_crtc only if there's only 1 other
4514 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004515 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004516 if (!crtc_it->active || crtc_it == crtc)
4517 continue;
4518
4519 if (other_active_crtc)
4520 return;
4521
4522 other_active_crtc = crtc_it;
4523 }
4524 if (!other_active_crtc)
4525 return;
4526
4527 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4528 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4529}
4530
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004531static void haswell_crtc_enable(struct drm_crtc *crtc)
4532{
4533 struct drm_device *dev = crtc->dev;
4534 struct drm_i915_private *dev_priv = dev->dev_private;
4535 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4536 struct intel_encoder *encoder;
4537 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004538
Matt Roper83d65732015-02-25 13:12:16 -08004539 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004540
4541 if (intel_crtc->active)
4542 return;
4543
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004544 if (intel_crtc_to_shared_dpll(intel_crtc))
4545 intel_enable_shared_dpll(intel_crtc);
4546
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004547 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304548 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004549
4550 intel_set_pipe_timings(intel_crtc);
4551
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004552 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4553 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4554 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004555 }
4556
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004557 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004558 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004559 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004560 }
4561
4562 haswell_set_pipeconf(crtc);
4563
4564 intel_set_pipe_csc(crtc);
4565
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004566 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004567
Daniel Vettera72e4c92014-09-30 10:56:47 +02004568 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004569 for_each_encoder_on_crtc(dev, crtc, encoder)
4570 if (encoder->pre_enable)
4571 encoder->pre_enable(encoder);
4572
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004573 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004574 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4575 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004576 dev_priv->display.fdi_link_train(crtc);
4577 }
4578
Paulo Zanoni1f544382012-10-24 11:32:00 -02004579 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004580
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004581 if (IS_SKYLAKE(dev))
4582 skylake_pfit_enable(intel_crtc);
4583 else
4584 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004585
4586 /*
4587 * On ILK+ LUT must be loaded before the pipe is running but with
4588 * clocks enabled
4589 */
4590 intel_crtc_load_lut(crtc);
4591
Paulo Zanoni1f544382012-10-24 11:32:00 -02004592 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004593 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004594
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004595 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004596 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004597
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004598 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004599 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004600
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004601 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004602 intel_ddi_set_vc_payload_alloc(crtc, true);
4603
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004604 assert_vblank_disabled(crtc);
4605 drm_crtc_vblank_on(crtc);
4606
Jani Nikula8807e552013-08-30 19:40:32 +03004607 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004608 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004609 intel_opregion_notify_encoder(encoder, true);
4610 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004611
Paulo Zanonie4916942013-09-20 16:21:19 -03004612 /* If we change the relative order between pipe/planes enabling, we need
4613 * to change the workaround. */
4614 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004615 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004616}
4617
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004618static void skylake_pfit_disable(struct intel_crtc *crtc)
4619{
4620 struct drm_device *dev = crtc->base.dev;
4621 struct drm_i915_private *dev_priv = dev->dev_private;
4622 int pipe = crtc->pipe;
4623
4624 /* To avoid upsetting the power well on haswell only disable the pfit if
4625 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004626 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004627 I915_WRITE(PS_CTL(pipe), 0);
4628 I915_WRITE(PS_WIN_POS(pipe), 0);
4629 I915_WRITE(PS_WIN_SZ(pipe), 0);
4630 }
4631}
4632
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004633static void ironlake_pfit_disable(struct intel_crtc *crtc)
4634{
4635 struct drm_device *dev = crtc->base.dev;
4636 struct drm_i915_private *dev_priv = dev->dev_private;
4637 int pipe = crtc->pipe;
4638
4639 /* To avoid upsetting the power well on haswell only disable the pfit if
4640 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004641 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004642 I915_WRITE(PF_CTL(pipe), 0);
4643 I915_WRITE(PF_WIN_POS(pipe), 0);
4644 I915_WRITE(PF_WIN_SZ(pipe), 0);
4645 }
4646}
4647
Jesse Barnes6be4a602010-09-10 10:26:01 -07004648static void ironlake_crtc_disable(struct drm_crtc *crtc)
4649{
4650 struct drm_device *dev = crtc->dev;
4651 struct drm_i915_private *dev_priv = dev->dev_private;
4652 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004653 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004654 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004655 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004656
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004657 if (!intel_crtc->active)
4658 return;
4659
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004660 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004661
Daniel Vetterea9d7582012-07-10 10:42:52 +02004662 for_each_encoder_on_crtc(dev, crtc, encoder)
4663 encoder->disable(encoder);
4664
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004665 drm_crtc_vblank_off(crtc);
4666 assert_vblank_disabled(crtc);
4667
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004668 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004669 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02004670
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004671 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004672
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004673 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004674
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004675 for_each_encoder_on_crtc(dev, crtc, encoder)
4676 if (encoder->post_disable)
4677 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004678
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004679 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004680 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004681
Daniel Vetterd925c592013-06-05 13:34:04 +02004682 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004683
Daniel Vetterd925c592013-06-05 13:34:04 +02004684 if (HAS_PCH_CPT(dev)) {
4685 /* disable TRANS_DP_CTL */
4686 reg = TRANS_DP_CTL(pipe);
4687 temp = I915_READ(reg);
4688 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4689 TRANS_DP_PORT_SEL_MASK);
4690 temp |= TRANS_DP_PORT_SEL_NONE;
4691 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004692
Daniel Vetterd925c592013-06-05 13:34:04 +02004693 /* disable DPLL_SEL */
4694 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004695 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004696 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004697 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004698
4699 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004700 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02004701
4702 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004703 }
4704
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004705 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004706 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004707
4708 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004709 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004710 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004711}
4712
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004713static void haswell_crtc_disable(struct drm_crtc *crtc)
4714{
4715 struct drm_device *dev = crtc->dev;
4716 struct drm_i915_private *dev_priv = dev->dev_private;
4717 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4718 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004719 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004720
4721 if (!intel_crtc->active)
4722 return;
4723
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004724 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03004725
Jani Nikula8807e552013-08-30 19:40:32 +03004726 for_each_encoder_on_crtc(dev, crtc, encoder) {
4727 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004728 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004729 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004730
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004731 drm_crtc_vblank_off(crtc);
4732 assert_vblank_disabled(crtc);
4733
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004734 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004735 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4736 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004737 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004738
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004739 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03004740 intel_ddi_set_vc_payload_alloc(crtc, false);
4741
Paulo Zanoniad80a812012-10-24 16:06:19 -02004742 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004743
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004744 if (IS_SKYLAKE(dev))
4745 skylake_pfit_disable(intel_crtc);
4746 else
4747 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004748
Paulo Zanoni1f544382012-10-24 11:32:00 -02004749 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004750
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004751 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02004752 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02004753 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02004754 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004755
Imre Deak97b040a2014-06-25 22:01:50 +03004756 for_each_encoder_on_crtc(dev, crtc, encoder)
4757 if (encoder->post_disable)
4758 encoder->post_disable(encoder);
4759
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004760 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004761 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004762
4763 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004764 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004765 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004766
4767 if (intel_crtc_to_shared_dpll(intel_crtc))
4768 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004769}
4770
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004771static void ironlake_crtc_off(struct drm_crtc *crtc)
4772{
4773 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004774 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004775}
4776
Paulo Zanoni6441ab52012-10-05 12:05:58 -03004777
Jesse Barnes2dd24552013-04-25 12:55:01 -07004778static void i9xx_pfit_enable(struct intel_crtc *crtc)
4779{
4780 struct drm_device *dev = crtc->base.dev;
4781 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004782 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07004783
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02004784 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07004785 return;
4786
Daniel Vetterc0b03412013-05-28 12:05:54 +02004787 /*
4788 * The panel fitter should only be adjusted whilst the pipe is disabled,
4789 * according to register description and PRM.
4790 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07004791 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4792 assert_pipe_disabled(dev_priv, crtc->pipe);
4793
Jesse Barnesb074cec2013-04-25 12:55:02 -07004794 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4795 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02004796
4797 /* Border color in case we don't scale up to the full screen. Black by
4798 * default, change to something else for debugging. */
4799 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07004800}
4801
Dave Airlied05410f2014-06-05 13:22:59 +10004802static enum intel_display_power_domain port_to_power_domain(enum port port)
4803{
4804 switch (port) {
4805 case PORT_A:
4806 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4807 case PORT_B:
4808 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4809 case PORT_C:
4810 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4811 case PORT_D:
4812 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4813 default:
4814 WARN_ON_ONCE(1);
4815 return POWER_DOMAIN_PORT_OTHER;
4816 }
4817}
4818
Imre Deak77d22dc2014-03-05 16:20:52 +02004819#define for_each_power_domain(domain, mask) \
4820 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4821 if ((1 << (domain)) & (mask))
4822
Imre Deak319be8a2014-03-04 19:22:57 +02004823enum intel_display_power_domain
4824intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02004825{
Imre Deak319be8a2014-03-04 19:22:57 +02004826 struct drm_device *dev = intel_encoder->base.dev;
4827 struct intel_digital_port *intel_dig_port;
4828
4829 switch (intel_encoder->type) {
4830 case INTEL_OUTPUT_UNKNOWN:
4831 /* Only DDI platforms should ever use this output type */
4832 WARN_ON_ONCE(!HAS_DDI(dev));
4833 case INTEL_OUTPUT_DISPLAYPORT:
4834 case INTEL_OUTPUT_HDMI:
4835 case INTEL_OUTPUT_EDP:
4836 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10004837 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10004838 case INTEL_OUTPUT_DP_MST:
4839 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4840 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02004841 case INTEL_OUTPUT_ANALOG:
4842 return POWER_DOMAIN_PORT_CRT;
4843 case INTEL_OUTPUT_DSI:
4844 return POWER_DOMAIN_PORT_DSI;
4845 default:
4846 return POWER_DOMAIN_PORT_OTHER;
4847 }
4848}
4849
4850static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4851{
4852 struct drm_device *dev = crtc->dev;
4853 struct intel_encoder *intel_encoder;
4854 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4855 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02004856 unsigned long mask;
4857 enum transcoder transcoder;
4858
4859 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4860
4861 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4862 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004863 if (intel_crtc->config->pch_pfit.enabled ||
4864 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02004865 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4866
Imre Deak319be8a2014-03-04 19:22:57 +02004867 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4868 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4869
Imre Deak77d22dc2014-03-05 16:20:52 +02004870 return mask;
4871}
4872
Imre Deak77d22dc2014-03-05 16:20:52 +02004873static void modeset_update_crtc_power_domains(struct drm_device *dev)
4874{
4875 struct drm_i915_private *dev_priv = dev->dev_private;
4876 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4877 struct intel_crtc *crtc;
4878
4879 /*
4880 * First get all needed power domains, then put all unneeded, to avoid
4881 * any unnecessary toggling of the power wells.
4882 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004883 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004884 enum intel_display_power_domain domain;
4885
Matt Roper83d65732015-02-25 13:12:16 -08004886 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02004887 continue;
4888
Imre Deak319be8a2014-03-04 19:22:57 +02004889 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02004890
4891 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4892 intel_display_power_get(dev_priv, domain);
4893 }
4894
Ville Syrjälä50f6e502014-11-06 14:49:12 +02004895 if (dev_priv->display.modeset_global_resources)
4896 dev_priv->display.modeset_global_resources(dev);
4897
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004898 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004899 enum intel_display_power_domain domain;
4900
4901 for_each_power_domain(domain, crtc->enabled_power_domains)
4902 intel_display_power_put(dev_priv, domain);
4903
4904 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4905 }
4906
4907 intel_display_set_init_power(dev_priv, false);
4908}
4909
Ville Syrjälädfcab172014-06-13 13:37:47 +03004910/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004911static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004912{
Jesse Barnes586f49d2013-11-04 16:06:59 -08004913 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08004914
Jesse Barnes586f49d2013-11-04 16:06:59 -08004915 /* Obtain SKU information */
4916 mutex_lock(&dev_priv->dpio_lock);
4917 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4918 CCK_FUSE_HPLL_FREQ_MASK;
4919 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004920
Ville Syrjälädfcab172014-06-13 13:37:47 +03004921 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004922}
4923
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004924static void vlv_update_cdclk(struct drm_device *dev)
4925{
4926 struct drm_i915_private *dev_priv = dev->dev_private;
4927
4928 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03004929 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004930 dev_priv->vlv_cdclk_freq);
4931
4932 /*
4933 * Program the gmbus_freq based on the cdclk frequency.
4934 * BSpec erroneously claims we should aim for 4MHz, but
4935 * in fact 1MHz is the correct frequency.
4936 */
Ville Syrjälä6be1e3d2014-10-16 20:52:31 +03004937 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004938}
4939
Jesse Barnes30a970c2013-11-04 13:48:12 -08004940/* Adjust CDclk dividers to allow high res or save power if possible */
4941static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4942{
4943 struct drm_i915_private *dev_priv = dev->dev_private;
4944 u32 val, cmd;
4945
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03004946 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02004947
Ville Syrjälädfcab172014-06-13 13:37:47 +03004948 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08004949 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03004950 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004951 cmd = 1;
4952 else
4953 cmd = 0;
4954
4955 mutex_lock(&dev_priv->rps.hw_lock);
4956 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4957 val &= ~DSPFREQGUAR_MASK;
4958 val |= (cmd << DSPFREQGUAR_SHIFT);
4959 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4960 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4961 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4962 50)) {
4963 DRM_ERROR("timed out waiting for CDclk change\n");
4964 }
4965 mutex_unlock(&dev_priv->rps.hw_lock);
4966
Ville Syrjälädfcab172014-06-13 13:37:47 +03004967 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03004968 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004969
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03004970 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004971
4972 mutex_lock(&dev_priv->dpio_lock);
4973 /* adjust cdclk divider */
4974 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03004975 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004976 val |= divider;
4977 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03004978
4979 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
4980 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
4981 50))
4982 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08004983 mutex_unlock(&dev_priv->dpio_lock);
4984 }
4985
4986 mutex_lock(&dev_priv->dpio_lock);
4987 /* adjust self-refresh exit latency value */
4988 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4989 val &= ~0x7f;
4990
4991 /*
4992 * For high bandwidth configs, we set a higher latency in the bunit
4993 * so that the core display fetch happens in time to avoid underruns.
4994 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03004995 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004996 val |= 4500 / 250; /* 4.5 usec */
4997 else
4998 val |= 3000 / 250; /* 3.0 usec */
4999 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5000 mutex_unlock(&dev_priv->dpio_lock);
5001
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005002 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005003}
5004
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005005static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5006{
5007 struct drm_i915_private *dev_priv = dev->dev_private;
5008 u32 val, cmd;
5009
5010 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
5011
5012 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005013 case 333333:
5014 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005015 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005016 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005017 break;
5018 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005019 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005020 return;
5021 }
5022
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005023 /*
5024 * Specs are full of misinformation, but testing on actual
5025 * hardware has shown that we just need to write the desired
5026 * CCK divider into the Punit register.
5027 */
5028 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5029
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005030 mutex_lock(&dev_priv->rps.hw_lock);
5031 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5032 val &= ~DSPFREQGUAR_MASK_CHV;
5033 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5034 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5035 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5036 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5037 50)) {
5038 DRM_ERROR("timed out waiting for CDclk change\n");
5039 }
5040 mutex_unlock(&dev_priv->rps.hw_lock);
5041
5042 vlv_update_cdclk(dev);
5043}
5044
Jesse Barnes30a970c2013-11-04 13:48:12 -08005045static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5046 int max_pixclk)
5047{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005048 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005049 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005050
Jesse Barnes30a970c2013-11-04 13:48:12 -08005051 /*
5052 * Really only a few cases to deal with, as only 4 CDclks are supported:
5053 * 200MHz
5054 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005055 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005056 * 400MHz (VLV only)
5057 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5058 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005059 *
5060 * We seem to get an unstable or solid color picture at 200MHz.
5061 * Not sure what's wrong. For now use 200MHz only when all pipes
5062 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005063 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005064 if (!IS_CHERRYVIEW(dev_priv) &&
5065 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005066 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005067 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005068 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005069 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005070 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005071 else
5072 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005073}
5074
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005075/* compute the max pixel clock for new configuration */
5076static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005077{
5078 struct drm_device *dev = dev_priv->dev;
5079 struct intel_crtc *intel_crtc;
5080 int max_pixclk = 0;
5081
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005082 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005083 if (intel_crtc->new_enabled)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005084 max_pixclk = max(max_pixclk,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02005085 intel_crtc->new_config->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005086 }
5087
5088 return max_pixclk;
5089}
5090
5091static void valleyview_modeset_global_pipes(struct drm_device *dev,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005092 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005093{
5094 struct drm_i915_private *dev_priv = dev->dev_private;
5095 struct intel_crtc *intel_crtc;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005096 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005097
Imre Deakd60c4472014-03-27 17:45:10 +02005098 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
5099 dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005100 return;
5101
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005102 /* disable/enable all currently active pipes while we change cdclk */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005103 for_each_intel_crtc(dev, intel_crtc)
Matt Roper83d65732015-02-25 13:12:16 -08005104 if (intel_crtc->base.state->enable)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005105 *prepare_pipes |= (1 << intel_crtc->pipe);
5106}
5107
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005108static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5109{
5110 unsigned int credits, default_credits;
5111
5112 if (IS_CHERRYVIEW(dev_priv))
5113 default_credits = PFI_CREDIT(12);
5114 else
5115 default_credits = PFI_CREDIT(8);
5116
5117 if (DIV_ROUND_CLOSEST(dev_priv->vlv_cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
5118 /* CHV suggested value is 31 or 63 */
5119 if (IS_CHERRYVIEW(dev_priv))
5120 credits = PFI_CREDIT_31;
5121 else
5122 credits = PFI_CREDIT(15);
5123 } else {
5124 credits = default_credits;
5125 }
5126
5127 /*
5128 * WA - write default credits before re-programming
5129 * FIXME: should we also set the resend bit here?
5130 */
5131 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5132 default_credits);
5133
5134 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5135 credits | PFI_CREDIT_RESEND);
5136
5137 /*
5138 * FIXME is this guaranteed to clear
5139 * immediately or should we poll for it?
5140 */
5141 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5142}
5143
Jesse Barnes30a970c2013-11-04 13:48:12 -08005144static void valleyview_modeset_global_resources(struct drm_device *dev)
5145{
5146 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005147 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005148 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5149
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005150 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005151 /*
5152 * FIXME: We can end up here with all power domains off, yet
5153 * with a CDCLK frequency other than the minimum. To account
5154 * for this take the PIPE-A power domain, which covers the HW
5155 * blocks needed for the following programming. This can be
5156 * removed once it's guaranteed that we get here either with
5157 * the minimum CDCLK set, or the required power domains
5158 * enabled.
5159 */
5160 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5161
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005162 if (IS_CHERRYVIEW(dev))
5163 cherryview_set_cdclk(dev, req_cdclk);
5164 else
5165 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005166
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005167 vlv_program_pfi_credits(dev_priv);
5168
Imre Deak738c05c2014-11-19 16:25:37 +02005169 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005170 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005171}
5172
Jesse Barnes89b667f2013-04-18 14:51:36 -07005173static void valleyview_crtc_enable(struct drm_crtc *crtc)
5174{
5175 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005176 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005177 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5178 struct intel_encoder *encoder;
5179 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005180 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005181
Matt Roper83d65732015-02-25 13:12:16 -08005182 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005183
5184 if (intel_crtc->active)
5185 return;
5186
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005187 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305188
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005189 if (!is_dsi) {
5190 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005191 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005192 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005193 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005194 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005195
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005196 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305197 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005198
5199 intel_set_pipe_timings(intel_crtc);
5200
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005201 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5202 struct drm_i915_private *dev_priv = dev->dev_private;
5203
5204 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5205 I915_WRITE(CHV_CANVAS(pipe), 0);
5206 }
5207
Daniel Vetter5b18e572014-04-24 23:55:06 +02005208 i9xx_set_pipeconf(intel_crtc);
5209
Jesse Barnes89b667f2013-04-18 14:51:36 -07005210 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005211
Daniel Vettera72e4c92014-09-30 10:56:47 +02005212 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005213
Jesse Barnes89b667f2013-04-18 14:51:36 -07005214 for_each_encoder_on_crtc(dev, crtc, encoder)
5215 if (encoder->pre_pll_enable)
5216 encoder->pre_pll_enable(encoder);
5217
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005218 if (!is_dsi) {
5219 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005220 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005221 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005222 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005223 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005224
5225 for_each_encoder_on_crtc(dev, crtc, encoder)
5226 if (encoder->pre_enable)
5227 encoder->pre_enable(encoder);
5228
Jesse Barnes2dd24552013-04-25 12:55:01 -07005229 i9xx_pfit_enable(intel_crtc);
5230
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005231 intel_crtc_load_lut(crtc);
5232
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005233 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005234 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005235
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005236 assert_vblank_disabled(crtc);
5237 drm_crtc_vblank_on(crtc);
5238
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005239 for_each_encoder_on_crtc(dev, crtc, encoder)
5240 encoder->enable(encoder);
5241
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005242 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005243
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005244 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005245 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005246}
5247
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005248static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5249{
5250 struct drm_device *dev = crtc->base.dev;
5251 struct drm_i915_private *dev_priv = dev->dev_private;
5252
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005253 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5254 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005255}
5256
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005257static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005258{
5259 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005260 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005262 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005263 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005264
Matt Roper83d65732015-02-25 13:12:16 -08005265 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02005266
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005267 if (intel_crtc->active)
5268 return;
5269
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005270 i9xx_set_pll_dividers(intel_crtc);
5271
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005272 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305273 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005274
5275 intel_set_pipe_timings(intel_crtc);
5276
Daniel Vetter5b18e572014-04-24 23:55:06 +02005277 i9xx_set_pipeconf(intel_crtc);
5278
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005279 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005280
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005281 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005282 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005283
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005284 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005285 if (encoder->pre_enable)
5286 encoder->pre_enable(encoder);
5287
Daniel Vetterf6736a12013-06-05 13:34:30 +02005288 i9xx_enable_pll(intel_crtc);
5289
Jesse Barnes2dd24552013-04-25 12:55:01 -07005290 i9xx_pfit_enable(intel_crtc);
5291
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005292 intel_crtc_load_lut(crtc);
5293
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005294 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005295 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005296
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005297 assert_vblank_disabled(crtc);
5298 drm_crtc_vblank_on(crtc);
5299
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005300 for_each_encoder_on_crtc(dev, crtc, encoder)
5301 encoder->enable(encoder);
5302
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005303 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005304
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005305 /*
5306 * Gen2 reports pipe underruns whenever all planes are disabled.
5307 * So don't enable underrun reporting before at least some planes
5308 * are enabled.
5309 * FIXME: Need to fix the logic to work when we turn off all planes
5310 * but leave the pipe running.
5311 */
5312 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005313 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005314
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005315 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005316 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005317}
5318
Daniel Vetter87476d62013-04-11 16:29:06 +02005319static void i9xx_pfit_disable(struct intel_crtc *crtc)
5320{
5321 struct drm_device *dev = crtc->base.dev;
5322 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02005323
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005324 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005325 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02005326
5327 assert_pipe_disabled(dev_priv, crtc->pipe);
5328
Daniel Vetter328d8e82013-05-08 10:36:31 +02005329 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5330 I915_READ(PFIT_CONTROL));
5331 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02005332}
5333
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005334static void i9xx_crtc_disable(struct drm_crtc *crtc)
5335{
5336 struct drm_device *dev = crtc->dev;
5337 struct drm_i915_private *dev_priv = dev->dev_private;
5338 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005339 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005340 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005341
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005342 if (!intel_crtc->active)
5343 return;
5344
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005345 /*
5346 * Gen2 reports pipe underruns whenever all planes are disabled.
5347 * So diasble underrun reporting before all the planes get disabled.
5348 * FIXME: Need to fix the logic to work when we turn off all planes
5349 * but leave the pipe running.
5350 */
5351 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005352 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005353
Imre Deak564ed192014-06-13 14:54:21 +03005354 /*
5355 * Vblank time updates from the shadow to live plane control register
5356 * are blocked if the memory self-refresh mode is active at that
5357 * moment. So to make sure the plane gets truly disabled, disable
5358 * first the self-refresh mode. The self-refresh enable bit in turn
5359 * will be checked/applied by the HW only at the next frame start
5360 * event which is after the vblank start event, so we need to have a
5361 * wait-for-vblank between disabling the plane and the pipe.
5362 */
5363 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005364 intel_crtc_disable_planes(crtc);
5365
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005366 /*
5367 * On gen2 planes are double buffered but the pipe isn't, so we must
5368 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03005369 * We also need to wait on all gmch platforms because of the
5370 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005371 */
Imre Deak564ed192014-06-13 14:54:21 +03005372 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005373
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005374 for_each_encoder_on_crtc(dev, crtc, encoder)
5375 encoder->disable(encoder);
5376
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005377 drm_crtc_vblank_off(crtc);
5378 assert_vblank_disabled(crtc);
5379
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005380 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005381
Daniel Vetter87476d62013-04-11 16:29:06 +02005382 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005383
Jesse Barnes89b667f2013-04-18 14:51:36 -07005384 for_each_encoder_on_crtc(dev, crtc, encoder)
5385 if (encoder->post_disable)
5386 encoder->post_disable(encoder);
5387
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005388 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005389 if (IS_CHERRYVIEW(dev))
5390 chv_disable_pll(dev_priv, pipe);
5391 else if (IS_VALLEYVIEW(dev))
5392 vlv_disable_pll(dev_priv, pipe);
5393 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03005394 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005395 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005396
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005397 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005398 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005399
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005400 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005401 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005402
Daniel Vetterefa96242014-04-24 23:55:02 +02005403 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005404 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02005405 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005406}
5407
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005408static void i9xx_crtc_off(struct drm_crtc *crtc)
5409{
5410}
5411
Borun Fub04c5bd2014-07-12 10:02:27 +05305412/* Master function to enable/disable CRTC and corresponding power wells */
5413void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01005414{
Chris Wilsoncdd59982010-09-08 16:30:16 +01005415 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005416 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005417 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005418 enum intel_display_power_domain domain;
5419 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005420
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005421 if (enable) {
5422 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005423 domains = get_crtc_power_domains(crtc);
5424 for_each_power_domain(domain, domains)
5425 intel_display_power_get(dev_priv, domain);
5426 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005427
5428 dev_priv->display.crtc_enable(crtc);
5429 }
5430 } else {
5431 if (intel_crtc->active) {
5432 dev_priv->display.crtc_disable(crtc);
5433
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005434 domains = intel_crtc->enabled_power_domains;
5435 for_each_power_domain(domain, domains)
5436 intel_display_power_put(dev_priv, domain);
5437 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005438 }
5439 }
Borun Fub04c5bd2014-07-12 10:02:27 +05305440}
5441
5442/**
5443 * Sets the power management mode of the pipe and plane.
5444 */
5445void intel_crtc_update_dpms(struct drm_crtc *crtc)
5446{
5447 struct drm_device *dev = crtc->dev;
5448 struct intel_encoder *intel_encoder;
5449 bool enable = false;
5450
5451 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5452 enable |= intel_encoder->connectors_active;
5453
5454 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005455}
5456
Daniel Vetter976f8a22012-07-08 22:34:21 +02005457static void intel_crtc_disable(struct drm_crtc *crtc)
5458{
5459 struct drm_device *dev = crtc->dev;
5460 struct drm_connector *connector;
5461 struct drm_i915_private *dev_priv = dev->dev_private;
5462
5463 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08005464 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005465
5466 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005467 dev_priv->display.off(crtc);
5468
Gustavo Padovan455a6802014-12-01 15:40:11 -08005469 crtc->primary->funcs->disable_plane(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005470
5471 /* Update computed state. */
5472 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5473 if (!connector->encoder || !connector->encoder->crtc)
5474 continue;
5475
5476 if (connector->encoder->crtc != crtc)
5477 continue;
5478
5479 connector->dpms = DRM_MODE_DPMS_OFF;
5480 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01005481 }
5482}
5483
Chris Wilsonea5b2132010-08-04 13:50:23 +01005484void intel_encoder_destroy(struct drm_encoder *encoder)
5485{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005486 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01005487
Chris Wilsonea5b2132010-08-04 13:50:23 +01005488 drm_encoder_cleanup(encoder);
5489 kfree(intel_encoder);
5490}
5491
Damien Lespiau92373292013-08-08 22:28:57 +01005492/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005493 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5494 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01005495static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005496{
5497 if (mode == DRM_MODE_DPMS_ON) {
5498 encoder->connectors_active = true;
5499
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005500 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005501 } else {
5502 encoder->connectors_active = false;
5503
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005504 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005505 }
5506}
5507
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005508/* Cross check the actual hw state with our own modeset state tracking (and it's
5509 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02005510static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005511{
5512 if (connector->get_hw_state(connector)) {
5513 struct intel_encoder *encoder = connector->encoder;
5514 struct drm_crtc *crtc;
5515 bool encoder_enabled;
5516 enum pipe pipe;
5517
5518 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5519 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03005520 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005521
Dave Airlie0e32b392014-05-02 14:02:48 +10005522 /* there is no real hw state for MST connectors */
5523 if (connector->mst_port)
5524 return;
5525
Rob Clarke2c719b2014-12-15 13:56:32 -05005526 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005527 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05005528 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005529 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005530
Dave Airlie36cd7442014-05-02 13:44:18 +10005531 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05005532 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10005533 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005534
Dave Airlie36cd7442014-05-02 13:44:18 +10005535 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05005536 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
5537 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10005538 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005539
Dave Airlie36cd7442014-05-02 13:44:18 +10005540 crtc = encoder->base.crtc;
5541
Matt Roper83d65732015-02-25 13:12:16 -08005542 I915_STATE_WARN(!crtc->state->enable,
5543 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05005544 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5545 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10005546 "encoder active on the wrong pipe\n");
5547 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005548 }
5549}
5550
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005551/* Even simpler default implementation, if there's really no special case to
5552 * consider. */
5553void intel_connector_dpms(struct drm_connector *connector, int mode)
5554{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005555 /* All the simple cases only support two dpms states. */
5556 if (mode != DRM_MODE_DPMS_ON)
5557 mode = DRM_MODE_DPMS_OFF;
5558
5559 if (mode == connector->dpms)
5560 return;
5561
5562 connector->dpms = mode;
5563
5564 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01005565 if (connector->encoder)
5566 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005567
Daniel Vetterb9805142012-08-31 17:37:33 +02005568 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005569}
5570
Daniel Vetterf0947c32012-07-02 13:10:34 +02005571/* Simple connector->get_hw_state implementation for encoders that support only
5572 * one connector and no cloning and hence the encoder state determines the state
5573 * of the connector. */
5574bool intel_connector_get_hw_state(struct intel_connector *connector)
5575{
Daniel Vetter24929352012-07-02 20:28:59 +02005576 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02005577 struct intel_encoder *encoder = connector->encoder;
5578
5579 return encoder->get_hw_state(encoder, &pipe);
5580}
5581
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02005582static int pipe_required_fdi_lanes(struct drm_device *dev, enum pipe pipe)
5583{
5584 struct intel_crtc *crtc =
5585 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5586
5587 if (crtc->base.state->enable &&
5588 crtc->config->has_pch_encoder)
5589 return crtc->config->fdi_lanes;
5590
5591 return 0;
5592}
5593
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005594static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005595 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005596{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005597 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5598 pipe_name(pipe), pipe_config->fdi_lanes);
5599 if (pipe_config->fdi_lanes > 4) {
5600 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5601 pipe_name(pipe), pipe_config->fdi_lanes);
5602 return false;
5603 }
5604
Paulo Zanonibafb6552013-11-02 21:07:44 -07005605 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005606 if (pipe_config->fdi_lanes > 2) {
5607 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5608 pipe_config->fdi_lanes);
5609 return false;
5610 } else {
5611 return true;
5612 }
5613 }
5614
5615 if (INTEL_INFO(dev)->num_pipes == 2)
5616 return true;
5617
5618 /* Ivybridge 3 pipe is really complicated */
5619 switch (pipe) {
5620 case PIPE_A:
5621 return true;
5622 case PIPE_B:
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02005623 if (pipe_config->fdi_lanes > 2 &&
5624 pipe_required_fdi_lanes(dev, PIPE_C) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005625 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5626 pipe_name(pipe), pipe_config->fdi_lanes);
5627 return false;
5628 }
5629 return true;
5630 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02005631 if (pipe_config->fdi_lanes > 2) {
5632 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
5633 pipe_name(pipe), pipe_config->fdi_lanes);
5634 return false;
5635 }
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02005636 if (pipe_required_fdi_lanes(dev, PIPE_B) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005637 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5638 return false;
5639 }
5640 return true;
5641 default:
5642 BUG();
5643 }
5644}
5645
Daniel Vettere29c22c2013-02-21 00:00:16 +01005646#define RETRY 1
5647static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005648 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02005649{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005650 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02005651 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02005652 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01005653 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005654
Daniel Vettere29c22c2013-02-21 00:00:16 +01005655retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02005656 /* FDI is a binary signal running at ~2.7GHz, encoding
5657 * each output octet as 10 bits. The actual frequency
5658 * is stored as a divider into a 100MHz clock, and the
5659 * mode pixel clock is stored in units of 1KHz.
5660 * Hence the bw of each lane in terms of the mode signal
5661 * is:
5662 */
5663 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5664
Damien Lespiau241bfc32013-09-25 16:45:37 +01005665 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005666
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005667 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005668 pipe_config->pipe_bpp);
5669
5670 pipe_config->fdi_lanes = lane;
5671
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005672 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005673 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005674
Daniel Vettere29c22c2013-02-21 00:00:16 +01005675 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5676 intel_crtc->pipe, pipe_config);
5677 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5678 pipe_config->pipe_bpp -= 2*3;
5679 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5680 pipe_config->pipe_bpp);
5681 needs_recompute = true;
5682 pipe_config->bw_constrained = true;
5683
5684 goto retry;
5685 }
5686
5687 if (needs_recompute)
5688 return RETRY;
5689
5690 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005691}
5692
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005693static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005694 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005695{
Jani Nikulad330a952014-01-21 11:24:25 +02005696 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03005697 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07005698 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005699}
5700
Daniel Vettera43f6e02013-06-07 23:10:32 +02005701static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005702 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08005703{
Daniel Vettera43f6e02013-06-07 23:10:32 +02005704 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02005705 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02005706 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01005707
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005708 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005709 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005710 int clock_limit =
5711 dev_priv->display.get_display_clock_speed(dev);
5712
5713 /*
5714 * Enable pixel doubling when the dot clock
5715 * is > 90% of the (display) core speed.
5716 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03005717 * GDG double wide on either pipe,
5718 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005719 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03005720 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01005721 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005722 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005723 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005724 }
5725
Damien Lespiau241bfc32013-09-25 16:45:37 +01005726 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005727 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005728 }
Chris Wilson89749352010-09-12 18:25:19 +01005729
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005730 /*
5731 * Pipe horizontal size must be even in:
5732 * - DVO ganged mode
5733 * - LVDS dual channel mode
5734 * - Double wide pipe
5735 */
Ander Conselvan de Oliveirab4f2bf42015-02-26 09:44:45 +02005736 if ((intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005737 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5738 pipe_config->pipe_src_w &= ~1;
5739
Damien Lespiau8693a822013-05-03 18:48:11 +01005740 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5741 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03005742 */
5743 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5744 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005745 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03005746
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005747 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005748 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005749 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005750 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5751 * for lvds. */
5752 pipe_config->pipe_bpp = 8*3;
5753 }
5754
Damien Lespiauf5adf942013-06-24 18:29:34 +01005755 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005756 hsw_compute_ips_config(crtc, pipe_config);
5757
Daniel Vetter877d48d2013-04-19 11:24:43 +02005758 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02005759 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02005760
Daniel Vettere29c22c2013-02-21 00:00:16 +01005761 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005762}
5763
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005764static int valleyview_get_display_clock_speed(struct drm_device *dev)
5765{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005766 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005767 u32 val;
5768 int divider;
5769
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005770 if (dev_priv->hpll_freq == 0)
5771 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
5772
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005773 mutex_lock(&dev_priv->dpio_lock);
5774 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5775 mutex_unlock(&dev_priv->dpio_lock);
5776
5777 divider = val & DISPLAY_FREQUENCY_VALUES;
5778
Ville Syrjälä7d007f42014-06-13 13:37:53 +03005779 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5780 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5781 "cdclk change in progress\n");
5782
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005783 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005784}
5785
Jesse Barnese70236a2009-09-21 10:42:27 -07005786static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08005787{
Jesse Barnese70236a2009-09-21 10:42:27 -07005788 return 400000;
5789}
Jesse Barnes79e53942008-11-07 14:24:08 -08005790
Jesse Barnese70236a2009-09-21 10:42:27 -07005791static int i915_get_display_clock_speed(struct drm_device *dev)
5792{
5793 return 333000;
5794}
Jesse Barnes79e53942008-11-07 14:24:08 -08005795
Jesse Barnese70236a2009-09-21 10:42:27 -07005796static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5797{
5798 return 200000;
5799}
Jesse Barnes79e53942008-11-07 14:24:08 -08005800
Daniel Vetter257a7ff2013-07-26 08:35:42 +02005801static int pnv_get_display_clock_speed(struct drm_device *dev)
5802{
5803 u16 gcfgc = 0;
5804
5805 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5806
5807 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5808 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5809 return 267000;
5810 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5811 return 333000;
5812 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5813 return 444000;
5814 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5815 return 200000;
5816 default:
5817 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5818 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5819 return 133000;
5820 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5821 return 167000;
5822 }
5823}
5824
Jesse Barnese70236a2009-09-21 10:42:27 -07005825static int i915gm_get_display_clock_speed(struct drm_device *dev)
5826{
5827 u16 gcfgc = 0;
5828
5829 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5830
5831 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08005832 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07005833 else {
5834 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5835 case GC_DISPLAY_CLOCK_333_MHZ:
5836 return 333000;
5837 default:
5838 case GC_DISPLAY_CLOCK_190_200_MHZ:
5839 return 190000;
5840 }
5841 }
5842}
Jesse Barnes79e53942008-11-07 14:24:08 -08005843
Jesse Barnese70236a2009-09-21 10:42:27 -07005844static int i865_get_display_clock_speed(struct drm_device *dev)
5845{
5846 return 266000;
5847}
5848
5849static int i855_get_display_clock_speed(struct drm_device *dev)
5850{
5851 u16 hpllcc = 0;
5852 /* Assume that the hardware is in the high speed state. This
5853 * should be the default.
5854 */
5855 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5856 case GC_CLOCK_133_200:
5857 case GC_CLOCK_100_200:
5858 return 200000;
5859 case GC_CLOCK_166_250:
5860 return 250000;
5861 case GC_CLOCK_100_133:
5862 return 133000;
5863 }
5864
5865 /* Shouldn't happen */
5866 return 0;
5867}
5868
5869static int i830_get_display_clock_speed(struct drm_device *dev)
5870{
5871 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08005872}
5873
Zhenyu Wang2c072452009-06-05 15:38:42 +08005874static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005875intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005876{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005877 while (*num > DATA_LINK_M_N_MASK ||
5878 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08005879 *num >>= 1;
5880 *den >>= 1;
5881 }
5882}
5883
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005884static void compute_m_n(unsigned int m, unsigned int n,
5885 uint32_t *ret_m, uint32_t *ret_n)
5886{
5887 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5888 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5889 intel_reduce_m_n_ratio(ret_m, ret_n);
5890}
5891
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005892void
5893intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5894 int pixel_clock, int link_clock,
5895 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005896{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005897 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005898
5899 compute_m_n(bits_per_pixel * pixel_clock,
5900 link_clock * nlanes * 8,
5901 &m_n->gmch_m, &m_n->gmch_n);
5902
5903 compute_m_n(pixel_clock, link_clock,
5904 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005905}
5906
Chris Wilsona7615032011-01-12 17:04:08 +00005907static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5908{
Jani Nikulad330a952014-01-21 11:24:25 +02005909 if (i915.panel_use_ssc >= 0)
5910 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005911 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07005912 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00005913}
5914
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005915static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005916{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005917 struct drm_device *dev = crtc->base.dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005918 struct drm_i915_private *dev_priv = dev->dev_private;
5919 int refclk;
5920
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005921 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02005922 refclk = 100000;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02005923 } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005924 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005925 refclk = dev_priv->vbt.lvds_ssc_freq;
5926 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005927 } else if (!IS_GEN2(dev)) {
5928 refclk = 96000;
5929 } else {
5930 refclk = 48000;
5931 }
5932
5933 return refclk;
5934}
5935
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005936static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005937{
Daniel Vetter7df00d72013-05-21 21:54:55 +02005938 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005939}
Daniel Vetterf47709a2013-03-28 10:42:02 +01005940
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005941static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5942{
5943 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005944}
5945
Daniel Vetterf47709a2013-03-28 10:42:02 +01005946static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005947 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08005948 intel_clock_t *reduced_clock)
5949{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005950 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005951 u32 fp, fp2 = 0;
5952
5953 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005954 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005955 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005956 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005957 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005958 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005959 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005960 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005961 }
5962
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005963 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005964
Daniel Vetterf47709a2013-03-28 10:42:02 +01005965 crtc->lowfreq_avail = false;
Bob Paauwee1f234b2014-11-11 09:29:18 -08005966 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
Jani Nikulad330a952014-01-21 11:24:25 +02005967 reduced_clock && i915.powersave) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005968 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005969 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005970 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005971 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005972 }
5973}
5974
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005975static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5976 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07005977{
5978 u32 reg_val;
5979
5980 /*
5981 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5982 * and set it to a reasonable value instead.
5983 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005984 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005985 reg_val &= 0xffffff00;
5986 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005987 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005988
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005989 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005990 reg_val &= 0x8cffffff;
5991 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005992 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005993
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005994 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005995 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005996 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005997
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005998 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005999 reg_val &= 0x00ffffff;
6000 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006001 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006002}
6003
Daniel Vetterb5518422013-05-03 11:49:48 +02006004static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6005 struct intel_link_m_n *m_n)
6006{
6007 struct drm_device *dev = crtc->base.dev;
6008 struct drm_i915_private *dev_priv = dev->dev_private;
6009 int pipe = crtc->pipe;
6010
Daniel Vettere3b95f12013-05-03 11:49:49 +02006011 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6012 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6013 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6014 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006015}
6016
6017static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006018 struct intel_link_m_n *m_n,
6019 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006020{
6021 struct drm_device *dev = crtc->base.dev;
6022 struct drm_i915_private *dev_priv = dev->dev_private;
6023 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006024 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006025
6026 if (INTEL_INFO(dev)->gen >= 5) {
6027 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6028 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6029 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6030 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006031 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6032 * for gen < 8) and if DRRS is supported (to make sure the
6033 * registers are not unnecessarily accessed).
6034 */
Durgadoss R44395bf2015-02-13 15:33:02 +05306035 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006036 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006037 I915_WRITE(PIPE_DATA_M2(transcoder),
6038 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6039 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6040 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6041 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6042 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006043 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006044 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6045 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6046 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6047 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006048 }
6049}
6050
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306051void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006052{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306053 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6054
6055 if (m_n == M1_N1) {
6056 dp_m_n = &crtc->config->dp_m_n;
6057 dp_m2_n2 = &crtc->config->dp_m2_n2;
6058 } else if (m_n == M2_N2) {
6059
6060 /*
6061 * M2_N2 registers are not supported. Hence m2_n2 divider value
6062 * needs to be programmed into M1_N1.
6063 */
6064 dp_m_n = &crtc->config->dp_m2_n2;
6065 } else {
6066 DRM_ERROR("Unsupported divider value\n");
6067 return;
6068 }
6069
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006070 if (crtc->config->has_pch_encoder)
6071 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006072 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306073 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006074}
6075
Ville Syrjäläd288f652014-10-28 13:20:22 +02006076static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006077 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006078{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006079 u32 dpll, dpll_md;
6080
6081 /*
6082 * Enable DPIO clock input. We should never disable the reference
6083 * clock for pipe B, since VGA hotplug / manual detection depends
6084 * on it.
6085 */
6086 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6087 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6088 /* We should never disable this, set it here for state tracking */
6089 if (crtc->pipe == PIPE_B)
6090 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6091 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006092 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006093
Ville Syrjäläd288f652014-10-28 13:20:22 +02006094 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006095 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006096 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006097}
6098
Ville Syrjäläd288f652014-10-28 13:20:22 +02006099static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006100 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006101{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006102 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006103 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006104 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006105 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006106 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006107 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006108
Daniel Vetter09153002012-12-12 14:06:44 +01006109 mutex_lock(&dev_priv->dpio_lock);
6110
Ville Syrjäläd288f652014-10-28 13:20:22 +02006111 bestn = pipe_config->dpll.n;
6112 bestm1 = pipe_config->dpll.m1;
6113 bestm2 = pipe_config->dpll.m2;
6114 bestp1 = pipe_config->dpll.p1;
6115 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006116
Jesse Barnes89b667f2013-04-18 14:51:36 -07006117 /* See eDP HDMI DPIO driver vbios notes doc */
6118
6119 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006120 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006121 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006122
6123 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006124 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006125
6126 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006127 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006128 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006129 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006130
6131 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006132 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006133
6134 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006135 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6136 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6137 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006138 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006139
6140 /*
6141 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6142 * but we don't support that).
6143 * Note: don't use the DAC post divider as it seems unstable.
6144 */
6145 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006146 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006147
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006148 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006149 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006150
Jesse Barnes89b667f2013-04-18 14:51:36 -07006151 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006152 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006153 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6154 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006155 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03006156 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006157 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006158 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006159 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006160
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02006161 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006162 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006163 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006164 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006165 0x0df40000);
6166 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006167 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006168 0x0df70000);
6169 } else { /* HDMI or VGA */
6170 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006171 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006172 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006173 0x0df70000);
6174 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006175 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006176 0x0df40000);
6177 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006178
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006179 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006180 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006181 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6182 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006183 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006184 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006185
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006186 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01006187 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006188}
6189
Ville Syrjäläd288f652014-10-28 13:20:22 +02006190static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006191 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006192{
Ville Syrjäläd288f652014-10-28 13:20:22 +02006193 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006194 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6195 DPLL_VCO_ENABLE;
6196 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006197 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006198
Ville Syrjäläd288f652014-10-28 13:20:22 +02006199 pipe_config->dpll_hw_state.dpll_md =
6200 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006201}
6202
Ville Syrjäläd288f652014-10-28 13:20:22 +02006203static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006204 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006205{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006206 struct drm_device *dev = crtc->base.dev;
6207 struct drm_i915_private *dev_priv = dev->dev_private;
6208 int pipe = crtc->pipe;
6209 int dpll_reg = DPLL(crtc->pipe);
6210 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306211 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006212 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306213 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306214 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006215
Ville Syrjäläd288f652014-10-28 13:20:22 +02006216 bestn = pipe_config->dpll.n;
6217 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6218 bestm1 = pipe_config->dpll.m1;
6219 bestm2 = pipe_config->dpll.m2 >> 22;
6220 bestp1 = pipe_config->dpll.p1;
6221 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306222 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306223 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306224 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006225
6226 /*
6227 * Enable Refclk and SSC
6228 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006229 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02006230 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006231
6232 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006233
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006234 /* p1 and p2 divider */
6235 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6236 5 << DPIO_CHV_S1_DIV_SHIFT |
6237 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6238 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6239 1 << DPIO_CHV_K_DIV_SHIFT);
6240
6241 /* Feedback post-divider - m2 */
6242 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6243
6244 /* Feedback refclk divider - n and m1 */
6245 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6246 DPIO_CHV_M1_DIV_BY_2 |
6247 1 << DPIO_CHV_N_DIV_SHIFT);
6248
6249 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306250 if (bestm2_frac)
6251 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006252
6253 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306254 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6255 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6256 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6257 if (bestm2_frac)
6258 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6259 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006260
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05306261 /* Program digital lock detect threshold */
6262 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6263 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6264 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6265 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6266 if (!bestm2_frac)
6267 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6268 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6269
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006270 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306271 if (vco == 5400000) {
6272 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6273 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6274 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6275 tribuf_calcntr = 0x9;
6276 } else if (vco <= 6200000) {
6277 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6278 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6279 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6280 tribuf_calcntr = 0x9;
6281 } else if (vco <= 6480000) {
6282 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6283 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6284 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6285 tribuf_calcntr = 0x8;
6286 } else {
6287 /* Not supported. Apply the same limits as in the max case */
6288 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6289 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6290 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6291 tribuf_calcntr = 0;
6292 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006293 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6294
Ville Syrjälä968040b2015-03-11 22:52:08 +02006295 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306296 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6297 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6298 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6299
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006300 /* AFC Recal */
6301 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6302 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6303 DPIO_AFC_RECAL);
6304
6305 mutex_unlock(&dev_priv->dpio_lock);
6306}
6307
Ville Syrjäläd288f652014-10-28 13:20:22 +02006308/**
6309 * vlv_force_pll_on - forcibly enable just the PLL
6310 * @dev_priv: i915 private structure
6311 * @pipe: pipe PLL to enable
6312 * @dpll: PLL configuration
6313 *
6314 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6315 * in cases where we need the PLL enabled even when @pipe is not going to
6316 * be enabled.
6317 */
6318void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
6319 const struct dpll *dpll)
6320{
6321 struct intel_crtc *crtc =
6322 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006323 struct intel_crtc_state pipe_config = {
Ville Syrjäläd288f652014-10-28 13:20:22 +02006324 .pixel_multiplier = 1,
6325 .dpll = *dpll,
6326 };
6327
6328 if (IS_CHERRYVIEW(dev)) {
6329 chv_update_pll(crtc, &pipe_config);
6330 chv_prepare_pll(crtc, &pipe_config);
6331 chv_enable_pll(crtc, &pipe_config);
6332 } else {
6333 vlv_update_pll(crtc, &pipe_config);
6334 vlv_prepare_pll(crtc, &pipe_config);
6335 vlv_enable_pll(crtc, &pipe_config);
6336 }
6337}
6338
6339/**
6340 * vlv_force_pll_off - forcibly disable just the PLL
6341 * @dev_priv: i915 private structure
6342 * @pipe: pipe PLL to disable
6343 *
6344 * Disable the PLL for @pipe. To be used in cases where we need
6345 * the PLL enabled even when @pipe is not going to be enabled.
6346 */
6347void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
6348{
6349 if (IS_CHERRYVIEW(dev))
6350 chv_disable_pll(to_i915(dev), pipe);
6351 else
6352 vlv_disable_pll(to_i915(dev), pipe);
6353}
6354
Daniel Vetterf47709a2013-03-28 10:42:02 +01006355static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006356 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01006357 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006358 int num_connectors)
6359{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006360 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006361 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006362 u32 dpll;
6363 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006364 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006365
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006366 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306367
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006368 is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
6369 intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006370
6371 dpll = DPLL_VGA_MODE_DIS;
6372
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006373 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006374 dpll |= DPLLB_MODE_LVDS;
6375 else
6376 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006377
Daniel Vetteref1b4602013-06-01 17:17:04 +02006378 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006379 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02006380 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006381 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02006382
6383 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006384 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006385
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006386 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006387 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006388
6389 /* compute bitmask from p1 value */
6390 if (IS_PINEVIEW(dev))
6391 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6392 else {
6393 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6394 if (IS_G4X(dev) && reduced_clock)
6395 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6396 }
6397 switch (clock->p2) {
6398 case 5:
6399 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6400 break;
6401 case 7:
6402 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6403 break;
6404 case 10:
6405 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6406 break;
6407 case 14:
6408 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6409 break;
6410 }
6411 if (INTEL_INFO(dev)->gen >= 4)
6412 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6413
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006414 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006415 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006416 else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006417 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6418 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6419 else
6420 dpll |= PLL_REF_INPUT_DREFCLK;
6421
6422 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006423 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006424
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006425 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006426 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02006427 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006428 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006429 }
6430}
6431
Daniel Vetterf47709a2013-03-28 10:42:02 +01006432static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006433 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01006434 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006435 int num_connectors)
6436{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006437 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006438 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006439 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006440 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006441
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006442 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306443
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006444 dpll = DPLL_VGA_MODE_DIS;
6445
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006446 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006447 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6448 } else {
6449 if (clock->p1 == 2)
6450 dpll |= PLL_P1_DIVIDE_BY_TWO;
6451 else
6452 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6453 if (clock->p2 == 4)
6454 dpll |= PLL_P2_DIVIDE_BY_4;
6455 }
6456
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006457 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02006458 dpll |= DPLL_DVO_2X_MODE;
6459
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006460 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006461 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6462 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6463 else
6464 dpll |= PLL_REF_INPUT_DREFCLK;
6465
6466 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006467 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006468}
6469
Daniel Vetter8a654f32013-06-01 17:16:22 +02006470static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006471{
6472 struct drm_device *dev = intel_crtc->base.dev;
6473 struct drm_i915_private *dev_priv = dev->dev_private;
6474 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006475 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02006476 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006477 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006478 uint32_t crtc_vtotal, crtc_vblank_end;
6479 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006480
6481 /* We need to be careful not to changed the adjusted mode, for otherwise
6482 * the hw state checker will get angry at the mismatch. */
6483 crtc_vtotal = adjusted_mode->crtc_vtotal;
6484 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006485
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006486 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006487 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006488 crtc_vtotal -= 1;
6489 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006490
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006491 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006492 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6493 else
6494 vsyncshift = adjusted_mode->crtc_hsync_start -
6495 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006496 if (vsyncshift < 0)
6497 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006498 }
6499
6500 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006501 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006502
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006503 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006504 (adjusted_mode->crtc_hdisplay - 1) |
6505 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006506 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006507 (adjusted_mode->crtc_hblank_start - 1) |
6508 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006509 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006510 (adjusted_mode->crtc_hsync_start - 1) |
6511 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6512
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006513 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006514 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006515 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006516 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006517 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006518 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006519 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006520 (adjusted_mode->crtc_vsync_start - 1) |
6521 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6522
Paulo Zanonib5e508d2012-10-24 11:34:43 -02006523 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6524 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6525 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6526 * bits. */
6527 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6528 (pipe == PIPE_B || pipe == PIPE_C))
6529 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6530
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006531 /* pipesrc controls the size that is scaled from, which should
6532 * always be the user's requested size.
6533 */
6534 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006535 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6536 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006537}
6538
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006539static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006540 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006541{
6542 struct drm_device *dev = crtc->base.dev;
6543 struct drm_i915_private *dev_priv = dev->dev_private;
6544 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6545 uint32_t tmp;
6546
6547 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006548 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6549 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006550 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006551 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6552 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006553 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006554 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6555 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006556
6557 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006558 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6559 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006560 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006561 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6562 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006563 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006564 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6565 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006566
6567 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006568 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6569 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
6570 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006571 }
6572
6573 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03006574 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6575 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6576
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006577 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
6578 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006579}
6580
Daniel Vetterf6a83282014-02-11 15:28:57 -08006581void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006582 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03006583{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006584 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
6585 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
6586 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
6587 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006588
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006589 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
6590 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
6591 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
6592 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006593
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006594 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006595
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006596 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
6597 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006598}
6599
Daniel Vetter84b046f2013-02-19 18:48:54 +01006600static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6601{
6602 struct drm_device *dev = intel_crtc->base.dev;
6603 struct drm_i915_private *dev_priv = dev->dev_private;
6604 uint32_t pipeconf;
6605
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006606 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006607
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03006608 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6609 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6610 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02006611
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006612 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006613 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006614
Daniel Vetterff9ce462013-04-24 14:57:17 +02006615 /* only g4x and later have fancy bpc/dither controls */
6616 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02006617 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006618 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02006619 pipeconf |= PIPECONF_DITHER_EN |
6620 PIPECONF_DITHER_TYPE_SP;
6621
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006622 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02006623 case 18:
6624 pipeconf |= PIPECONF_6BPC;
6625 break;
6626 case 24:
6627 pipeconf |= PIPECONF_8BPC;
6628 break;
6629 case 30:
6630 pipeconf |= PIPECONF_10BPC;
6631 break;
6632 default:
6633 /* Case prevented by intel_choose_pipe_bpp_dither. */
6634 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01006635 }
6636 }
6637
6638 if (HAS_PIPE_CXSR(dev)) {
6639 if (intel_crtc->lowfreq_avail) {
6640 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6641 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6642 } else {
6643 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01006644 }
6645 }
6646
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006647 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006648 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006649 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006650 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6651 else
6652 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6653 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01006654 pipeconf |= PIPECONF_PROGRESSIVE;
6655
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006656 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006657 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03006658
Daniel Vetter84b046f2013-02-19 18:48:54 +01006659 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6660 POSTING_READ(PIPECONF(intel_crtc->pipe));
6661}
6662
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006663static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
6664 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08006665{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006666 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08006667 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07006668 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07006669 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02006670 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006671 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01006672 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08006673 const intel_limit_t *limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08006674
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006675 for_each_intel_encoder(dev, encoder) {
6676 if (encoder->new_crtc != crtc)
6677 continue;
6678
Chris Wilson5eddb702010-09-11 13:48:45 +01006679 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006680 case INTEL_OUTPUT_LVDS:
6681 is_lvds = true;
6682 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006683 case INTEL_OUTPUT_DSI:
6684 is_dsi = true;
6685 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02006686 default:
6687 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006688 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006689
Eric Anholtc751ce42010-03-25 11:48:48 -07006690 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08006691 }
6692
Jani Nikulaf2335332013-09-13 11:03:09 +03006693 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02006694 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006695
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006696 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006697 refclk = i9xx_get_refclk(crtc, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03006698
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006699 /*
6700 * Returns a set of divisors for the desired target clock with
6701 * the given refclk, or FALSE. The returned values represent
6702 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6703 * 2) / p1 / p2.
6704 */
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006705 limit = intel_limit(crtc, refclk);
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006706 ok = dev_priv->display.find_dpll(limit, crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006707 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006708 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03006709 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006710 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6711 return -EINVAL;
6712 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006713
Jani Nikulaf2335332013-09-13 11:03:09 +03006714 if (is_lvds && dev_priv->lvds_downclock_avail) {
6715 /*
6716 * Ensure we match the reduced clock's P to the target
6717 * clock. If the clocks don't match, we can't switch
6718 * the display clock by using the FP0/FP1. In such case
6719 * we will disable the LVDS downclock feature.
6720 */
6721 has_reduced_clock =
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006722 dev_priv->display.find_dpll(limit, crtc,
Jani Nikulaf2335332013-09-13 11:03:09 +03006723 dev_priv->lvds_downclock,
6724 refclk, &clock,
6725 &reduced_clock);
6726 }
6727 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006728 crtc_state->dpll.n = clock.n;
6729 crtc_state->dpll.m1 = clock.m1;
6730 crtc_state->dpll.m2 = clock.m2;
6731 crtc_state->dpll.p1 = clock.p1;
6732 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006733 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006734
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006735 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006736 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306737 has_reduced_clock ? &reduced_clock : NULL,
6738 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006739 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006740 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006741 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006742 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006743 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006744 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006745 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02006746 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006747 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006748
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02006749 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07006750}
6751
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006752static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006753 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006754{
6755 struct drm_device *dev = crtc->base.dev;
6756 struct drm_i915_private *dev_priv = dev->dev_private;
6757 uint32_t tmp;
6758
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02006759 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6760 return;
6761
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006762 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02006763 if (!(tmp & PFIT_ENABLE))
6764 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006765
Daniel Vetter06922822013-07-11 13:35:40 +02006766 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006767 if (INTEL_INFO(dev)->gen < 4) {
6768 if (crtc->pipe != PIPE_B)
6769 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006770 } else {
6771 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6772 return;
6773 }
6774
Daniel Vetter06922822013-07-11 13:35:40 +02006775 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006776 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6777 if (INTEL_INFO(dev)->gen < 5)
6778 pipe_config->gmch_pfit.lvds_border_bits =
6779 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6780}
6781
Jesse Barnesacbec812013-09-20 11:29:32 -07006782static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006783 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07006784{
6785 struct drm_device *dev = crtc->base.dev;
6786 struct drm_i915_private *dev_priv = dev->dev_private;
6787 int pipe = pipe_config->cpu_transcoder;
6788 intel_clock_t clock;
6789 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07006790 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07006791
Shobhit Kumarf573de52014-07-30 20:32:37 +05306792 /* In case of MIPI DPLL will not even be used */
6793 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6794 return;
6795
Jesse Barnesacbec812013-09-20 11:29:32 -07006796 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006797 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07006798 mutex_unlock(&dev_priv->dpio_lock);
6799
6800 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6801 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6802 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6803 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6804 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6805
Ville Syrjäläf6466282013-10-14 14:50:31 +03006806 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07006807
Ville Syrjäläf6466282013-10-14 14:50:31 +03006808 /* clock.dot is the fast clock */
6809 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07006810}
6811
Damien Lespiau5724dbd2015-01-20 12:51:52 +00006812static void
6813i9xx_get_initial_plane_config(struct intel_crtc *crtc,
6814 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006815{
6816 struct drm_device *dev = crtc->base.dev;
6817 struct drm_i915_private *dev_priv = dev->dev_private;
6818 u32 val, base, offset;
6819 int pipe = crtc->pipe, plane = crtc->plane;
6820 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00006821 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006822 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00006823 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006824
Damien Lespiau42a7b082015-02-05 19:35:13 +00006825 val = I915_READ(DSPCNTR(plane));
6826 if (!(val & DISPLAY_PLANE_ENABLE))
6827 return;
6828
Damien Lespiaud9806c92015-01-21 14:07:19 +00006829 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00006830 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006831 DRM_DEBUG_KMS("failed to alloc fb\n");
6832 return;
6833 }
6834
Damien Lespiau1b842c82015-01-21 13:50:54 +00006835 fb = &intel_fb->base;
6836
Daniel Vetter18c52472015-02-10 17:16:09 +00006837 if (INTEL_INFO(dev)->gen >= 4) {
6838 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00006839 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00006840 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
6841 }
6842 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006843
6844 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00006845 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006846 fb->pixel_format = fourcc;
6847 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006848
6849 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00006850 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006851 offset = I915_READ(DSPTILEOFF(plane));
6852 else
6853 offset = I915_READ(DSPLINOFF(plane));
6854 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6855 } else {
6856 base = I915_READ(DSPADDR(plane));
6857 }
6858 plane_config->base = base;
6859
6860 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006861 fb->width = ((val >> 16) & 0xfff) + 1;
6862 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006863
6864 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006865 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006866
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006867 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00006868 fb->pixel_format,
6869 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006870
Daniel Vetterf37b5c22015-02-10 23:12:27 +01006871 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006872
Damien Lespiau2844a922015-01-20 12:51:48 +00006873 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
6874 pipe_name(pipe), plane, fb->width, fb->height,
6875 fb->bits_per_pixel, base, fb->pitches[0],
6876 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006877
Damien Lespiau2d140302015-02-05 17:22:18 +00006878 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006879}
6880
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006881static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006882 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006883{
6884 struct drm_device *dev = crtc->base.dev;
6885 struct drm_i915_private *dev_priv = dev->dev_private;
6886 int pipe = pipe_config->cpu_transcoder;
6887 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6888 intel_clock_t clock;
6889 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6890 int refclk = 100000;
6891
6892 mutex_lock(&dev_priv->dpio_lock);
6893 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6894 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6895 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6896 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6897 mutex_unlock(&dev_priv->dpio_lock);
6898
6899 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6900 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6901 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6902 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6903 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6904
6905 chv_clock(refclk, &clock);
6906
6907 /* clock.dot is the fast clock */
6908 pipe_config->port_clock = clock.dot / 5;
6909}
6910
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006911static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006912 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006913{
6914 struct drm_device *dev = crtc->base.dev;
6915 struct drm_i915_private *dev_priv = dev->dev_private;
6916 uint32_t tmp;
6917
Daniel Vetterf458ebb2014-09-30 10:56:39 +02006918 if (!intel_display_power_is_enabled(dev_priv,
6919 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02006920 return false;
6921
Daniel Vettere143a212013-07-04 12:01:15 +02006922 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006923 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006924
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006925 tmp = I915_READ(PIPECONF(crtc->pipe));
6926 if (!(tmp & PIPECONF_ENABLE))
6927 return false;
6928
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006929 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6930 switch (tmp & PIPECONF_BPC_MASK) {
6931 case PIPECONF_6BPC:
6932 pipe_config->pipe_bpp = 18;
6933 break;
6934 case PIPECONF_8BPC:
6935 pipe_config->pipe_bpp = 24;
6936 break;
6937 case PIPECONF_10BPC:
6938 pipe_config->pipe_bpp = 30;
6939 break;
6940 default:
6941 break;
6942 }
6943 }
6944
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02006945 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6946 pipe_config->limited_color_range = true;
6947
Ville Syrjälä282740f2013-09-04 18:30:03 +03006948 if (INTEL_INFO(dev)->gen < 4)
6949 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6950
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006951 intel_get_pipe_timings(crtc, pipe_config);
6952
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006953 i9xx_get_pfit_config(crtc, pipe_config);
6954
Daniel Vetter6c49f242013-06-06 12:45:25 +02006955 if (INTEL_INFO(dev)->gen >= 4) {
6956 tmp = I915_READ(DPLL_MD(crtc->pipe));
6957 pipe_config->pixel_multiplier =
6958 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6959 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006960 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02006961 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6962 tmp = I915_READ(DPLL(crtc->pipe));
6963 pipe_config->pixel_multiplier =
6964 ((tmp & SDVO_MULTIPLIER_MASK)
6965 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6966 } else {
6967 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6968 * port and will be fixed up in the encoder->get_config
6969 * function. */
6970 pipe_config->pixel_multiplier = 1;
6971 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006972 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6973 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006974 /*
6975 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
6976 * on 830. Filter it out here so that we don't
6977 * report errors due to that.
6978 */
6979 if (IS_I830(dev))
6980 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
6981
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006982 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6983 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03006984 } else {
6985 /* Mask out read-only status bits. */
6986 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6987 DPLL_PORTC_READY_MASK |
6988 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006989 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02006990
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006991 if (IS_CHERRYVIEW(dev))
6992 chv_crtc_clock_get(crtc, pipe_config);
6993 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07006994 vlv_crtc_clock_get(crtc, pipe_config);
6995 else
6996 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03006997
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006998 return true;
6999}
7000
Paulo Zanonidde86e22012-12-01 12:04:25 -02007001static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007002{
7003 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007004 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007005 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007006 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007007 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007008 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007009 bool has_ck505 = false;
7010 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007011
7012 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01007013 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007014 switch (encoder->type) {
7015 case INTEL_OUTPUT_LVDS:
7016 has_panel = true;
7017 has_lvds = true;
7018 break;
7019 case INTEL_OUTPUT_EDP:
7020 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03007021 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007022 has_cpu_edp = true;
7023 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007024 default:
7025 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007026 }
7027 }
7028
Keith Packard99eb6a02011-09-26 14:29:12 -07007029 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007030 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007031 can_ssc = has_ck505;
7032 } else {
7033 has_ck505 = false;
7034 can_ssc = true;
7035 }
7036
Imre Deak2de69052013-05-08 13:14:04 +03007037 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7038 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007039
7040 /* Ironlake: try to setup display ref clock before DPLL
7041 * enabling. This is only under driver's control after
7042 * PCH B stepping, previous chipset stepping should be
7043 * ignoring this setting.
7044 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007045 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007046
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007047 /* As we must carefully and slowly disable/enable each source in turn,
7048 * compute the final state we want first and check if we need to
7049 * make any changes at all.
7050 */
7051 final = val;
7052 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007053 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007054 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007055 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007056 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7057
7058 final &= ~DREF_SSC_SOURCE_MASK;
7059 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7060 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007061
Keith Packard199e5d72011-09-22 12:01:57 -07007062 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007063 final |= DREF_SSC_SOURCE_ENABLE;
7064
7065 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7066 final |= DREF_SSC1_ENABLE;
7067
7068 if (has_cpu_edp) {
7069 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7070 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7071 else
7072 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7073 } else
7074 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7075 } else {
7076 final |= DREF_SSC_SOURCE_DISABLE;
7077 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7078 }
7079
7080 if (final == val)
7081 return;
7082
7083 /* Always enable nonspread source */
7084 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7085
7086 if (has_ck505)
7087 val |= DREF_NONSPREAD_CK505_ENABLE;
7088 else
7089 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7090
7091 if (has_panel) {
7092 val &= ~DREF_SSC_SOURCE_MASK;
7093 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007094
Keith Packard199e5d72011-09-22 12:01:57 -07007095 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007096 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007097 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007098 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007099 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007100 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007101
7102 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007103 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007104 POSTING_READ(PCH_DREF_CONTROL);
7105 udelay(200);
7106
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007107 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007108
7109 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007110 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007111 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007112 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007113 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007114 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007115 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007116 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007117 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007118
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007119 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007120 POSTING_READ(PCH_DREF_CONTROL);
7121 udelay(200);
7122 } else {
7123 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7124
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007125 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007126
7127 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007128 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007129
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007130 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007131 POSTING_READ(PCH_DREF_CONTROL);
7132 udelay(200);
7133
7134 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007135 val &= ~DREF_SSC_SOURCE_MASK;
7136 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007137
7138 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007139 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007140
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007141 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007142 POSTING_READ(PCH_DREF_CONTROL);
7143 udelay(200);
7144 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007145
7146 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007147}
7148
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007149static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007150{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007151 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007152
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007153 tmp = I915_READ(SOUTH_CHICKEN2);
7154 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7155 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007156
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007157 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7158 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7159 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007160
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007161 tmp = I915_READ(SOUTH_CHICKEN2);
7162 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7163 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007164
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007165 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7166 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7167 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007168}
7169
7170/* WaMPhyProgramming:hsw */
7171static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7172{
7173 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007174
7175 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7176 tmp &= ~(0xFF << 24);
7177 tmp |= (0x12 << 24);
7178 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7179
Paulo Zanonidde86e22012-12-01 12:04:25 -02007180 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7181 tmp |= (1 << 11);
7182 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7183
7184 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7185 tmp |= (1 << 11);
7186 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7187
Paulo Zanonidde86e22012-12-01 12:04:25 -02007188 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7189 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7190 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7191
7192 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7193 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7194 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7195
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007196 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7197 tmp &= ~(7 << 13);
7198 tmp |= (5 << 13);
7199 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007200
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007201 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7202 tmp &= ~(7 << 13);
7203 tmp |= (5 << 13);
7204 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007205
7206 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7207 tmp &= ~0xFF;
7208 tmp |= 0x1C;
7209 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7210
7211 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7212 tmp &= ~0xFF;
7213 tmp |= 0x1C;
7214 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7215
7216 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7217 tmp &= ~(0xFF << 16);
7218 tmp |= (0x1C << 16);
7219 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7220
7221 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7222 tmp &= ~(0xFF << 16);
7223 tmp |= (0x1C << 16);
7224 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7225
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007226 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7227 tmp |= (1 << 27);
7228 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007229
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007230 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7231 tmp |= (1 << 27);
7232 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007233
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007234 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7235 tmp &= ~(0xF << 28);
7236 tmp |= (4 << 28);
7237 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007238
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007239 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7240 tmp &= ~(0xF << 28);
7241 tmp |= (4 << 28);
7242 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007243}
7244
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007245/* Implements 3 different sequences from BSpec chapter "Display iCLK
7246 * Programming" based on the parameters passed:
7247 * - Sequence to enable CLKOUT_DP
7248 * - Sequence to enable CLKOUT_DP without spread
7249 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7250 */
7251static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
7252 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007253{
7254 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007255 uint32_t reg, tmp;
7256
7257 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7258 with_spread = true;
7259 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
7260 with_fdi, "LP PCH doesn't have FDI\n"))
7261 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007262
7263 mutex_lock(&dev_priv->dpio_lock);
7264
7265 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7266 tmp &= ~SBI_SSCCTL_DISABLE;
7267 tmp |= SBI_SSCCTL_PATHALT;
7268 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7269
7270 udelay(24);
7271
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007272 if (with_spread) {
7273 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7274 tmp &= ~SBI_SSCCTL_PATHALT;
7275 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007276
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007277 if (with_fdi) {
7278 lpt_reset_fdi_mphy(dev_priv);
7279 lpt_program_fdi_mphy(dev_priv);
7280 }
7281 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02007282
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007283 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7284 SBI_GEN0 : SBI_DBUFF0;
7285 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7286 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7287 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01007288
7289 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007290}
7291
Paulo Zanoni47701c32013-07-23 11:19:25 -03007292/* Sequence to disable CLKOUT_DP */
7293static void lpt_disable_clkout_dp(struct drm_device *dev)
7294{
7295 struct drm_i915_private *dev_priv = dev->dev_private;
7296 uint32_t reg, tmp;
7297
7298 mutex_lock(&dev_priv->dpio_lock);
7299
7300 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7301 SBI_GEN0 : SBI_DBUFF0;
7302 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7303 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7304 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7305
7306 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7307 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7308 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7309 tmp |= SBI_SSCCTL_PATHALT;
7310 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7311 udelay(32);
7312 }
7313 tmp |= SBI_SSCCTL_DISABLE;
7314 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7315 }
7316
7317 mutex_unlock(&dev_priv->dpio_lock);
7318}
7319
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007320static void lpt_init_pch_refclk(struct drm_device *dev)
7321{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007322 struct intel_encoder *encoder;
7323 bool has_vga = false;
7324
Damien Lespiaub2784e12014-08-05 11:29:37 +01007325 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007326 switch (encoder->type) {
7327 case INTEL_OUTPUT_ANALOG:
7328 has_vga = true;
7329 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007330 default:
7331 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007332 }
7333 }
7334
Paulo Zanoni47701c32013-07-23 11:19:25 -03007335 if (has_vga)
7336 lpt_enable_clkout_dp(dev, true, true);
7337 else
7338 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007339}
7340
Paulo Zanonidde86e22012-12-01 12:04:25 -02007341/*
7342 * Initialize reference clocks when the driver loads
7343 */
7344void intel_init_pch_refclk(struct drm_device *dev)
7345{
7346 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7347 ironlake_init_pch_refclk(dev);
7348 else if (HAS_PCH_LPT(dev))
7349 lpt_init_pch_refclk(dev);
7350}
7351
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007352static int ironlake_get_refclk(struct drm_crtc *crtc)
7353{
7354 struct drm_device *dev = crtc->dev;
7355 struct drm_i915_private *dev_priv = dev->dev_private;
7356 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007357 int num_connectors = 0;
7358 bool is_lvds = false;
7359
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007360 for_each_intel_encoder(dev, encoder) {
7361 if (encoder->new_crtc != to_intel_crtc(crtc))
7362 continue;
7363
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007364 switch (encoder->type) {
7365 case INTEL_OUTPUT_LVDS:
7366 is_lvds = true;
7367 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007368 default:
7369 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007370 }
7371 num_connectors++;
7372 }
7373
7374 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007375 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007376 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007377 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007378 }
7379
7380 return 120000;
7381}
7382
Daniel Vetter6ff93602013-04-19 11:24:36 +02007383static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03007384{
7385 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
7386 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7387 int pipe = intel_crtc->pipe;
7388 uint32_t val;
7389
Daniel Vetter78114072013-06-13 00:54:57 +02007390 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03007391
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007392 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03007393 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007394 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007395 break;
7396 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007397 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007398 break;
7399 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007400 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007401 break;
7402 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007403 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007404 break;
7405 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03007406 /* Case prevented by intel_choose_pipe_bpp_dither. */
7407 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03007408 }
7409
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007410 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03007411 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7412
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007413 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03007414 val |= PIPECONF_INTERLACED_ILK;
7415 else
7416 val |= PIPECONF_PROGRESSIVE;
7417
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007418 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007419 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007420
Paulo Zanonic8203562012-09-12 10:06:29 -03007421 I915_WRITE(PIPECONF(pipe), val);
7422 POSTING_READ(PIPECONF(pipe));
7423}
7424
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007425/*
7426 * Set up the pipe CSC unit.
7427 *
7428 * Currently only full range RGB to limited range RGB conversion
7429 * is supported, but eventually this should handle various
7430 * RGB<->YCbCr scenarios as well.
7431 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01007432static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007433{
7434 struct drm_device *dev = crtc->dev;
7435 struct drm_i915_private *dev_priv = dev->dev_private;
7436 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7437 int pipe = intel_crtc->pipe;
7438 uint16_t coeff = 0x7800; /* 1.0 */
7439
7440 /*
7441 * TODO: Check what kind of values actually come out of the pipe
7442 * with these coeff/postoff values and adjust to get the best
7443 * accuracy. Perhaps we even need to take the bpc value into
7444 * consideration.
7445 */
7446
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007447 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007448 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
7449
7450 /*
7451 * GY/GU and RY/RU should be the other way around according
7452 * to BSpec, but reality doesn't agree. Just set them up in
7453 * a way that results in the correct picture.
7454 */
7455 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
7456 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
7457
7458 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
7459 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
7460
7461 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
7462 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
7463
7464 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
7465 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
7466 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
7467
7468 if (INTEL_INFO(dev)->gen > 6) {
7469 uint16_t postoff = 0;
7470
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007471 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02007472 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007473
7474 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
7475 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
7476 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
7477
7478 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
7479 } else {
7480 uint32_t mode = CSC_MODE_YUV_TO_RGB;
7481
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007482 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007483 mode |= CSC_BLACK_SCREEN_OFFSET;
7484
7485 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
7486 }
7487}
7488
Daniel Vetter6ff93602013-04-19 11:24:36 +02007489static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007490{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007491 struct drm_device *dev = crtc->dev;
7492 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007493 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007494 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007495 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007496 uint32_t val;
7497
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007498 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007499
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007500 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007501 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7502
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007503 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007504 val |= PIPECONF_INTERLACED_ILK;
7505 else
7506 val |= PIPECONF_PROGRESSIVE;
7507
Paulo Zanoni702e7a52012-10-23 18:29:59 -02007508 I915_WRITE(PIPECONF(cpu_transcoder), val);
7509 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007510
7511 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
7512 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007513
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05307514 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007515 val = 0;
7516
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007517 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007518 case 18:
7519 val |= PIPEMISC_DITHER_6_BPC;
7520 break;
7521 case 24:
7522 val |= PIPEMISC_DITHER_8_BPC;
7523 break;
7524 case 30:
7525 val |= PIPEMISC_DITHER_10_BPC;
7526 break;
7527 case 36:
7528 val |= PIPEMISC_DITHER_12_BPC;
7529 break;
7530 default:
7531 /* Case prevented by pipe_config_set_bpp. */
7532 BUG();
7533 }
7534
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007535 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007536 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7537
7538 I915_WRITE(PIPEMISC(pipe), val);
7539 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007540}
7541
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007542static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007543 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007544 intel_clock_t *clock,
7545 bool *has_reduced_clock,
7546 intel_clock_t *reduced_clock)
7547{
7548 struct drm_device *dev = crtc->dev;
7549 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03007550 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007551 int refclk;
7552 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02007553 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007554
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007555 is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007556
7557 refclk = ironlake_get_refclk(crtc);
7558
7559 /*
7560 * Returns a set of divisors for the desired target clock with the given
7561 * refclk, or FALSE. The returned values represent the clock equation:
7562 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7563 */
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007564 limit = intel_limit(intel_crtc, refclk);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03007565 ret = dev_priv->display.find_dpll(limit, intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007566 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007567 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007568 if (!ret)
7569 return false;
7570
7571 if (is_lvds && dev_priv->lvds_downclock_avail) {
7572 /*
7573 * Ensure we match the reduced clock's P to the target clock.
7574 * If the clocks don't match, we can't switch the display clock
7575 * by using the FP0/FP1. In such case we will disable the LVDS
7576 * downclock feature.
7577 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02007578 *has_reduced_clock =
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03007579 dev_priv->display.find_dpll(limit, intel_crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007580 dev_priv->lvds_downclock,
7581 refclk, clock,
7582 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007583 }
7584
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007585 return true;
7586}
7587
Paulo Zanonid4b19312012-11-29 11:29:32 -02007588int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7589{
7590 /*
7591 * Account for spread spectrum to avoid
7592 * oversubscribing the link. Max center spread
7593 * is 2.5%; use 5% for safety's sake.
7594 */
7595 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02007596 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02007597}
7598
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007599static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02007600{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007601 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03007602}
7603
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007604static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007605 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007606 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007607 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007608{
7609 struct drm_crtc *crtc = &intel_crtc->base;
7610 struct drm_device *dev = crtc->dev;
7611 struct drm_i915_private *dev_priv = dev->dev_private;
7612 struct intel_encoder *intel_encoder;
7613 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007614 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02007615 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007616
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007617 for_each_intel_encoder(dev, intel_encoder) {
7618 if (intel_encoder->new_crtc != to_intel_crtc(crtc))
7619 continue;
7620
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007621 switch (intel_encoder->type) {
7622 case INTEL_OUTPUT_LVDS:
7623 is_lvds = true;
7624 break;
7625 case INTEL_OUTPUT_SDVO:
7626 case INTEL_OUTPUT_HDMI:
7627 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007628 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007629 default:
7630 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007631 }
7632
7633 num_connectors++;
7634 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007635
Chris Wilsonc1858122010-12-03 21:35:48 +00007636 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07007637 factor = 21;
7638 if (is_lvds) {
7639 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007640 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02007641 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07007642 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007643 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07007644 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00007645
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007646 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02007647 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00007648
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007649 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7650 *fp2 |= FP_CB_TUNE;
7651
Chris Wilson5eddb702010-09-11 13:48:45 +01007652 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08007653
Eric Anholta07d6782011-03-30 13:01:08 -07007654 if (is_lvds)
7655 dpll |= DPLLB_MODE_LVDS;
7656 else
7657 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007658
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007659 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007660 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007661
7662 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007663 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007664 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007665 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08007666
Eric Anholta07d6782011-03-30 13:01:08 -07007667 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007668 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007669 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007670 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007671
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007672 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07007673 case 5:
7674 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7675 break;
7676 case 7:
7677 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7678 break;
7679 case 10:
7680 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7681 break;
7682 case 14:
7683 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7684 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007685 }
7686
Daniel Vetterb4c09f32013-04-30 14:01:42 +02007687 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007688 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08007689 else
7690 dpll |= PLL_REF_INPUT_DREFCLK;
7691
Daniel Vetter959e16d2013-06-05 13:34:21 +02007692 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007693}
7694
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007695static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
7696 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007697{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007698 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007699 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007700 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03007701 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01007702 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007703 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007704
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007705 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08007706
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007707 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7708 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
7709
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007710 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007711 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007712 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007713 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7714 return -EINVAL;
7715 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01007716 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007717 if (!crtc_state->clock_set) {
7718 crtc_state->dpll.n = clock.n;
7719 crtc_state->dpll.m1 = clock.m1;
7720 crtc_state->dpll.m2 = clock.m2;
7721 crtc_state->dpll.p1 = clock.p1;
7722 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007723 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007724
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007725 /* 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 +02007726 if (crtc_state->has_pch_encoder) {
7727 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007728 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007729 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007730
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007731 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007732 &fp, &reduced_clock,
7733 has_reduced_clock ? &fp2 : NULL);
7734
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007735 crtc_state->dpll_hw_state.dpll = dpll;
7736 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007737 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007738 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007739 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007740 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007741
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007742 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007743 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03007744 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007745 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07007746 return -EINVAL;
7747 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02007748 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007749
Jani Nikulad330a952014-01-21 11:24:25 +02007750 if (is_lvds && has_reduced_clock && i915.powersave)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007751 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02007752 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007753 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007754
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007755 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007756}
7757
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007758static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7759 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02007760{
7761 struct drm_device *dev = crtc->base.dev;
7762 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007763 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02007764
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007765 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7766 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7767 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7768 & ~TU_SIZE_MASK;
7769 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7770 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7771 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7772}
7773
7774static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7775 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007776 struct intel_link_m_n *m_n,
7777 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007778{
7779 struct drm_device *dev = crtc->base.dev;
7780 struct drm_i915_private *dev_priv = dev->dev_private;
7781 enum pipe pipe = crtc->pipe;
7782
7783 if (INTEL_INFO(dev)->gen >= 5) {
7784 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7785 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7786 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7787 & ~TU_SIZE_MASK;
7788 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7789 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7790 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007791 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7792 * gen < 8) and if DRRS is supported (to make sure the
7793 * registers are not unnecessarily read).
7794 */
7795 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007796 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007797 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7798 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7799 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7800 & ~TU_SIZE_MASK;
7801 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7802 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7803 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7804 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007805 } else {
7806 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7807 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7808 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7809 & ~TU_SIZE_MASK;
7810 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7811 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7812 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7813 }
7814}
7815
7816void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007817 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007818{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007819 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007820 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7821 else
7822 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007823 &pipe_config->dp_m_n,
7824 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007825}
7826
Daniel Vetter72419202013-04-04 13:28:53 +02007827static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007828 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02007829{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007830 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007831 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02007832}
7833
Jesse Barnesbd2e2442014-11-13 17:51:47 +00007834static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007835 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00007836{
7837 struct drm_device *dev = crtc->base.dev;
7838 struct drm_i915_private *dev_priv = dev->dev_private;
7839 uint32_t tmp;
7840
7841 tmp = I915_READ(PS_CTL(crtc->pipe));
7842
7843 if (tmp & PS_ENABLE) {
7844 pipe_config->pch_pfit.enabled = true;
7845 pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
7846 pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
7847 }
7848}
7849
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007850static void
7851skylake_get_initial_plane_config(struct intel_crtc *crtc,
7852 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007853{
7854 struct drm_device *dev = crtc->base.dev;
7855 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00007856 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007857 int pipe = crtc->pipe;
7858 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007859 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007860 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007861 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007862
Damien Lespiaud9806c92015-01-21 14:07:19 +00007863 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007864 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007865 DRM_DEBUG_KMS("failed to alloc fb\n");
7866 return;
7867 }
7868
Damien Lespiau1b842c82015-01-21 13:50:54 +00007869 fb = &intel_fb->base;
7870
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007871 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00007872 if (!(val & PLANE_CTL_ENABLE))
7873 goto error;
7874
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007875 pixel_format = val & PLANE_CTL_FORMAT_MASK;
7876 fourcc = skl_format_to_fourcc(pixel_format,
7877 val & PLANE_CTL_ORDER_RGBX,
7878 val & PLANE_CTL_ALPHA_MASK);
7879 fb->pixel_format = fourcc;
7880 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
7881
Damien Lespiau40f46282015-02-27 11:15:21 +00007882 tiling = val & PLANE_CTL_TILED_MASK;
7883 switch (tiling) {
7884 case PLANE_CTL_TILED_LINEAR:
7885 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
7886 break;
7887 case PLANE_CTL_TILED_X:
7888 plane_config->tiling = I915_TILING_X;
7889 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7890 break;
7891 case PLANE_CTL_TILED_Y:
7892 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
7893 break;
7894 case PLANE_CTL_TILED_YF:
7895 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
7896 break;
7897 default:
7898 MISSING_CASE(tiling);
7899 goto error;
7900 }
7901
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007902 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
7903 plane_config->base = base;
7904
7905 offset = I915_READ(PLANE_OFFSET(pipe, 0));
7906
7907 val = I915_READ(PLANE_SIZE(pipe, 0));
7908 fb->height = ((val >> 16) & 0xfff) + 1;
7909 fb->width = ((val >> 0) & 0x1fff) + 1;
7910
7911 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00007912 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
7913 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007914 fb->pitches[0] = (val & 0x3ff) * stride_mult;
7915
7916 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007917 fb->pixel_format,
7918 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007919
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007920 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007921
7922 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7923 pipe_name(pipe), fb->width, fb->height,
7924 fb->bits_per_pixel, base, fb->pitches[0],
7925 plane_config->size);
7926
Damien Lespiau2d140302015-02-05 17:22:18 +00007927 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007928 return;
7929
7930error:
7931 kfree(fb);
7932}
7933
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007934static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007935 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007936{
7937 struct drm_device *dev = crtc->base.dev;
7938 struct drm_i915_private *dev_priv = dev->dev_private;
7939 uint32_t tmp;
7940
7941 tmp = I915_READ(PF_CTL(crtc->pipe));
7942
7943 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01007944 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007945 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7946 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02007947
7948 /* We currently do not free assignements of panel fitters on
7949 * ivb/hsw (since we don't use the higher upscaling modes which
7950 * differentiates them) so just WARN about this case for now. */
7951 if (IS_GEN7(dev)) {
7952 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7953 PF_PIPE_SEL_IVB(crtc->pipe));
7954 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007955 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007956}
7957
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007958static void
7959ironlake_get_initial_plane_config(struct intel_crtc *crtc,
7960 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007961{
7962 struct drm_device *dev = crtc->base.dev;
7963 struct drm_i915_private *dev_priv = dev->dev_private;
7964 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00007965 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007966 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007967 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007968 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007969 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007970
Damien Lespiau42a7b082015-02-05 19:35:13 +00007971 val = I915_READ(DSPCNTR(pipe));
7972 if (!(val & DISPLAY_PLANE_ENABLE))
7973 return;
7974
Damien Lespiaud9806c92015-01-21 14:07:19 +00007975 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007976 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007977 DRM_DEBUG_KMS("failed to alloc fb\n");
7978 return;
7979 }
7980
Damien Lespiau1b842c82015-01-21 13:50:54 +00007981 fb = &intel_fb->base;
7982
Daniel Vetter18c52472015-02-10 17:16:09 +00007983 if (INTEL_INFO(dev)->gen >= 4) {
7984 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007985 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007986 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7987 }
7988 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007989
7990 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007991 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007992 fb->pixel_format = fourcc;
7993 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007994
Damien Lespiauaeee5a42015-01-20 12:51:47 +00007995 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007996 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00007997 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007998 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00007999 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008000 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008001 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008002 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008003 }
8004 plane_config->base = base;
8005
8006 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008007 fb->width = ((val >> 16) & 0xfff) + 1;
8008 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008009
8010 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008011 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008012
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008013 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008014 fb->pixel_format,
8015 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008016
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008017 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008018
Damien Lespiau2844a922015-01-20 12:51:48 +00008019 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8020 pipe_name(pipe), fb->width, fb->height,
8021 fb->bits_per_pixel, base, fb->pitches[0],
8022 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008023
Damien Lespiau2d140302015-02-05 17:22:18 +00008024 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008025}
8026
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008027static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008028 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008029{
8030 struct drm_device *dev = crtc->base.dev;
8031 struct drm_i915_private *dev_priv = dev->dev_private;
8032 uint32_t tmp;
8033
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008034 if (!intel_display_power_is_enabled(dev_priv,
8035 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008036 return false;
8037
Daniel Vettere143a212013-07-04 12:01:15 +02008038 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008039 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008040
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008041 tmp = I915_READ(PIPECONF(crtc->pipe));
8042 if (!(tmp & PIPECONF_ENABLE))
8043 return false;
8044
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008045 switch (tmp & PIPECONF_BPC_MASK) {
8046 case PIPECONF_6BPC:
8047 pipe_config->pipe_bpp = 18;
8048 break;
8049 case PIPECONF_8BPC:
8050 pipe_config->pipe_bpp = 24;
8051 break;
8052 case PIPECONF_10BPC:
8053 pipe_config->pipe_bpp = 30;
8054 break;
8055 case PIPECONF_12BPC:
8056 pipe_config->pipe_bpp = 36;
8057 break;
8058 default:
8059 break;
8060 }
8061
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008062 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8063 pipe_config->limited_color_range = true;
8064
Daniel Vetterab9412b2013-05-03 11:49:46 +02008065 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008066 struct intel_shared_dpll *pll;
8067
Daniel Vetter88adfff2013-03-28 10:42:01 +01008068 pipe_config->has_pch_encoder = true;
8069
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008070 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8071 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8072 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008073
8074 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008075
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008076 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02008077 pipe_config->shared_dpll =
8078 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008079 } else {
8080 tmp = I915_READ(PCH_DPLL_SEL);
8081 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8082 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8083 else
8084 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8085 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008086
8087 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8088
8089 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8090 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008091
8092 tmp = pipe_config->dpll_hw_state.dpll;
8093 pipe_config->pixel_multiplier =
8094 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8095 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008096
8097 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008098 } else {
8099 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008100 }
8101
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008102 intel_get_pipe_timings(crtc, pipe_config);
8103
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008104 ironlake_get_pfit_config(crtc, pipe_config);
8105
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008106 return true;
8107}
8108
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008109static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8110{
8111 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008112 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008113
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008114 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008115 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008116 pipe_name(crtc->pipe));
8117
Rob Clarke2c719b2014-12-15 13:56:32 -05008118 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8119 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8120 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8121 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8122 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8123 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008124 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03008125 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05008126 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008127 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008128 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008129 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008130 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008131 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008132 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008133
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008134 /*
8135 * In theory we can still leave IRQs enabled, as long as only the HPD
8136 * interrupts remain enabled. We used to check for that, but since it's
8137 * gen-specific and since we only disable LCPLL after we fully disable
8138 * the interrupts, the check below should be enough.
8139 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008140 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008141}
8142
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008143static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8144{
8145 struct drm_device *dev = dev_priv->dev;
8146
8147 if (IS_HASWELL(dev))
8148 return I915_READ(D_COMP_HSW);
8149 else
8150 return I915_READ(D_COMP_BDW);
8151}
8152
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008153static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8154{
8155 struct drm_device *dev = dev_priv->dev;
8156
8157 if (IS_HASWELL(dev)) {
8158 mutex_lock(&dev_priv->rps.hw_lock);
8159 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8160 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03008161 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008162 mutex_unlock(&dev_priv->rps.hw_lock);
8163 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008164 I915_WRITE(D_COMP_BDW, val);
8165 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008166 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008167}
8168
8169/*
8170 * This function implements pieces of two sequences from BSpec:
8171 * - Sequence for display software to disable LCPLL
8172 * - Sequence for display software to allow package C8+
8173 * The steps implemented here are just the steps that actually touch the LCPLL
8174 * register. Callers should take care of disabling all the display engine
8175 * functions, doing the mode unset, fixing interrupts, etc.
8176 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008177static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8178 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008179{
8180 uint32_t val;
8181
8182 assert_can_disable_lcpll(dev_priv);
8183
8184 val = I915_READ(LCPLL_CTL);
8185
8186 if (switch_to_fclk) {
8187 val |= LCPLL_CD_SOURCE_FCLK;
8188 I915_WRITE(LCPLL_CTL, val);
8189
8190 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
8191 LCPLL_CD_SOURCE_FCLK_DONE, 1))
8192 DRM_ERROR("Switching to FCLK failed\n");
8193
8194 val = I915_READ(LCPLL_CTL);
8195 }
8196
8197 val |= LCPLL_PLL_DISABLE;
8198 I915_WRITE(LCPLL_CTL, val);
8199 POSTING_READ(LCPLL_CTL);
8200
8201 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
8202 DRM_ERROR("LCPLL still locked\n");
8203
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008204 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008205 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008206 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008207 ndelay(100);
8208
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008209 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8210 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008211 DRM_ERROR("D_COMP RCOMP still in progress\n");
8212
8213 if (allow_power_down) {
8214 val = I915_READ(LCPLL_CTL);
8215 val |= LCPLL_POWER_DOWN_ALLOW;
8216 I915_WRITE(LCPLL_CTL, val);
8217 POSTING_READ(LCPLL_CTL);
8218 }
8219}
8220
8221/*
8222 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8223 * source.
8224 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008225static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008226{
8227 uint32_t val;
8228
8229 val = I915_READ(LCPLL_CTL);
8230
8231 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8232 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8233 return;
8234
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008235 /*
8236 * Make sure we're not on PC8 state before disabling PC8, otherwise
8237 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008238 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02008239 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03008240
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008241 if (val & LCPLL_POWER_DOWN_ALLOW) {
8242 val &= ~LCPLL_POWER_DOWN_ALLOW;
8243 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02008244 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008245 }
8246
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008247 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008248 val |= D_COMP_COMP_FORCE;
8249 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008250 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008251
8252 val = I915_READ(LCPLL_CTL);
8253 val &= ~LCPLL_PLL_DISABLE;
8254 I915_WRITE(LCPLL_CTL, val);
8255
8256 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
8257 DRM_ERROR("LCPLL not locked yet\n");
8258
8259 if (val & LCPLL_CD_SOURCE_FCLK) {
8260 val = I915_READ(LCPLL_CTL);
8261 val &= ~LCPLL_CD_SOURCE_FCLK;
8262 I915_WRITE(LCPLL_CTL, val);
8263
8264 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
8265 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
8266 DRM_ERROR("Switching back to LCPLL failed\n");
8267 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03008268
Mika Kuoppala59bad942015-01-16 11:34:40 +02008269 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008270}
8271
Paulo Zanoni765dab672014-03-07 20:08:18 -03008272/*
8273 * Package states C8 and deeper are really deep PC states that can only be
8274 * reached when all the devices on the system allow it, so even if the graphics
8275 * device allows PC8+, it doesn't mean the system will actually get to these
8276 * states. Our driver only allows PC8+ when going into runtime PM.
8277 *
8278 * The requirements for PC8+ are that all the outputs are disabled, the power
8279 * well is disabled and most interrupts are disabled, and these are also
8280 * requirements for runtime PM. When these conditions are met, we manually do
8281 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8282 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8283 * hang the machine.
8284 *
8285 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8286 * the state of some registers, so when we come back from PC8+ we need to
8287 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8288 * need to take care of the registers kept by RC6. Notice that this happens even
8289 * if we don't put the device in PCI D3 state (which is what currently happens
8290 * because of the runtime PM support).
8291 *
8292 * For more, read "Display Sequences for Package C8" on the hardware
8293 * documentation.
8294 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008295void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008296{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008297 struct drm_device *dev = dev_priv->dev;
8298 uint32_t val;
8299
Paulo Zanonic67a4702013-08-19 13:18:09 -03008300 DRM_DEBUG_KMS("Enabling package C8+\n");
8301
Paulo Zanonic67a4702013-08-19 13:18:09 -03008302 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8303 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8304 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8305 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8306 }
8307
8308 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008309 hsw_disable_lcpll(dev_priv, true, true);
8310}
8311
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008312void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008313{
8314 struct drm_device *dev = dev_priv->dev;
8315 uint32_t val;
8316
Paulo Zanonic67a4702013-08-19 13:18:09 -03008317 DRM_DEBUG_KMS("Disabling package C8+\n");
8318
8319 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008320 lpt_init_pch_refclk(dev);
8321
8322 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8323 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8324 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8325 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8326 }
8327
8328 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008329}
8330
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008331static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8332 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008333{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008334 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008335 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03008336
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008337 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02008338
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008339 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008340}
8341
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008342static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8343 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008344 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008345{
Damien Lespiau3148ade2014-11-21 16:14:56 +00008346 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008347
8348 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
8349 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
8350
8351 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00008352 case SKL_DPLL0:
8353 /*
8354 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
8355 * of the shared DPLL framework and thus needs to be read out
8356 * separately
8357 */
8358 dpll_ctl1 = I915_READ(DPLL_CTRL1);
8359 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
8360 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008361 case SKL_DPLL1:
8362 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
8363 break;
8364 case SKL_DPLL2:
8365 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
8366 break;
8367 case SKL_DPLL3:
8368 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
8369 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008370 }
8371}
8372
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008373static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8374 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008375 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008376{
8377 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8378
8379 switch (pipe_config->ddi_pll_sel) {
8380 case PORT_CLK_SEL_WRPLL1:
8381 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
8382 break;
8383 case PORT_CLK_SEL_WRPLL2:
8384 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
8385 break;
8386 }
8387}
8388
Daniel Vetter26804af2014-06-25 22:01:55 +03008389static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008390 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03008391{
8392 struct drm_device *dev = crtc->base.dev;
8393 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03008394 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03008395 enum port port;
8396 uint32_t tmp;
8397
8398 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
8399
8400 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
8401
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008402 if (IS_SKYLAKE(dev))
8403 skylake_get_ddi_pll(dev_priv, port, pipe_config);
8404 else
8405 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03008406
Daniel Vetterd452c5b2014-07-04 11:27:39 -03008407 if (pipe_config->shared_dpll >= 0) {
8408 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8409
8410 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8411 &pipe_config->dpll_hw_state));
8412 }
8413
Daniel Vetter26804af2014-06-25 22:01:55 +03008414 /*
8415 * Haswell has only FDI/PCH transcoder A. It is which is connected to
8416 * DDI E. So just check whether this pipe is wired to DDI E and whether
8417 * the PCH transcoder is on.
8418 */
Damien Lespiauca370452013-12-03 13:56:24 +00008419 if (INTEL_INFO(dev)->gen < 9 &&
8420 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03008421 pipe_config->has_pch_encoder = true;
8422
8423 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
8424 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8425 FDI_DP_PORT_WIDTH_SHIFT) + 1;
8426
8427 ironlake_get_fdi_m_n_config(crtc, pipe_config);
8428 }
8429}
8430
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008431static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008432 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008433{
8434 struct drm_device *dev = crtc->base.dev;
8435 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008436 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008437 uint32_t tmp;
8438
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008439 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02008440 POWER_DOMAIN_PIPE(crtc->pipe)))
8441 return false;
8442
Daniel Vettere143a212013-07-04 12:01:15 +02008443 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008444 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
8445
Daniel Vettereccb1402013-05-22 00:50:22 +02008446 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8447 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8448 enum pipe trans_edp_pipe;
8449 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8450 default:
8451 WARN(1, "unknown pipe linked to edp transcoder\n");
8452 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8453 case TRANS_DDI_EDP_INPUT_A_ON:
8454 trans_edp_pipe = PIPE_A;
8455 break;
8456 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8457 trans_edp_pipe = PIPE_B;
8458 break;
8459 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8460 trans_edp_pipe = PIPE_C;
8461 break;
8462 }
8463
8464 if (trans_edp_pipe == crtc->pipe)
8465 pipe_config->cpu_transcoder = TRANSCODER_EDP;
8466 }
8467
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008468 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02008469 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03008470 return false;
8471
Daniel Vettereccb1402013-05-22 00:50:22 +02008472 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008473 if (!(tmp & PIPECONF_ENABLE))
8474 return false;
8475
Daniel Vetter26804af2014-06-25 22:01:55 +03008476 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008477
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008478 intel_get_pipe_timings(crtc, pipe_config);
8479
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008480 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008481 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
8482 if (IS_SKYLAKE(dev))
8483 skylake_get_pfit_config(crtc, pipe_config);
8484 else
8485 ironlake_get_pfit_config(crtc, pipe_config);
8486 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01008487
Jesse Barnese59150d2014-01-07 13:30:45 -08008488 if (IS_HASWELL(dev))
8489 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
8490 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008491
Clint Taylorebb69c92014-09-30 10:30:22 -07008492 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
8493 pipe_config->pixel_multiplier =
8494 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
8495 } else {
8496 pipe_config->pixel_multiplier = 1;
8497 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008498
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008499 return true;
8500}
8501
Chris Wilson560b85b2010-08-07 11:01:38 +01008502static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8503{
8504 struct drm_device *dev = crtc->dev;
8505 struct drm_i915_private *dev_priv = dev->dev_private;
8506 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03008507 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01008508
Ville Syrjälädc41c152014-08-13 11:57:05 +03008509 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08008510 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
8511 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03008512 unsigned int stride = roundup_pow_of_two(width) * 4;
8513
8514 switch (stride) {
8515 default:
8516 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8517 width, stride);
8518 stride = 256;
8519 /* fallthrough */
8520 case 256:
8521 case 512:
8522 case 1024:
8523 case 2048:
8524 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008525 }
8526
Ville Syrjälädc41c152014-08-13 11:57:05 +03008527 cntl |= CURSOR_ENABLE |
8528 CURSOR_GAMMA_ENABLE |
8529 CURSOR_FORMAT_ARGB |
8530 CURSOR_STRIDE(stride);
8531
8532 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008533 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008534
Ville Syrjälädc41c152014-08-13 11:57:05 +03008535 if (intel_crtc->cursor_cntl != 0 &&
8536 (intel_crtc->cursor_base != base ||
8537 intel_crtc->cursor_size != size ||
8538 intel_crtc->cursor_cntl != cntl)) {
8539 /* On these chipsets we can only modify the base/size/stride
8540 * whilst the cursor is disabled.
8541 */
8542 I915_WRITE(_CURACNTR, 0);
8543 POSTING_READ(_CURACNTR);
8544 intel_crtc->cursor_cntl = 0;
8545 }
8546
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008547 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03008548 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008549 intel_crtc->cursor_base = base;
8550 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03008551
8552 if (intel_crtc->cursor_size != size) {
8553 I915_WRITE(CURSIZE, size);
8554 intel_crtc->cursor_size = size;
8555 }
8556
Chris Wilson4b0e3332014-05-30 16:35:26 +03008557 if (intel_crtc->cursor_cntl != cntl) {
8558 I915_WRITE(_CURACNTR, cntl);
8559 POSTING_READ(_CURACNTR);
8560 intel_crtc->cursor_cntl = cntl;
8561 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008562}
8563
8564static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
8565{
8566 struct drm_device *dev = crtc->dev;
8567 struct drm_i915_private *dev_priv = dev->dev_private;
8568 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8569 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008570 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01008571
Chris Wilson4b0e3332014-05-30 16:35:26 +03008572 cntl = 0;
8573 if (base) {
8574 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08008575 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308576 case 64:
8577 cntl |= CURSOR_MODE_64_ARGB_AX;
8578 break;
8579 case 128:
8580 cntl |= CURSOR_MODE_128_ARGB_AX;
8581 break;
8582 case 256:
8583 cntl |= CURSOR_MODE_256_ARGB_AX;
8584 break;
8585 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08008586 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308587 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01008588 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008589 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03008590
8591 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8592 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01008593 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008594
Matt Roper8e7d6882015-01-21 16:35:41 -08008595 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008596 cntl |= CURSOR_ROTATE_180;
8597
Chris Wilson4b0e3332014-05-30 16:35:26 +03008598 if (intel_crtc->cursor_cntl != cntl) {
8599 I915_WRITE(CURCNTR(pipe), cntl);
8600 POSTING_READ(CURCNTR(pipe));
8601 intel_crtc->cursor_cntl = cntl;
8602 }
8603
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008604 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008605 I915_WRITE(CURBASE(pipe), base);
8606 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008607
8608 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008609}
8610
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008611/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01008612static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8613 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008614{
8615 struct drm_device *dev = crtc->dev;
8616 struct drm_i915_private *dev_priv = dev->dev_private;
8617 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8618 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07008619 int x = crtc->cursor_x;
8620 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008621 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008622
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008623 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008624 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008625
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008626 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008627 base = 0;
8628
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008629 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008630 base = 0;
8631
8632 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08008633 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008634 base = 0;
8635
8636 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8637 x = -x;
8638 }
8639 pos |= x << CURSOR_X_SHIFT;
8640
8641 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08008642 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008643 base = 0;
8644
8645 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8646 y = -y;
8647 }
8648 pos |= y << CURSOR_Y_SHIFT;
8649
Chris Wilson4b0e3332014-05-30 16:35:26 +03008650 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008651 return;
8652
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008653 I915_WRITE(CURPOS(pipe), pos);
8654
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008655 /* ILK+ do this automagically */
8656 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08008657 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08008658 base += (intel_crtc->base.cursor->state->crtc_h *
8659 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008660 }
8661
Ville Syrjälä8ac54662014-08-12 19:39:54 +03008662 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008663 i845_update_cursor(crtc, base);
8664 else
8665 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008666}
8667
Ville Syrjälädc41c152014-08-13 11:57:05 +03008668static bool cursor_size_ok(struct drm_device *dev,
8669 uint32_t width, uint32_t height)
8670{
8671 if (width == 0 || height == 0)
8672 return false;
8673
8674 /*
8675 * 845g/865g are special in that they are only limited by
8676 * the width of their cursors, the height is arbitrary up to
8677 * the precision of the register. Everything else requires
8678 * square cursors, limited to a few power-of-two sizes.
8679 */
8680 if (IS_845G(dev) || IS_I865G(dev)) {
8681 if ((width & 63) != 0)
8682 return false;
8683
8684 if (width > (IS_845G(dev) ? 64 : 512))
8685 return false;
8686
8687 if (height > 1023)
8688 return false;
8689 } else {
8690 switch (width | height) {
8691 case 256:
8692 case 128:
8693 if (IS_GEN2(dev))
8694 return false;
8695 case 64:
8696 break;
8697 default:
8698 return false;
8699 }
8700 }
8701
8702 return true;
8703}
8704
Jesse Barnes79e53942008-11-07 14:24:08 -08008705static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01008706 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08008707{
James Simmons72034252010-08-03 01:33:19 +01008708 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08008709 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008710
James Simmons72034252010-08-03 01:33:19 +01008711 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008712 intel_crtc->lut_r[i] = red[i] >> 8;
8713 intel_crtc->lut_g[i] = green[i] >> 8;
8714 intel_crtc->lut_b[i] = blue[i] >> 8;
8715 }
8716
8717 intel_crtc_load_lut(crtc);
8718}
8719
Jesse Barnes79e53942008-11-07 14:24:08 -08008720/* VESA 640x480x72Hz mode to set on the pipe */
8721static struct drm_display_mode load_detect_mode = {
8722 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8723 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8724};
8725
Daniel Vettera8bb6812014-02-10 18:00:39 +01008726struct drm_framebuffer *
8727__intel_framebuffer_create(struct drm_device *dev,
8728 struct drm_mode_fb_cmd2 *mode_cmd,
8729 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01008730{
8731 struct intel_framebuffer *intel_fb;
8732 int ret;
8733
8734 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8735 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03008736 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01008737 return ERR_PTR(-ENOMEM);
8738 }
8739
8740 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008741 if (ret)
8742 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01008743
8744 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008745err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03008746 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008747 kfree(intel_fb);
8748
8749 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01008750}
8751
Daniel Vetterb5ea6422014-03-02 21:18:00 +01008752static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01008753intel_framebuffer_create(struct drm_device *dev,
8754 struct drm_mode_fb_cmd2 *mode_cmd,
8755 struct drm_i915_gem_object *obj)
8756{
8757 struct drm_framebuffer *fb;
8758 int ret;
8759
8760 ret = i915_mutex_lock_interruptible(dev);
8761 if (ret)
8762 return ERR_PTR(ret);
8763 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8764 mutex_unlock(&dev->struct_mutex);
8765
8766 return fb;
8767}
8768
Chris Wilsond2dff872011-04-19 08:36:26 +01008769static u32
8770intel_framebuffer_pitch_for_width(int width, int bpp)
8771{
8772 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8773 return ALIGN(pitch, 64);
8774}
8775
8776static u32
8777intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8778{
8779 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02008780 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01008781}
8782
8783static struct drm_framebuffer *
8784intel_framebuffer_create_for_mode(struct drm_device *dev,
8785 struct drm_display_mode *mode,
8786 int depth, int bpp)
8787{
8788 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00008789 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01008790
8791 obj = i915_gem_alloc_object(dev,
8792 intel_framebuffer_size_for_mode(mode, bpp));
8793 if (obj == NULL)
8794 return ERR_PTR(-ENOMEM);
8795
8796 mode_cmd.width = mode->hdisplay;
8797 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008798 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8799 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00008800 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01008801
8802 return intel_framebuffer_create(dev, &mode_cmd, obj);
8803}
8804
8805static struct drm_framebuffer *
8806mode_fits_in_fbdev(struct drm_device *dev,
8807 struct drm_display_mode *mode)
8808{
Daniel Vetter4520f532013-10-09 09:18:51 +02008809#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01008810 struct drm_i915_private *dev_priv = dev->dev_private;
8811 struct drm_i915_gem_object *obj;
8812 struct drm_framebuffer *fb;
8813
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008814 if (!dev_priv->fbdev)
8815 return NULL;
8816
8817 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01008818 return NULL;
8819
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008820 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008821 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01008822
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008823 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008824 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8825 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01008826 return NULL;
8827
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008828 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01008829 return NULL;
8830
8831 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02008832#else
8833 return NULL;
8834#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01008835}
8836
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008837bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01008838 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05008839 struct intel_load_detect_pipe *old,
8840 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08008841{
8842 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008843 struct intel_encoder *intel_encoder =
8844 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08008845 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008846 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08008847 struct drm_crtc *crtc = NULL;
8848 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02008849 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05008850 struct drm_mode_config *config = &dev->mode_config;
8851 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008852
Chris Wilsond2dff872011-04-19 08:36:26 +01008853 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008854 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03008855 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008856
Rob Clark51fd3712013-11-19 12:10:12 -05008857retry:
8858 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8859 if (ret)
8860 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02008861
Jesse Barnes79e53942008-11-07 14:24:08 -08008862 /*
8863 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01008864 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008865 * - if the connector already has an assigned crtc, use it (but make
8866 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01008867 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008868 * - try to find the first unused crtc that can drive this connector,
8869 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08008870 */
8871
8872 /* See if we already have a CRTC for this connector */
8873 if (encoder->crtc) {
8874 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01008875
Rob Clark51fd3712013-11-19 12:10:12 -05008876 ret = drm_modeset_lock(&crtc->mutex, ctx);
8877 if (ret)
8878 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01008879 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
8880 if (ret)
8881 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01008882
Daniel Vetter24218aa2012-08-12 19:27:11 +02008883 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008884 old->load_detect_temp = false;
8885
8886 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008887 if (connector->dpms != DRM_MODE_DPMS_ON)
8888 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01008889
Chris Wilson71731882011-04-19 23:10:58 +01008890 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08008891 }
8892
8893 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008894 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008895 i++;
8896 if (!(encoder->possible_crtcs & (1 << i)))
8897 continue;
Matt Roper83d65732015-02-25 13:12:16 -08008898 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +03008899 continue;
8900 /* This can occur when applying the pipe A quirk on resume. */
8901 if (to_intel_crtc(possible_crtc)->new_enabled)
8902 continue;
8903
8904 crtc = possible_crtc;
8905 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008906 }
8907
8908 /*
8909 * If we didn't find an unused CRTC, don't use any.
8910 */
8911 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01008912 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05008913 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08008914 }
8915
Rob Clark51fd3712013-11-19 12:10:12 -05008916 ret = drm_modeset_lock(&crtc->mutex, ctx);
8917 if (ret)
8918 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01008919 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
8920 if (ret)
8921 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02008922 intel_encoder->new_crtc = to_intel_crtc(crtc);
8923 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008924
8925 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008926 intel_crtc->new_enabled = true;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008927 intel_crtc->new_config = intel_crtc->config;
Daniel Vetter24218aa2012-08-12 19:27:11 +02008928 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008929 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01008930 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08008931
Chris Wilson64927112011-04-20 07:25:26 +01008932 if (!mode)
8933 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08008934
Chris Wilsond2dff872011-04-19 08:36:26 +01008935 /* We need a framebuffer large enough to accommodate all accesses
8936 * that the plane may generate whilst we perform load detection.
8937 * We can not rely on the fbcon either being present (we get called
8938 * during its initialisation to detect all boot displays, or it may
8939 * not even exist) or that it is large enough to satisfy the
8940 * requested mode.
8941 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02008942 fb = mode_fits_in_fbdev(dev, mode);
8943 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008944 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008945 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8946 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01008947 } else
8948 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008949 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008950 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008951 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008952 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008953
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008954 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01008955 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01008956 if (old->release_fb)
8957 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008958 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008959 }
Daniel Vetter9128b042015-03-03 17:31:21 +01008960 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +01008961
Jesse Barnes79e53942008-11-07 14:24:08 -08008962 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008963 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01008964 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008965
8966 fail:
Matt Roper83d65732015-02-25 13:12:16 -08008967 intel_crtc->new_enabled = crtc->state->enable;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008968 if (intel_crtc->new_enabled)
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008969 intel_crtc->new_config = intel_crtc->config;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008970 else
8971 intel_crtc->new_config = NULL;
Rob Clark51fd3712013-11-19 12:10:12 -05008972fail_unlock:
8973 if (ret == -EDEADLK) {
8974 drm_modeset_backoff(ctx);
8975 goto retry;
8976 }
8977
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008978 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08008979}
8980
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008981void intel_release_load_detect_pipe(struct drm_connector *connector,
Ville Syrjälä208bf9f2014-08-11 13:15:35 +03008982 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08008983{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008984 struct intel_encoder *intel_encoder =
8985 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01008986 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01008987 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008988 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008989
Chris Wilsond2dff872011-04-19 08:36:26 +01008990 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008991 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03008992 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008993
Chris Wilson8261b192011-04-19 23:18:09 +01008994 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02008995 to_intel_connector(connector)->new_encoder = NULL;
8996 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008997 intel_crtc->new_enabled = false;
8998 intel_crtc->new_config = NULL;
Daniel Vetterfc303102012-07-09 10:40:58 +02008999 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01009000
Daniel Vetter36206362012-12-10 20:42:17 +01009001 if (old->release_fb) {
9002 drm_framebuffer_unregister_private(old->release_fb);
9003 drm_framebuffer_unreference(old->release_fb);
9004 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009005
Chris Wilson0622a532011-04-21 09:32:11 +01009006 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009007 }
9008
Eric Anholtc751ce42010-03-25 11:48:48 -07009009 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009010 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9011 connector->funcs->dpms(connector, old->dpms_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08009012}
9013
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009014static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009015 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009016{
9017 struct drm_i915_private *dev_priv = dev->dev_private;
9018 u32 dpll = pipe_config->dpll_hw_state.dpll;
9019
9020 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009021 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009022 else if (HAS_PCH_SPLIT(dev))
9023 return 120000;
9024 else if (!IS_GEN2(dev))
9025 return 96000;
9026 else
9027 return 48000;
9028}
9029
Jesse Barnes79e53942008-11-07 14:24:08 -08009030/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009031static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009032 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08009033{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009034 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08009035 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009036 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009037 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08009038 u32 fp;
9039 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009040 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08009041
9042 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03009043 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009044 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03009045 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009046
9047 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009048 if (IS_PINEVIEW(dev)) {
9049 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9050 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08009051 } else {
9052 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9053 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9054 }
9055
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009056 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009057 if (IS_PINEVIEW(dev))
9058 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9059 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08009060 else
9061 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08009062 DPLL_FPA01_P1_POST_DIV_SHIFT);
9063
9064 switch (dpll & DPLL_MODE_MASK) {
9065 case DPLLB_MODE_DAC_SERIAL:
9066 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9067 5 : 10;
9068 break;
9069 case DPLLB_MODE_LVDS:
9070 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9071 7 : 14;
9072 break;
9073 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08009074 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08009075 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009076 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009077 }
9078
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009079 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009080 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009081 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009082 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009083 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02009084 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009085 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08009086
9087 if (is_lvds) {
9088 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
9089 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009090
9091 if (lvds & LVDS_CLKB_POWER_UP)
9092 clock.p2 = 7;
9093 else
9094 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08009095 } else {
9096 if (dpll & PLL_P1_DIVIDE_BY_TWO)
9097 clock.p1 = 2;
9098 else {
9099 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
9100 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
9101 }
9102 if (dpll & PLL_P2_DIVIDE_BY_4)
9103 clock.p2 = 4;
9104 else
9105 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08009106 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009107
9108 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009109 }
9110
Ville Syrjälä18442d02013-09-13 16:00:08 +03009111 /*
9112 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01009113 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03009114 * encoder's get_config() function.
9115 */
9116 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009117}
9118
Ville Syrjälä6878da02013-09-13 15:59:11 +03009119int intel_dotclock_calculate(int link_freq,
9120 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009121{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009122 /*
9123 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009124 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009125 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009126 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009127 *
9128 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009129 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08009130 */
9131
Ville Syrjälä6878da02013-09-13 15:59:11 +03009132 if (!m_n->link_n)
9133 return 0;
9134
9135 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
9136}
9137
Ville Syrjälä18442d02013-09-13 16:00:08 +03009138static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009139 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03009140{
9141 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009142
9143 /* read out port_clock from the DPLL */
9144 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03009145
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009146 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03009147 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01009148 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03009149 * agree once we know their relationship in the encoder's
9150 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009151 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02009152 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03009153 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
9154 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08009155}
9156
9157/** Returns the currently programmed mode of the given pipe. */
9158struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9159 struct drm_crtc *crtc)
9160{
Jesse Barnes548f2452011-02-17 10:40:53 -08009161 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08009162 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009163 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009164 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009165 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02009166 int htot = I915_READ(HTOTAL(cpu_transcoder));
9167 int hsync = I915_READ(HSYNC(cpu_transcoder));
9168 int vtot = I915_READ(VTOTAL(cpu_transcoder));
9169 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03009170 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08009171
9172 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
9173 if (!mode)
9174 return NULL;
9175
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009176 /*
9177 * Construct a pipe_config sufficient for getting the clock info
9178 * back out of crtc_clock_get.
9179 *
9180 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
9181 * to use a real value here instead.
9182 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03009183 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009184 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009185 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
9186 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
9187 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009188 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
9189
Ville Syrjälä773ae032013-09-23 17:48:20 +03009190 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08009191 mode->hdisplay = (htot & 0xffff) + 1;
9192 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
9193 mode->hsync_start = (hsync & 0xffff) + 1;
9194 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
9195 mode->vdisplay = (vtot & 0xffff) + 1;
9196 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
9197 mode->vsync_start = (vsync & 0xffff) + 1;
9198 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
9199
9200 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08009201
9202 return mode;
9203}
9204
Jesse Barnes652c3932009-08-17 13:31:43 -07009205static void intel_decrease_pllclock(struct drm_crtc *crtc)
9206{
9207 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03009208 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07009209 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009210
Sonika Jindalbaff2962014-07-22 11:16:35 +05309211 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07009212 return;
9213
9214 if (!dev_priv->lvds_downclock_avail)
9215 return;
9216
9217 /*
9218 * Since this is called by a timer, we should never get here in
9219 * the manual case.
9220 */
9221 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01009222 int pipe = intel_crtc->pipe;
9223 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02009224 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01009225
Zhao Yakui44d98a62009-10-09 11:39:40 +08009226 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009227
Sean Paul8ac5a6d2012-02-13 13:14:51 -05009228 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009229
Chris Wilson074b5e12012-05-02 12:07:06 +01009230 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07009231 dpll |= DISPLAY_RATE_SELECT_FPA1;
9232 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009233 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009234 dpll = I915_READ(dpll_reg);
9235 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08009236 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009237 }
9238
9239}
9240
Chris Wilsonf047e392012-07-21 12:31:41 +01009241void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07009242{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009243 struct drm_i915_private *dev_priv = dev->dev_private;
9244
Chris Wilsonf62a0072014-02-21 17:55:39 +00009245 if (dev_priv->mm.busy)
9246 return;
9247
Paulo Zanoni43694d62014-03-07 20:08:08 -03009248 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009249 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +00009250 if (INTEL_INFO(dev)->gen >= 6)
9251 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +00009252 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +01009253}
9254
9255void intel_mark_idle(struct drm_device *dev)
9256{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009257 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00009258 struct drm_crtc *crtc;
9259
Chris Wilsonf62a0072014-02-21 17:55:39 +00009260 if (!dev_priv->mm.busy)
9261 return;
9262
9263 dev_priv->mm.busy = false;
9264
Jani Nikulad330a952014-01-21 11:24:25 +02009265 if (!i915.powersave)
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03009266 goto out;
Chris Wilson725a5b52013-01-08 11:02:57 +00009267
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009268 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07009269 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +00009270 continue;
9271
9272 intel_decrease_pllclock(crtc);
9273 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009274
Damien Lespiau3d13ef22014-02-07 19:12:47 +00009275 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009276 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03009277
9278out:
Paulo Zanoni43694d62014-03-07 20:08:08 -03009279 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01009280}
9281
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009282static void intel_crtc_set_state(struct intel_crtc *crtc,
9283 struct intel_crtc_state *crtc_state)
9284{
9285 kfree(crtc->config);
9286 crtc->config = crtc_state;
Ander Conselvan de Oliveira16f3f652015-01-15 14:55:27 +02009287 crtc->base.state = &crtc_state->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009288}
9289
Jesse Barnes79e53942008-11-07 14:24:08 -08009290static void intel_crtc_destroy(struct drm_crtc *crtc)
9291{
9292 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009293 struct drm_device *dev = crtc->dev;
9294 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +02009295
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009296 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009297 work = intel_crtc->unpin_work;
9298 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009299 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009300
9301 if (work) {
9302 cancel_work_sync(&work->work);
9303 kfree(work);
9304 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009305
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009306 intel_crtc_set_state(intel_crtc, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08009307 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009308
Jesse Barnes79e53942008-11-07 14:24:08 -08009309 kfree(intel_crtc);
9310}
9311
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009312static void intel_unpin_work_fn(struct work_struct *__work)
9313{
9314 struct intel_unpin_work *work =
9315 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009316 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +02009317 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009318
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009319 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +00009320 intel_unpin_fb_obj(intel_fb_obj(work->old_fb));
Chris Wilson05394f32010-11-08 19:18:58 +00009321 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00009322
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02009323 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +00009324
9325 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +00009326 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009327 mutex_unlock(&dev->struct_mutex);
9328
Daniel Vetterf99d7062014-06-19 16:01:59 +02009329 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +00009330 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +02009331
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009332 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9333 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9334
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009335 kfree(work);
9336}
9337
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009338static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01009339 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009340{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009341 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9342 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009343 unsigned long flags;
9344
9345 /* Ignore early vblank irqs */
9346 if (intel_crtc == NULL)
9347 return;
9348
Daniel Vetterf3260382014-09-15 14:55:23 +02009349 /*
9350 * This is called both by irq handlers and the reset code (to complete
9351 * lost pageflips) so needs the full irqsave spinlocks.
9352 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009353 spin_lock_irqsave(&dev->event_lock, flags);
9354 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00009355
9356 /* Ensure we don't miss a work->pending update ... */
9357 smp_rmb();
9358
9359 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009360 spin_unlock_irqrestore(&dev->event_lock, flags);
9361 return;
9362 }
9363
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009364 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01009365
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009366 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009367}
9368
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009369void intel_finish_page_flip(struct drm_device *dev, int pipe)
9370{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009371 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009372 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9373
Mario Kleiner49b14a52010-12-09 07:00:07 +01009374 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009375}
9376
9377void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9378{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009379 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009380 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9381
Mario Kleiner49b14a52010-12-09 07:00:07 +01009382 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009383}
9384
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009385/* Is 'a' after or equal to 'b'? */
9386static bool g4x_flip_count_after_eq(u32 a, u32 b)
9387{
9388 return !((a - b) & 0x80000000);
9389}
9390
9391static bool page_flip_finished(struct intel_crtc *crtc)
9392{
9393 struct drm_device *dev = crtc->base.dev;
9394 struct drm_i915_private *dev_priv = dev->dev_private;
9395
Ville Syrjäläbdfa7542014-05-27 21:33:09 +03009396 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
9397 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
9398 return true;
9399
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009400 /*
9401 * The relevant registers doen't exist on pre-ctg.
9402 * As the flip done interrupt doesn't trigger for mmio
9403 * flips on gmch platforms, a flip count check isn't
9404 * really needed there. But since ctg has the registers,
9405 * include it in the check anyway.
9406 */
9407 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9408 return true;
9409
9410 /*
9411 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9412 * used the same base address. In that case the mmio flip might
9413 * have completed, but the CS hasn't even executed the flip yet.
9414 *
9415 * A flip count check isn't enough as the CS might have updated
9416 * the base address just after start of vblank, but before we
9417 * managed to process the interrupt. This means we'd complete the
9418 * CS flip too soon.
9419 *
9420 * Combining both checks should get us a good enough result. It may
9421 * still happen that the CS flip has been executed, but has not
9422 * yet actually completed. But in case the base address is the same
9423 * anyway, we don't really care.
9424 */
9425 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9426 crtc->unpin_work->gtt_offset &&
9427 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9428 crtc->unpin_work->flip_count);
9429}
9430
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009431void intel_prepare_page_flip(struct drm_device *dev, int plane)
9432{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009433 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009434 struct intel_crtc *intel_crtc =
9435 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9436 unsigned long flags;
9437
Daniel Vetterf3260382014-09-15 14:55:23 +02009438
9439 /*
9440 * This is called both by irq handlers and the reset code (to complete
9441 * lost pageflips) so needs the full irqsave spinlocks.
9442 *
9443 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +00009444 * generate a page-flip completion irq, i.e. every modeset
9445 * is also accompanied by a spurious intel_prepare_page_flip().
9446 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009447 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009448 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +00009449 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009450 spin_unlock_irqrestore(&dev->event_lock, flags);
9451}
9452
Robin Schroereba905b2014-05-18 02:24:50 +02009453static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +00009454{
9455 /* Ensure that the work item is consistent when activating it ... */
9456 smp_wmb();
9457 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9458 /* and that it is marked active as soon as the irq could fire. */
9459 smp_wmb();
9460}
9461
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009462static int intel_gen2_queue_flip(struct drm_device *dev,
9463 struct drm_crtc *crtc,
9464 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009465 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009466 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009467 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009468{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009469 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009470 u32 flip_mask;
9471 int ret;
9472
Daniel Vetter6d90c952012-04-26 23:28:05 +02009473 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009474 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009475 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009476
9477 /* Can't queue multiple flips, so wait for the previous
9478 * one to finish before executing the next.
9479 */
9480 if (intel_crtc->plane)
9481 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9482 else
9483 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009484 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9485 intel_ring_emit(ring, MI_NOOP);
9486 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9487 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9488 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009489 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009490 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00009491
9492 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009493 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009494 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009495}
9496
9497static int intel_gen3_queue_flip(struct drm_device *dev,
9498 struct drm_crtc *crtc,
9499 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009500 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009501 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009502 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009503{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009504 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009505 u32 flip_mask;
9506 int ret;
9507
Daniel Vetter6d90c952012-04-26 23:28:05 +02009508 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009509 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009510 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009511
9512 if (intel_crtc->plane)
9513 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9514 else
9515 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009516 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9517 intel_ring_emit(ring, MI_NOOP);
9518 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9519 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9520 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009521 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009522 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009523
Chris Wilsone7d841c2012-12-03 11:36:30 +00009524 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009525 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009526 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009527}
9528
9529static int intel_gen4_queue_flip(struct drm_device *dev,
9530 struct drm_crtc *crtc,
9531 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009532 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009533 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009534 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009535{
9536 struct drm_i915_private *dev_priv = dev->dev_private;
9537 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9538 uint32_t pf, pipesrc;
9539 int ret;
9540
Daniel Vetter6d90c952012-04-26 23:28:05 +02009541 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009542 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009543 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009544
9545 /* i965+ uses the linear or tiled offsets from the
9546 * Display Registers (which do not change across a page-flip)
9547 * so we need only reprogram the base address.
9548 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02009549 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9550 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9551 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009552 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +02009553 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009554
9555 /* XXX Enabling the panel-fitter across page-flip is so far
9556 * untested on non-native modes, so ignore it for now.
9557 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9558 */
9559 pf = 0;
9560 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009561 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009562
9563 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009564 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009565 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009566}
9567
9568static int intel_gen6_queue_flip(struct drm_device *dev,
9569 struct drm_crtc *crtc,
9570 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009571 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009572 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009573 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009574{
9575 struct drm_i915_private *dev_priv = dev->dev_private;
9576 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9577 uint32_t pf, pipesrc;
9578 int ret;
9579
Daniel Vetter6d90c952012-04-26 23:28:05 +02009580 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009581 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009582 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009583
Daniel Vetter6d90c952012-04-26 23:28:05 +02009584 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9585 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9586 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009587 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009588
Chris Wilson99d9acd2012-04-17 20:37:00 +01009589 /* Contrary to the suggestions in the documentation,
9590 * "Enable Panel Fitter" does not seem to be required when page
9591 * flipping with a non-native mode, and worse causes a normal
9592 * modeset to fail.
9593 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9594 */
9595 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009596 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009597 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009598
9599 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009600 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009601 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009602}
9603
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009604static int intel_gen7_queue_flip(struct drm_device *dev,
9605 struct drm_crtc *crtc,
9606 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009607 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009608 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009609 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009610{
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009611 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009612 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01009613 int len, ret;
9614
Robin Schroereba905b2014-05-18 02:24:50 +02009615 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009616 case PLANE_A:
9617 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9618 break;
9619 case PLANE_B:
9620 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9621 break;
9622 case PLANE_C:
9623 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9624 break;
9625 default:
9626 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009627 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009628 }
9629
Chris Wilsonffe74d72013-08-26 20:58:12 +01009630 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +01009631 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +01009632 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +01009633 /*
9634 * On Gen 8, SRM is now taking an extra dword to accommodate
9635 * 48bits addresses, and we need a NOOP for the batch size to
9636 * stay even.
9637 */
9638 if (IS_GEN8(dev))
9639 len += 2;
9640 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009641
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009642 /*
9643 * BSpec MI_DISPLAY_FLIP for IVB:
9644 * "The full packet must be contained within the same cache line."
9645 *
9646 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9647 * cacheline, if we ever start emitting more commands before
9648 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9649 * then do the cacheline alignment, and finally emit the
9650 * MI_DISPLAY_FLIP.
9651 */
9652 ret = intel_ring_cacheline_align(ring);
9653 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009654 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009655
Chris Wilsonffe74d72013-08-26 20:58:12 +01009656 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009657 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009658 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009659
Chris Wilsonffe74d72013-08-26 20:58:12 +01009660 /* Unmask the flip-done completion message. Note that the bspec says that
9661 * we should do this for both the BCS and RCS, and that we must not unmask
9662 * more than one flip event at any time (or ensure that one flip message
9663 * can be sent by waiting for flip-done prior to queueing new flips).
9664 * Experimentation says that BCS works despite DERRMR masking all
9665 * flip-done completion events and that unmasking all planes at once
9666 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9667 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9668 */
9669 if (ring->id == RCS) {
9670 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9671 intel_ring_emit(ring, DERRMR);
9672 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9673 DERRMR_PIPEB_PRI_FLIP_DONE |
9674 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +01009675 if (IS_GEN8(dev))
9676 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9677 MI_SRM_LRM_GLOBAL_GTT);
9678 else
9679 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9680 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +01009681 intel_ring_emit(ring, DERRMR);
9682 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +01009683 if (IS_GEN8(dev)) {
9684 intel_ring_emit(ring, 0);
9685 intel_ring_emit(ring, MI_NOOP);
9686 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009687 }
9688
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009689 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009690 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009691 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009692 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00009693
9694 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009695 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009696 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009697}
9698
Sourab Gupta84c33a62014-06-02 16:47:17 +05309699static bool use_mmio_flip(struct intel_engine_cs *ring,
9700 struct drm_i915_gem_object *obj)
9701{
9702 /*
9703 * This is not being used for older platforms, because
9704 * non-availability of flip done interrupt forces us to use
9705 * CS flips. Older platforms derive flip done using some clever
9706 * tricks involving the flip_pending status bits and vblank irqs.
9707 * So using MMIO flips there would disrupt this mechanism.
9708 */
9709
Chris Wilson8e09bf82014-07-08 10:40:30 +01009710 if (ring == NULL)
9711 return true;
9712
Sourab Gupta84c33a62014-06-02 16:47:17 +05309713 if (INTEL_INFO(ring->dev)->gen < 5)
9714 return false;
9715
9716 if (i915.use_mmio_flip < 0)
9717 return false;
9718 else if (i915.use_mmio_flip > 0)
9719 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +01009720 else if (i915.enable_execlists)
9721 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +05309722 else
John Harrison41c52412014-11-24 18:49:43 +00009723 return ring != i915_gem_request_get_ring(obj->last_read_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309724}
9725
Damien Lespiauff944562014-11-20 14:58:16 +00009726static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
9727{
9728 struct drm_device *dev = intel_crtc->base.dev;
9729 struct drm_i915_private *dev_priv = dev->dev_private;
9730 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
9731 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
9732 struct drm_i915_gem_object *obj = intel_fb->obj;
9733 const enum pipe pipe = intel_crtc->pipe;
9734 u32 ctl, stride;
9735
9736 ctl = I915_READ(PLANE_CTL(pipe, 0));
9737 ctl &= ~PLANE_CTL_TILED_MASK;
9738 if (obj->tiling_mode == I915_TILING_X)
9739 ctl |= PLANE_CTL_TILED_X;
9740
9741 /*
9742 * The stride is either expressed as a multiple of 64 bytes chunks for
9743 * linear buffers or in number of tiles for tiled buffers.
9744 */
9745 stride = fb->pitches[0] >> 6;
9746 if (obj->tiling_mode == I915_TILING_X)
9747 stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
9748
9749 /*
9750 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
9751 * PLANE_SURF updates, the update is then guaranteed to be atomic.
9752 */
9753 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
9754 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
9755
9756 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
9757 POSTING_READ(PLANE_SURF(pipe, 0));
9758}
9759
9760static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +05309761{
9762 struct drm_device *dev = intel_crtc->base.dev;
9763 struct drm_i915_private *dev_priv = dev->dev_private;
9764 struct intel_framebuffer *intel_fb =
9765 to_intel_framebuffer(intel_crtc->base.primary->fb);
9766 struct drm_i915_gem_object *obj = intel_fb->obj;
9767 u32 dspcntr;
9768 u32 reg;
9769
Sourab Gupta84c33a62014-06-02 16:47:17 +05309770 reg = DSPCNTR(intel_crtc->plane);
9771 dspcntr = I915_READ(reg);
9772
Damien Lespiauc5d97472014-10-25 00:11:11 +01009773 if (obj->tiling_mode != I915_TILING_NONE)
9774 dspcntr |= DISPPLANE_TILED;
9775 else
9776 dspcntr &= ~DISPPLANE_TILED;
9777
Sourab Gupta84c33a62014-06-02 16:47:17 +05309778 I915_WRITE(reg, dspcntr);
9779
9780 I915_WRITE(DSPSURF(intel_crtc->plane),
9781 intel_crtc->unpin_work->gtt_offset);
9782 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +02009783
Damien Lespiauff944562014-11-20 14:58:16 +00009784}
9785
9786/*
9787 * XXX: This is the temporary way to update the plane registers until we get
9788 * around to using the usual plane update functions for MMIO flips
9789 */
9790static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9791{
9792 struct drm_device *dev = intel_crtc->base.dev;
9793 bool atomic_update;
9794 u32 start_vbl_count;
9795
9796 intel_mark_page_flip_active(intel_crtc);
9797
9798 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
9799
9800 if (INTEL_INFO(dev)->gen >= 9)
9801 skl_do_mmio_flip(intel_crtc);
9802 else
9803 /* use_mmio_flip() retricts MMIO flips to ilk+ */
9804 ilk_do_mmio_flip(intel_crtc);
9805
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +02009806 if (atomic_update)
9807 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309808}
9809
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +02009810static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +05309811{
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009812 struct intel_crtc *crtc =
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +02009813 container_of(work, struct intel_crtc, mmio_flip.work);
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009814 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +05309815
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009816 mmio_flip = &crtc->mmio_flip;
9817 if (mmio_flip->req)
John Harrison9c654812014-11-24 18:49:35 +00009818 WARN_ON(__i915_wait_request(mmio_flip->req,
9819 crtc->reset_counter,
9820 false, NULL, NULL) != 0);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309821
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009822 intel_do_mmio_flip(crtc);
9823 if (mmio_flip->req) {
9824 mutex_lock(&crtc->base.dev->struct_mutex);
John Harrison146d84f2014-12-05 13:49:33 +00009825 i915_gem_request_assign(&mmio_flip->req, NULL);
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009826 mutex_unlock(&crtc->base.dev->struct_mutex);
9827 }
Sourab Gupta84c33a62014-06-02 16:47:17 +05309828}
9829
9830static int intel_queue_mmio_flip(struct drm_device *dev,
9831 struct drm_crtc *crtc,
9832 struct drm_framebuffer *fb,
9833 struct drm_i915_gem_object *obj,
9834 struct intel_engine_cs *ring,
9835 uint32_t flags)
9836{
Sourab Gupta84c33a62014-06-02 16:47:17 +05309837 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309838
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009839 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
9840 obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309841
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +02009842 schedule_work(&intel_crtc->mmio_flip.work);
9843
Sourab Gupta84c33a62014-06-02 16:47:17 +05309844 return 0;
9845}
9846
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009847static int intel_default_queue_flip(struct drm_device *dev,
9848 struct drm_crtc *crtc,
9849 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009850 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009851 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009852 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009853{
9854 return -ENODEV;
9855}
9856
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009857static bool __intel_pageflip_stall_check(struct drm_device *dev,
9858 struct drm_crtc *crtc)
9859{
9860 struct drm_i915_private *dev_priv = dev->dev_private;
9861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9862 struct intel_unpin_work *work = intel_crtc->unpin_work;
9863 u32 addr;
9864
9865 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
9866 return true;
9867
9868 if (!work->enable_stall_check)
9869 return false;
9870
9871 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +01009872 if (work->flip_queued_req &&
9873 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009874 return false;
9875
Daniel Vetter1e3feef2015-02-13 21:03:45 +01009876 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009877 }
9878
Daniel Vetter1e3feef2015-02-13 21:03:45 +01009879 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009880 return false;
9881
9882 /* Potential stall - if we see that the flip has happened,
9883 * assume a missed interrupt. */
9884 if (INTEL_INFO(dev)->gen >= 4)
9885 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
9886 else
9887 addr = I915_READ(DSPADDR(intel_crtc->plane));
9888
9889 /* There is a potential issue here with a false positive after a flip
9890 * to the same address. We could address this by checking for a
9891 * non-incrementing frame counter.
9892 */
9893 return addr == work->gtt_offset;
9894}
9895
9896void intel_check_page_flip(struct drm_device *dev, int pipe)
9897{
9898 struct drm_i915_private *dev_priv = dev->dev_private;
9899 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9900 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterf3260382014-09-15 14:55:23 +02009901
Dave Gordon6c51d462015-03-06 15:34:26 +00009902 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009903
9904 if (crtc == NULL)
9905 return;
9906
Daniel Vetterf3260382014-09-15 14:55:23 +02009907 spin_lock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009908 if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
9909 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Daniel Vetter1e3feef2015-02-13 21:03:45 +01009910 intel_crtc->unpin_work->flip_queued_vblank,
9911 drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009912 page_flip_completed(intel_crtc);
9913 }
Daniel Vetterf3260382014-09-15 14:55:23 +02009914 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009915}
9916
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009917static int intel_crtc_page_flip(struct drm_crtc *crtc,
9918 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009919 struct drm_pending_vblank_event *event,
9920 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009921{
9922 struct drm_device *dev = crtc->dev;
9923 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -07009924 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -07009925 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009926 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -08009927 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +02009928 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009929 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009930 struct intel_engine_cs *ring;
Chris Wilson52e68632010-08-08 10:15:59 +01009931 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009932
Matt Roper2ff8fde2014-07-08 07:50:07 -07009933 /*
9934 * drm_mode_page_flip_ioctl() should already catch this, but double
9935 * check to be safe. In the future we may enable pageflipping from
9936 * a disabled primary plane.
9937 */
9938 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
9939 return -EBUSY;
9940
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009941 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -07009942 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009943 return -EINVAL;
9944
9945 /*
9946 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9947 * Note that pitch changes could also affect these register.
9948 */
9949 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -07009950 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9951 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009952 return -EINVAL;
9953
Chris Wilsonf900db42014-02-20 09:26:13 +00009954 if (i915_terminally_wedged(&dev_priv->gpu_error))
9955 goto out_hang;
9956
Daniel Vetterb14c5672013-09-19 12:18:32 +02009957 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009958 if (work == NULL)
9959 return -ENOMEM;
9960
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009961 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009962 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +00009963 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009964 INIT_WORK(&work->work, intel_unpin_work_fn);
9965
Daniel Vetter87b6b102014-05-15 15:33:46 +02009966 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009967 if (ret)
9968 goto free_work;
9969
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009970 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009971 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009972 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009973 /* Before declaring the flip queue wedged, check if
9974 * the hardware completed the operation behind our backs.
9975 */
9976 if (__intel_pageflip_stall_check(dev, crtc)) {
9977 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
9978 page_flip_completed(intel_crtc);
9979 } else {
9980 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009981 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +01009982
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009983 drm_crtc_vblank_put(crtc);
9984 kfree(work);
9985 return -EBUSY;
9986 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009987 }
9988 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009989 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009990
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009991 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9992 flush_workqueue(dev_priv->wq);
9993
Jesse Barnes75dfca82010-02-10 15:09:44 -08009994 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +00009995 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009996 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009997
Matt Roperf4510a22014-04-01 15:22:40 -07009998 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -08009999 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080010000
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010001 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010002
Chris Wilson89ed88b2015-02-16 14:31:49 +000010003 ret = i915_mutex_lock_interruptible(dev);
10004 if (ret)
10005 goto cleanup;
10006
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010007 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020010008 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010009
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010010 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020010011 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010012
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010013 if (IS_VALLEYVIEW(dev)) {
10014 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010015 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010010016 /* vlv: DISPLAY_FLIP fails to change tiling */
10017 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000010018 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010010019 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010020 } else if (INTEL_INFO(dev)->gen >= 7) {
John Harrison41c52412014-11-24 18:49:43 +000010021 ring = i915_gem_request_get_ring(obj->last_read_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010022 if (ring == NULL || ring->id != RCS)
10023 ring = &dev_priv->ring[BCS];
10024 } else {
10025 ring = &dev_priv->ring[RCS];
10026 }
10027
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +000010028 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010029 if (ret)
10030 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010031
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010032 work->gtt_offset =
10033 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
10034
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010035 if (use_mmio_flip(ring, obj)) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010036 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10037 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010038 if (ret)
10039 goto cleanup_unpin;
10040
John Harrisonf06cc1b2014-11-24 18:49:37 +000010041 i915_gem_request_assign(&work->flip_queued_req,
10042 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010043 } else {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010044 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010045 page_flip_flags);
10046 if (ret)
10047 goto cleanup_unpin;
10048
John Harrisonf06cc1b2014-11-24 18:49:37 +000010049 i915_gem_request_assign(&work->flip_queued_req,
10050 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010051 }
10052
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010053 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010054 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010055
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010056 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020010057 INTEL_FRONTBUFFER_PRIMARY(pipe));
10058
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010059 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010060 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010061 mutex_unlock(&dev->struct_mutex);
10062
Jesse Barnese5510fa2010-07-01 16:48:37 -070010063 trace_i915_flip_request(intel_crtc->plane, obj);
10064
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010065 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010010066
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010067cleanup_unpin:
10068 intel_unpin_fb_obj(obj);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010069cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010070 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010071 mutex_unlock(&dev->struct_mutex);
10072cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070010073 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010074 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010010075
Chris Wilson89ed88b2015-02-16 14:31:49 +000010076 drm_gem_object_unreference_unlocked(&obj->base);
10077 drm_framebuffer_unreference(work->old_fb);
10078
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010079 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010080 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010081 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010082
Daniel Vetter87b6b102014-05-15 15:33:46 +020010083 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010084free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010010085 kfree(work);
10086
Chris Wilsonf900db42014-02-20 09:26:13 +000010087 if (ret == -EIO) {
10088out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080010089 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010090 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010091 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020010092 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010093 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010094 }
Chris Wilsonf900db42014-02-20 09:26:13 +000010095 }
Chris Wilson96b099f2010-06-07 14:03:04 +010010096 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010097}
10098
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010099static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010100 .mode_set_base_atomic = intel_pipe_set_base_atomic,
10101 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080010102 .atomic_begin = intel_begin_crtc_commit,
10103 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010104};
10105
Daniel Vetter9a935852012-07-05 22:34:27 +020010106/**
10107 * intel_modeset_update_staged_output_state
10108 *
10109 * Updates the staged output configuration state, e.g. after we've read out the
10110 * current hw state.
10111 */
10112static void intel_modeset_update_staged_output_state(struct drm_device *dev)
10113{
Ville Syrjälä76688512014-01-10 11:28:06 +020010114 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010115 struct intel_encoder *encoder;
10116 struct intel_connector *connector;
10117
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010118 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010119 connector->new_encoder =
10120 to_intel_encoder(connector->base.encoder);
10121 }
10122
Damien Lespiaub2784e12014-08-05 11:29:37 +010010123 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010124 encoder->new_crtc =
10125 to_intel_crtc(encoder->base.crtc);
10126 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010127
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010128 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010129 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010130
10131 if (crtc->new_enabled)
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010132 crtc->new_config = crtc->config;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010133 else
10134 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010135 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010136}
10137
10138/**
10139 * intel_modeset_commit_output_state
10140 *
10141 * This function copies the stage display pipe configuration to the real one.
10142 */
10143static void intel_modeset_commit_output_state(struct drm_device *dev)
10144{
Ville Syrjälä76688512014-01-10 11:28:06 +020010145 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010146 struct intel_encoder *encoder;
10147 struct intel_connector *connector;
10148
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010149 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010150 connector->base.encoder = &connector->new_encoder->base;
10151 }
10152
Damien Lespiaub2784e12014-08-05 11:29:37 +010010153 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010154 encoder->base.crtc = &encoder->new_crtc->base;
10155 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010156
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010157 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010158 crtc->base.state->enable = crtc->new_enabled;
Ville Syrjälä76688512014-01-10 11:28:06 +020010159 crtc->base.enabled = crtc->new_enabled;
10160 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010161}
10162
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010163static void
Robin Schroereba905b2014-05-18 02:24:50 +020010164connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010165 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010166{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010167 int bpp = pipe_config->pipe_bpp;
10168
10169 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10170 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010171 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010172
10173 /* Don't use an invalid EDID bpc value */
10174 if (connector->base.display_info.bpc &&
10175 connector->base.display_info.bpc * 3 < bpp) {
10176 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10177 bpp, connector->base.display_info.bpc*3);
10178 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10179 }
10180
10181 /* Clamp bpp to 8 on screens without EDID 1.4 */
10182 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10183 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10184 bpp);
10185 pipe_config->pipe_bpp = 24;
10186 }
10187}
10188
10189static int
10190compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10191 struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010192 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010193{
10194 struct drm_device *dev = crtc->base.dev;
10195 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010196 int bpp;
10197
Daniel Vetterd42264b2013-03-28 16:38:08 +010010198 switch (fb->pixel_format) {
10199 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010200 bpp = 8*3; /* since we go through a colormap */
10201 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010202 case DRM_FORMAT_XRGB1555:
10203 case DRM_FORMAT_ARGB1555:
10204 /* checked in intel_framebuffer_init already */
10205 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10206 return -EINVAL;
10207 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010208 bpp = 6*3; /* min is 18bpp */
10209 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010210 case DRM_FORMAT_XBGR8888:
10211 case DRM_FORMAT_ABGR8888:
10212 /* checked in intel_framebuffer_init already */
10213 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10214 return -EINVAL;
10215 case DRM_FORMAT_XRGB8888:
10216 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010217 bpp = 8*3;
10218 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010219 case DRM_FORMAT_XRGB2101010:
10220 case DRM_FORMAT_ARGB2101010:
10221 case DRM_FORMAT_XBGR2101010:
10222 case DRM_FORMAT_ABGR2101010:
10223 /* checked in intel_framebuffer_init already */
10224 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +010010225 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010226 bpp = 10*3;
10227 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +010010228 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010229 default:
10230 DRM_DEBUG_KMS("unsupported depth\n");
10231 return -EINVAL;
10232 }
10233
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010234 pipe_config->pipe_bpp = bpp;
10235
10236 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010237 for_each_intel_connector(dev, connector) {
Daniel Vetter1b829e02013-06-02 13:26:24 +020010238 if (!connector->new_encoder ||
10239 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010240 continue;
10241
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010242 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010243 }
10244
10245 return bpp;
10246}
10247
Daniel Vetter644db712013-09-19 14:53:58 +020010248static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10249{
10250 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10251 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010010252 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020010253 mode->crtc_hdisplay, mode->crtc_hsync_start,
10254 mode->crtc_hsync_end, mode->crtc_htotal,
10255 mode->crtc_vdisplay, mode->crtc_vsync_start,
10256 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10257}
10258
Daniel Vetterc0b03412013-05-28 12:05:54 +020010259static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010260 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020010261 const char *context)
10262{
10263 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10264 context, pipe_name(crtc->pipe));
10265
10266 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10267 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10268 pipe_config->pipe_bpp, pipe_config->dither);
10269 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10270 pipe_config->has_pch_encoder,
10271 pipe_config->fdi_lanes,
10272 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10273 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10274 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010275 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10276 pipe_config->has_dp_encoder,
10277 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10278 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10279 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010280
10281 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10282 pipe_config->has_dp_encoder,
10283 pipe_config->dp_m2_n2.gmch_m,
10284 pipe_config->dp_m2_n2.gmch_n,
10285 pipe_config->dp_m2_n2.link_m,
10286 pipe_config->dp_m2_n2.link_n,
10287 pipe_config->dp_m2_n2.tu);
10288
Daniel Vetter55072d12014-11-20 16:10:28 +010010289 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
10290 pipe_config->has_audio,
10291 pipe_config->has_infoframe);
10292
Daniel Vetterc0b03412013-05-28 12:05:54 +020010293 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010294 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010295 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010296 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
10297 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030010298 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010299 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10300 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010301 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10302 pipe_config->gmch_pfit.control,
10303 pipe_config->gmch_pfit.pgm_ratios,
10304 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010305 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020010306 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010307 pipe_config->pch_pfit.size,
10308 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010309 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030010310 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010311}
10312
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010313static bool encoders_cloneable(const struct intel_encoder *a,
10314 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010315{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010316 /* masks could be asymmetric, so check both ways */
10317 return a == b || (a->cloneable & (1 << b->type) &&
10318 b->cloneable & (1 << a->type));
10319}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010320
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010321static bool check_single_encoder_cloning(struct intel_crtc *crtc,
10322 struct intel_encoder *encoder)
10323{
10324 struct drm_device *dev = crtc->base.dev;
10325 struct intel_encoder *source_encoder;
10326
Damien Lespiaub2784e12014-08-05 11:29:37 +010010327 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010328 if (source_encoder->new_crtc != crtc)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010329 continue;
10330
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010331 if (!encoders_cloneable(encoder, source_encoder))
10332 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010333 }
10334
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010335 return true;
10336}
10337
10338static bool check_encoder_cloning(struct intel_crtc *crtc)
10339{
10340 struct drm_device *dev = crtc->base.dev;
10341 struct intel_encoder *encoder;
10342
Damien Lespiaub2784e12014-08-05 11:29:37 +010010343 for_each_intel_encoder(dev, encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010344 if (encoder->new_crtc != crtc)
10345 continue;
10346
10347 if (!check_single_encoder_cloning(crtc, encoder))
10348 return false;
10349 }
10350
10351 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010352}
10353
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010354static bool check_digital_port_conflicts(struct drm_device *dev)
10355{
10356 struct intel_connector *connector;
10357 unsigned int used_ports = 0;
10358
10359 /*
10360 * Walk the connector list instead of the encoder
10361 * list to detect the problem on ddi platforms
10362 * where there's just one encoder per digital port.
10363 */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010364 for_each_intel_connector(dev, connector) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010365 struct intel_encoder *encoder = connector->new_encoder;
10366
10367 if (!encoder)
10368 continue;
10369
10370 WARN_ON(!encoder->new_crtc);
10371
10372 switch (encoder->type) {
10373 unsigned int port_mask;
10374 case INTEL_OUTPUT_UNKNOWN:
10375 if (WARN_ON(!HAS_DDI(dev)))
10376 break;
10377 case INTEL_OUTPUT_DISPLAYPORT:
10378 case INTEL_OUTPUT_HDMI:
10379 case INTEL_OUTPUT_EDP:
10380 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
10381
10382 /* the same port mustn't appear more than once */
10383 if (used_ports & port_mask)
10384 return false;
10385
10386 used_ports |= port_mask;
10387 default:
10388 break;
10389 }
10390 }
10391
10392 return true;
10393}
10394
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010395static struct intel_crtc_state *
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010396intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010397 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010398 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +020010399{
10400 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +020010401 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010402 struct intel_crtc_state *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +010010403 int plane_bpp, ret = -EINVAL;
10404 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020010405
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010406 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010407 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10408 return ERR_PTR(-EINVAL);
10409 }
10410
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010411 if (!check_digital_port_conflicts(dev)) {
10412 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
10413 return ERR_PTR(-EINVAL);
10414 }
10415
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010416 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10417 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020010418 return ERR_PTR(-ENOMEM);
10419
Matt Roper07878242015-02-25 11:43:26 -080010420 pipe_config->base.crtc = crtc;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010421 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
10422 drm_mode_copy(&pipe_config->base.mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010423
Daniel Vettere143a212013-07-04 12:01:15 +020010424 pipe_config->cpu_transcoder =
10425 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010426 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010427
Imre Deak2960bc92013-07-30 13:36:32 +030010428 /*
10429 * Sanitize sync polarity flags based on requested ones. If neither
10430 * positive or negative polarity is requested, treat this as meaning
10431 * negative polarity.
10432 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010433 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030010434 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010435 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030010436
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010437 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030010438 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010439 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030010440
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010441 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10442 * plane pixel format and any sink constraints into account. Returns the
10443 * source plane bpp so that dithering can be selected on mismatches
10444 * after encoders and crtc also have had their say. */
10445 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10446 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010447 if (plane_bpp < 0)
10448 goto fail;
10449
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030010450 /*
10451 * Determine the real pipe dimensions. Note that stereo modes can
10452 * increase the actual pipe size due to the frame doubling and
10453 * insertion of additional space for blanks between the frame. This
10454 * is stored in the crtc timings. We use the requested mode to do this
10455 * computation to clearly distinguish it from the adjusted mode, which
10456 * can be changed by the connectors in the below retry loop.
10457 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010458 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080010459 &pipe_config->pipe_src_w,
10460 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030010461
Daniel Vettere29c22c2013-02-21 00:00:16 +010010462encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020010463 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020010464 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020010465 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010466
Daniel Vetter135c81b2013-07-21 21:37:09 +020010467 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010468 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
10469 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020010470
Daniel Vetter7758a112012-07-08 19:40:39 +020010471 /* Pass our mode to the connectors and the CRTC to give them a chance to
10472 * adjust it according to limitations or connector properties, and also
10473 * a chance to reject the mode entirely.
10474 */
Damien Lespiaub2784e12014-08-05 11:29:37 +010010475 for_each_intel_encoder(dev, encoder) {
Daniel Vetter7758a112012-07-08 19:40:39 +020010476
10477 if (&encoder->new_crtc->base != crtc)
10478 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +010010479
Daniel Vetterefea6e82013-07-21 21:36:59 +020010480 if (!(encoder->compute_config(encoder, pipe_config))) {
10481 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020010482 goto fail;
10483 }
10484 }
10485
Daniel Vetterff9a6752013-06-01 17:16:21 +020010486 /* Set default port clock if not overwritten by the encoder. Needs to be
10487 * done afterwards in case the encoder adjusts the mode. */
10488 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010489 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010010490 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010491
Daniel Vettera43f6e02013-06-07 23:10:32 +020010492 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010010493 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020010494 DRM_DEBUG_KMS("CRTC fixup failed\n");
10495 goto fail;
10496 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010010497
10498 if (ret == RETRY) {
10499 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10500 ret = -EINVAL;
10501 goto fail;
10502 }
10503
10504 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10505 retry = false;
10506 goto encoder_retry;
10507 }
10508
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010509 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10510 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10511 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10512
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010513 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020010514fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010515 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010010516 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +020010517}
10518
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010519/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10520 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10521static void
10522intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10523 unsigned *prepare_pipes, unsigned *disable_pipes)
10524{
10525 struct intel_crtc *intel_crtc;
10526 struct drm_device *dev = crtc->dev;
10527 struct intel_encoder *encoder;
10528 struct intel_connector *connector;
10529 struct drm_crtc *tmp_crtc;
10530
10531 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
10532
10533 /* Check which crtcs have changed outputs connected to them, these need
10534 * to be part of the prepare_pipes mask. We don't (yet) support global
10535 * modeset across multiple crtcs, so modeset_pipes will only have one
10536 * bit set at most. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010537 for_each_intel_connector(dev, connector) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010538 if (connector->base.encoder == &connector->new_encoder->base)
10539 continue;
10540
10541 if (connector->base.encoder) {
10542 tmp_crtc = connector->base.encoder->crtc;
10543
10544 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10545 }
10546
10547 if (connector->new_encoder)
10548 *prepare_pipes |=
10549 1 << connector->new_encoder->new_crtc->pipe;
10550 }
10551
Damien Lespiaub2784e12014-08-05 11:29:37 +010010552 for_each_intel_encoder(dev, encoder) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010553 if (encoder->base.crtc == &encoder->new_crtc->base)
10554 continue;
10555
10556 if (encoder->base.crtc) {
10557 tmp_crtc = encoder->base.crtc;
10558
10559 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10560 }
10561
10562 if (encoder->new_crtc)
10563 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
10564 }
10565
Ville Syrjälä76688512014-01-10 11:28:06 +020010566 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010567 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010568 if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010569 continue;
10570
Ville Syrjälä76688512014-01-10 11:28:06 +020010571 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010572 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +020010573 else
10574 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010575 }
10576
10577
10578 /* set_mode is also used to update properties on life display pipes. */
10579 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +020010580 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010581 *prepare_pipes |= 1 << intel_crtc->pipe;
10582
Daniel Vetterb6c51642013-04-12 18:48:43 +020010583 /*
10584 * For simplicity do a full modeset on any pipe where the output routing
10585 * changed. We could be more clever, but that would require us to be
10586 * more careful with calling the relevant encoder->mode_set functions.
10587 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010588 if (*prepare_pipes)
10589 *modeset_pipes = *prepare_pipes;
10590
10591 /* ... and mask these out. */
10592 *modeset_pipes &= ~(*disable_pipes);
10593 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020010594
10595 /*
10596 * HACK: We don't (yet) fully support global modesets. intel_set_config
10597 * obies this rule, but the modeset restore mode of
10598 * intel_modeset_setup_hw_state does not.
10599 */
10600 *modeset_pipes &= 1 << intel_crtc->pipe;
10601 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020010602
10603 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10604 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010605}
10606
Daniel Vetterea9d7582012-07-10 10:42:52 +020010607static bool intel_crtc_in_use(struct drm_crtc *crtc)
10608{
10609 struct drm_encoder *encoder;
10610 struct drm_device *dev = crtc->dev;
10611
10612 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10613 if (encoder->crtc == crtc)
10614 return true;
10615
10616 return false;
10617}
10618
10619static void
10620intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10621{
Daniel Vetterba41c0de2014-11-03 15:04:55 +010010622 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020010623 struct intel_encoder *intel_encoder;
10624 struct intel_crtc *intel_crtc;
10625 struct drm_connector *connector;
10626
Daniel Vetterba41c0de2014-11-03 15:04:55 +010010627 intel_shared_dpll_commit(dev_priv);
10628
Damien Lespiaub2784e12014-08-05 11:29:37 +010010629 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020010630 if (!intel_encoder->base.crtc)
10631 continue;
10632
10633 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10634
10635 if (prepare_pipes & (1 << intel_crtc->pipe))
10636 intel_encoder->connectors_active = false;
10637 }
10638
10639 intel_modeset_commit_output_state(dev);
10640
Ville Syrjälä76688512014-01-10 11:28:06 +020010641 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010642 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010643 WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010644 WARN_ON(intel_crtc->new_config &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010645 intel_crtc->new_config != intel_crtc->config);
Matt Roper83d65732015-02-25 13:12:16 -080010646 WARN_ON(intel_crtc->base.state->enable != !!intel_crtc->new_config);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010647 }
10648
10649 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10650 if (!connector->encoder || !connector->encoder->crtc)
10651 continue;
10652
10653 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10654
10655 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020010656 struct drm_property *dpms_property =
10657 dev->mode_config.dpms_property;
10658
Daniel Vetterea9d7582012-07-10 10:42:52 +020010659 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050010660 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020010661 dpms_property,
10662 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010663
10664 intel_encoder = to_intel_encoder(connector->encoder);
10665 intel_encoder->connectors_active = true;
10666 }
10667 }
10668
10669}
10670
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010671static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010672{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010673 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010674
10675 if (clock1 == clock2)
10676 return true;
10677
10678 if (!clock1 || !clock2)
10679 return false;
10680
10681 diff = abs(clock1 - clock2);
10682
10683 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10684 return true;
10685
10686 return false;
10687}
10688
Daniel Vetter25c5b262012-07-08 22:08:04 +020010689#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10690 list_for_each_entry((intel_crtc), \
10691 &(dev)->mode_config.crtc_list, \
10692 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020010693 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020010694
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010695static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010696intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010697 struct intel_crtc_state *current_config,
10698 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010699{
Daniel Vetter66e985c2013-06-05 13:34:20 +020010700#define PIPE_CONF_CHECK_X(name) \
10701 if (current_config->name != pipe_config->name) { \
10702 DRM_ERROR("mismatch in " #name " " \
10703 "(expected 0x%08x, found 0x%08x)\n", \
10704 current_config->name, \
10705 pipe_config->name); \
10706 return false; \
10707 }
10708
Daniel Vetter08a24032013-04-19 11:25:34 +020010709#define PIPE_CONF_CHECK_I(name) \
10710 if (current_config->name != pipe_config->name) { \
10711 DRM_ERROR("mismatch in " #name " " \
10712 "(expected %i, found %i)\n", \
10713 current_config->name, \
10714 pipe_config->name); \
10715 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010010716 }
10717
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010718/* This is required for BDW+ where there is only one set of registers for
10719 * switching between high and low RR.
10720 * This macro can be used whenever a comparison has to be made between one
10721 * hw state and multiple sw state variables.
10722 */
10723#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10724 if ((current_config->name != pipe_config->name) && \
10725 (current_config->alt_name != pipe_config->name)) { \
10726 DRM_ERROR("mismatch in " #name " " \
10727 "(expected %i or %i, found %i)\n", \
10728 current_config->name, \
10729 current_config->alt_name, \
10730 pipe_config->name); \
10731 return false; \
10732 }
10733
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010734#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10735 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070010736 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010737 "(expected %i, found %i)\n", \
10738 current_config->name & (mask), \
10739 pipe_config->name & (mask)); \
10740 return false; \
10741 }
10742
Ville Syrjälä5e550652013-09-06 23:29:07 +030010743#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10744 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10745 DRM_ERROR("mismatch in " #name " " \
10746 "(expected %i, found %i)\n", \
10747 current_config->name, \
10748 pipe_config->name); \
10749 return false; \
10750 }
10751
Daniel Vetterbb760062013-06-06 14:55:52 +020010752#define PIPE_CONF_QUIRK(quirk) \
10753 ((current_config->quirks | pipe_config->quirks) & (quirk))
10754
Daniel Vettereccb1402013-05-22 00:50:22 +020010755 PIPE_CONF_CHECK_I(cpu_transcoder);
10756
Daniel Vetter08a24032013-04-19 11:25:34 +020010757 PIPE_CONF_CHECK_I(has_pch_encoder);
10758 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020010759 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10760 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10761 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10762 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10763 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020010764
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010765 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010766
10767 if (INTEL_INFO(dev)->gen < 8) {
10768 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10769 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10770 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10771 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10772 PIPE_CONF_CHECK_I(dp_m_n.tu);
10773
10774 if (current_config->has_drrs) {
10775 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
10776 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
10777 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
10778 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
10779 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
10780 }
10781 } else {
10782 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
10783 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
10784 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
10785 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
10786 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
10787 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010788
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010789 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
10790 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
10791 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
10792 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
10793 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
10794 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010795
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010796 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
10797 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
10798 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
10799 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
10800 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
10801 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010802
Daniel Vetterc93f54c2013-06-27 19:47:19 +020010803 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020010804 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020010805 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10806 IS_VALLEYVIEW(dev))
10807 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080010808 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020010809
Daniel Vetter9ed109a2014-04-24 23:54:52 +020010810 PIPE_CONF_CHECK_I(has_audio);
10811
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010812 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010813 DRM_MODE_FLAG_INTERLACE);
10814
Daniel Vetterbb760062013-06-06 14:55:52 +020010815 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010816 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020010817 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010818 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020010819 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010820 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020010821 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010822 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020010823 DRM_MODE_FLAG_NVSYNC);
10824 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010825
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010826 PIPE_CONF_CHECK_I(pipe_src_w);
10827 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010828
Daniel Vetter99535992014-04-13 12:00:33 +020010829 /*
10830 * FIXME: BIOS likes to set up a cloned config with lvds+external
10831 * screen. Since we don't yet re-compute the pipe config when moving
10832 * just the lvds port away to another pipe the sw tracking won't match.
10833 *
10834 * Proper atomic modesets with recomputed global state will fix this.
10835 * Until then just don't check gmch state for inherited modes.
10836 */
10837 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10838 PIPE_CONF_CHECK_I(gmch_pfit.control);
10839 /* pfit ratios are autocomputed by the hw on gen4+ */
10840 if (INTEL_INFO(dev)->gen < 4)
10841 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10842 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10843 }
10844
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010845 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10846 if (current_config->pch_pfit.enabled) {
10847 PIPE_CONF_CHECK_I(pch_pfit.pos);
10848 PIPE_CONF_CHECK_I(pch_pfit.size);
10849 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010850
Jesse Barnese59150d2014-01-07 13:30:45 -080010851 /* BDW+ don't expose a synchronous way to read the state */
10852 if (IS_HASWELL(dev))
10853 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010854
Ville Syrjälä282740f2013-09-04 18:30:03 +030010855 PIPE_CONF_CHECK_I(double_wide);
10856
Daniel Vetter26804af2014-06-25 22:01:55 +030010857 PIPE_CONF_CHECK_X(ddi_pll_sel);
10858
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010859 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010860 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020010861 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010862 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10863 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010864 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000010865 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
10866 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
10867 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010868
Ville Syrjälä42571ae2013-09-06 23:29:00 +030010869 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10870 PIPE_CONF_CHECK_I(pipe_bpp);
10871
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010872 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080010873 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030010874
Daniel Vetter66e985c2013-06-05 13:34:20 +020010875#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020010876#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010877#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010878#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030010879#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020010880#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010881
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010882 return true;
10883}
10884
Damien Lespiau08db6652014-11-04 17:06:52 +000010885static void check_wm_state(struct drm_device *dev)
10886{
10887 struct drm_i915_private *dev_priv = dev->dev_private;
10888 struct skl_ddb_allocation hw_ddb, *sw_ddb;
10889 struct intel_crtc *intel_crtc;
10890 int plane;
10891
10892 if (INTEL_INFO(dev)->gen < 9)
10893 return;
10894
10895 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
10896 sw_ddb = &dev_priv->wm.skl_hw.ddb;
10897
10898 for_each_intel_crtc(dev, intel_crtc) {
10899 struct skl_ddb_entry *hw_entry, *sw_entry;
10900 const enum pipe pipe = intel_crtc->pipe;
10901
10902 if (!intel_crtc->active)
10903 continue;
10904
10905 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000010906 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000010907 hw_entry = &hw_ddb.plane[pipe][plane];
10908 sw_entry = &sw_ddb->plane[pipe][plane];
10909
10910 if (skl_ddb_entry_equal(hw_entry, sw_entry))
10911 continue;
10912
10913 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
10914 "(expected (%u,%u), found (%u,%u))\n",
10915 pipe_name(pipe), plane + 1,
10916 sw_entry->start, sw_entry->end,
10917 hw_entry->start, hw_entry->end);
10918 }
10919
10920 /* cursor */
10921 hw_entry = &hw_ddb.cursor[pipe];
10922 sw_entry = &sw_ddb->cursor[pipe];
10923
10924 if (skl_ddb_entry_equal(hw_entry, sw_entry))
10925 continue;
10926
10927 DRM_ERROR("mismatch in DDB state pipe %c cursor "
10928 "(expected (%u,%u), found (%u,%u))\n",
10929 pipe_name(pipe),
10930 sw_entry->start, sw_entry->end,
10931 hw_entry->start, hw_entry->end);
10932 }
10933}
10934
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010935static void
10936check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010937{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010938 struct intel_connector *connector;
10939
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010940 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010941 /* This also checks the encoder/connector hw state with the
10942 * ->get_hw_state callbacks. */
10943 intel_connector_check_state(connector);
10944
Rob Clarke2c719b2014-12-15 13:56:32 -050010945 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010946 "connector's staged encoder doesn't match current encoder\n");
10947 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010948}
10949
10950static void
10951check_encoder_state(struct drm_device *dev)
10952{
10953 struct intel_encoder *encoder;
10954 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010955
Damien Lespiaub2784e12014-08-05 11:29:37 +010010956 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010957 bool enabled = false;
10958 bool active = false;
10959 enum pipe pipe, tracked_pipe;
10960
10961 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10962 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030010963 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010964
Rob Clarke2c719b2014-12-15 13:56:32 -050010965 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010966 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010967 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010968 "encoder's active_connectors set, but no crtc\n");
10969
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010970 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010971 if (connector->base.encoder != &encoder->base)
10972 continue;
10973 enabled = true;
10974 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10975 active = true;
10976 }
Dave Airlie0e32b392014-05-02 14:02:48 +100010977 /*
10978 * for MST connectors if we unplug the connector is gone
10979 * away but the encoder is still connected to a crtc
10980 * until a modeset happens in response to the hotplug.
10981 */
10982 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
10983 continue;
10984
Rob Clarke2c719b2014-12-15 13:56:32 -050010985 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010986 "encoder's enabled state mismatch "
10987 "(expected %i, found %i)\n",
10988 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050010989 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010990 "active encoder with no crtc\n");
10991
Rob Clarke2c719b2014-12-15 13:56:32 -050010992 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010993 "encoder's computed active state doesn't match tracked active state "
10994 "(expected %i, found %i)\n", active, encoder->connectors_active);
10995
10996 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050010997 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010998 "encoder's hw state doesn't match sw tracking "
10999 "(expected %i, found %i)\n",
11000 encoder->connectors_active, active);
11001
11002 if (!encoder->base.crtc)
11003 continue;
11004
11005 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050011006 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011007 "active encoder's pipe doesn't match"
11008 "(expected %i, found %i)\n",
11009 tracked_pipe, pipe);
11010
11011 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011012}
11013
11014static void
11015check_crtc_state(struct drm_device *dev)
11016{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011017 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011018 struct intel_crtc *crtc;
11019 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011020 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011021
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011022 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011023 bool enabled = false;
11024 bool active = false;
11025
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011026 memset(&pipe_config, 0, sizeof(pipe_config));
11027
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011028 DRM_DEBUG_KMS("[CRTC:%d]\n",
11029 crtc->base.base.id);
11030
Matt Roper83d65732015-02-25 13:12:16 -080011031 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011032 "active crtc, but not enabled in sw tracking\n");
11033
Damien Lespiaub2784e12014-08-05 11:29:37 +010011034 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011035 if (encoder->base.crtc != &crtc->base)
11036 continue;
11037 enabled = true;
11038 if (encoder->connectors_active)
11039 active = true;
11040 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020011041
Rob Clarke2c719b2014-12-15 13:56:32 -050011042 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011043 "crtc's computed active state doesn't match tracked active state "
11044 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080011045 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011046 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080011047 "(expected %i, found %i)\n", enabled,
11048 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011049
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011050 active = dev_priv->display.get_pipe_config(crtc,
11051 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020011052
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030011053 /* hw state is inconsistent with the pipe quirk */
11054 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
11055 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020011056 active = crtc->active;
11057
Damien Lespiaub2784e12014-08-05 11:29:37 +010011058 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030011059 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020011060 if (encoder->base.crtc != &crtc->base)
11061 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010011062 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020011063 encoder->get_config(encoder, &pipe_config);
11064 }
11065
Rob Clarke2c719b2014-12-15 13:56:32 -050011066 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011067 "crtc active state doesn't match with hw state "
11068 "(expected %i, found %i)\n", crtc->active, active);
11069
Daniel Vetterc0b03412013-05-28 12:05:54 +020011070 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011071 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050011072 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020011073 intel_dump_pipe_config(crtc, &pipe_config,
11074 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011075 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011076 "[sw state]");
11077 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011078 }
11079}
11080
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011081static void
11082check_shared_dpll_state(struct drm_device *dev)
11083{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011084 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011085 struct intel_crtc *crtc;
11086 struct intel_dpll_hw_state dpll_hw_state;
11087 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020011088
11089 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11090 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11091 int enabled_crtcs = 0, active_crtcs = 0;
11092 bool active;
11093
11094 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
11095
11096 DRM_DEBUG_KMS("%s\n", pll->name);
11097
11098 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
11099
Rob Clarke2c719b2014-12-15 13:56:32 -050011100 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020011101 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011102 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050011103 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020011104 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011105 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020011106 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011107 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020011108 "pll on state mismatch (expected %i, found %i)\n",
11109 pll->on, active);
11110
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011111 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011112 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020011113 enabled_crtcs++;
11114 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11115 active_crtcs++;
11116 }
Rob Clarke2c719b2014-12-15 13:56:32 -050011117 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020011118 "pll active crtcs mismatch (expected %i, found %i)\n",
11119 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050011120 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020011121 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011122 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011123
Rob Clarke2c719b2014-12-15 13:56:32 -050011124 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020011125 sizeof(dpll_hw_state)),
11126 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020011127 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011128}
11129
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011130void
11131intel_modeset_check_state(struct drm_device *dev)
11132{
Damien Lespiau08db6652014-11-04 17:06:52 +000011133 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011134 check_connector_state(dev);
11135 check_encoder_state(dev);
11136 check_crtc_state(dev);
11137 check_shared_dpll_state(dev);
11138}
11139
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011140void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030011141 int dotclock)
11142{
11143 /*
11144 * FDI already provided one idea for the dotclock.
11145 * Yell if the encoder disagrees.
11146 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011147 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030011148 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011149 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030011150}
11151
Ville Syrjälä80715b22014-05-15 20:23:23 +030011152static void update_scanline_offset(struct intel_crtc *crtc)
11153{
11154 struct drm_device *dev = crtc->base.dev;
11155
11156 /*
11157 * The scanline counter increments at the leading edge of hsync.
11158 *
11159 * On most platforms it starts counting from vtotal-1 on the
11160 * first active line. That means the scanline counter value is
11161 * always one less than what we would expect. Ie. just after
11162 * start of vblank, which also occurs at start of hsync (on the
11163 * last active line), the scanline counter will read vblank_start-1.
11164 *
11165 * On gen2 the scanline counter starts counting from 1 instead
11166 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11167 * to keep the value positive), instead of adding one.
11168 *
11169 * On HSW+ the behaviour of the scanline counter depends on the output
11170 * type. For DP ports it behaves like most other platforms, but on HDMI
11171 * there's an extra 1 line difference. So we need to add two instead of
11172 * one to the value.
11173 */
11174 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011175 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030011176 int vtotal;
11177
11178 vtotal = mode->crtc_vtotal;
11179 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
11180 vtotal /= 2;
11181
11182 crtc->scanline_offset = vtotal - 1;
11183 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030011184 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030011185 crtc->scanline_offset = 2;
11186 } else
11187 crtc->scanline_offset = 1;
11188}
11189
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011190static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011191intel_modeset_compute_config(struct drm_crtc *crtc,
11192 struct drm_display_mode *mode,
11193 struct drm_framebuffer *fb,
11194 unsigned *modeset_pipes,
11195 unsigned *prepare_pipes,
11196 unsigned *disable_pipes)
11197{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011198 struct intel_crtc_state *pipe_config = NULL;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011199
11200 intel_modeset_affected_pipes(crtc, modeset_pipes,
11201 prepare_pipes, disable_pipes);
11202
11203 if ((*modeset_pipes) == 0)
11204 goto out;
11205
11206 /*
11207 * Note this needs changes when we start tracking multiple modes
11208 * and crtcs. At that point we'll need to compute the whole config
11209 * (i.e. one pipe_config for each crtc) rather than just the one
11210 * for this crtc.
11211 */
11212 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
11213 if (IS_ERR(pipe_config)) {
11214 goto out;
11215 }
11216 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
11217 "[modeset]");
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011218
11219out:
11220 return pipe_config;
11221}
11222
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011223static int __intel_set_mode_setup_plls(struct drm_device *dev,
11224 unsigned modeset_pipes,
11225 unsigned disable_pipes)
11226{
11227 struct drm_i915_private *dev_priv = to_i915(dev);
11228 unsigned clear_pipes = modeset_pipes | disable_pipes;
11229 struct intel_crtc *intel_crtc;
11230 int ret = 0;
11231
11232 if (!dev_priv->display.crtc_compute_clock)
11233 return 0;
11234
11235 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
11236 if (ret)
11237 goto done;
11238
11239 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
11240 struct intel_crtc_state *state = intel_crtc->new_config;
11241 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11242 state);
11243 if (ret) {
11244 intel_shared_dpll_abort_config(dev_priv);
11245 goto done;
11246 }
11247 }
11248
11249done:
11250 return ret;
11251}
11252
Daniel Vetterf30da182013-04-11 20:22:50 +020011253static int __intel_set_mode(struct drm_crtc *crtc,
11254 struct drm_display_mode *mode,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011255 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011256 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011257 unsigned modeset_pipes,
11258 unsigned prepare_pipes,
11259 unsigned disable_pipes)
Daniel Vettera6778b32012-07-02 09:56:42 +020011260{
11261 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030011262 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011263 struct drm_display_mode *saved_mode;
Daniel Vetter25c5b262012-07-08 22:08:04 +020011264 struct intel_crtc *intel_crtc;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011265 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020011266
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011267 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011268 if (!saved_mode)
11269 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020011270
Tim Gardner3ac18232012-12-07 07:54:26 -070011271 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020011272
Ville Syrjäläb9950a12014-11-21 21:00:36 +020011273 if (modeset_pipes)
11274 to_intel_crtc(crtc)->new_config = pipe_config;
11275
Jesse Barnes30a970c2013-11-04 13:48:12 -080011276 /*
11277 * See if the config requires any additional preparation, e.g.
11278 * to adjust global state with pipes off. We need to do this
11279 * here so we can get the modeset_pipe updated config for the new
11280 * mode set on this crtc. For other crtcs we need to use the
11281 * adjusted_mode bits in the crtc directly.
11282 */
Ville Syrjäläc164f832013-11-05 22:34:12 +020011283 if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +020011284 valleyview_modeset_global_pipes(dev, &prepare_pipes);
Jesse Barnes30a970c2013-11-04 13:48:12 -080011285
Ville Syrjäläc164f832013-11-05 22:34:12 +020011286 /* may have added more to prepare_pipes than we should */
11287 prepare_pipes &= ~disable_pipes;
11288 }
11289
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011290 ret = __intel_set_mode_setup_plls(dev, modeset_pipes, disable_pipes);
11291 if (ret)
11292 goto done;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +020011293
Daniel Vetter460da9162013-03-27 00:44:51 +010011294 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
11295 intel_crtc_disable(&intel_crtc->base);
11296
Daniel Vetterea9d7582012-07-10 10:42:52 +020011297 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011298 if (intel_crtc->base.state->enable)
Daniel Vetterea9d7582012-07-10 10:42:52 +020011299 dev_priv->display.crtc_disable(&intel_crtc->base);
11300 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011301
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020011302 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
11303 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011304 *
11305 * Note we'll need to fix this up when we start tracking multiple
11306 * pipes; here we assume a single modeset_pipe and only track the
11307 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020011308 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011309 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020011310 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011311 /* mode_set/enable/disable functions rely on a correct pipe
11312 * config. */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020011313 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020011314
11315 /*
11316 * Calculate and store various constants which
11317 * are later needed by vblank and swap-completion
11318 * timestamping. They are derived from true hwmode.
11319 */
11320 drm_calc_timestamping_constants(crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011321 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011322 }
Daniel Vetter7758a112012-07-08 19:40:39 +020011323
Daniel Vetterea9d7582012-07-10 10:42:52 +020011324 /* Only after disabling all output pipelines that will be changed can we
11325 * update the the output configuration. */
11326 intel_modeset_update_state(dev, prepare_pipes);
11327
Ville Syrjälä50f6e502014-11-06 14:49:12 +020011328 modeset_update_crtc_power_domains(dev);
Daniel Vetter47fab732012-10-26 10:58:18 +020011329
Daniel Vettera6778b32012-07-02 09:56:42 +020011330 /* Set up the DPLL and any encoders state that needs to adjust or depend
11331 * on the DPLL.
11332 */
Daniel Vetter25c5b262012-07-08 22:08:04 +020011333 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Gustavo Padovan455a6802014-12-01 15:40:11 -080011334 struct drm_plane *primary = intel_crtc->base.primary;
11335 int vdisplay, hdisplay;
Daniel Vetter4c107942014-04-24 23:55:05 +020011336
Gustavo Padovan455a6802014-12-01 15:40:11 -080011337 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
11338 ret = primary->funcs->update_plane(primary, &intel_crtc->base,
11339 fb, 0, 0,
11340 hdisplay, vdisplay,
11341 x << 16, y << 16,
11342 hdisplay << 16, vdisplay << 16);
Daniel Vettera6778b32012-07-02 09:56:42 +020011343 }
11344
11345 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030011346 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11347 update_scanline_offset(intel_crtc);
11348
Daniel Vetter25c5b262012-07-08 22:08:04 +020011349 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030011350 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011351
Daniel Vettera6778b32012-07-02 09:56:42 +020011352 /* FIXME: add subpixel order */
11353done:
Matt Roper83d65732015-02-25 13:12:16 -080011354 if (ret && crtc->state->enable)
Tim Gardner3ac18232012-12-07 07:54:26 -070011355 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020011356
Tim Gardner3ac18232012-12-07 07:54:26 -070011357 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020011358 return ret;
11359}
11360
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011361static int intel_set_mode_pipes(struct drm_crtc *crtc,
11362 struct drm_display_mode *mode,
11363 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011364 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011365 unsigned modeset_pipes,
11366 unsigned prepare_pipes,
11367 unsigned disable_pipes)
11368{
11369 int ret;
11370
11371 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
11372 prepare_pipes, disable_pipes);
11373
11374 if (ret == 0)
11375 intel_modeset_check_state(crtc->dev);
11376
11377 return ret;
11378}
11379
Damien Lespiaue7457a92013-08-08 22:28:59 +010011380static int intel_set_mode(struct drm_crtc *crtc,
11381 struct drm_display_mode *mode,
11382 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +020011383{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011384 struct intel_crtc_state *pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011385 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Daniel Vetterf30da182013-04-11 20:22:50 +020011386
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011387 pipe_config = intel_modeset_compute_config(crtc, mode, fb,
11388 &modeset_pipes,
11389 &prepare_pipes,
11390 &disable_pipes);
Daniel Vetterf30da182013-04-11 20:22:50 +020011391
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011392 if (IS_ERR(pipe_config))
11393 return PTR_ERR(pipe_config);
Daniel Vetterf30da182013-04-11 20:22:50 +020011394
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011395 return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
11396 modeset_pipes, prepare_pipes,
11397 disable_pipes);
Daniel Vetterf30da182013-04-11 20:22:50 +020011398}
11399
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011400void intel_crtc_restore_mode(struct drm_crtc *crtc)
11401{
Matt Roperf4510a22014-04-01 15:22:40 -070011402 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011403}
11404
Daniel Vetter25c5b262012-07-08 22:08:04 +020011405#undef for_each_intel_crtc_masked
11406
Daniel Vetterd9e55602012-07-04 22:16:09 +020011407static void intel_set_config_free(struct intel_set_config *config)
11408{
11409 if (!config)
11410 return;
11411
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011412 kfree(config->save_connector_encoders);
11413 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020011414 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020011415 kfree(config);
11416}
11417
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011418static int intel_set_config_save_state(struct drm_device *dev,
11419 struct intel_set_config *config)
11420{
Ville Syrjälä76688512014-01-10 11:28:06 +020011421 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011422 struct drm_encoder *encoder;
11423 struct drm_connector *connector;
11424 int count;
11425
Ville Syrjälä76688512014-01-10 11:28:06 +020011426 config->save_crtc_enabled =
11427 kcalloc(dev->mode_config.num_crtc,
11428 sizeof(bool), GFP_KERNEL);
11429 if (!config->save_crtc_enabled)
11430 return -ENOMEM;
11431
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011432 config->save_encoder_crtcs =
11433 kcalloc(dev->mode_config.num_encoder,
11434 sizeof(struct drm_crtc *), GFP_KERNEL);
11435 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011436 return -ENOMEM;
11437
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011438 config->save_connector_encoders =
11439 kcalloc(dev->mode_config.num_connector,
11440 sizeof(struct drm_encoder *), GFP_KERNEL);
11441 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011442 return -ENOMEM;
11443
11444 /* Copy data. Note that driver private data is not affected.
11445 * Should anything bad happen only the expected state is
11446 * restored, not the drivers personal bookkeeping.
11447 */
11448 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010011449 for_each_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011450 config->save_crtc_enabled[count++] = crtc->state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011451 }
11452
11453 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011454 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011455 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011456 }
11457
11458 count = 0;
11459 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011460 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011461 }
11462
11463 return 0;
11464}
11465
11466static void intel_set_config_restore_state(struct drm_device *dev,
11467 struct intel_set_config *config)
11468{
Ville Syrjälä76688512014-01-10 11:28:06 +020011469 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011470 struct intel_encoder *encoder;
11471 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011472 int count;
11473
11474 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011475 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011476 crtc->new_enabled = config->save_crtc_enabled[count++];
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011477
11478 if (crtc->new_enabled)
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011479 crtc->new_config = crtc->config;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011480 else
11481 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020011482 }
11483
11484 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010011485 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011486 encoder->new_crtc =
11487 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011488 }
11489
11490 count = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011491 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011492 connector->new_encoder =
11493 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011494 }
11495}
11496
Imre Deake3de42b2013-05-03 19:44:07 +020011497static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010011498is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020011499{
11500 int i;
11501
Chris Wilson2e57f472013-07-17 12:14:40 +010011502 if (set->num_connectors == 0)
11503 return false;
11504
11505 if (WARN_ON(set->connectors == NULL))
11506 return false;
11507
11508 for (i = 0; i < set->num_connectors; i++)
11509 if (set->connectors[i]->encoder &&
11510 set->connectors[i]->encoder->crtc == set->crtc &&
11511 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020011512 return true;
11513
11514 return false;
11515}
11516
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011517static void
11518intel_set_config_compute_mode_changes(struct drm_mode_set *set,
11519 struct intel_set_config *config)
11520{
11521
11522 /* We should be able to check here if the fb has the same properties
11523 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010011524 if (is_crtc_connector_off(set)) {
11525 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070011526 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070011527 /*
11528 * If we have no fb, we can only flip as long as the crtc is
11529 * active, otherwise we need a full mode set. The crtc may
11530 * be active if we've only disabled the primary plane, or
11531 * in fastboot situations.
11532 */
Matt Roperf4510a22014-04-01 15:22:40 -070011533 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030011534 struct intel_crtc *intel_crtc =
11535 to_intel_crtc(set->crtc);
11536
Matt Roper3b150f02014-05-29 08:06:53 -070011537 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030011538 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
11539 config->fb_changed = true;
11540 } else {
11541 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
11542 config->mode_changed = true;
11543 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011544 } else if (set->fb == NULL) {
11545 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010011546 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070011547 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011548 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020011549 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011550 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020011551 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011552 }
11553
Daniel Vetter835c5872012-07-10 18:11:08 +020011554 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011555 config->fb_changed = true;
11556
11557 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
11558 DRM_DEBUG_KMS("modes are different, full mode set\n");
11559 drm_mode_debug_printmodeline(&set->crtc->mode);
11560 drm_mode_debug_printmodeline(set->mode);
11561 config->mode_changed = true;
11562 }
Chris Wilsona1d95702013-08-13 18:48:47 +010011563
11564 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
11565 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011566}
11567
Daniel Vetter2e431052012-07-04 22:42:15 +020011568static int
Daniel Vetter9a935852012-07-05 22:34:27 +020011569intel_modeset_stage_output_state(struct drm_device *dev,
11570 struct drm_mode_set *set,
11571 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +020011572{
Daniel Vetter9a935852012-07-05 22:34:27 +020011573 struct intel_connector *connector;
11574 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020011575 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030011576 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020011577
Damien Lespiau9abdda72013-02-13 13:29:23 +000011578 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020011579 * of connectors. For paranoia, double-check this. */
11580 WARN_ON(!set->fb && (set->num_connectors != 0));
11581 WARN_ON(set->fb && (set->num_connectors == 0));
11582
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011583 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011584 /* Otherwise traverse passed in connector list and get encoders
11585 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020011586 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011587 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100011588 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020011589 break;
11590 }
11591 }
11592
Daniel Vetter9a935852012-07-05 22:34:27 +020011593 /* If we disable the crtc, disable all its connectors. Also, if
11594 * the connector is on the changing crtc but not on the new
11595 * connector list, disable it. */
11596 if ((!set->fb || ro == set->num_connectors) &&
11597 connector->base.encoder &&
11598 connector->base.encoder->crtc == set->crtc) {
11599 connector->new_encoder = NULL;
11600
11601 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11602 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011603 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020011604 }
11605
11606
11607 if (&connector->new_encoder->base != connector->base.encoder) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020011608 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
11609 connector->base.base.id,
11610 connector->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011611 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020011612 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011613 }
11614 /* connector->new_encoder is now updated for all connectors. */
11615
11616 /* Update crtc of enabled connectors. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011617 for_each_intel_connector(dev, connector) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011618 struct drm_crtc *new_crtc;
11619
Daniel Vetter9a935852012-07-05 22:34:27 +020011620 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020011621 continue;
11622
Daniel Vetter9a935852012-07-05 22:34:27 +020011623 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020011624
11625 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011626 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020011627 new_crtc = set->crtc;
11628 }
11629
11630 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010011631 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11632 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011633 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020011634 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011635 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020011636
11637 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11638 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011639 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020011640 new_crtc->base.id);
11641 }
11642
11643 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010011644 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011645 int num_connectors = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011646 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011647 if (connector->new_encoder == encoder) {
11648 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011649 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020011650 }
11651 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011652
11653 if (num_connectors == 0)
11654 encoder->new_crtc = NULL;
11655 else if (num_connectors > 1)
11656 return -EINVAL;
11657
Daniel Vetter9a935852012-07-05 22:34:27 +020011658 /* Only now check for crtc changes so we don't miss encoders
11659 * that will be disabled. */
11660 if (&encoder->new_crtc->base != encoder->base.crtc) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020011661 DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
11662 encoder->base.base.id,
11663 encoder->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011664 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020011665 }
11666 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011667 /* Now we've also updated encoder->new_crtc for all encoders. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011668 for_each_intel_connector(dev, connector) {
Dave Airlie0e32b392014-05-02 14:02:48 +100011669 if (connector->new_encoder)
11670 if (connector->new_encoder != connector->encoder)
11671 connector->encoder = connector->new_encoder;
11672 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011673 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011674 crtc->new_enabled = false;
11675
Damien Lespiaub2784e12014-08-05 11:29:37 +010011676 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011677 if (encoder->new_crtc == crtc) {
11678 crtc->new_enabled = true;
11679 break;
11680 }
11681 }
11682
Matt Roper83d65732015-02-25 13:12:16 -080011683 if (crtc->new_enabled != crtc->base.state->enable) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020011684 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
11685 crtc->base.base.id,
Ville Syrjälä76688512014-01-10 11:28:06 +020011686 crtc->new_enabled ? "en" : "dis");
11687 config->mode_changed = true;
11688 }
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011689
11690 if (crtc->new_enabled)
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011691 crtc->new_config = crtc->config;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011692 else
11693 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020011694 }
11695
Daniel Vetter2e431052012-07-04 22:42:15 +020011696 return 0;
11697}
11698
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011699static void disable_crtc_nofb(struct intel_crtc *crtc)
11700{
11701 struct drm_device *dev = crtc->base.dev;
11702 struct intel_encoder *encoder;
11703 struct intel_connector *connector;
11704
11705 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
11706 pipe_name(crtc->pipe));
11707
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011708 for_each_intel_connector(dev, connector) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011709 if (connector->new_encoder &&
11710 connector->new_encoder->new_crtc == crtc)
11711 connector->new_encoder = NULL;
11712 }
11713
Damien Lespiaub2784e12014-08-05 11:29:37 +010011714 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011715 if (encoder->new_crtc == crtc)
11716 encoder->new_crtc = NULL;
11717 }
11718
11719 crtc->new_enabled = false;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011720 crtc->new_config = NULL;
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011721}
11722
Daniel Vetter2e431052012-07-04 22:42:15 +020011723static int intel_crtc_set_config(struct drm_mode_set *set)
11724{
11725 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020011726 struct drm_mode_set save_set;
11727 struct intel_set_config *config;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011728 struct intel_crtc_state *pipe_config;
Jesse Barnes50f52752014-11-07 13:11:00 -080011729 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Daniel Vetter2e431052012-07-04 22:42:15 +020011730 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020011731
Daniel Vetter8d3e3752012-07-05 16:09:09 +020011732 BUG_ON(!set);
11733 BUG_ON(!set->crtc);
11734 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020011735
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010011736 /* Enforce sane interface api - has been abused by the fb helper. */
11737 BUG_ON(!set->mode && set->fb);
11738 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020011739
Daniel Vetter2e431052012-07-04 22:42:15 +020011740 if (set->fb) {
11741 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
11742 set->crtc->base.id, set->fb->base.id,
11743 (int)set->num_connectors, set->x, set->y);
11744 } else {
11745 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020011746 }
11747
11748 dev = set->crtc->dev;
11749
11750 ret = -ENOMEM;
11751 config = kzalloc(sizeof(*config), GFP_KERNEL);
11752 if (!config)
11753 goto out_config;
11754
11755 ret = intel_set_config_save_state(dev, config);
11756 if (ret)
11757 goto out_config;
11758
11759 save_set.crtc = set->crtc;
11760 save_set.mode = &set->crtc->mode;
11761 save_set.x = set->crtc->x;
11762 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070011763 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020011764
11765 /* Compute whether we need a full modeset, only an fb base update or no
11766 * change at all. In the future we might also check whether only the
11767 * mode changed, e.g. for LVDS where we only change the panel fitter in
11768 * such cases. */
11769 intel_set_config_compute_mode_changes(set, config);
11770
Daniel Vetter9a935852012-07-05 22:34:27 +020011771 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +020011772 if (ret)
11773 goto fail;
11774
Jesse Barnes50f52752014-11-07 13:11:00 -080011775 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
11776 set->fb,
11777 &modeset_pipes,
11778 &prepare_pipes,
11779 &disable_pipes);
Jesse Barnes20664592014-11-05 14:26:09 -080011780 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080011781 ret = PTR_ERR(pipe_config);
Jesse Barnes50f52752014-11-07 13:11:00 -080011782 goto fail;
Jesse Barnes20664592014-11-05 14:26:09 -080011783 } else if (pipe_config) {
Ville Syrjäläb9950a12014-11-21 21:00:36 +020011784 if (pipe_config->has_audio !=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011785 to_intel_crtc(set->crtc)->config->has_audio)
Jesse Barnes20664592014-11-05 14:26:09 -080011786 config->mode_changed = true;
11787
Jesse Barnesaf15d2c2014-12-01 09:54:28 -080011788 /*
11789 * Note we have an issue here with infoframes: current code
11790 * only updates them on the full mode set path per hw
11791 * requirements. So here we should be checking for any
11792 * required changes and forcing a mode set.
11793 */
Jesse Barnes20664592014-11-05 14:26:09 -080011794 }
Jesse Barnes50f52752014-11-07 13:11:00 -080011795
11796 /* set_mode will free it in the mode_changed case */
11797 if (!config->mode_changed)
11798 kfree(pipe_config);
11799
Jesse Barnes1f9954d2014-11-05 14:26:10 -080011800 intel_update_pipe_size(to_intel_crtc(set->crtc));
11801
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011802 if (config->mode_changed) {
Jesse Barnes50f52752014-11-07 13:11:00 -080011803 ret = intel_set_mode_pipes(set->crtc, set->mode,
11804 set->x, set->y, set->fb, pipe_config,
11805 modeset_pipes, prepare_pipes,
11806 disable_pipes);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011807 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070011808 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011809 struct drm_plane *primary = set->crtc->primary;
11810 int vdisplay, hdisplay;
Matt Roper3b150f02014-05-29 08:06:53 -070011811
Gustavo Padovan455a6802014-12-01 15:40:11 -080011812 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
11813 ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
11814 0, 0, hdisplay, vdisplay,
11815 set->x << 16, set->y << 16,
11816 hdisplay << 16, vdisplay << 16);
Matt Roper3b150f02014-05-29 08:06:53 -070011817
11818 /*
11819 * We need to make sure the primary plane is re-enabled if it
11820 * has previously been turned off.
11821 */
11822 if (!intel_crtc->primary_enabled && ret == 0) {
11823 WARN_ON(!intel_crtc->active);
Ville Syrjäläfdd508a62014-08-08 21:51:11 +030011824 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070011825 }
11826
Jesse Barnes7ca51a32014-01-07 13:50:49 -080011827 /*
11828 * In the fastboot case this may be our only check of the
11829 * state after boot. It would be better to only do it on
11830 * the first update, but we don't have a nice way of doing that
11831 * (and really, set_config isn't used much for high freq page
11832 * flipping, so increasing its cost here shouldn't be a big
11833 * deal).
11834 */
Jani Nikulad330a952014-01-21 11:24:25 +020011835 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080011836 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020011837 }
11838
Chris Wilson2d05eae2013-05-03 17:36:25 +010011839 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020011840 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11841 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020011842fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010011843 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020011844
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011845 /*
11846 * HACK: if the pipe was on, but we didn't have a framebuffer,
11847 * force the pipe off to avoid oopsing in the modeset code
11848 * due to fb==NULL. This should only happen during boot since
11849 * we don't yet reconstruct the FB from the hardware state.
11850 */
11851 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11852 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11853
Chris Wilson2d05eae2013-05-03 17:36:25 +010011854 /* Try to restore the config */
11855 if (config->mode_changed &&
11856 intel_set_mode(save_set.crtc, save_set.mode,
11857 save_set.x, save_set.y, save_set.fb))
11858 DRM_ERROR("failed to restore config after modeset failure\n");
11859 }
Daniel Vetter50f56112012-07-02 09:35:43 +020011860
Daniel Vetterd9e55602012-07-04 22:16:09 +020011861out_config:
11862 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020011863 return ret;
11864}
11865
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011866static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011867 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020011868 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011869 .destroy = intel_crtc_destroy,
11870 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080011871 .atomic_duplicate_state = intel_crtc_duplicate_state,
11872 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011873};
11874
Daniel Vetter53589012013-06-05 13:34:16 +020011875static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11876 struct intel_shared_dpll *pll,
11877 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011878{
Daniel Vetter53589012013-06-05 13:34:16 +020011879 uint32_t val;
11880
Daniel Vetterf458ebb2014-09-30 10:56:39 +020011881 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030011882 return false;
11883
Daniel Vetter53589012013-06-05 13:34:16 +020011884 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020011885 hw_state->dpll = val;
11886 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11887 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020011888
11889 return val & DPLL_VCO_ENABLE;
11890}
11891
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011892static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11893 struct intel_shared_dpll *pll)
11894{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011895 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
11896 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011897}
11898
Daniel Vettere7b903d2013-06-05 13:34:14 +020011899static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11900 struct intel_shared_dpll *pll)
11901{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011902 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020011903 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020011904
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011905 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011906
11907 /* Wait for the clocks to stabilize. */
11908 POSTING_READ(PCH_DPLL(pll->id));
11909 udelay(150);
11910
11911 /* The pixel multiplier can only be updated once the
11912 * DPLL is enabled and the clocks are stable.
11913 *
11914 * So write it again.
11915 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011916 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011917 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020011918 udelay(200);
11919}
11920
11921static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11922 struct intel_shared_dpll *pll)
11923{
11924 struct drm_device *dev = dev_priv->dev;
11925 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011926
11927 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011928 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020011929 if (intel_crtc_to_shared_dpll(crtc) == pll)
11930 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
11931 }
11932
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011933 I915_WRITE(PCH_DPLL(pll->id), 0);
11934 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020011935 udelay(200);
11936}
11937
Daniel Vetter46edb022013-06-05 13:34:12 +020011938static char *ibx_pch_dpll_names[] = {
11939 "PCH DPLL A",
11940 "PCH DPLL B",
11941};
11942
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011943static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011944{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011945 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011946 int i;
11947
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011948 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011949
Daniel Vettere72f9fb2013-06-05 13:34:06 +020011950 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020011951 dev_priv->shared_dplls[i].id = i;
11952 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011953 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011954 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11955 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020011956 dev_priv->shared_dplls[i].get_hw_state =
11957 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011958 }
11959}
11960
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011961static void intel_shared_dpll_init(struct drm_device *dev)
11962{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011963 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011964
Daniel Vetter9cd86932014-06-25 22:01:57 +030011965 if (HAS_DDI(dev))
11966 intel_ddi_pll_init(dev);
11967 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011968 ibx_pch_dpll_init(dev);
11969 else
11970 dev_priv->num_shared_dpll = 0;
11971
11972 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011973}
11974
Matt Roper6beb8c232014-12-01 15:40:14 -080011975/**
11976 * intel_prepare_plane_fb - Prepare fb for usage on plane
11977 * @plane: drm plane to prepare for
11978 * @fb: framebuffer to prepare for presentation
11979 *
11980 * Prepares a framebuffer for usage on a display plane. Generally this
11981 * involves pinning the underlying object and updating the frontbuffer tracking
11982 * bits. Some older platforms need special physical address handling for
11983 * cursor planes.
11984 *
11985 * Returns 0 on success, negative error code on failure.
11986 */
11987int
11988intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000011989 struct drm_framebuffer *fb,
11990 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070011991{
11992 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080011993 struct intel_plane *intel_plane = to_intel_plane(plane);
11994 enum pipe pipe = intel_plane->pipe;
11995 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11996 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
11997 unsigned frontbuffer_bits = 0;
11998 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070011999
Matt Roperea2c67b2014-12-23 10:41:52 -080012000 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070012001 return 0;
12002
Matt Roper6beb8c232014-12-01 15:40:14 -080012003 switch (plane->type) {
12004 case DRM_PLANE_TYPE_PRIMARY:
12005 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
12006 break;
12007 case DRM_PLANE_TYPE_CURSOR:
12008 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
12009 break;
12010 case DRM_PLANE_TYPE_OVERLAY:
12011 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
12012 break;
12013 }
Matt Roper465c1202014-05-29 08:06:54 -070012014
Matt Roper4c345742014-07-09 16:22:10 -070012015 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070012016
Matt Roper6beb8c232014-12-01 15:40:14 -080012017 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
12018 INTEL_INFO(dev)->cursor_needs_physical) {
12019 int align = IS_I830(dev) ? 16 * 1024 : 256;
12020 ret = i915_gem_object_attach_phys(obj, align);
12021 if (ret)
12022 DRM_DEBUG_KMS("failed to attach phys object\n");
12023 } else {
12024 ret = intel_pin_and_fence_fb_obj(plane, fb, NULL);
12025 }
12026
12027 if (ret == 0)
12028 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
12029
12030 mutex_unlock(&dev->struct_mutex);
12031
12032 return ret;
12033}
12034
Matt Roper38f3ce32014-12-02 07:45:25 -080012035/**
12036 * intel_cleanup_plane_fb - Cleans up an fb after plane use
12037 * @plane: drm plane to clean up for
12038 * @fb: old framebuffer that was on plane
12039 *
12040 * Cleans up a framebuffer that has just been removed from a plane.
12041 */
12042void
12043intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000012044 struct drm_framebuffer *fb,
12045 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080012046{
12047 struct drm_device *dev = plane->dev;
12048 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12049
12050 if (WARN_ON(!obj))
12051 return;
12052
12053 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
12054 !INTEL_INFO(dev)->cursor_needs_physical) {
12055 mutex_lock(&dev->struct_mutex);
12056 intel_unpin_fb_obj(obj);
12057 mutex_unlock(&dev->struct_mutex);
12058 }
Matt Roper465c1202014-05-29 08:06:54 -070012059}
12060
12061static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012062intel_check_primary_plane(struct drm_plane *plane,
12063 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070012064{
Matt Roper32b7eee2014-12-24 07:59:06 -080012065 struct drm_device *dev = plane->dev;
12066 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080012067 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080012068 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080012069 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012070 struct drm_rect *dest = &state->dst;
12071 struct drm_rect *src = &state->src;
12072 const struct drm_rect *clip = &state->clip;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012073 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012074
Matt Roperea2c67b2014-12-23 10:41:52 -080012075 crtc = crtc ? crtc : plane->crtc;
12076 intel_crtc = to_intel_crtc(crtc);
12077
Matt Roperc59cb172014-12-01 15:40:16 -080012078 ret = drm_plane_helper_check_update(plane, crtc, fb,
12079 src, dest, clip,
12080 DRM_PLANE_HELPER_NO_SCALING,
12081 DRM_PLANE_HELPER_NO_SCALING,
12082 false, true, &state->visible);
12083 if (ret)
12084 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012085
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012086 if (intel_crtc->active) {
Matt Roper32b7eee2014-12-24 07:59:06 -080012087 intel_crtc->atomic.wait_for_flips = true;
12088
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012089 /*
12090 * FBC does not work on some platforms for rotated
12091 * planes, so disable it when rotation is not 0 and
12092 * update it when rotation is set back to 0.
12093 *
12094 * FIXME: This is redundant with the fbc update done in
12095 * the primary plane enable function except that that
12096 * one is done too late. We eventually need to unify
12097 * this.
12098 */
12099 if (intel_crtc->primary_enabled &&
12100 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020012101 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080012102 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080012103 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012104 }
12105
12106 if (state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080012107 /*
12108 * BDW signals flip done immediately if the plane
12109 * is disabled, even if the plane enable is already
12110 * armed to occur at the next vblank :(
12111 */
12112 if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
12113 intel_crtc->atomic.wait_vblank = true;
12114 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012115
Matt Roper32b7eee2014-12-24 07:59:06 -080012116 intel_crtc->atomic.fb_bits |=
12117 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
12118
12119 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000012120
12121 /* Update watermarks on tiling changes. */
12122 if (!plane->state->fb || !state->base.fb ||
12123 plane->state->fb->modifier[0] !=
12124 state->base.fb->modifier[0])
12125 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080012126 }
12127
12128 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070012129}
12130
Sonika Jindal48404c12014-08-22 14:06:04 +053012131static void
12132intel_commit_primary_plane(struct drm_plane *plane,
12133 struct intel_plane_state *state)
12134{
Matt Roper2b875c22014-12-01 15:40:13 -080012135 struct drm_crtc *crtc = state->base.crtc;
12136 struct drm_framebuffer *fb = state->base.fb;
12137 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053012138 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080012139 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053012140 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080012141
Matt Roperea2c67b2014-12-23 10:41:52 -080012142 crtc = crtc ? crtc : plane->crtc;
12143 intel_crtc = to_intel_crtc(crtc);
12144
Matt Ropercf4c7c12014-12-04 10:27:42 -080012145 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053012146 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070012147 crtc->y = src->y1 >> 16;
12148
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012149 if (intel_crtc->active) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012150 if (state->visible) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012151 /* FIXME: kill this fastboot hack */
12152 intel_update_pipe_size(intel_crtc);
12153
12154 intel_crtc->primary_enabled = true;
12155
12156 dev_priv->display.update_primary_plane(crtc, plane->fb,
12157 crtc->x, crtc->y);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012158 } else {
12159 /*
12160 * If clipping results in a non-visible primary plane,
12161 * we'll disable the primary plane. Note that this is
12162 * a bit different than what happens if userspace
12163 * explicitly disables the plane by passing fb=0
12164 * because plane->fb still gets set and pinned.
12165 */
12166 intel_disable_primary_hw_plane(plane, crtc);
12167 }
Matt Roper32b7eee2014-12-24 07:59:06 -080012168 }
12169}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012170
Matt Roper32b7eee2014-12-24 07:59:06 -080012171static void intel_begin_crtc_commit(struct drm_crtc *crtc)
12172{
12173 struct drm_device *dev = crtc->dev;
12174 struct drm_i915_private *dev_priv = dev->dev_private;
12175 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080012176 struct intel_plane *intel_plane;
12177 struct drm_plane *p;
12178 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012179
Matt Roperea2c67b2014-12-23 10:41:52 -080012180 /* Track fb's for any planes being disabled */
12181 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
12182 intel_plane = to_intel_plane(p);
12183
12184 if (intel_crtc->atomic.disabled_planes &
12185 (1 << drm_plane_index(p))) {
12186 switch (p->type) {
12187 case DRM_PLANE_TYPE_PRIMARY:
12188 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
12189 break;
12190 case DRM_PLANE_TYPE_CURSOR:
12191 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
12192 break;
12193 case DRM_PLANE_TYPE_OVERLAY:
12194 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
12195 break;
12196 }
12197
12198 mutex_lock(&dev->struct_mutex);
12199 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
12200 mutex_unlock(&dev->struct_mutex);
12201 }
12202 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012203
Matt Roper32b7eee2014-12-24 07:59:06 -080012204 if (intel_crtc->atomic.wait_for_flips)
12205 intel_crtc_wait_for_pending_flips(crtc);
12206
12207 if (intel_crtc->atomic.disable_fbc)
12208 intel_fbc_disable(dev);
12209
12210 if (intel_crtc->atomic.pre_disable_primary)
12211 intel_pre_disable_primary(crtc);
12212
12213 if (intel_crtc->atomic.update_wm)
12214 intel_update_watermarks(crtc);
12215
12216 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080012217
12218 /* Perform vblank evasion around commit operation */
12219 if (intel_crtc->active)
12220 intel_crtc->atomic.evade =
12221 intel_pipe_update_start(intel_crtc,
12222 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080012223}
12224
12225static void intel_finish_crtc_commit(struct drm_crtc *crtc)
12226{
12227 struct drm_device *dev = crtc->dev;
12228 struct drm_i915_private *dev_priv = dev->dev_private;
12229 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12230 struct drm_plane *p;
12231
Matt Roperc34c9ee2014-12-23 10:41:50 -080012232 if (intel_crtc->atomic.evade)
12233 intel_pipe_update_end(intel_crtc,
12234 intel_crtc->atomic.start_vbl_count);
12235
Matt Roper32b7eee2014-12-24 07:59:06 -080012236 intel_runtime_pm_put(dev_priv);
12237
12238 if (intel_crtc->atomic.wait_vblank)
12239 intel_wait_for_vblank(dev, intel_crtc->pipe);
12240
12241 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
12242
12243 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012244 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020012245 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012246 mutex_unlock(&dev->struct_mutex);
12247 }
Matt Roper465c1202014-05-29 08:06:54 -070012248
Matt Roper32b7eee2014-12-24 07:59:06 -080012249 if (intel_crtc->atomic.post_enable_primary)
12250 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012251
Matt Roper32b7eee2014-12-24 07:59:06 -080012252 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
12253 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
12254 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
12255 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012256
Matt Roper32b7eee2014-12-24 07:59:06 -080012257 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012258}
12259
Matt Ropercf4c7c12014-12-04 10:27:42 -080012260/**
Matt Roper4a3b8762014-12-23 10:41:51 -080012261 * intel_plane_destroy - destroy a plane
12262 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080012263 *
Matt Roper4a3b8762014-12-23 10:41:51 -080012264 * Common destruction function for all types of planes (primary, cursor,
12265 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080012266 */
Matt Roper4a3b8762014-12-23 10:41:51 -080012267void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070012268{
12269 struct intel_plane *intel_plane = to_intel_plane(plane);
12270 drm_plane_cleanup(plane);
12271 kfree(intel_plane);
12272}
12273
Matt Roper65a3fea2015-01-21 16:35:42 -080012274const struct drm_plane_funcs intel_plane_funcs = {
Daniel Vetterff42e092015-03-02 16:35:20 +010012275 .update_plane = drm_plane_helper_update,
12276 .disable_plane = drm_plane_helper_disable,
Matt Roper3d7d6512014-06-10 08:28:13 -070012277 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080012278 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080012279 .atomic_get_property = intel_plane_atomic_get_property,
12280 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080012281 .atomic_duplicate_state = intel_plane_duplicate_state,
12282 .atomic_destroy_state = intel_plane_destroy_state,
12283
Matt Roper465c1202014-05-29 08:06:54 -070012284};
12285
12286static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
12287 int pipe)
12288{
12289 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080012290 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070012291 const uint32_t *intel_primary_formats;
12292 int num_formats;
12293
12294 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
12295 if (primary == NULL)
12296 return NULL;
12297
Matt Roper8e7d6882015-01-21 16:35:41 -080012298 state = intel_create_plane_state(&primary->base);
12299 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080012300 kfree(primary);
12301 return NULL;
12302 }
Matt Roper8e7d6882015-01-21 16:35:41 -080012303 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080012304
Matt Roper465c1202014-05-29 08:06:54 -070012305 primary->can_scale = false;
12306 primary->max_downscale = 1;
12307 primary->pipe = pipe;
12308 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080012309 primary->check_plane = intel_check_primary_plane;
12310 primary->commit_plane = intel_commit_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070012311 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
12312 primary->plane = !pipe;
12313
12314 if (INTEL_INFO(dev)->gen <= 3) {
12315 intel_primary_formats = intel_primary_formats_gen2;
12316 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
12317 } else {
12318 intel_primary_formats = intel_primary_formats_gen4;
12319 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
12320 }
12321
12322 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080012323 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070012324 intel_primary_formats, num_formats,
12325 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053012326
12327 if (INTEL_INFO(dev)->gen >= 4) {
12328 if (!dev->mode_config.rotation_property)
12329 dev->mode_config.rotation_property =
12330 drm_mode_create_rotation_property(dev,
12331 BIT(DRM_ROTATE_0) |
12332 BIT(DRM_ROTATE_180));
12333 if (dev->mode_config.rotation_property)
12334 drm_object_attach_property(&primary->base.base,
12335 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080012336 state->base.rotation);
Sonika Jindal48404c12014-08-22 14:06:04 +053012337 }
12338
Matt Roperea2c67b2014-12-23 10:41:52 -080012339 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
12340
Matt Roper465c1202014-05-29 08:06:54 -070012341 return &primary->base;
12342}
12343
Matt Roper3d7d6512014-06-10 08:28:13 -070012344static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030012345intel_check_cursor_plane(struct drm_plane *plane,
12346 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070012347{
Matt Roper2b875c22014-12-01 15:40:13 -080012348 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080012349 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080012350 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012351 struct drm_rect *dest = &state->dst;
12352 struct drm_rect *src = &state->src;
12353 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012354 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080012355 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012356 unsigned stride;
12357 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012358
Matt Roperea2c67b2014-12-23 10:41:52 -080012359 crtc = crtc ? crtc : plane->crtc;
12360 intel_crtc = to_intel_crtc(crtc);
12361
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012362 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030012363 src, dest, clip,
12364 DRM_PLANE_HELPER_NO_SCALING,
12365 DRM_PLANE_HELPER_NO_SCALING,
12366 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012367 if (ret)
12368 return ret;
12369
12370
12371 /* if we want to turn off the cursor ignore width and height */
12372 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080012373 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012374
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012375 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080012376 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
12377 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
12378 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012379 return -EINVAL;
12380 }
12381
Matt Roperea2c67b2014-12-23 10:41:52 -080012382 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
12383 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012384 DRM_DEBUG_KMS("buffer is too small\n");
12385 return -ENOMEM;
12386 }
12387
Ville Syrjälä3a656b52015-03-09 21:08:37 +020012388 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012389 DRM_DEBUG_KMS("cursor cannot be tiled\n");
12390 ret = -EINVAL;
12391 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012392
Matt Roper32b7eee2014-12-24 07:59:06 -080012393finish:
12394 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020012395 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080012396 intel_crtc->atomic.update_wm = true;
12397
12398 intel_crtc->atomic.fb_bits |=
12399 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
12400 }
12401
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012402 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012403}
12404
Matt Roperf4a2cf22014-12-01 15:40:12 -080012405static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030012406intel_commit_cursor_plane(struct drm_plane *plane,
12407 struct intel_plane_state *state)
12408{
Matt Roper2b875c22014-12-01 15:40:13 -080012409 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080012410 struct drm_device *dev = plane->dev;
12411 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080012412 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080012413 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070012414
Matt Roperea2c67b2014-12-23 10:41:52 -080012415 crtc = crtc ? crtc : plane->crtc;
12416 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070012417
Matt Roperea2c67b2014-12-23 10:41:52 -080012418 plane->fb = state->base.fb;
12419 crtc->cursor_x = state->base.crtc_x;
12420 crtc->cursor_y = state->base.crtc_y;
12421
Gustavo Padovana912f122014-12-01 15:40:10 -080012422 if (intel_crtc->cursor_bo == obj)
12423 goto update;
12424
Matt Roperf4a2cf22014-12-01 15:40:12 -080012425 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080012426 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080012427 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080012428 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080012429 else
Gustavo Padovana912f122014-12-01 15:40:10 -080012430 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080012431
Gustavo Padovana912f122014-12-01 15:40:10 -080012432 intel_crtc->cursor_addr = addr;
12433 intel_crtc->cursor_bo = obj;
12434update:
Gustavo Padovana912f122014-12-01 15:40:10 -080012435
Matt Roper32b7eee2014-12-24 07:59:06 -080012436 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030012437 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070012438}
Gustavo Padovan852e7872014-09-05 17:22:31 -030012439
Matt Roper3d7d6512014-06-10 08:28:13 -070012440static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
12441 int pipe)
12442{
12443 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080012444 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070012445
12446 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
12447 if (cursor == NULL)
12448 return NULL;
12449
Matt Roper8e7d6882015-01-21 16:35:41 -080012450 state = intel_create_plane_state(&cursor->base);
12451 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080012452 kfree(cursor);
12453 return NULL;
12454 }
Matt Roper8e7d6882015-01-21 16:35:41 -080012455 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080012456
Matt Roper3d7d6512014-06-10 08:28:13 -070012457 cursor->can_scale = false;
12458 cursor->max_downscale = 1;
12459 cursor->pipe = pipe;
12460 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080012461 cursor->check_plane = intel_check_cursor_plane;
12462 cursor->commit_plane = intel_commit_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070012463
12464 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080012465 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070012466 intel_cursor_formats,
12467 ARRAY_SIZE(intel_cursor_formats),
12468 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070012469
12470 if (INTEL_INFO(dev)->gen >= 4) {
12471 if (!dev->mode_config.rotation_property)
12472 dev->mode_config.rotation_property =
12473 drm_mode_create_rotation_property(dev,
12474 BIT(DRM_ROTATE_0) |
12475 BIT(DRM_ROTATE_180));
12476 if (dev->mode_config.rotation_property)
12477 drm_object_attach_property(&cursor->base.base,
12478 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080012479 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070012480 }
12481
Matt Roperea2c67b2014-12-23 10:41:52 -080012482 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
12483
Matt Roper3d7d6512014-06-10 08:28:13 -070012484 return &cursor->base;
12485}
12486
Hannes Ederb358d0a2008-12-18 21:18:47 +010012487static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080012488{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012489 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080012490 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012491 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070012492 struct drm_plane *primary = NULL;
12493 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070012494 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080012495
Daniel Vetter955382f2013-09-19 14:05:45 +020012496 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080012497 if (intel_crtc == NULL)
12498 return;
12499
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012500 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
12501 if (!crtc_state)
12502 goto fail;
12503 intel_crtc_set_state(intel_crtc, crtc_state);
Matt Roper07878242015-02-25 11:43:26 -080012504 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012505
Matt Roper465c1202014-05-29 08:06:54 -070012506 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070012507 if (!primary)
12508 goto fail;
12509
12510 cursor = intel_cursor_plane_create(dev, pipe);
12511 if (!cursor)
12512 goto fail;
12513
Matt Roper465c1202014-05-29 08:06:54 -070012514 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070012515 cursor, &intel_crtc_funcs);
12516 if (ret)
12517 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080012518
12519 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080012520 for (i = 0; i < 256; i++) {
12521 intel_crtc->lut_r[i] = i;
12522 intel_crtc->lut_g[i] = i;
12523 intel_crtc->lut_b[i] = i;
12524 }
12525
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020012526 /*
12527 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020012528 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020012529 */
Jesse Barnes80824002009-09-10 15:28:06 -070012530 intel_crtc->pipe = pipe;
12531 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010012532 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080012533 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010012534 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070012535 }
12536
Chris Wilson4b0e3332014-05-30 16:35:26 +030012537 intel_crtc->cursor_base = ~0;
12538 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030012539 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030012540
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080012541 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
12542 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
12543 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
12544 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
12545
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020012546 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
12547
Jesse Barnes79e53942008-11-07 14:24:08 -080012548 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020012549
12550 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070012551 return;
12552
12553fail:
12554 if (primary)
12555 drm_plane_cleanup(primary);
12556 if (cursor)
12557 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012558 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070012559 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080012560}
12561
Jesse Barnes752aa882013-10-31 18:55:49 +020012562enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
12563{
12564 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012565 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020012566
Rob Clark51fd3712013-11-19 12:10:12 -050012567 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020012568
Ville Syrjäläd3babd32014-11-07 11:16:01 +020012569 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020012570 return INVALID_PIPE;
12571
12572 return to_intel_crtc(encoder->crtc)->pipe;
12573}
12574
Carl Worth08d7b3d2009-04-29 14:43:54 -070012575int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000012576 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070012577{
Carl Worth08d7b3d2009-04-29 14:43:54 -070012578 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040012579 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020012580 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012581
Rob Clark7707e652014-07-17 23:30:04 -040012582 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070012583
Rob Clark7707e652014-07-17 23:30:04 -040012584 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070012585 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030012586 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012587 }
12588
Rob Clark7707e652014-07-17 23:30:04 -040012589 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020012590 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012591
Daniel Vetterc05422d2009-08-11 16:05:30 +020012592 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012593}
12594
Daniel Vetter66a92782012-07-12 20:08:18 +020012595static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080012596{
Daniel Vetter66a92782012-07-12 20:08:18 +020012597 struct drm_device *dev = encoder->base.dev;
12598 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080012599 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080012600 int entry = 0;
12601
Damien Lespiaub2784e12014-08-05 11:29:37 +010012602 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012603 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020012604 index_mask |= (1 << entry);
12605
Jesse Barnes79e53942008-11-07 14:24:08 -080012606 entry++;
12607 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010012608
Jesse Barnes79e53942008-11-07 14:24:08 -080012609 return index_mask;
12610}
12611
Chris Wilson4d302442010-12-14 19:21:29 +000012612static bool has_edp_a(struct drm_device *dev)
12613{
12614 struct drm_i915_private *dev_priv = dev->dev_private;
12615
12616 if (!IS_MOBILE(dev))
12617 return false;
12618
12619 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
12620 return false;
12621
Damien Lespiaue3589902014-02-07 19:12:50 +000012622 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000012623 return false;
12624
12625 return true;
12626}
12627
Jesse Barnes84b4e042014-06-25 08:24:29 -070012628static bool intel_crt_present(struct drm_device *dev)
12629{
12630 struct drm_i915_private *dev_priv = dev->dev_private;
12631
Damien Lespiau884497e2013-12-03 13:56:23 +000012632 if (INTEL_INFO(dev)->gen >= 9)
12633 return false;
12634
Damien Lespiaucf404ce2014-10-01 20:04:15 +010012635 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070012636 return false;
12637
12638 if (IS_CHERRYVIEW(dev))
12639 return false;
12640
12641 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
12642 return false;
12643
12644 return true;
12645}
12646
Jesse Barnes79e53942008-11-07 14:24:08 -080012647static void intel_setup_outputs(struct drm_device *dev)
12648{
Eric Anholt725e30a2009-01-22 13:01:02 -080012649 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010012650 struct intel_encoder *encoder;
Matt Roperc6f95f22015-01-22 16:50:32 -080012651 struct drm_connector *connector;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012652 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080012653
Daniel Vetterc9093352013-06-06 22:22:47 +020012654 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012655
Jesse Barnes84b4e042014-06-25 08:24:29 -070012656 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020012657 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012658
Paulo Zanoniaffa9352012-11-23 15:30:39 -020012659 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030012660 int found;
12661
Jesse Barnesde31fac2015-03-06 15:53:32 -080012662 /*
12663 * Haswell uses DDI functions to detect digital outputs.
12664 * On SKL pre-D0 the strap isn't connected, so we assume
12665 * it's there.
12666 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030012667 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080012668 /* WaIgnoreDDIAStrap: skl */
12669 if (found ||
12670 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030012671 intel_ddi_init(dev, PORT_A);
12672
12673 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
12674 * register */
12675 found = I915_READ(SFUSE_STRAP);
12676
12677 if (found & SFUSE_STRAP_DDIB_DETECTED)
12678 intel_ddi_init(dev, PORT_B);
12679 if (found & SFUSE_STRAP_DDIC_DETECTED)
12680 intel_ddi_init(dev, PORT_C);
12681 if (found & SFUSE_STRAP_DDID_DETECTED)
12682 intel_ddi_init(dev, PORT_D);
12683 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012684 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020012685 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020012686
12687 if (has_edp_a(dev))
12688 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012689
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012690 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080012691 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010012692 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012693 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012694 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012695 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012696 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012697 }
12698
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012699 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012700 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012701
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012702 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012703 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012704
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012705 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012706 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012707
Daniel Vetter270b3042012-10-27 15:52:05 +020012708 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012709 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070012710 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012711 /*
12712 * The DP_DETECTED bit is the latched state of the DDC
12713 * SDA pin at boot. However since eDP doesn't require DDC
12714 * (no way to plug in a DP->HDMI dongle) the DDC pins for
12715 * eDP ports may have been muxed to an alternate function.
12716 * Thus we can't rely on the DP_DETECTED bit alone to detect
12717 * eDP ports. Consult the VBT as well as DP_DETECTED to
12718 * detect eDP ports.
12719 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020012720 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
12721 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030012722 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
12723 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012724 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
12725 intel_dp_is_edp(dev, PORT_B))
12726 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030012727
Ville Syrjäläd2182a62015-01-09 14:21:14 +020012728 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
12729 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070012730 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
12731 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012732 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
12733 intel_dp_is_edp(dev, PORT_C))
12734 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053012735
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030012736 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012737 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030012738 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
12739 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012740 /* eDP not supported on port D, so don't check VBT */
12741 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
12742 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030012743 }
12744
Jani Nikula3cfca972013-08-27 15:12:26 +030012745 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080012746 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080012747 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080012748
Paulo Zanonie2debe92013-02-18 19:00:27 -030012749 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012750 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012751 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012752 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
12753 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012754 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012755 }
Ma Ling27185ae2009-08-24 13:50:23 +080012756
Imre Deake7281ea2013-05-08 13:14:08 +030012757 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012758 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080012759 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040012760
12761 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040012762
Paulo Zanonie2debe92013-02-18 19:00:27 -030012763 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012764 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012765 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012766 }
Ma Ling27185ae2009-08-24 13:50:23 +080012767
Paulo Zanonie2debe92013-02-18 19:00:27 -030012768 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080012769
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012770 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
12771 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012772 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012773 }
Imre Deake7281ea2013-05-08 13:14:08 +030012774 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012775 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080012776 }
Ma Ling27185ae2009-08-24 13:50:23 +080012777
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012778 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030012779 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012780 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070012781 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080012782 intel_dvo_init(dev);
12783
Zhenyu Wang103a1962009-11-27 11:44:36 +080012784 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080012785 intel_tv_init(dev);
12786
Matt Roperc6f95f22015-01-22 16:50:32 -080012787 /*
12788 * FIXME: We don't have full atomic support yet, but we want to be
12789 * able to enable/test plane updates via the atomic interface in the
12790 * meantime. However as soon as we flip DRIVER_ATOMIC on, the DRM core
12791 * will take some atomic codepaths to lookup properties during
12792 * drmModeGetConnector() that unconditionally dereference
12793 * connector->state.
12794 *
12795 * We create a dummy connector state here for each connector to ensure
12796 * the DRM core doesn't try to dereference a NULL connector->state.
12797 * The actual connector properties will never be updated or contain
12798 * useful information, but since we're doing this specifically for
12799 * testing/debug of the plane operations (and only when a specific
12800 * kernel module option is given), that shouldn't really matter.
12801 *
12802 * Once atomic support for crtc's + connectors lands, this loop should
12803 * be removed since we'll be setting up real connector state, which
12804 * will contain Intel-specific properties.
12805 */
12806 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
12807 list_for_each_entry(connector,
12808 &dev->mode_config.connector_list,
12809 head) {
12810 if (!WARN_ON(connector->state)) {
12811 connector->state =
12812 kzalloc(sizeof(*connector->state),
12813 GFP_KERNEL);
12814 }
12815 }
12816 }
12817
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080012818 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070012819
Damien Lespiaub2784e12014-08-05 11:29:37 +010012820 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010012821 encoder->base.possible_crtcs = encoder->crtc_mask;
12822 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020012823 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080012824 }
Chris Wilson47356eb2011-01-11 17:06:04 +000012825
Paulo Zanonidde86e22012-12-01 12:04:25 -020012826 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020012827
12828 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012829}
12830
12831static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
12832{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012833 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080012834 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080012835
Daniel Vetteref2d6332014-02-10 18:00:38 +010012836 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012837 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010012838 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012839 drm_gem_object_unreference(&intel_fb->obj->base);
12840 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080012841 kfree(intel_fb);
12842}
12843
12844static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000012845 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080012846 unsigned int *handle)
12847{
12848 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000012849 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080012850
Chris Wilson05394f32010-11-08 19:18:58 +000012851 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080012852}
12853
12854static const struct drm_framebuffer_funcs intel_fb_funcs = {
12855 .destroy = intel_user_framebuffer_destroy,
12856 .create_handle = intel_user_framebuffer_create_handle,
12857};
12858
Damien Lespiaub3218032015-02-27 11:15:18 +000012859static
12860u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
12861 uint32_t pixel_format)
12862{
12863 u32 gen = INTEL_INFO(dev)->gen;
12864
12865 if (gen >= 9) {
12866 /* "The stride in bytes must not exceed the of the size of 8K
12867 * pixels and 32K bytes."
12868 */
12869 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
12870 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
12871 return 32*1024;
12872 } else if (gen >= 4) {
12873 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
12874 return 16*1024;
12875 else
12876 return 32*1024;
12877 } else if (gen >= 3) {
12878 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
12879 return 8*1024;
12880 else
12881 return 16*1024;
12882 } else {
12883 /* XXX DSPC is limited to 4k tiled */
12884 return 8*1024;
12885 }
12886}
12887
Daniel Vetterb5ea6422014-03-02 21:18:00 +010012888static int intel_framebuffer_init(struct drm_device *dev,
12889 struct intel_framebuffer *intel_fb,
12890 struct drm_mode_fb_cmd2 *mode_cmd,
12891 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080012892{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000012893 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080012894 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000012895 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080012896
Daniel Vetterdd4916c2013-10-09 21:23:51 +020012897 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
12898
Daniel Vetter2a80ead2015-02-10 17:16:06 +000012899 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
12900 /* Enforce that fb modifier and tiling mode match, but only for
12901 * X-tiled. This is needed for FBC. */
12902 if (!!(obj->tiling_mode == I915_TILING_X) !=
12903 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
12904 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
12905 return -EINVAL;
12906 }
12907 } else {
12908 if (obj->tiling_mode == I915_TILING_X)
12909 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
12910 else if (obj->tiling_mode == I915_TILING_Y) {
12911 DRM_DEBUG("No Y tiling for legacy addfb\n");
12912 return -EINVAL;
12913 }
12914 }
12915
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000012916 /* Passed in modifier sanity checking. */
12917 switch (mode_cmd->modifier[0]) {
12918 case I915_FORMAT_MOD_Y_TILED:
12919 case I915_FORMAT_MOD_Yf_TILED:
12920 if (INTEL_INFO(dev)->gen < 9) {
12921 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
12922 mode_cmd->modifier[0]);
12923 return -EINVAL;
12924 }
12925 case DRM_FORMAT_MOD_NONE:
12926 case I915_FORMAT_MOD_X_TILED:
12927 break;
12928 default:
12929 DRM_ERROR("Unsupported fb modifier 0x%llx!\n",
12930 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010012931 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012932 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012933
Damien Lespiaub3218032015-02-27 11:15:18 +000012934 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
12935 mode_cmd->pixel_format);
12936 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
12937 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
12938 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010012939 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012940 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012941
Damien Lespiaub3218032015-02-27 11:15:18 +000012942 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
12943 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010012944 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000012945 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
12946 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000012947 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010012948 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012949 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012950 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012951
Daniel Vetter2a80ead2015-02-10 17:16:06 +000012952 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012953 mode_cmd->pitches[0] != obj->stride) {
12954 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
12955 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012956 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012957 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012958
Ville Syrjälä57779d02012-10-31 17:50:14 +020012959 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012960 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020012961 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020012962 case DRM_FORMAT_RGB565:
12963 case DRM_FORMAT_XRGB8888:
12964 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020012965 break;
12966 case DRM_FORMAT_XRGB1555:
12967 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012968 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012969 DRM_DEBUG("unsupported pixel format: %s\n",
12970 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012971 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012972 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020012973 break;
12974 case DRM_FORMAT_XBGR8888:
12975 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020012976 case DRM_FORMAT_XRGB2101010:
12977 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020012978 case DRM_FORMAT_XBGR2101010:
12979 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012980 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012981 DRM_DEBUG("unsupported pixel format: %s\n",
12982 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012983 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012984 }
Jesse Barnesb5626742011-06-24 12:19:27 -070012985 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020012986 case DRM_FORMAT_YUYV:
12987 case DRM_FORMAT_UYVY:
12988 case DRM_FORMAT_YVYU:
12989 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012990 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012991 DRM_DEBUG("unsupported pixel format: %s\n",
12992 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012993 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012994 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012995 break;
12996 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012997 DRM_DEBUG("unsupported pixel format: %s\n",
12998 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010012999 return -EINVAL;
13000 }
13001
Ville Syrjälä90f9a332012-10-31 17:50:19 +020013002 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
13003 if (mode_cmd->offsets[0] != 0)
13004 return -EINVAL;
13005
Damien Lespiauec2c9812015-01-20 12:51:45 +000013006 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000013007 mode_cmd->pixel_format,
13008 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020013009 /* FIXME drm helper for size checks (especially planar formats)? */
13010 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
13011 return -EINVAL;
13012
Daniel Vetterc7d73f62012-12-13 23:38:38 +010013013 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
13014 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020013015 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010013016
Jesse Barnes79e53942008-11-07 14:24:08 -080013017 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
13018 if (ret) {
13019 DRM_ERROR("framebuffer init failed %d\n", ret);
13020 return ret;
13021 }
13022
Jesse Barnes79e53942008-11-07 14:24:08 -080013023 return 0;
13024}
13025
Jesse Barnes79e53942008-11-07 14:24:08 -080013026static struct drm_framebuffer *
13027intel_user_framebuffer_create(struct drm_device *dev,
13028 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013029 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080013030{
Chris Wilson05394f32010-11-08 19:18:58 +000013031 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080013032
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013033 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
13034 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000013035 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010013036 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080013037
Chris Wilsond2dff872011-04-19 08:36:26 +010013038 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080013039}
13040
Daniel Vetter4520f532013-10-09 09:18:51 +020013041#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020013042static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020013043{
13044}
13045#endif
13046
Jesse Barnes79e53942008-11-07 14:24:08 -080013047static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080013048 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020013049 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080013050 .atomic_check = intel_atomic_check,
13051 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080013052};
13053
Jesse Barnese70236a2009-09-21 10:42:27 -070013054/* Set up chip specific display functions */
13055static void intel_init_display(struct drm_device *dev)
13056{
13057 struct drm_i915_private *dev_priv = dev->dev_private;
13058
Daniel Vetteree9300b2013-06-03 22:40:22 +020013059 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
13060 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030013061 else if (IS_CHERRYVIEW(dev))
13062 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020013063 else if (IS_VALLEYVIEW(dev))
13064 dev_priv->display.find_dpll = vlv_find_best_dpll;
13065 else if (IS_PINEVIEW(dev))
13066 dev_priv->display.find_dpll = pnv_find_best_dpll;
13067 else
13068 dev_priv->display.find_dpll = i9xx_find_best_dpll;
13069
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000013070 if (INTEL_INFO(dev)->gen >= 9) {
13071 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013072 dev_priv->display.get_initial_plane_config =
13073 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000013074 dev_priv->display.crtc_compute_clock =
13075 haswell_crtc_compute_clock;
13076 dev_priv->display.crtc_enable = haswell_crtc_enable;
13077 dev_priv->display.crtc_disable = haswell_crtc_disable;
13078 dev_priv->display.off = ironlake_crtc_off;
13079 dev_priv->display.update_primary_plane =
13080 skylake_update_primary_plane;
13081 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013082 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013083 dev_priv->display.get_initial_plane_config =
13084 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020013085 dev_priv->display.crtc_compute_clock =
13086 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020013087 dev_priv->display.crtc_enable = haswell_crtc_enable;
13088 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030013089 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000013090 dev_priv->display.update_primary_plane =
13091 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030013092 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013093 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013094 dev_priv->display.get_initial_plane_config =
13095 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020013096 dev_priv->display.crtc_compute_clock =
13097 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020013098 dev_priv->display.crtc_enable = ironlake_crtc_enable;
13099 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013100 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070013101 dev_priv->display.update_primary_plane =
13102 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070013103 } else if (IS_VALLEYVIEW(dev)) {
13104 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013105 dev_priv->display.get_initial_plane_config =
13106 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020013107 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070013108 dev_priv->display.crtc_enable = valleyview_crtc_enable;
13109 dev_priv->display.crtc_disable = i9xx_crtc_disable;
13110 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070013111 dev_priv->display.update_primary_plane =
13112 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070013113 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013114 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013115 dev_priv->display.get_initial_plane_config =
13116 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020013117 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020013118 dev_priv->display.crtc_enable = i9xx_crtc_enable;
13119 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013120 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070013121 dev_priv->display.update_primary_plane =
13122 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070013123 }
Jesse Barnese70236a2009-09-21 10:42:27 -070013124
Jesse Barnese70236a2009-09-21 10:42:27 -070013125 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070013126 if (IS_VALLEYVIEW(dev))
13127 dev_priv->display.get_display_clock_speed =
13128 valleyview_get_display_clock_speed;
13129 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070013130 dev_priv->display.get_display_clock_speed =
13131 i945_get_display_clock_speed;
13132 else if (IS_I915G(dev))
13133 dev_priv->display.get_display_clock_speed =
13134 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020013135 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070013136 dev_priv->display.get_display_clock_speed =
13137 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020013138 else if (IS_PINEVIEW(dev))
13139 dev_priv->display.get_display_clock_speed =
13140 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070013141 else if (IS_I915GM(dev))
13142 dev_priv->display.get_display_clock_speed =
13143 i915gm_get_display_clock_speed;
13144 else if (IS_I865G(dev))
13145 dev_priv->display.get_display_clock_speed =
13146 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020013147 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070013148 dev_priv->display.get_display_clock_speed =
13149 i855_get_display_clock_speed;
13150 else /* 852, 830 */
13151 dev_priv->display.get_display_clock_speed =
13152 i830_get_display_clock_speed;
13153
Jani Nikula7c10a2b2014-10-27 16:26:43 +020013154 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013155 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013156 } else if (IS_GEN6(dev)) {
13157 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013158 } else if (IS_IVYBRIDGE(dev)) {
13159 /* FIXME: detect B0+ stepping and use auto training */
13160 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030013161 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013162 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080013163 } else if (IS_VALLEYVIEW(dev)) {
13164 dev_priv->display.modeset_global_resources =
13165 valleyview_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070013166 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070013167
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070013168 switch (INTEL_INFO(dev)->gen) {
13169 case 2:
13170 dev_priv->display.queue_flip = intel_gen2_queue_flip;
13171 break;
13172
13173 case 3:
13174 dev_priv->display.queue_flip = intel_gen3_queue_flip;
13175 break;
13176
13177 case 4:
13178 case 5:
13179 dev_priv->display.queue_flip = intel_gen4_queue_flip;
13180 break;
13181
13182 case 6:
13183 dev_priv->display.queue_flip = intel_gen6_queue_flip;
13184 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070013185 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070013186 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070013187 dev_priv->display.queue_flip = intel_gen7_queue_flip;
13188 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000013189 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000013190 /* Drop through - unsupported since execlist only. */
13191 default:
13192 /* Default just returns -ENODEV to indicate unsupported */
13193 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070013194 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020013195
13196 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030013197
13198 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070013199}
13200
Jesse Barnesb690e962010-07-19 13:53:12 -070013201/*
13202 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
13203 * resume, or other times. This quirk makes sure that's the case for
13204 * affected systems.
13205 */
Akshay Joshi0206e352011-08-16 15:34:10 -040013206static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070013207{
13208 struct drm_i915_private *dev_priv = dev->dev_private;
13209
13210 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013211 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070013212}
13213
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030013214static void quirk_pipeb_force(struct drm_device *dev)
13215{
13216 struct drm_i915_private *dev_priv = dev->dev_private;
13217
13218 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
13219 DRM_INFO("applying pipe b force quirk\n");
13220}
13221
Keith Packard435793d2011-07-12 14:56:22 -070013222/*
13223 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
13224 */
13225static void quirk_ssc_force_disable(struct drm_device *dev)
13226{
13227 struct drm_i915_private *dev_priv = dev->dev_private;
13228 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013229 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070013230}
13231
Carsten Emde4dca20e2012-03-15 15:56:26 +010013232/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010013233 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
13234 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010013235 */
13236static void quirk_invert_brightness(struct drm_device *dev)
13237{
13238 struct drm_i915_private *dev_priv = dev->dev_private;
13239 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013240 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070013241}
13242
Scot Doyle9c72cc62014-07-03 23:27:50 +000013243/* Some VBT's incorrectly indicate no backlight is present */
13244static void quirk_backlight_present(struct drm_device *dev)
13245{
13246 struct drm_i915_private *dev_priv = dev->dev_private;
13247 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
13248 DRM_INFO("applying backlight present quirk\n");
13249}
13250
Jesse Barnesb690e962010-07-19 13:53:12 -070013251struct intel_quirk {
13252 int device;
13253 int subsystem_vendor;
13254 int subsystem_device;
13255 void (*hook)(struct drm_device *dev);
13256};
13257
Egbert Eich5f85f172012-10-14 15:46:38 +020013258/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
13259struct intel_dmi_quirk {
13260 void (*hook)(struct drm_device *dev);
13261 const struct dmi_system_id (*dmi_id_list)[];
13262};
13263
13264static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
13265{
13266 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
13267 return 1;
13268}
13269
13270static const struct intel_dmi_quirk intel_dmi_quirks[] = {
13271 {
13272 .dmi_id_list = &(const struct dmi_system_id[]) {
13273 {
13274 .callback = intel_dmi_reverse_brightness,
13275 .ident = "NCR Corporation",
13276 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
13277 DMI_MATCH(DMI_PRODUCT_NAME, ""),
13278 },
13279 },
13280 { } /* terminating entry */
13281 },
13282 .hook = quirk_invert_brightness,
13283 },
13284};
13285
Ben Widawskyc43b5632012-04-16 14:07:40 -070013286static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070013287 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040013288 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070013289
Jesse Barnesb690e962010-07-19 13:53:12 -070013290 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
13291 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
13292
Jesse Barnesb690e962010-07-19 13:53:12 -070013293 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
13294 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
13295
Ville Syrjälä5f080c02014-08-15 01:22:06 +030013296 /* 830 needs to leave pipe A & dpll A up */
13297 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
13298
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030013299 /* 830 needs to leave pipe B & dpll B up */
13300 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
13301
Keith Packard435793d2011-07-12 14:56:22 -070013302 /* Lenovo U160 cannot use SSC on LVDS */
13303 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020013304
13305 /* Sony Vaio Y cannot use SSC on LVDS */
13306 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010013307
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010013308 /* Acer Aspire 5734Z must invert backlight brightness */
13309 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
13310
13311 /* Acer/eMachines G725 */
13312 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
13313
13314 /* Acer/eMachines e725 */
13315 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
13316
13317 /* Acer/Packard Bell NCL20 */
13318 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
13319
13320 /* Acer Aspire 4736Z */
13321 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020013322
13323 /* Acer Aspire 5336 */
13324 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000013325
13326 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
13327 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000013328
Scot Doyledfb3d47b2014-08-21 16:08:02 +000013329 /* Acer C720 Chromebook (Core i3 4005U) */
13330 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
13331
jens steinb2a96012014-10-28 20:25:53 +010013332 /* Apple Macbook 2,1 (Core 2 T7400) */
13333 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
13334
Scot Doyled4967d82014-07-03 23:27:52 +000013335 /* Toshiba CB35 Chromebook (Celeron 2955U) */
13336 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000013337
13338 /* HP Chromebook 14 (Celeron 2955U) */
13339 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020013340
13341 /* Dell Chromebook 11 */
13342 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070013343};
13344
13345static void intel_init_quirks(struct drm_device *dev)
13346{
13347 struct pci_dev *d = dev->pdev;
13348 int i;
13349
13350 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
13351 struct intel_quirk *q = &intel_quirks[i];
13352
13353 if (d->device == q->device &&
13354 (d->subsystem_vendor == q->subsystem_vendor ||
13355 q->subsystem_vendor == PCI_ANY_ID) &&
13356 (d->subsystem_device == q->subsystem_device ||
13357 q->subsystem_device == PCI_ANY_ID))
13358 q->hook(dev);
13359 }
Egbert Eich5f85f172012-10-14 15:46:38 +020013360 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
13361 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
13362 intel_dmi_quirks[i].hook(dev);
13363 }
Jesse Barnesb690e962010-07-19 13:53:12 -070013364}
13365
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013366/* Disable the VGA plane that we never use */
13367static void i915_disable_vga(struct drm_device *dev)
13368{
13369 struct drm_i915_private *dev_priv = dev->dev_private;
13370 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020013371 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013372
Ville Syrjälä2b37c612014-01-22 21:32:38 +020013373 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013374 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070013375 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013376 sr1 = inb(VGA_SR_DATA);
13377 outb(sr1 | 1<<5, VGA_SR_DATA);
13378 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
13379 udelay(300);
13380
Ville Syrjälä01f5a622014-12-16 18:38:37 +020013381 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013382 POSTING_READ(vga_reg);
13383}
13384
Daniel Vetterf8175862012-04-10 15:50:11 +020013385void intel_modeset_init_hw(struct drm_device *dev)
13386{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030013387 intel_prepare_ddi(dev);
13388
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030013389 if (IS_VALLEYVIEW(dev))
13390 vlv_update_cdclk(dev);
13391
Daniel Vetterf8175862012-04-10 15:50:11 +020013392 intel_init_clock_gating(dev);
13393
Daniel Vetter8090c6b2012-06-24 16:42:32 +020013394 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020013395}
13396
Jesse Barnes79e53942008-11-07 14:24:08 -080013397void intel_modeset_init(struct drm_device *dev)
13398{
Jesse Barnes652c3932009-08-17 13:31:43 -070013399 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000013400 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000013401 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080013402 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080013403
13404 drm_mode_config_init(dev);
13405
13406 dev->mode_config.min_width = 0;
13407 dev->mode_config.min_height = 0;
13408
Dave Airlie019d96c2011-09-29 16:20:42 +010013409 dev->mode_config.preferred_depth = 24;
13410 dev->mode_config.prefer_shadow = 1;
13411
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000013412 dev->mode_config.allow_fb_modifiers = true;
13413
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020013414 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080013415
Jesse Barnesb690e962010-07-19 13:53:12 -070013416 intel_init_quirks(dev);
13417
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030013418 intel_init_pm(dev);
13419
Ben Widawskye3c74752013-04-05 13:12:39 -070013420 if (INTEL_INFO(dev)->num_pipes == 0)
13421 return;
13422
Jesse Barnese70236a2009-09-21 10:42:27 -070013423 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020013424 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070013425
Chris Wilsona6c45cf2010-09-17 00:32:17 +010013426 if (IS_GEN2(dev)) {
13427 dev->mode_config.max_width = 2048;
13428 dev->mode_config.max_height = 2048;
13429 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070013430 dev->mode_config.max_width = 4096;
13431 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080013432 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010013433 dev->mode_config.max_width = 8192;
13434 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080013435 }
Damien Lespiau068be562014-03-28 14:17:49 +000013436
Ville Syrjälädc41c152014-08-13 11:57:05 +030013437 if (IS_845G(dev) || IS_I865G(dev)) {
13438 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
13439 dev->mode_config.cursor_height = 1023;
13440 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000013441 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
13442 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
13443 } else {
13444 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
13445 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
13446 }
13447
Ben Widawsky5d4545a2013-01-17 12:45:15 -080013448 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080013449
Zhao Yakui28c97732009-10-09 11:39:41 +080013450 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070013451 INTEL_INFO(dev)->num_pipes,
13452 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080013453
Damien Lespiau055e3932014-08-18 13:49:10 +010013454 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000013455 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000013456 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000013457 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070013458 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030013459 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000013460 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070013461 }
Jesse Barnes79e53942008-11-07 14:24:08 -080013462 }
13463
Jesse Barnesf42bb702013-12-16 16:34:23 -080013464 intel_init_dpio(dev);
13465
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013466 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013467
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013468 /* Just disable it once at startup */
13469 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013470 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000013471
13472 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013473 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080013474
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013475 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080013476 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013477 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080013478
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013479 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080013480 if (!crtc->active)
13481 continue;
13482
Jesse Barnes46f297f2014-03-07 08:57:48 -080013483 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080013484 * Note that reserving the BIOS fb up front prevents us
13485 * from stuffing other stolen allocations like the ring
13486 * on top. This prevents some ugliness at boot time, and
13487 * can even allow for smooth boot transitions if the BIOS
13488 * fb is large enough for the active pipe configuration.
13489 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013490 if (dev_priv->display.get_initial_plane_config) {
13491 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080013492 &crtc->plane_config);
13493 /*
13494 * If the fb is shared between multiple heads, we'll
13495 * just get the first one.
13496 */
Jesse Barnes484b41d2014-03-07 08:57:55 -080013497 intel_find_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080013498 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080013499 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010013500}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080013501
Daniel Vetter7fad7982012-07-04 17:51:47 +020013502static void intel_enable_pipe_a(struct drm_device *dev)
13503{
13504 struct intel_connector *connector;
13505 struct drm_connector *crt = NULL;
13506 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030013507 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020013508
13509 /* We can't just switch on the pipe A, we need to set things up with a
13510 * proper mode and output configuration. As a gross hack, enable pipe A
13511 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013512 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020013513 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
13514 crt = &connector->base;
13515 break;
13516 }
13517 }
13518
13519 if (!crt)
13520 return;
13521
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030013522 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
13523 intel_release_load_detect_pipe(crt, &load_detect_temp);
Daniel Vetter7fad7982012-07-04 17:51:47 +020013524}
13525
Daniel Vetterfa555832012-10-10 23:14:00 +020013526static bool
13527intel_check_plane_mapping(struct intel_crtc *crtc)
13528{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070013529 struct drm_device *dev = crtc->base.dev;
13530 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020013531 u32 reg, val;
13532
Ben Widawsky7eb552a2013-03-13 14:05:41 -070013533 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020013534 return true;
13535
13536 reg = DSPCNTR(!crtc->plane);
13537 val = I915_READ(reg);
13538
13539 if ((val & DISPLAY_PLANE_ENABLE) &&
13540 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
13541 return false;
13542
13543 return true;
13544}
13545
Daniel Vetter24929352012-07-02 20:28:59 +020013546static void intel_sanitize_crtc(struct intel_crtc *crtc)
13547{
13548 struct drm_device *dev = crtc->base.dev;
13549 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020013550 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020013551
Daniel Vetter24929352012-07-02 20:28:59 +020013552 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013553 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020013554 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
13555
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030013556 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010013557 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030013558 if (crtc->active) {
13559 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010013560 drm_crtc_vblank_on(&crtc->base);
13561 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030013562
Daniel Vetter24929352012-07-02 20:28:59 +020013563 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020013564 * disable the crtc (and hence change the state) if it is wrong. Note
13565 * that gen4+ has a fixed plane -> pipe mapping. */
13566 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020013567 struct intel_connector *connector;
13568 bool plane;
13569
Daniel Vetter24929352012-07-02 20:28:59 +020013570 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
13571 crtc->base.base.id);
13572
13573 /* Pipe has the wrong plane attached and the plane is active.
13574 * Temporarily change the plane mapping and disable everything
13575 * ... */
13576 plane = crtc->plane;
13577 crtc->plane = !plane;
Daniel Vetter9c8958b2014-07-14 19:35:31 +020013578 crtc->primary_enabled = true;
Daniel Vetter24929352012-07-02 20:28:59 +020013579 dev_priv->display.crtc_disable(&crtc->base);
13580 crtc->plane = plane;
13581
13582 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013583 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020013584 if (connector->encoder->base.crtc != &crtc->base)
13585 continue;
13586
Egbert Eich7f1950f2014-04-25 10:56:22 +020013587 connector->base.dpms = DRM_MODE_DPMS_OFF;
13588 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020013589 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020013590 /* multiple connectors may have the same encoder:
13591 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013592 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020013593 if (connector->encoder->base.crtc == &crtc->base) {
13594 connector->encoder->base.crtc = NULL;
13595 connector->encoder->connectors_active = false;
13596 }
Daniel Vetter24929352012-07-02 20:28:59 +020013597
13598 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080013599 crtc->base.state->enable = false;
Daniel Vetter24929352012-07-02 20:28:59 +020013600 crtc->base.enabled = false;
13601 }
Daniel Vetter24929352012-07-02 20:28:59 +020013602
Daniel Vetter7fad7982012-07-04 17:51:47 +020013603 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
13604 crtc->pipe == PIPE_A && !crtc->active) {
13605 /* BIOS forgot to enable pipe A, this mostly happens after
13606 * resume. Force-enable the pipe to fix this, the update_dpms
13607 * call below we restore the pipe to the right state, but leave
13608 * the required bits on. */
13609 intel_enable_pipe_a(dev);
13610 }
13611
Daniel Vetter24929352012-07-02 20:28:59 +020013612 /* Adjust the state of the output pipe according to whether we
13613 * have active connectors/encoders. */
13614 intel_crtc_update_dpms(&crtc->base);
13615
Matt Roper83d65732015-02-25 13:12:16 -080013616 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020013617 struct intel_encoder *encoder;
13618
13619 /* This can happen either due to bugs in the get_hw_state
13620 * functions or because the pipe is force-enabled due to the
13621 * pipe A quirk. */
13622 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
13623 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080013624 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020013625 crtc->active ? "enabled" : "disabled");
13626
Matt Roper83d65732015-02-25 13:12:16 -080013627 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020013628 crtc->base.enabled = crtc->active;
13629
13630 /* Because we only establish the connector -> encoder ->
13631 * crtc links if something is active, this means the
13632 * crtc is now deactivated. Break the links. connector
13633 * -> encoder links are only establish when things are
13634 * actually up, hence no need to break them. */
13635 WARN_ON(crtc->active);
13636
13637 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
13638 WARN_ON(encoder->connectors_active);
13639 encoder->base.crtc = NULL;
13640 }
13641 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020013642
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030013643 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010013644 /*
13645 * We start out with underrun reporting disabled to avoid races.
13646 * For correct bookkeeping mark this on active crtcs.
13647 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020013648 * Also on gmch platforms we dont have any hardware bits to
13649 * disable the underrun reporting. Which means we need to start
13650 * out with underrun reporting disabled also on inactive pipes,
13651 * since otherwise we'll complain about the garbage we read when
13652 * e.g. coming up after runtime pm.
13653 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010013654 * No protection against concurrent access is required - at
13655 * worst a fifo underrun happens which also sets this to false.
13656 */
13657 crtc->cpu_fifo_underrun_disabled = true;
13658 crtc->pch_fifo_underrun_disabled = true;
13659 }
Daniel Vetter24929352012-07-02 20:28:59 +020013660}
13661
13662static void intel_sanitize_encoder(struct intel_encoder *encoder)
13663{
13664 struct intel_connector *connector;
13665 struct drm_device *dev = encoder->base.dev;
13666
13667 /* We need to check both for a crtc link (meaning that the
13668 * encoder is active and trying to read from a pipe) and the
13669 * pipe itself being active. */
13670 bool has_active_crtc = encoder->base.crtc &&
13671 to_intel_crtc(encoder->base.crtc)->active;
13672
13673 if (encoder->connectors_active && !has_active_crtc) {
13674 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
13675 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013676 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020013677
13678 /* Connector is active, but has no active pipe. This is
13679 * fallout from our resume register restoring. Disable
13680 * the encoder manually again. */
13681 if (encoder->base.crtc) {
13682 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
13683 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013684 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020013685 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030013686 if (encoder->post_disable)
13687 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020013688 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020013689 encoder->base.crtc = NULL;
13690 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020013691
13692 /* Inconsistent output/port/pipe state happens presumably due to
13693 * a bug in one of the get_hw_state functions. Or someplace else
13694 * in our code, like the register restore mess on resume. Clamp
13695 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013696 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020013697 if (connector->encoder != encoder)
13698 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020013699 connector->base.dpms = DRM_MODE_DPMS_OFF;
13700 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020013701 }
13702 }
13703 /* Enabled encoders without active connectors will be fixed in
13704 * the crtc fixup. */
13705}
13706
Imre Deak04098752014-02-18 00:02:16 +020013707void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010013708{
13709 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020013710 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010013711
Imre Deak04098752014-02-18 00:02:16 +020013712 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
13713 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
13714 i915_disable_vga(dev);
13715 }
13716}
13717
13718void i915_redisable_vga(struct drm_device *dev)
13719{
13720 struct drm_i915_private *dev_priv = dev->dev_private;
13721
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030013722 /* This function can be called both from intel_modeset_setup_hw_state or
13723 * at a very early point in our resume sequence, where the power well
13724 * structures are not yet restored. Since this function is at a very
13725 * paranoid "someone might have enabled VGA while we were not looking"
13726 * level, just check if the power well is enabled instead of trying to
13727 * follow the "don't touch the power well if we don't need it" policy
13728 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013729 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030013730 return;
13731
Imre Deak04098752014-02-18 00:02:16 +020013732 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010013733}
13734
Ville Syrjälä98ec7732014-04-30 17:43:01 +030013735static bool primary_get_hw_state(struct intel_crtc *crtc)
13736{
13737 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
13738
13739 if (!crtc->active)
13740 return false;
13741
13742 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
13743}
13744
Daniel Vetter30e984d2013-06-05 13:34:17 +020013745static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020013746{
13747 struct drm_i915_private *dev_priv = dev->dev_private;
13748 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020013749 struct intel_crtc *crtc;
13750 struct intel_encoder *encoder;
13751 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020013752 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020013753
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013754 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013755 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020013756
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013757 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020013758
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013759 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013760 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020013761
Matt Roper83d65732015-02-25 13:12:16 -080013762 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020013763 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030013764 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020013765
13766 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
13767 crtc->base.base.id,
13768 crtc->active ? "enabled" : "disabled");
13769 }
13770
Daniel Vetter53589012013-06-05 13:34:16 +020013771 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13772 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13773
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013774 pll->on = pll->get_hw_state(dev_priv, pll,
13775 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020013776 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013777 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013778 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020013779 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020013780 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013781 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020013782 }
Daniel Vetter53589012013-06-05 13:34:16 +020013783 }
Daniel Vetter53589012013-06-05 13:34:16 +020013784
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020013785 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013786 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013787
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013788 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013789 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020013790 }
13791
Damien Lespiaub2784e12014-08-05 11:29:37 +010013792 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020013793 pipe = 0;
13794
13795 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070013796 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13797 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013798 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020013799 } else {
13800 encoder->base.crtc = NULL;
13801 }
13802
13803 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010013804 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020013805 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013806 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020013807 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010013808 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020013809 }
13810
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013811 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020013812 if (connector->get_hw_state(connector)) {
13813 connector->base.dpms = DRM_MODE_DPMS_ON;
13814 connector->encoder->connectors_active = true;
13815 connector->base.encoder = &connector->encoder->base;
13816 } else {
13817 connector->base.dpms = DRM_MODE_DPMS_OFF;
13818 connector->base.encoder = NULL;
13819 }
13820 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
13821 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013822 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020013823 connector->base.encoder ? "enabled" : "disabled");
13824 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020013825}
13826
13827/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
13828 * and i915 state tracking structures. */
13829void intel_modeset_setup_hw_state(struct drm_device *dev,
13830 bool force_restore)
13831{
13832 struct drm_i915_private *dev_priv = dev->dev_private;
13833 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020013834 struct intel_crtc *crtc;
13835 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020013836 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020013837
13838 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020013839
Jesse Barnesbabea612013-06-26 18:57:38 +030013840 /*
13841 * Now that we have the config, copy it to each CRTC struct
13842 * Note that this could go away if we move to using crtc_config
13843 * checking everywhere.
13844 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013845 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020013846 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013847 intel_mode_from_pipe_config(&crtc->base.mode,
13848 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030013849 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
13850 crtc->base.base.id);
13851 drm_mode_debug_printmodeline(&crtc->base.mode);
13852 }
13853 }
13854
Daniel Vetter24929352012-07-02 20:28:59 +020013855 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010013856 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020013857 intel_sanitize_encoder(encoder);
13858 }
13859
Damien Lespiau055e3932014-08-18 13:49:10 +010013860 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020013861 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13862 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013863 intel_dump_pipe_config(crtc, crtc->config,
13864 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020013865 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013866
Daniel Vetter35c95372013-07-17 06:55:04 +020013867 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13868 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13869
13870 if (!pll->on || pll->active)
13871 continue;
13872
13873 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
13874
13875 pll->disable(dev_priv, pll);
13876 pll->on = false;
13877 }
13878
Pradeep Bhat30789992014-11-04 17:06:45 +000013879 if (IS_GEN9(dev))
13880 skl_wm_get_hw_state(dev);
13881 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030013882 ilk_wm_get_hw_state(dev);
13883
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010013884 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030013885 i915_redisable_vga(dev);
13886
Daniel Vetterf30da182013-04-11 20:22:50 +020013887 /*
13888 * We need to use raw interfaces for restoring state to avoid
13889 * checking (bogus) intermediate states.
13890 */
Damien Lespiau055e3932014-08-18 13:49:10 +010013891 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070013892 struct drm_crtc *crtc =
13893 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020013894
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013895 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
13896 crtc->primary->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010013897 }
13898 } else {
13899 intel_modeset_update_staged_output_state(dev);
13900 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013901
13902 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010013903}
13904
13905void intel_modeset_gem_init(struct drm_device *dev)
13906{
Jesse Barnes92122782014-10-09 12:57:42 -070013907 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080013908 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070013909 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -080013910
Imre Deakae484342014-03-31 15:10:44 +030013911 mutex_lock(&dev->struct_mutex);
13912 intel_init_gt_powersave(dev);
13913 mutex_unlock(&dev->struct_mutex);
13914
Jesse Barnes92122782014-10-09 12:57:42 -070013915 /*
13916 * There may be no VBT; and if the BIOS enabled SSC we can
13917 * just keep using it to avoid unnecessary flicker. Whereas if the
13918 * BIOS isn't using it, don't assume it will work even if the VBT
13919 * indicates as much.
13920 */
13921 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
13922 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
13923 DREF_SSC1_ENABLE);
13924
Chris Wilson1833b132012-05-09 11:56:28 +010013925 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020013926
13927 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080013928
13929 /*
13930 * Make sure any fbs we allocated at startup are properly
13931 * pinned & fenced. When we do the allocation it's too early
13932 * for this.
13933 */
13934 mutex_lock(&dev->struct_mutex);
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010013935 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070013936 obj = intel_fb_obj(c->primary->fb);
13937 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080013938 continue;
13939
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +000013940 if (intel_pin_and_fence_fb_obj(c->primary,
13941 c->primary->fb,
13942 NULL)) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080013943 DRM_ERROR("failed to pin boot fb on pipe %d\n",
13944 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100013945 drm_framebuffer_unreference(c->primary->fb);
13946 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080013947 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080013948 }
13949 }
13950 mutex_unlock(&dev->struct_mutex);
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020013951
13952 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013953}
13954
Imre Deak4932e2c2014-02-11 17:12:48 +020013955void intel_connector_unregister(struct intel_connector *intel_connector)
13956{
13957 struct drm_connector *connector = &intel_connector->base;
13958
13959 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010013960 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020013961}
13962
Jesse Barnes79e53942008-11-07 14:24:08 -080013963void intel_modeset_cleanup(struct drm_device *dev)
13964{
Jesse Barnes652c3932009-08-17 13:31:43 -070013965 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030013966 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070013967
Imre Deak2eb52522014-11-19 15:30:05 +020013968 intel_disable_gt_powersave(dev);
13969
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020013970 intel_backlight_unregister(dev);
13971
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013972 /*
13973 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020013974 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013975 * experience fancy races otherwise.
13976 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020013977 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070013978
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013979 /*
13980 * Due to the hpd irq storm handling the hotplug work can re-arm the
13981 * poll handlers. Hence disable polling after hpd handling is shut down.
13982 */
Keith Packardf87ea762010-10-03 19:36:26 -070013983 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013984
Jesse Barnes652c3932009-08-17 13:31:43 -070013985 mutex_lock(&dev->struct_mutex);
13986
Jesse Barnes723bfd72010-10-07 16:01:13 -070013987 intel_unregister_dsm_handler();
13988
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013989 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070013990
Kristian Høgsberg69341a52009-11-11 12:19:17 -050013991 mutex_unlock(&dev->struct_mutex);
13992
Chris Wilson1630fe72011-07-08 12:22:42 +010013993 /* flush any delayed tasks or pending work */
13994 flush_scheduled_work();
13995
Jani Nikuladb31af1d2013-11-08 16:48:53 +020013996 /* destroy the backlight and sysfs files before encoders/connectors */
13997 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020013998 struct intel_connector *intel_connector;
13999
14000 intel_connector = to_intel_connector(connector);
14001 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020014002 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030014003
Jesse Barnes79e53942008-11-07 14:24:08 -080014004 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010014005
14006 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030014007
14008 mutex_lock(&dev->struct_mutex);
14009 intel_cleanup_gt_powersave(dev);
14010 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014011}
14012
Dave Airlie28d52042009-09-21 14:33:58 +100014013/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080014014 * Return which encoder is currently attached for connector.
14015 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010014016struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080014017{
Chris Wilsondf0e9242010-09-09 16:20:55 +010014018 return &intel_attached_encoder(connector)->base;
14019}
Jesse Barnes79e53942008-11-07 14:24:08 -080014020
Chris Wilsondf0e9242010-09-09 16:20:55 +010014021void intel_connector_attach_encoder(struct intel_connector *connector,
14022 struct intel_encoder *encoder)
14023{
14024 connector->encoder = encoder;
14025 drm_mode_connector_attach_encoder(&connector->base,
14026 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080014027}
Dave Airlie28d52042009-09-21 14:33:58 +100014028
14029/*
14030 * set vga decode state - true == enable VGA decode
14031 */
14032int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
14033{
14034 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000014035 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100014036 u16 gmch_ctrl;
14037
Chris Wilson75fa0412014-02-07 18:37:02 -020014038 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
14039 DRM_ERROR("failed to read control word\n");
14040 return -EIO;
14041 }
14042
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020014043 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
14044 return 0;
14045
Dave Airlie28d52042009-09-21 14:33:58 +100014046 if (state)
14047 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
14048 else
14049 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020014050
14051 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
14052 DRM_ERROR("failed to write control word\n");
14053 return -EIO;
14054 }
14055
Dave Airlie28d52042009-09-21 14:33:58 +100014056 return 0;
14057}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014058
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014059struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030014060
14061 u32 power_well_driver;
14062
Chris Wilson63b66e52013-08-08 15:12:06 +020014063 int num_transcoders;
14064
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014065 struct intel_cursor_error_state {
14066 u32 control;
14067 u32 position;
14068 u32 base;
14069 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010014070 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014071
14072 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020014073 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014074 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030014075 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010014076 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014077
14078 struct intel_plane_error_state {
14079 u32 control;
14080 u32 stride;
14081 u32 size;
14082 u32 pos;
14083 u32 addr;
14084 u32 surface;
14085 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010014086 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020014087
14088 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020014089 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020014090 enum transcoder cpu_transcoder;
14091
14092 u32 conf;
14093
14094 u32 htotal;
14095 u32 hblank;
14096 u32 hsync;
14097 u32 vtotal;
14098 u32 vblank;
14099 u32 vsync;
14100 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014101};
14102
14103struct intel_display_error_state *
14104intel_display_capture_error_state(struct drm_device *dev)
14105{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014106 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014107 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020014108 int transcoders[] = {
14109 TRANSCODER_A,
14110 TRANSCODER_B,
14111 TRANSCODER_C,
14112 TRANSCODER_EDP,
14113 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014114 int i;
14115
Chris Wilson63b66e52013-08-08 15:12:06 +020014116 if (INTEL_INFO(dev)->num_pipes == 0)
14117 return NULL;
14118
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020014119 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014120 if (error == NULL)
14121 return NULL;
14122
Imre Deak190be112013-11-25 17:15:31 +020014123 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030014124 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
14125
Damien Lespiau055e3932014-08-18 13:49:10 +010014126 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020014127 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014128 __intel_display_power_is_enabled(dev_priv,
14129 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020014130 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020014131 continue;
14132
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030014133 error->cursor[i].control = I915_READ(CURCNTR(i));
14134 error->cursor[i].position = I915_READ(CURPOS(i));
14135 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014136
14137 error->plane[i].control = I915_READ(DSPCNTR(i));
14138 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014139 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030014140 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014141 error->plane[i].pos = I915_READ(DSPPOS(i));
14142 }
Paulo Zanonica291362013-03-06 20:03:14 -030014143 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
14144 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014145 if (INTEL_INFO(dev)->gen >= 4) {
14146 error->plane[i].surface = I915_READ(DSPSURF(i));
14147 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
14148 }
14149
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014150 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030014151
Sonika Jindal3abfce72014-07-21 15:23:43 +053014152 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e12014-04-18 15:55:04 +030014153 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020014154 }
14155
14156 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
14157 if (HAS_DDI(dev_priv->dev))
14158 error->num_transcoders++; /* Account for eDP. */
14159
14160 for (i = 0; i < error->num_transcoders; i++) {
14161 enum transcoder cpu_transcoder = transcoders[i];
14162
Imre Deakddf9c532013-11-27 22:02:02 +020014163 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014164 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020014165 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020014166 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020014167 continue;
14168
Chris Wilson63b66e52013-08-08 15:12:06 +020014169 error->transcoder[i].cpu_transcoder = cpu_transcoder;
14170
14171 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
14172 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
14173 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
14174 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
14175 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
14176 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
14177 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014178 }
14179
14180 return error;
14181}
14182
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014183#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
14184
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014185void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014186intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014187 struct drm_device *dev,
14188 struct intel_display_error_state *error)
14189{
Damien Lespiau055e3932014-08-18 13:49:10 +010014190 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014191 int i;
14192
Chris Wilson63b66e52013-08-08 15:12:06 +020014193 if (!error)
14194 return;
14195
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014196 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020014197 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014198 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030014199 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010014200 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014201 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020014202 err_printf(m, " Power: %s\n",
14203 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014204 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030014205 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014206
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014207 err_printf(m, "Plane [%d]:\n", i);
14208 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
14209 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014210 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014211 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
14212 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014213 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030014214 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014215 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014216 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014217 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
14218 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014219 }
14220
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014221 err_printf(m, "Cursor [%d]:\n", i);
14222 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
14223 err_printf(m, " POS: %08x\n", error->cursor[i].position);
14224 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014225 }
Chris Wilson63b66e52013-08-08 15:12:06 +020014226
14227 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010014228 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020014229 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020014230 err_printf(m, " Power: %s\n",
14231 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020014232 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
14233 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
14234 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
14235 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
14236 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
14237 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
14238 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
14239 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014240}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014241
14242void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
14243{
14244 struct intel_crtc *crtc;
14245
14246 for_each_intel_crtc(dev, crtc) {
14247 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014248
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020014249 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014250
14251 work = crtc->unpin_work;
14252
14253 if (work && work->event &&
14254 work->event->base.file_priv == file) {
14255 kfree(work->event);
14256 work->event = NULL;
14257 }
14258
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020014259 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014260 }
14261}