blob: f75173c20f47677f1a8462dabc78b10ac7b4645f [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 Barnesa0c4da22012-06-15 11:55:13 -0700379 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da22012-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 Barnesa0c4da22012-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},
394 .vco = { .min = 4860000, .max = 6700000 },
395 .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 Barnesa0c4da22012-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
Zhenyu Wang2c072452009-06-05 15:38:42 +0800779static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300780vlv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200781 int target, int refclk, intel_clock_t *match_clock,
782 intel_clock_t *best_clock)
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700783{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300784 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300785 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300786 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300787 /* min update 19.2 MHz */
788 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300789 bool found = false;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700790
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300791 target *= 5; /* fast clock */
792
793 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700794
795 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300796 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300797 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300798 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300799 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300800 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700801 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300802 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300803 unsigned int ppm, diff;
804
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300805 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
806 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300807
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300808 vlv_clock(refclk, &clock);
809
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300810 if (!intel_PLL_is_valid(dev, limit,
811 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300812 continue;
813
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300814 diff = abs(clock.dot - target);
815 ppm = div_u64(1000000ULL * diff, target);
816
817 if (ppm < 100 && clock.p > best_clock->p) {
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300818 bestppm = 0;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300819 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300820 found = true;
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300821 }
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300822
Ville Syrjäläc6861222013-09-24 21:26:21 +0300823 if (bestppm >= 10 && ppm < bestppm - 10) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300824 bestppm = ppm;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300825 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300826 found = true;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700827 }
828 }
829 }
830 }
831 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700832
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300833 return found;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700834}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700835
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300836static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300837chv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300838 int target, int refclk, intel_clock_t *match_clock,
839 intel_clock_t *best_clock)
840{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300841 struct drm_device *dev = crtc->base.dev;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300842 intel_clock_t clock;
843 uint64_t m2;
844 int found = false;
845
846 memset(best_clock, 0, sizeof(*best_clock));
847
848 /*
849 * Based on hardware doc, the n always set to 1, and m1 always
850 * set to 2. If requires to support 200Mhz refclk, we need to
851 * revisit this because n may not 1 anymore.
852 */
853 clock.n = 1, clock.m1 = 2;
854 target *= 5; /* fast clock */
855
856 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
857 for (clock.p2 = limit->p2.p2_fast;
858 clock.p2 >= limit->p2.p2_slow;
859 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
860
861 clock.p = clock.p1 * clock.p2;
862
863 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
864 clock.n) << 22, refclk * clock.m1);
865
866 if (m2 > INT_MAX/clock.m1)
867 continue;
868
869 clock.m2 = m2;
870
871 chv_clock(refclk, &clock);
872
873 if (!intel_PLL_is_valid(dev, limit, &clock))
874 continue;
875
876 /* based on hardware requirement, prefer bigger p
877 */
878 if (clock.p > best_clock->p) {
879 *best_clock = clock;
880 found = true;
881 }
882 }
883 }
884
885 return found;
886}
887
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300888bool intel_crtc_active(struct drm_crtc *crtc)
889{
890 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
891
892 /* Be paranoid as we can arrive here with only partial
893 * state retrieved from the hardware during setup.
894 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100895 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300896 * as Haswell has gained clock readout/fastboot support.
897 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000898 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300899 * properly reconstruct framebuffers.
900 */
Matt Roperf4510a22014-04-01 15:22:40 -0700901 return intel_crtc->active && crtc->primary->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200902 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300903}
904
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200905enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
906 enum pipe pipe)
907{
908 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
909 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
910
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200911 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200912}
913
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300914static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
915{
916 struct drm_i915_private *dev_priv = dev->dev_private;
917 u32 reg = PIPEDSL(pipe);
918 u32 line1, line2;
919 u32 line_mask;
920
921 if (IS_GEN2(dev))
922 line_mask = DSL_LINEMASK_GEN2;
923 else
924 line_mask = DSL_LINEMASK_GEN3;
925
926 line1 = I915_READ(reg) & line_mask;
927 mdelay(5);
928 line2 = I915_READ(reg) & line_mask;
929
930 return line1 == line2;
931}
932
Keith Packardab7ad7f2010-10-03 00:33:06 -0700933/*
934 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300935 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700936 *
937 * After disabling a pipe, we can't wait for vblank in the usual way,
938 * spinning on the vblank interrupt status bit, since we won't actually
939 * see an interrupt when the pipe is disabled.
940 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700941 * On Gen4 and above:
942 * wait for the pipe register state bit to turn off
943 *
944 * Otherwise:
945 * wait for the display line value to settle (it usually
946 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100947 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700948 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300949static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700950{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300951 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700952 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200953 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300954 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700955
Keith Packardab7ad7f2010-10-03 00:33:06 -0700956 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200957 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700958
Keith Packardab7ad7f2010-10-03 00:33:06 -0700959 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100960 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
961 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200962 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700963 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -0700964 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300965 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200966 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700967 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800968}
969
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000970/*
971 * ibx_digital_port_connected - is the specified port connected?
972 * @dev_priv: i915 private structure
973 * @port: the port to test
974 *
975 * Returns true if @port is connected, false otherwise.
976 */
977bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
978 struct intel_digital_port *port)
979{
980 u32 bit;
981
Damien Lespiauc36346e2012-12-13 16:09:03 +0000982 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +0200983 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +0000984 case PORT_B:
985 bit = SDE_PORTB_HOTPLUG;
986 break;
987 case PORT_C:
988 bit = SDE_PORTC_HOTPLUG;
989 break;
990 case PORT_D:
991 bit = SDE_PORTD_HOTPLUG;
992 break;
993 default:
994 return true;
995 }
996 } else {
Robin Schroereba905b2014-05-18 02:24:50 +0200997 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +0000998 case PORT_B:
999 bit = SDE_PORTB_HOTPLUG_CPT;
1000 break;
1001 case PORT_C:
1002 bit = SDE_PORTC_HOTPLUG_CPT;
1003 break;
1004 case PORT_D:
1005 bit = SDE_PORTD_HOTPLUG_CPT;
1006 break;
1007 default:
1008 return true;
1009 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001010 }
1011
1012 return I915_READ(SDEISR) & bit;
1013}
1014
Jesse Barnesb24e7172011-01-04 15:09:30 -08001015static const char *state_string(bool enabled)
1016{
1017 return enabled ? "on" : "off";
1018}
1019
1020/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001021void assert_pll(struct drm_i915_private *dev_priv,
1022 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001023{
1024 int reg;
1025 u32 val;
1026 bool cur_state;
1027
1028 reg = DPLL(pipe);
1029 val = I915_READ(reg);
1030 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001031 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001032 "PLL state assertion failure (expected %s, current %s)\n",
1033 state_string(state), state_string(cur_state));
1034}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001035
Jani Nikula23538ef2013-08-27 15:12:22 +03001036/* XXX: the dsi pll is shared between MIPI DSI ports */
1037static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1038{
1039 u32 val;
1040 bool cur_state;
1041
1042 mutex_lock(&dev_priv->dpio_lock);
1043 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1044 mutex_unlock(&dev_priv->dpio_lock);
1045
1046 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001047 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001048 "DSI PLL state assertion failure (expected %s, current %s)\n",
1049 state_string(state), state_string(cur_state));
1050}
1051#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1052#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1053
Daniel Vetter55607e82013-06-16 21:42:39 +02001054struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001055intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001056{
Daniel Vettere2b78262013-06-07 23:10:03 +02001057 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1058
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001059 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001060 return NULL;
1061
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001062 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001063}
1064
Jesse Barnesb24e7172011-01-04 15:09:30 -08001065/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001066void assert_shared_dpll(struct drm_i915_private *dev_priv,
1067 struct intel_shared_dpll *pll,
1068 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001069{
Jesse Barnes040484a2011-01-03 12:14:26 -08001070 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001071 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001072
Chris Wilson92b27b02012-05-20 18:10:50 +01001073 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001074 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001075 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001076
Daniel Vetter53589012013-06-05 13:34:16 +02001077 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001078 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001079 "%s assertion failure (expected %s, current %s)\n",
1080 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001081}
Jesse Barnes040484a2011-01-03 12:14:26 -08001082
1083static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1084 enum pipe pipe, bool state)
1085{
1086 int reg;
1087 u32 val;
1088 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001089 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1090 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001091
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001092 if (HAS_DDI(dev_priv->dev)) {
1093 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001094 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001095 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001096 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001097 } else {
1098 reg = FDI_TX_CTL(pipe);
1099 val = I915_READ(reg);
1100 cur_state = !!(val & FDI_TX_ENABLE);
1101 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001102 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001103 "FDI TX state assertion failure (expected %s, current %s)\n",
1104 state_string(state), state_string(cur_state));
1105}
1106#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1107#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1108
1109static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1110 enum pipe pipe, bool state)
1111{
1112 int reg;
1113 u32 val;
1114 bool cur_state;
1115
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001116 reg = FDI_RX_CTL(pipe);
1117 val = I915_READ(reg);
1118 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001119 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001120 "FDI RX state assertion failure (expected %s, current %s)\n",
1121 state_string(state), state_string(cur_state));
1122}
1123#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1124#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1125
1126static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1127 enum pipe pipe)
1128{
1129 int reg;
1130 u32 val;
1131
1132 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001133 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001134 return;
1135
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001136 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001137 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001138 return;
1139
Jesse Barnes040484a2011-01-03 12:14:26 -08001140 reg = FDI_TX_CTL(pipe);
1141 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001142 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 -08001143}
1144
Daniel Vetter55607e82013-06-16 21:42:39 +02001145void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1146 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001147{
1148 int reg;
1149 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001150 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001151
1152 reg = FDI_RX_CTL(pipe);
1153 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001154 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001155 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001156 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1157 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001158}
1159
Daniel Vetterb680c372014-09-19 18:27:27 +02001160void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1161 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001162{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001163 struct drm_device *dev = dev_priv->dev;
1164 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001165 u32 val;
1166 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001167 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001168
Jani Nikulabedd4db2014-08-22 15:04:13 +03001169 if (WARN_ON(HAS_DDI(dev)))
1170 return;
1171
1172 if (HAS_PCH_SPLIT(dev)) {
1173 u32 port_sel;
1174
Jesse Barnesea0760c2011-01-04 15:09:32 -08001175 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001176 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1177
1178 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1179 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1180 panel_pipe = PIPE_B;
1181 /* XXX: else fix for eDP */
1182 } else if (IS_VALLEYVIEW(dev)) {
1183 /* presumably write lock depends on pipe, not port select */
1184 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1185 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001186 } else {
1187 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001188 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1189 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001190 }
1191
1192 val = I915_READ(pp_reg);
1193 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001194 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001195 locked = false;
1196
Rob Clarke2c719b2014-12-15 13:56:32 -05001197 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001198 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001199 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001200}
1201
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001202static void assert_cursor(struct drm_i915_private *dev_priv,
1203 enum pipe pipe, bool state)
1204{
1205 struct drm_device *dev = dev_priv->dev;
1206 bool cur_state;
1207
Paulo Zanonid9d82082014-02-27 16:30:56 -03001208 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001209 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001210 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001211 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001212
Rob Clarke2c719b2014-12-15 13:56:32 -05001213 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001214 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1215 pipe_name(pipe), state_string(state), state_string(cur_state));
1216}
1217#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1218#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1219
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001220void assert_pipe(struct drm_i915_private *dev_priv,
1221 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001222{
1223 int reg;
1224 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001225 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001226 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1227 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001228
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001229 /* if we need the pipe quirk it must be always on */
1230 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1231 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001232 state = true;
1233
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001234 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001235 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001236 cur_state = false;
1237 } else {
1238 reg = PIPECONF(cpu_transcoder);
1239 val = I915_READ(reg);
1240 cur_state = !!(val & PIPECONF_ENABLE);
1241 }
1242
Rob Clarke2c719b2014-12-15 13:56:32 -05001243 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001244 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001245 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001246}
1247
Chris Wilson931872f2012-01-16 23:01:13 +00001248static void assert_plane(struct drm_i915_private *dev_priv,
1249 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001250{
1251 int reg;
1252 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001253 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001254
1255 reg = DSPCNTR(plane);
1256 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001257 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001258 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001259 "plane %c assertion failure (expected %s, current %s)\n",
1260 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001261}
1262
Chris Wilson931872f2012-01-16 23:01:13 +00001263#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1264#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1265
Jesse Barnesb24e7172011-01-04 15:09:30 -08001266static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1267 enum pipe pipe)
1268{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001269 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001270 int reg, i;
1271 u32 val;
1272 int cur_pipe;
1273
Ville Syrjälä653e1022013-06-04 13:49:05 +03001274 /* Primary planes are fixed to pipes on gen4+ */
1275 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001276 reg = DSPCNTR(pipe);
1277 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001278 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001279 "plane %c assertion failure, should be disabled but not\n",
1280 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001281 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001282 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001283
Jesse Barnesb24e7172011-01-04 15:09:30 -08001284 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001285 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001286 reg = DSPCNTR(i);
1287 val = I915_READ(reg);
1288 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1289 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001290 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001291 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1292 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001293 }
1294}
1295
Jesse Barnes19332d72013-03-28 09:55:38 -07001296static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1297 enum pipe pipe)
1298{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001299 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001300 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001301 u32 val;
1302
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001303 if (INTEL_INFO(dev)->gen >= 9) {
1304 for_each_sprite(pipe, sprite) {
1305 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001306 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001307 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1308 sprite, pipe_name(pipe));
1309 }
1310 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001311 for_each_sprite(pipe, sprite) {
1312 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001313 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001314 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001315 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001316 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001317 }
1318 } else if (INTEL_INFO(dev)->gen >= 7) {
1319 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001320 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001321 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001322 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001323 plane_name(pipe), pipe_name(pipe));
1324 } else if (INTEL_INFO(dev)->gen >= 5) {
1325 reg = DVSCNTR(pipe);
1326 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001327 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001328 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1329 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001330 }
1331}
1332
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001333static void assert_vblank_disabled(struct drm_crtc *crtc)
1334{
Rob Clarke2c719b2014-12-15 13:56:32 -05001335 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001336 drm_crtc_vblank_put(crtc);
1337}
1338
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001339static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001340{
1341 u32 val;
1342 bool enabled;
1343
Rob Clarke2c719b2014-12-15 13:56:32 -05001344 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001345
Jesse Barnes92f25842011-01-04 15:09:34 -08001346 val = I915_READ(PCH_DREF_CONTROL);
1347 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1348 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001349 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001350}
1351
Daniel Vetterab9412b2013-05-03 11:49:46 +02001352static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1353 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001354{
1355 int reg;
1356 u32 val;
1357 bool enabled;
1358
Daniel Vetterab9412b2013-05-03 11:49:46 +02001359 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001360 val = I915_READ(reg);
1361 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001362 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001363 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1364 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001365}
1366
Keith Packard4e634382011-08-06 10:39:45 -07001367static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1368 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001369{
1370 if ((val & DP_PORT_EN) == 0)
1371 return false;
1372
1373 if (HAS_PCH_CPT(dev_priv->dev)) {
1374 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1375 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1376 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1377 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001378 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1379 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1380 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001381 } else {
1382 if ((val & DP_PIPE_MASK) != (pipe << 30))
1383 return false;
1384 }
1385 return true;
1386}
1387
Keith Packard1519b992011-08-06 10:35:34 -07001388static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1389 enum pipe pipe, u32 val)
1390{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001391 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001392 return false;
1393
1394 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001395 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001396 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001397 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1398 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1399 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001400 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001401 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001402 return false;
1403 }
1404 return true;
1405}
1406
1407static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1408 enum pipe pipe, u32 val)
1409{
1410 if ((val & LVDS_PORT_EN) == 0)
1411 return false;
1412
1413 if (HAS_PCH_CPT(dev_priv->dev)) {
1414 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1415 return false;
1416 } else {
1417 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1418 return false;
1419 }
1420 return true;
1421}
1422
1423static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1424 enum pipe pipe, u32 val)
1425{
1426 if ((val & ADPA_DAC_ENABLE) == 0)
1427 return false;
1428 if (HAS_PCH_CPT(dev_priv->dev)) {
1429 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1430 return false;
1431 } else {
1432 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1433 return false;
1434 }
1435 return true;
1436}
1437
Jesse Barnes291906f2011-02-02 12:28:03 -08001438static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001439 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001440{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001441 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001442 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001443 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001444 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001445
Rob Clarke2c719b2014-12-15 13:56:32 -05001446 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001447 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001448 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001449}
1450
1451static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1452 enum pipe pipe, int reg)
1453{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001454 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001455 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001456 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001457 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001458
Rob Clarke2c719b2014-12-15 13:56:32 -05001459 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001460 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001461 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001462}
1463
1464static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1465 enum pipe pipe)
1466{
1467 int reg;
1468 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001469
Keith Packardf0575e92011-07-25 22:12:43 -07001470 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1471 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1472 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001473
1474 reg = PCH_ADPA;
1475 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001476 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001477 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001478 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001479
1480 reg = PCH_LVDS;
1481 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001482 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001483 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001484 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001485
Paulo Zanonie2debe92013-02-18 19:00:27 -03001486 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1487 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1488 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001489}
1490
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001491static void intel_init_dpio(struct drm_device *dev)
1492{
1493 struct drm_i915_private *dev_priv = dev->dev_private;
1494
1495 if (!IS_VALLEYVIEW(dev))
1496 return;
1497
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001498 /*
1499 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1500 * CHV x1 PHY (DP/HDMI D)
1501 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1502 */
1503 if (IS_CHERRYVIEW(dev)) {
1504 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1505 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1506 } else {
1507 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1508 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001509}
1510
Ville Syrjäläd288f652014-10-28 13:20:22 +02001511static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001512 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001513{
Daniel Vetter426115c2013-07-11 22:13:42 +02001514 struct drm_device *dev = crtc->base.dev;
1515 struct drm_i915_private *dev_priv = dev->dev_private;
1516 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001517 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001518
Daniel Vetter426115c2013-07-11 22:13:42 +02001519 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001520
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001521 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001522 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1523
1524 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001525 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001526 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001527
Daniel Vetter426115c2013-07-11 22:13:42 +02001528 I915_WRITE(reg, dpll);
1529 POSTING_READ(reg);
1530 udelay(150);
1531
1532 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1533 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1534
Ville Syrjäläd288f652014-10-28 13:20:22 +02001535 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001536 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001537
1538 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001539 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001540 POSTING_READ(reg);
1541 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001542 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001543 POSTING_READ(reg);
1544 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001545 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001546 POSTING_READ(reg);
1547 udelay(150); /* wait for warmup */
1548}
1549
Ville Syrjäläd288f652014-10-28 13:20:22 +02001550static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001551 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001552{
1553 struct drm_device *dev = crtc->base.dev;
1554 struct drm_i915_private *dev_priv = dev->dev_private;
1555 int pipe = crtc->pipe;
1556 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001557 u32 tmp;
1558
1559 assert_pipe_disabled(dev_priv, crtc->pipe);
1560
1561 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1562
1563 mutex_lock(&dev_priv->dpio_lock);
1564
1565 /* Enable back the 10bit clock to display controller */
1566 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1567 tmp |= DPIO_DCLKP_EN;
1568 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1569
1570 /*
1571 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1572 */
1573 udelay(1);
1574
1575 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001576 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001577
1578 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001579 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001580 DRM_ERROR("PLL %d failed to lock\n", pipe);
1581
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001582 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001583 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001584 POSTING_READ(DPLL_MD(pipe));
1585
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001586 mutex_unlock(&dev_priv->dpio_lock);
1587}
1588
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001589static int intel_num_dvo_pipes(struct drm_device *dev)
1590{
1591 struct intel_crtc *crtc;
1592 int count = 0;
1593
1594 for_each_intel_crtc(dev, crtc)
1595 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001596 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001597
1598 return count;
1599}
1600
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001601static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001602{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001603 struct drm_device *dev = crtc->base.dev;
1604 struct drm_i915_private *dev_priv = dev->dev_private;
1605 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001606 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001607
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001608 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001609
1610 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001611 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001612
1613 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001614 if (IS_MOBILE(dev) && !IS_I830(dev))
1615 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001616
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001617 /* Enable DVO 2x clock on both PLLs if necessary */
1618 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1619 /*
1620 * It appears to be important that we don't enable this
1621 * for the current pipe before otherwise configuring the
1622 * PLL. No idea how this should be handled if multiple
1623 * DVO outputs are enabled simultaneosly.
1624 */
1625 dpll |= DPLL_DVO_2X_MODE;
1626 I915_WRITE(DPLL(!crtc->pipe),
1627 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1628 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001629
1630 /* Wait for the clocks to stabilize. */
1631 POSTING_READ(reg);
1632 udelay(150);
1633
1634 if (INTEL_INFO(dev)->gen >= 4) {
1635 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001636 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001637 } else {
1638 /* The pixel multiplier can only be updated once the
1639 * DPLL is enabled and the clocks are stable.
1640 *
1641 * So write it again.
1642 */
1643 I915_WRITE(reg, dpll);
1644 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001645
1646 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001647 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001648 POSTING_READ(reg);
1649 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001650 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001651 POSTING_READ(reg);
1652 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001653 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001654 POSTING_READ(reg);
1655 udelay(150); /* wait for warmup */
1656}
1657
1658/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001659 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001660 * @dev_priv: i915 private structure
1661 * @pipe: pipe PLL to disable
1662 *
1663 * Disable the PLL for @pipe, making sure the pipe is off first.
1664 *
1665 * Note! This is for pre-ILK only.
1666 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001667static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001668{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001669 struct drm_device *dev = crtc->base.dev;
1670 struct drm_i915_private *dev_priv = dev->dev_private;
1671 enum pipe pipe = crtc->pipe;
1672
1673 /* Disable DVO 2x clock on both PLLs if necessary */
1674 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001675 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001676 intel_num_dvo_pipes(dev) == 1) {
1677 I915_WRITE(DPLL(PIPE_B),
1678 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1679 I915_WRITE(DPLL(PIPE_A),
1680 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1681 }
1682
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001683 /* Don't disable pipe or pipe PLLs if needed */
1684 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1685 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001686 return;
1687
1688 /* Make sure the pipe isn't still relying on us */
1689 assert_pipe_disabled(dev_priv, pipe);
1690
Daniel Vetter50b44a42013-06-05 13:34:33 +02001691 I915_WRITE(DPLL(pipe), 0);
1692 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001693}
1694
Jesse Barnesf6071162013-10-01 10:41:38 -07001695static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1696{
1697 u32 val = 0;
1698
1699 /* Make sure the pipe isn't still relying on us */
1700 assert_pipe_disabled(dev_priv, pipe);
1701
Imre Deake5cbfbf2014-01-09 17:08:16 +02001702 /*
1703 * Leave integrated clock source and reference clock enabled for pipe B.
1704 * The latter is needed for VGA hotplug / manual detection.
1705 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001706 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001707 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001708 I915_WRITE(DPLL(pipe), val);
1709 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001710
1711}
1712
1713static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1714{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001715 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001716 u32 val;
1717
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001718 /* Make sure the pipe isn't still relying on us */
1719 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001720
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001721 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001722 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001723 if (pipe != PIPE_A)
1724 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1725 I915_WRITE(DPLL(pipe), val);
1726 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001727
1728 mutex_lock(&dev_priv->dpio_lock);
1729
1730 /* Disable 10bit clock to display controller */
1731 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1732 val &= ~DPIO_DCLKP_EN;
1733 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1734
Ville Syrjälä61407f62014-05-27 16:32:55 +03001735 /* disable left/right clock distribution */
1736 if (pipe != PIPE_B) {
1737 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1738 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1739 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1740 } else {
1741 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1742 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1743 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1744 }
1745
Ville Syrjäläd7520482014-04-09 13:28:59 +03001746 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001747}
1748
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001749void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1750 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001751{
1752 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001753 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001754
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001755 switch (dport->port) {
1756 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001757 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001758 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001759 break;
1760 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001761 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001762 dpll_reg = DPLL(0);
1763 break;
1764 case PORT_D:
1765 port_mask = DPLL_PORTD_READY_MASK;
1766 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001767 break;
1768 default:
1769 BUG();
1770 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001771
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001772 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
Jesse Barnes89b667f2013-04-18 14:51:36 -07001773 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001774 port_name(dport->port), I915_READ(dpll_reg));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001775}
1776
Daniel Vetterb14b1052014-04-24 23:55:13 +02001777static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1778{
1779 struct drm_device *dev = crtc->base.dev;
1780 struct drm_i915_private *dev_priv = dev->dev_private;
1781 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1782
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001783 if (WARN_ON(pll == NULL))
1784 return;
1785
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001786 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001787 if (pll->active == 0) {
1788 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1789 WARN_ON(pll->on);
1790 assert_shared_dpll_disabled(dev_priv, pll);
1791
1792 pll->mode_set(dev_priv, pll);
1793 }
1794}
1795
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001796/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001797 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001798 * @dev_priv: i915 private structure
1799 * @pipe: pipe PLL to enable
1800 *
1801 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1802 * drives the transcoder clock.
1803 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001804static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001805{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001806 struct drm_device *dev = crtc->base.dev;
1807 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001808 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001809
Daniel Vetter87a875b2013-06-05 13:34:19 +02001810 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001811 return;
1812
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001813 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001814 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001815
Damien Lespiau74dd6922014-07-29 18:06:17 +01001816 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001817 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001818 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001819
Daniel Vettercdbd2312013-06-05 13:34:03 +02001820 if (pll->active++) {
1821 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001822 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001823 return;
1824 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001825 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001826
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001827 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1828
Daniel Vetter46edb022013-06-05 13:34:12 +02001829 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001830 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001831 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001832}
1833
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001834static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001835{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001836 struct drm_device *dev = crtc->base.dev;
1837 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001838 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001839
Jesse Barnes92f25842011-01-04 15:09:34 -08001840 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001841 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001842 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001843 return;
1844
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001845 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001846 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001847
Daniel Vetter46edb022013-06-05 13:34:12 +02001848 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1849 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001850 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001851
Chris Wilson48da64a2012-05-13 20:16:12 +01001852 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001853 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001854 return;
1855 }
1856
Daniel Vettere9d69442013-06-05 13:34:15 +02001857 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001858 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001859 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001860 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001861
Daniel Vetter46edb022013-06-05 13:34:12 +02001862 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001863 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001864 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001865
1866 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001867}
1868
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001869static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1870 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001871{
Daniel Vetter23670b322012-11-01 09:15:30 +01001872 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001873 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001874 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001875 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001876
1877 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001878 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001879
1880 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001881 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001882 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001883
1884 /* FDI must be feeding us bits for PCH ports */
1885 assert_fdi_tx_enabled(dev_priv, pipe);
1886 assert_fdi_rx_enabled(dev_priv, pipe);
1887
Daniel Vetter23670b322012-11-01 09:15:30 +01001888 if (HAS_PCH_CPT(dev)) {
1889 /* Workaround: Set the timing override bit before enabling the
1890 * pch transcoder. */
1891 reg = TRANS_CHICKEN2(pipe);
1892 val = I915_READ(reg);
1893 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1894 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001895 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001896
Daniel Vetterab9412b2013-05-03 11:49:46 +02001897 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001898 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001899 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001900
1901 if (HAS_PCH_IBX(dev_priv->dev)) {
1902 /*
1903 * make the BPC in transcoder be consistent with
1904 * that in pipeconf reg.
1905 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001906 val &= ~PIPECONF_BPC_MASK;
1907 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001908 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001909
1910 val &= ~TRANS_INTERLACE_MASK;
1911 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001912 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001913 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001914 val |= TRANS_LEGACY_INTERLACED_ILK;
1915 else
1916 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001917 else
1918 val |= TRANS_PROGRESSIVE;
1919
Jesse Barnes040484a2011-01-03 12:14:26 -08001920 I915_WRITE(reg, val | TRANS_ENABLE);
1921 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001922 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001923}
1924
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001925static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001926 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001927{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001928 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001929
1930 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001931 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001932
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001933 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001934 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001935 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001936
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001937 /* Workaround: set timing override bit. */
1938 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001939 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001940 I915_WRITE(_TRANSA_CHICKEN2, val);
1941
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001942 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001943 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001944
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001945 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1946 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001947 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001948 else
1949 val |= TRANS_PROGRESSIVE;
1950
Daniel Vetterab9412b2013-05-03 11:49:46 +02001951 I915_WRITE(LPT_TRANSCONF, val);
1952 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001953 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001954}
1955
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001956static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1957 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001958{
Daniel Vetter23670b322012-11-01 09:15:30 +01001959 struct drm_device *dev = dev_priv->dev;
1960 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001961
1962 /* FDI relies on the transcoder */
1963 assert_fdi_tx_disabled(dev_priv, pipe);
1964 assert_fdi_rx_disabled(dev_priv, pipe);
1965
Jesse Barnes291906f2011-02-02 12:28:03 -08001966 /* Ports must be off as well */
1967 assert_pch_ports_disabled(dev_priv, pipe);
1968
Daniel Vetterab9412b2013-05-03 11:49:46 +02001969 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001970 val = I915_READ(reg);
1971 val &= ~TRANS_ENABLE;
1972 I915_WRITE(reg, val);
1973 /* wait for PCH transcoder off, transcoder state */
1974 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001975 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001976
1977 if (!HAS_PCH_IBX(dev)) {
1978 /* Workaround: Clear the timing override chicken bit again. */
1979 reg = TRANS_CHICKEN2(pipe);
1980 val = I915_READ(reg);
1981 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1982 I915_WRITE(reg, val);
1983 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001984}
1985
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001986static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001987{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001988 u32 val;
1989
Daniel Vetterab9412b2013-05-03 11:49:46 +02001990 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001991 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001992 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001993 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001994 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001995 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001996
1997 /* Workaround: clear timing override bit. */
1998 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001999 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002000 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002001}
2002
2003/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002004 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002005 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002006 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002007 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002008 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002009 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002010static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002011{
Paulo Zanoni03722642014-01-17 13:51:09 -02002012 struct drm_device *dev = crtc->base.dev;
2013 struct drm_i915_private *dev_priv = dev->dev_private;
2014 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002015 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2016 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002017 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002018 int reg;
2019 u32 val;
2020
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002021 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002022 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002023 assert_sprites_disabled(dev_priv, pipe);
2024
Paulo Zanoni681e5812012-12-06 11:12:38 -02002025 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002026 pch_transcoder = TRANSCODER_A;
2027 else
2028 pch_transcoder = pipe;
2029
Jesse Barnesb24e7172011-01-04 15:09:30 -08002030 /*
2031 * A pipe without a PLL won't actually be able to drive bits from
2032 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2033 * need the check.
2034 */
2035 if (!HAS_PCH_SPLIT(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002036 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002037 assert_dsi_pll_enabled(dev_priv);
2038 else
2039 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002040 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002041 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002042 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002043 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002044 assert_fdi_tx_pll_enabled(dev_priv,
2045 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002046 }
2047 /* FIXME: assert CPU port conditions for SNB+ */
2048 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002049
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002050 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002051 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002052 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002053 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2054 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002055 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002056 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002057
2058 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002059 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002060}
2061
2062/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002063 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002064 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002065 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002066 * Disable the pipe of @crtc, making sure that various hardware
2067 * specific requirements are met, if applicable, e.g. plane
2068 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002069 *
2070 * Will wait until the pipe has shut down before returning.
2071 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002072static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002073{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002074 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002075 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002076 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002077 int reg;
2078 u32 val;
2079
2080 /*
2081 * Make sure planes won't keep trying to pump pixels to us,
2082 * or we might hang the display.
2083 */
2084 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002085 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002086 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002087
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002088 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002089 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002090 if ((val & PIPECONF_ENABLE) == 0)
2091 return;
2092
Ville Syrjälä67adc642014-08-15 01:21:57 +03002093 /*
2094 * Double wide has implications for planes
2095 * so best keep it disabled when not needed.
2096 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002097 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002098 val &= ~PIPECONF_DOUBLE_WIDE;
2099
2100 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002101 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2102 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002103 val &= ~PIPECONF_ENABLE;
2104
2105 I915_WRITE(reg, val);
2106 if ((val & PIPECONF_ENABLE) == 0)
2107 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002108}
2109
Keith Packardd74362c2011-07-28 14:47:14 -07002110/*
2111 * Plane regs are double buffered, going from enabled->disabled needs a
2112 * trigger in order to latch. The display address reg provides this.
2113 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002114void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2115 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002116{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002117 struct drm_device *dev = dev_priv->dev;
2118 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002119
2120 I915_WRITE(reg, I915_READ(reg));
2121 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002122}
2123
Jesse Barnesb24e7172011-01-04 15:09:30 -08002124/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002125 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002126 * @plane: plane to be enabled
2127 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002128 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002129 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002130 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002131static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2132 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002133{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002134 struct drm_device *dev = plane->dev;
2135 struct drm_i915_private *dev_priv = dev->dev_private;
2136 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002137
2138 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002139 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002140
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002141 if (intel_crtc->primary_enabled)
2142 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002143
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002144 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002145
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002146 dev_priv->display.update_primary_plane(crtc, plane->fb,
2147 crtc->x, crtc->y);
Ville Syrjälä33c3b0d2014-06-24 13:59:28 +03002148
2149 /*
2150 * BDW signals flip done immediately if the plane
2151 * is disabled, even if the plane enable is already
2152 * armed to occur at the next vblank :(
2153 */
2154 if (IS_BROADWELL(dev))
2155 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002156}
2157
Jesse Barnesb24e7172011-01-04 15:09:30 -08002158/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002159 * intel_disable_primary_hw_plane - disable the primary hardware plane
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002160 * @plane: plane to be disabled
2161 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002162 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002163 * Disable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002164 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002165static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2166 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002167{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002168 struct drm_device *dev = plane->dev;
2169 struct drm_i915_private *dev_priv = dev->dev_private;
2170 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2171
Matt Roper32b7eee2014-12-24 07:59:06 -08002172 if (WARN_ON(!intel_crtc->active))
2173 return;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002174
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002175 if (!intel_crtc->primary_enabled)
2176 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002177
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002178 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002179
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002180 dev_priv->display.update_primary_plane(crtc, plane->fb,
2181 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002182}
2183
Chris Wilson693db182013-03-05 14:52:39 +00002184static bool need_vtd_wa(struct drm_device *dev)
2185{
2186#ifdef CONFIG_INTEL_IOMMU
2187 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2188 return true;
2189#endif
2190 return false;
2191}
2192
Damien Lespiauec2c9812015-01-20 12:51:45 +00002193int
2194intel_fb_align_height(struct drm_device *dev, int height, unsigned int tiling)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002195{
2196 int tile_height;
2197
Damien Lespiauec2c9812015-01-20 12:51:45 +00002198 tile_height = tiling ? (IS_GEN2(dev) ? 16 : 8) : 1;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002199 return ALIGN(height, tile_height);
2200}
2201
Chris Wilson127bd2a2010-07-23 23:32:05 +01002202int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002203intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2204 struct drm_framebuffer *fb,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002205 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002206{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002207 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002208 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002209 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002210 u32 alignment;
2211 int ret;
2212
Matt Roperebcdd392014-07-09 16:22:11 -07002213 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2214
Chris Wilson05394f32010-11-08 19:18:58 +00002215 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002216 case I915_TILING_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002217 if (INTEL_INFO(dev)->gen >= 9)
2218 alignment = 256 * 1024;
2219 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002220 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002221 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002222 alignment = 4 * 1024;
2223 else
2224 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002225 break;
2226 case I915_TILING_X:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002227 if (INTEL_INFO(dev)->gen >= 9)
2228 alignment = 256 * 1024;
2229 else {
2230 /* pin() will align the object as required by fence */
2231 alignment = 0;
2232 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002233 break;
2234 case I915_TILING_Y:
Daniel Vetter80075d42013-10-09 21:23:52 +02002235 WARN(1, "Y tiled bo slipped through, driver bug!\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002236 return -EINVAL;
2237 default:
2238 BUG();
2239 }
2240
Chris Wilson693db182013-03-05 14:52:39 +00002241 /* Note that the w/a also requires 64 PTE of padding following the
2242 * bo. We currently fill all unused PTE with the shadow page and so
2243 * we should always have valid PTE following the scanout preventing
2244 * the VT-d warning.
2245 */
2246 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2247 alignment = 256 * 1024;
2248
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002249 /*
2250 * Global gtt pte registers are special registers which actually forward
2251 * writes to a chunk of system memory. Which means that there is no risk
2252 * that the register values disappear as soon as we call
2253 * intel_runtime_pm_put(), so it is correct to wrap only the
2254 * pin/unpin/fence and not more.
2255 */
2256 intel_runtime_pm_get(dev_priv);
2257
Chris Wilsonce453d82011-02-21 14:43:56 +00002258 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002259 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002260 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002261 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002262
2263 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2264 * fence, whereas 965+ only requires a fence if using
2265 * framebuffer compression. For simplicity, we always install
2266 * a fence as the cost is not that onerous.
2267 */
Chris Wilson06d98132012-04-17 15:31:24 +01002268 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002269 if (ret)
2270 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002271
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002272 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002273
Chris Wilsonce453d82011-02-21 14:43:56 +00002274 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002275 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002276 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002277
2278err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01002279 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002280err_interruptible:
2281 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002282 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002283 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002284}
2285
Chris Wilson1690e1e2011-12-14 13:57:08 +01002286void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2287{
Matt Roperebcdd392014-07-09 16:22:11 -07002288 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2289
Chris Wilson1690e1e2011-12-14 13:57:08 +01002290 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01002291 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002292}
2293
Daniel Vetterc2c75132012-07-05 12:17:30 +02002294/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2295 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002296unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2297 unsigned int tiling_mode,
2298 unsigned int cpp,
2299 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002300{
Chris Wilsonbc752862013-02-21 20:04:31 +00002301 if (tiling_mode != I915_TILING_NONE) {
2302 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002303
Chris Wilsonbc752862013-02-21 20:04:31 +00002304 tile_rows = *y / 8;
2305 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002306
Chris Wilsonbc752862013-02-21 20:04:31 +00002307 tiles = *x / (512/cpp);
2308 *x %= 512/cpp;
2309
2310 return tile_rows * pitch * 8 + tiles * 4096;
2311 } else {
2312 unsigned int offset;
2313
2314 offset = *y * pitch + *x * cpp;
2315 *y = 0;
2316 *x = (offset & 4095) / cpp;
2317 return offset & -4096;
2318 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002319}
2320
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002321static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002322{
2323 switch (format) {
2324 case DISPPLANE_8BPP:
2325 return DRM_FORMAT_C8;
2326 case DISPPLANE_BGRX555:
2327 return DRM_FORMAT_XRGB1555;
2328 case DISPPLANE_BGRX565:
2329 return DRM_FORMAT_RGB565;
2330 default:
2331 case DISPPLANE_BGRX888:
2332 return DRM_FORMAT_XRGB8888;
2333 case DISPPLANE_RGBX888:
2334 return DRM_FORMAT_XBGR8888;
2335 case DISPPLANE_BGRX101010:
2336 return DRM_FORMAT_XRGB2101010;
2337 case DISPPLANE_RGBX101010:
2338 return DRM_FORMAT_XBGR2101010;
2339 }
2340}
2341
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002342static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2343{
2344 switch (format) {
2345 case PLANE_CTL_FORMAT_RGB_565:
2346 return DRM_FORMAT_RGB565;
2347 default:
2348 case PLANE_CTL_FORMAT_XRGB_8888:
2349 if (rgb_order) {
2350 if (alpha)
2351 return DRM_FORMAT_ABGR8888;
2352 else
2353 return DRM_FORMAT_XBGR8888;
2354 } else {
2355 if (alpha)
2356 return DRM_FORMAT_ARGB8888;
2357 else
2358 return DRM_FORMAT_XRGB8888;
2359 }
2360 case PLANE_CTL_FORMAT_XRGB_2101010:
2361 if (rgb_order)
2362 return DRM_FORMAT_XBGR2101010;
2363 else
2364 return DRM_FORMAT_XRGB2101010;
2365 }
2366}
2367
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002368static bool
2369intel_alloc_plane_obj(struct intel_crtc *crtc,
2370 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002371{
2372 struct drm_device *dev = crtc->base.dev;
2373 struct drm_i915_gem_object *obj = NULL;
2374 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002375 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2376 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2377 PAGE_SIZE);
2378
2379 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002380
Chris Wilsonff2652e2014-03-10 08:07:02 +00002381 if (plane_config->size == 0)
2382 return false;
2383
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002384 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2385 base_aligned,
2386 base_aligned,
2387 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002388 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002389 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002390
Damien Lespiau49af4492015-01-20 12:51:44 +00002391 obj->tiling_mode = plane_config->tiling;
2392 if (obj->tiling_mode == I915_TILING_X)
Dave Airlie66e514c2014-04-03 07:51:54 +10002393 obj->stride = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002394
Dave Airlie66e514c2014-04-03 07:51:54 +10002395 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2396 mode_cmd.width = crtc->base.primary->fb->width;
2397 mode_cmd.height = crtc->base.primary->fb->height;
2398 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002399
2400 mutex_lock(&dev->struct_mutex);
2401
Dave Airlie66e514c2014-04-03 07:51:54 +10002402 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002403 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002404 DRM_DEBUG_KMS("intel fb init failed\n");
2405 goto out_unref_obj;
2406 }
2407
Daniel Vettera071fa02014-06-18 23:28:09 +02002408 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002409 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002410
2411 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2412 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002413
2414out_unref_obj:
2415 drm_gem_object_unreference(&obj->base);
2416 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002417 return false;
2418}
2419
Xi Ruoyao319c1d42015-03-12 20:16:32 +08002420/* Update plane->state->fb to match plane->fb after driver-internal updates */
2421static void
2422update_state_fb(struct drm_plane *plane)
2423{
2424 if (plane->fb != plane->state->fb)
2425 drm_atomic_set_fb_for_plane(plane->state, plane->fb);
2426}
2427
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002428static void
2429intel_find_plane_obj(struct intel_crtc *intel_crtc,
2430 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002431{
2432 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002433 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002434 struct drm_crtc *c;
2435 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002436 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002437
Dave Airlie66e514c2014-04-03 07:51:54 +10002438 if (!intel_crtc->base.primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002439 return;
2440
Damien Lespiau59a58cb2015-02-05 18:30:20 +00002441 if (intel_alloc_plane_obj(intel_crtc, plane_config)) {
Damien Lespiau3164a802015-02-05 19:24:25 +00002442 struct drm_plane *primary = intel_crtc->base.primary;
2443
2444 primary->state->crtc = &intel_crtc->base;
Daniel Vetter5f407752015-03-25 18:30:38 +01002445 primary->crtc = &intel_crtc->base;
Damien Lespiau3164a802015-02-05 19:24:25 +00002446 update_state_fb(primary);
2447
Jesse Barnes484b41d2014-03-07 08:57:55 -08002448 return;
Damien Lespiau59a58cb2015-02-05 18:30:20 +00002449 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002450
Dave Airlie66e514c2014-04-03 07:51:54 +10002451 kfree(intel_crtc->base.primary->fb);
2452 intel_crtc->base.primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002453
2454 /*
2455 * Failed to alloc the obj, check to see if we should share
2456 * an fb with another CRTC instead
2457 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002458 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002459 i = to_intel_crtc(c);
2460
2461 if (c == &intel_crtc->base)
2462 continue;
2463
Matt Roper2ff8fde2014-07-08 07:50:07 -07002464 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002465 continue;
2466
Matt Roper2ff8fde2014-07-08 07:50:07 -07002467 obj = intel_fb_obj(c->primary->fb);
2468 if (obj == NULL)
2469 continue;
2470
2471 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Damien Lespiau3164a802015-02-05 19:24:25 +00002472 struct drm_plane *primary = intel_crtc->base.primary;
2473
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002474 if (obj->tiling_mode != I915_TILING_NONE)
2475 dev_priv->preserve_bios_swizzle = true;
2476
Dave Airlie66e514c2014-04-03 07:51:54 +10002477 drm_framebuffer_reference(c->primary->fb);
Damien Lespiau3164a802015-02-05 19:24:25 +00002478 primary->fb = c->primary->fb;
2479 primary->state->crtc = &intel_crtc->base;
Daniel Vetter5f407752015-03-25 18:30:38 +01002480 primary->crtc = &intel_crtc->base;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002481 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002482 break;
2483 }
2484 }
Xi Ruoyao319c1d42015-03-12 20:16:32 +08002485
2486 update_state_fb(intel_crtc->base.primary);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002487}
2488
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002489static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2490 struct drm_framebuffer *fb,
2491 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002492{
2493 struct drm_device *dev = crtc->dev;
2494 struct drm_i915_private *dev_priv = dev->dev_private;
2495 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002496 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002497 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002498 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002499 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002500 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302501 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002502
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002503 if (!intel_crtc->primary_enabled) {
2504 I915_WRITE(reg, 0);
2505 if (INTEL_INFO(dev)->gen >= 4)
2506 I915_WRITE(DSPSURF(plane), 0);
2507 else
2508 I915_WRITE(DSPADDR(plane), 0);
2509 POSTING_READ(reg);
2510 return;
2511 }
2512
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002513 obj = intel_fb_obj(fb);
2514 if (WARN_ON(obj == NULL))
2515 return;
2516
2517 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2518
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002519 dspcntr = DISPPLANE_GAMMA_ENABLE;
2520
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002521 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002522
2523 if (INTEL_INFO(dev)->gen < 4) {
2524 if (intel_crtc->pipe == PIPE_B)
2525 dspcntr |= DISPPLANE_SEL_PIPE_B;
2526
2527 /* pipesrc and dspsize control the size that is scaled from,
2528 * which should always be the user's requested size.
2529 */
2530 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002531 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2532 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002533 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002534 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2535 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002536 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2537 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002538 I915_WRITE(PRIMPOS(plane), 0);
2539 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002540 }
2541
Ville Syrjälä57779d02012-10-31 17:50:14 +02002542 switch (fb->pixel_format) {
2543 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002544 dspcntr |= DISPPLANE_8BPP;
2545 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002546 case DRM_FORMAT_XRGB1555:
2547 case DRM_FORMAT_ARGB1555:
2548 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002549 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002550 case DRM_FORMAT_RGB565:
2551 dspcntr |= DISPPLANE_BGRX565;
2552 break;
2553 case DRM_FORMAT_XRGB8888:
2554 case DRM_FORMAT_ARGB8888:
2555 dspcntr |= DISPPLANE_BGRX888;
2556 break;
2557 case DRM_FORMAT_XBGR8888:
2558 case DRM_FORMAT_ABGR8888:
2559 dspcntr |= DISPPLANE_RGBX888;
2560 break;
2561 case DRM_FORMAT_XRGB2101010:
2562 case DRM_FORMAT_ARGB2101010:
2563 dspcntr |= DISPPLANE_BGRX101010;
2564 break;
2565 case DRM_FORMAT_XBGR2101010:
2566 case DRM_FORMAT_ABGR2101010:
2567 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002568 break;
2569 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002570 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002571 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002572
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002573 if (INTEL_INFO(dev)->gen >= 4 &&
2574 obj->tiling_mode != I915_TILING_NONE)
2575 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002576
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002577 if (IS_G4X(dev))
2578 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2579
Ville Syrjäläb98971272014-08-27 16:51:22 +03002580 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002581
Daniel Vetterc2c75132012-07-05 12:17:30 +02002582 if (INTEL_INFO(dev)->gen >= 4) {
2583 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002584 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002585 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002586 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002587 linear_offset -= intel_crtc->dspaddr_offset;
2588 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002589 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002590 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002591
Matt Roper8e7d6882015-01-21 16:35:41 -08002592 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302593 dspcntr |= DISPPLANE_ROTATE_180;
2594
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002595 x += (intel_crtc->config->pipe_src_w - 1);
2596 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302597
2598 /* Finding the last pixel of the last line of the display
2599 data and adding to linear_offset*/
2600 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002601 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2602 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302603 }
2604
2605 I915_WRITE(reg, dspcntr);
2606
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002607 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2608 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2609 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002610 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002611 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002612 I915_WRITE(DSPSURF(plane),
2613 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002614 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002615 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002616 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002617 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002618 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002619}
2620
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002621static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2622 struct drm_framebuffer *fb,
2623 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002624{
2625 struct drm_device *dev = crtc->dev;
2626 struct drm_i915_private *dev_priv = dev->dev_private;
2627 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002628 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002629 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002630 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002631 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002632 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302633 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002634
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002635 if (!intel_crtc->primary_enabled) {
2636 I915_WRITE(reg, 0);
2637 I915_WRITE(DSPSURF(plane), 0);
2638 POSTING_READ(reg);
2639 return;
2640 }
2641
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002642 obj = intel_fb_obj(fb);
2643 if (WARN_ON(obj == NULL))
2644 return;
2645
2646 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2647
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002648 dspcntr = DISPPLANE_GAMMA_ENABLE;
2649
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002650 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002651
2652 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2653 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2654
Ville Syrjälä57779d02012-10-31 17:50:14 +02002655 switch (fb->pixel_format) {
2656 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002657 dspcntr |= DISPPLANE_8BPP;
2658 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002659 case DRM_FORMAT_RGB565:
2660 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002661 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002662 case DRM_FORMAT_XRGB8888:
2663 case DRM_FORMAT_ARGB8888:
2664 dspcntr |= DISPPLANE_BGRX888;
2665 break;
2666 case DRM_FORMAT_XBGR8888:
2667 case DRM_FORMAT_ABGR8888:
2668 dspcntr |= DISPPLANE_RGBX888;
2669 break;
2670 case DRM_FORMAT_XRGB2101010:
2671 case DRM_FORMAT_ARGB2101010:
2672 dspcntr |= DISPPLANE_BGRX101010;
2673 break;
2674 case DRM_FORMAT_XBGR2101010:
2675 case DRM_FORMAT_ABGR2101010:
2676 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002677 break;
2678 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002679 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002680 }
2681
2682 if (obj->tiling_mode != I915_TILING_NONE)
2683 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002684
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002685 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002686 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002687
Ville Syrjäläb98971272014-08-27 16:51:22 +03002688 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002689 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002690 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002691 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002692 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002693 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002694 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302695 dspcntr |= DISPPLANE_ROTATE_180;
2696
2697 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002698 x += (intel_crtc->config->pipe_src_w - 1);
2699 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302700
2701 /* Finding the last pixel of the last line of the display
2702 data and adding to linear_offset*/
2703 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002704 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2705 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302706 }
2707 }
2708
2709 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002710
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002711 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2712 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2713 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002714 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002715 I915_WRITE(DSPSURF(plane),
2716 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002717 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002718 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2719 } else {
2720 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2721 I915_WRITE(DSPLINOFF(plane), linear_offset);
2722 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002723 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002724}
2725
Damien Lespiau70d21f02013-07-03 21:06:04 +01002726static void skylake_update_primary_plane(struct drm_crtc *crtc,
2727 struct drm_framebuffer *fb,
2728 int x, int y)
2729{
2730 struct drm_device *dev = crtc->dev;
2731 struct drm_i915_private *dev_priv = dev->dev_private;
2732 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2733 struct intel_framebuffer *intel_fb;
2734 struct drm_i915_gem_object *obj;
2735 int pipe = intel_crtc->pipe;
2736 u32 plane_ctl, stride;
2737
2738 if (!intel_crtc->primary_enabled) {
2739 I915_WRITE(PLANE_CTL(pipe, 0), 0);
2740 I915_WRITE(PLANE_SURF(pipe, 0), 0);
2741 POSTING_READ(PLANE_CTL(pipe, 0));
2742 return;
2743 }
2744
2745 plane_ctl = PLANE_CTL_ENABLE |
2746 PLANE_CTL_PIPE_GAMMA_ENABLE |
2747 PLANE_CTL_PIPE_CSC_ENABLE;
2748
2749 switch (fb->pixel_format) {
2750 case DRM_FORMAT_RGB565:
2751 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
2752 break;
2753 case DRM_FORMAT_XRGB8888:
2754 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2755 break;
Jani Nikulaf75fb422015-02-10 13:15:49 +02002756 case DRM_FORMAT_ARGB8888:
2757 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2758 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2759 break;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002760 case DRM_FORMAT_XBGR8888:
2761 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2762 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2763 break;
Jani Nikulaf75fb422015-02-10 13:15:49 +02002764 case DRM_FORMAT_ABGR8888:
2765 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2766 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2767 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2768 break;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002769 case DRM_FORMAT_XRGB2101010:
2770 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2771 break;
2772 case DRM_FORMAT_XBGR2101010:
2773 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2774 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2775 break;
2776 default:
2777 BUG();
2778 }
2779
2780 intel_fb = to_intel_framebuffer(fb);
2781 obj = intel_fb->obj;
2782
2783 /*
2784 * The stride is either expressed as a multiple of 64 bytes chunks for
2785 * linear buffers or in number of tiles for tiled buffers.
2786 */
2787 switch (obj->tiling_mode) {
2788 case I915_TILING_NONE:
2789 stride = fb->pitches[0] >> 6;
2790 break;
2791 case I915_TILING_X:
2792 plane_ctl |= PLANE_CTL_TILED_X;
2793 stride = fb->pitches[0] >> 9;
2794 break;
2795 default:
2796 BUG();
2797 }
2798
2799 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Matt Roper8e7d6882015-01-21 16:35:41 -08002800 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180))
Sonika Jindal1447dde2014-10-04 10:53:31 +01002801 plane_ctl |= PLANE_CTL_ROTATE_180;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002802
2803 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
2804
2805 DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
2806 i915_gem_obj_ggtt_offset(obj),
2807 x, y, fb->width, fb->height,
2808 fb->pitches[0]);
2809
2810 I915_WRITE(PLANE_POS(pipe, 0), 0);
2811 I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
2812 I915_WRITE(PLANE_SIZE(pipe, 0),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002813 (intel_crtc->config->pipe_src_h - 1) << 16 |
2814 (intel_crtc->config->pipe_src_w - 1));
Damien Lespiau70d21f02013-07-03 21:06:04 +01002815 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
2816 I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
2817
2818 POSTING_READ(PLANE_SURF(pipe, 0));
2819}
2820
Jesse Barnes17638cd2011-06-24 12:19:23 -07002821/* Assume fb object is pinned & idle & fenced and just update base pointers */
2822static int
2823intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2824 int x, int y, enum mode_set_atomic state)
2825{
2826 struct drm_device *dev = crtc->dev;
2827 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002828
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002829 if (dev_priv->display.disable_fbc)
2830 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07002831
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002832 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2833
2834 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07002835}
2836
Ville Syrjälä75147472014-11-24 18:28:11 +02002837static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02002838{
Ville Syrjälä96a02912013-02-18 19:08:49 +02002839 struct drm_crtc *crtc;
2840
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002841 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002842 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2843 enum plane plane = intel_crtc->plane;
2844
2845 intel_prepare_page_flip(dev, plane);
2846 intel_finish_page_flip_plane(dev, plane);
2847 }
Ville Syrjälä75147472014-11-24 18:28:11 +02002848}
2849
2850static void intel_update_primary_planes(struct drm_device *dev)
2851{
2852 struct drm_i915_private *dev_priv = dev->dev_private;
2853 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02002854
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002855 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002856 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2857
Rob Clark51fd3712013-11-19 12:10:12 -05002858 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002859 /*
2860 * FIXME: Once we have proper support for primary planes (and
2861 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10002862 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002863 */
Matt Roperf4510a22014-04-01 15:22:40 -07002864 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07002865 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10002866 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07002867 crtc->x,
2868 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05002869 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02002870 }
2871}
2872
Ville Syrjälä75147472014-11-24 18:28:11 +02002873void intel_prepare_reset(struct drm_device *dev)
2874{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02002875 struct drm_i915_private *dev_priv = to_i915(dev);
2876 struct intel_crtc *crtc;
2877
Ville Syrjälä75147472014-11-24 18:28:11 +02002878 /* no reset support for gen2 */
2879 if (IS_GEN2(dev))
2880 return;
2881
2882 /* reset doesn't touch the display */
2883 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
2884 return;
2885
2886 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02002887
2888 /*
2889 * Disabling the crtcs gracefully seems nicer. Also the
2890 * g33 docs say we should at least disable all the planes.
2891 */
2892 for_each_intel_crtc(dev, crtc) {
2893 if (crtc->active)
2894 dev_priv->display.crtc_disable(&crtc->base);
2895 }
Ville Syrjälä75147472014-11-24 18:28:11 +02002896}
2897
2898void intel_finish_reset(struct drm_device *dev)
2899{
2900 struct drm_i915_private *dev_priv = to_i915(dev);
2901
2902 /*
2903 * Flips in the rings will be nuked by the reset,
2904 * so complete all pending flips so that user space
2905 * will get its events and not get stuck.
2906 */
2907 intel_complete_page_flips(dev);
2908
2909 /* no reset support for gen2 */
2910 if (IS_GEN2(dev))
2911 return;
2912
2913 /* reset doesn't touch the display */
2914 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
2915 /*
2916 * Flips in the rings have been nuked by the reset,
2917 * so update the base address of all primary
2918 * planes to the the last fb to make sure we're
2919 * showing the correct fb after a reset.
2920 */
2921 intel_update_primary_planes(dev);
2922 return;
2923 }
2924
2925 /*
2926 * The display has been reset as well,
2927 * so need a full re-initialization.
2928 */
2929 intel_runtime_pm_disable_interrupts(dev_priv);
2930 intel_runtime_pm_enable_interrupts(dev_priv);
2931
2932 intel_modeset_init_hw(dev);
2933
2934 spin_lock_irq(&dev_priv->irq_lock);
2935 if (dev_priv->display.hpd_irq_setup)
2936 dev_priv->display.hpd_irq_setup(dev);
2937 spin_unlock_irq(&dev_priv->irq_lock);
2938
2939 intel_modeset_setup_hw_state(dev, true);
2940
2941 intel_hpd_init(dev_priv);
2942
2943 drm_modeset_unlock_all(dev);
2944}
2945
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002946static int
Chris Wilson14667a42012-04-03 17:58:35 +01002947intel_finish_fb(struct drm_framebuffer *old_fb)
2948{
Matt Roper2ff8fde2014-07-08 07:50:07 -07002949 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01002950 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2951 bool was_interruptible = dev_priv->mm.interruptible;
2952 int ret;
2953
Chris Wilson14667a42012-04-03 17:58:35 +01002954 /* Big Hammer, we also need to ensure that any pending
2955 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2956 * current scanout is retired before unpinning the old
2957 * framebuffer.
2958 *
2959 * This should only fail upon a hung GPU, in which case we
2960 * can safely continue.
2961 */
2962 dev_priv->mm.interruptible = false;
2963 ret = i915_gem_object_finish_gpu(obj);
2964 dev_priv->mm.interruptible = was_interruptible;
2965
2966 return ret;
2967}
2968
Chris Wilson7d5e3792014-03-04 13:15:08 +00002969static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2970{
2971 struct drm_device *dev = crtc->dev;
2972 struct drm_i915_private *dev_priv = dev->dev_private;
2973 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00002974 bool pending;
2975
2976 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2977 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2978 return false;
2979
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02002980 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00002981 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02002982 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00002983
2984 return pending;
2985}
2986
Gustavo Padovane30e8f72014-09-10 12:04:17 -03002987static void intel_update_pipe_size(struct intel_crtc *crtc)
2988{
2989 struct drm_device *dev = crtc->base.dev;
2990 struct drm_i915_private *dev_priv = dev->dev_private;
2991 const struct drm_display_mode *adjusted_mode;
2992
2993 if (!i915.fastboot)
2994 return;
2995
2996 /*
2997 * Update pipe size and adjust fitter if needed: the reason for this is
2998 * that in compute_mode_changes we check the native mode (not the pfit
2999 * mode) to see if we can flip rather than do a full mode set. In the
3000 * fastboot case, we'll flip, but if we don't update the pipesrc and
3001 * pfit state, we'll end up with a big fb scanned out into the wrong
3002 * sized surface.
3003 *
3004 * To fix this properly, we need to hoist the checks up into
3005 * compute_mode_changes (or above), check the actual pfit state and
3006 * whether the platform allows pfit disable with pipe active, and only
3007 * then update the pipesrc and pfit state, even on the flip path.
3008 */
3009
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003010 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003011
3012 I915_WRITE(PIPESRC(crtc->pipe),
3013 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3014 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003015 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003016 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3017 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003018 I915_WRITE(PF_CTL(crtc->pipe), 0);
3019 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3020 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3021 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003022 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3023 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003024}
3025
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003026static void intel_fdi_normal_train(struct drm_crtc *crtc)
3027{
3028 struct drm_device *dev = crtc->dev;
3029 struct drm_i915_private *dev_priv = dev->dev_private;
3030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3031 int pipe = intel_crtc->pipe;
3032 u32 reg, temp;
3033
3034 /* enable normal train */
3035 reg = FDI_TX_CTL(pipe);
3036 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003037 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003038 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3039 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003040 } else {
3041 temp &= ~FDI_LINK_TRAIN_NONE;
3042 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003043 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003044 I915_WRITE(reg, temp);
3045
3046 reg = FDI_RX_CTL(pipe);
3047 temp = I915_READ(reg);
3048 if (HAS_PCH_CPT(dev)) {
3049 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3050 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3051 } else {
3052 temp &= ~FDI_LINK_TRAIN_NONE;
3053 temp |= FDI_LINK_TRAIN_NONE;
3054 }
3055 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3056
3057 /* wait one idle pattern time */
3058 POSTING_READ(reg);
3059 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003060
3061 /* IVB wants error correction enabled */
3062 if (IS_IVYBRIDGE(dev))
3063 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3064 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003065}
3066
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003067static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01003068{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003069 return crtc->base.enabled && crtc->active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003070 crtc->config->has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01003071}
3072
Daniel Vetter01a415f2012-10-27 15:58:40 +02003073static void ivb_modeset_global_resources(struct drm_device *dev)
3074{
3075 struct drm_i915_private *dev_priv = dev->dev_private;
3076 struct intel_crtc *pipe_B_crtc =
3077 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
3078 struct intel_crtc *pipe_C_crtc =
3079 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
3080 uint32_t temp;
3081
Daniel Vetter1e833f42013-02-19 22:31:57 +01003082 /*
3083 * When everything is off disable fdi C so that we could enable fdi B
3084 * with all lanes. Note that we don't care about enabled pipes without
3085 * an enabled pch encoder.
3086 */
3087 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
3088 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02003089 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3090 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3091
3092 temp = I915_READ(SOUTH_CHICKEN1);
3093 temp &= ~FDI_BC_BIFURCATION_SELECT;
3094 DRM_DEBUG_KMS("disabling fdi C rx\n");
3095 I915_WRITE(SOUTH_CHICKEN1, temp);
3096 }
3097}
3098
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003099/* The FDI link training functions for ILK/Ibexpeak. */
3100static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3101{
3102 struct drm_device *dev = crtc->dev;
3103 struct drm_i915_private *dev_priv = dev->dev_private;
3104 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3105 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003106 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003107
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003108 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003109 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003110
Adam Jacksone1a44742010-06-25 15:32:14 -04003111 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3112 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003113 reg = FDI_RX_IMR(pipe);
3114 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003115 temp &= ~FDI_RX_SYMBOL_LOCK;
3116 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003117 I915_WRITE(reg, temp);
3118 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003119 udelay(150);
3120
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003121 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003122 reg = FDI_TX_CTL(pipe);
3123 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003124 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003125 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003126 temp &= ~FDI_LINK_TRAIN_NONE;
3127 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003128 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003129
Chris Wilson5eddb702010-09-11 13:48:45 +01003130 reg = FDI_RX_CTL(pipe);
3131 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003132 temp &= ~FDI_LINK_TRAIN_NONE;
3133 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003134 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3135
3136 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003137 udelay(150);
3138
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003139 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003140 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3141 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3142 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003143
Chris Wilson5eddb702010-09-11 13:48:45 +01003144 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003145 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003146 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003147 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3148
3149 if ((temp & FDI_RX_BIT_LOCK)) {
3150 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003151 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003152 break;
3153 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003154 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003155 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003156 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003157
3158 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003159 reg = FDI_TX_CTL(pipe);
3160 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003161 temp &= ~FDI_LINK_TRAIN_NONE;
3162 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003163 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003164
Chris Wilson5eddb702010-09-11 13:48:45 +01003165 reg = FDI_RX_CTL(pipe);
3166 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003167 temp &= ~FDI_LINK_TRAIN_NONE;
3168 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003169 I915_WRITE(reg, temp);
3170
3171 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003172 udelay(150);
3173
Chris Wilson5eddb702010-09-11 13:48:45 +01003174 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003175 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003176 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003177 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3178
3179 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003180 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003181 DRM_DEBUG_KMS("FDI train 2 done.\n");
3182 break;
3183 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003184 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003185 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003186 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003187
3188 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003189
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003190}
3191
Akshay Joshi0206e352011-08-16 15:34:10 -04003192static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003193 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3194 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3195 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3196 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3197};
3198
3199/* The FDI link training functions for SNB/Cougarpoint. */
3200static void gen6_fdi_link_train(struct drm_crtc *crtc)
3201{
3202 struct drm_device *dev = crtc->dev;
3203 struct drm_i915_private *dev_priv = dev->dev_private;
3204 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3205 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003206 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003207
Adam Jacksone1a44742010-06-25 15:32:14 -04003208 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3209 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003210 reg = FDI_RX_IMR(pipe);
3211 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003212 temp &= ~FDI_RX_SYMBOL_LOCK;
3213 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003214 I915_WRITE(reg, temp);
3215
3216 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003217 udelay(150);
3218
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003219 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003220 reg = FDI_TX_CTL(pipe);
3221 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003222 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003223 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003224 temp &= ~FDI_LINK_TRAIN_NONE;
3225 temp |= FDI_LINK_TRAIN_PATTERN_1;
3226 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3227 /* SNB-B */
3228 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003229 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003230
Daniel Vetterd74cf322012-10-26 10:58:13 +02003231 I915_WRITE(FDI_RX_MISC(pipe),
3232 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3233
Chris Wilson5eddb702010-09-11 13:48:45 +01003234 reg = FDI_RX_CTL(pipe);
3235 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003236 if (HAS_PCH_CPT(dev)) {
3237 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3238 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3239 } else {
3240 temp &= ~FDI_LINK_TRAIN_NONE;
3241 temp |= FDI_LINK_TRAIN_PATTERN_1;
3242 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003243 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3244
3245 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003246 udelay(150);
3247
Akshay Joshi0206e352011-08-16 15:34:10 -04003248 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003249 reg = FDI_TX_CTL(pipe);
3250 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003251 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3252 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003253 I915_WRITE(reg, temp);
3254
3255 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003256 udelay(500);
3257
Sean Paulfa37d392012-03-02 12:53:39 -05003258 for (retry = 0; retry < 5; retry++) {
3259 reg = FDI_RX_IIR(pipe);
3260 temp = I915_READ(reg);
3261 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3262 if (temp & FDI_RX_BIT_LOCK) {
3263 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3264 DRM_DEBUG_KMS("FDI train 1 done.\n");
3265 break;
3266 }
3267 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003268 }
Sean Paulfa37d392012-03-02 12:53:39 -05003269 if (retry < 5)
3270 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003271 }
3272 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003273 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003274
3275 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003276 reg = FDI_TX_CTL(pipe);
3277 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003278 temp &= ~FDI_LINK_TRAIN_NONE;
3279 temp |= FDI_LINK_TRAIN_PATTERN_2;
3280 if (IS_GEN6(dev)) {
3281 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3282 /* SNB-B */
3283 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3284 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003285 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003286
Chris Wilson5eddb702010-09-11 13:48:45 +01003287 reg = FDI_RX_CTL(pipe);
3288 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003289 if (HAS_PCH_CPT(dev)) {
3290 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3291 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3292 } else {
3293 temp &= ~FDI_LINK_TRAIN_NONE;
3294 temp |= FDI_LINK_TRAIN_PATTERN_2;
3295 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003296 I915_WRITE(reg, temp);
3297
3298 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003299 udelay(150);
3300
Akshay Joshi0206e352011-08-16 15:34:10 -04003301 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003302 reg = FDI_TX_CTL(pipe);
3303 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003304 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3305 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003306 I915_WRITE(reg, temp);
3307
3308 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003309 udelay(500);
3310
Sean Paulfa37d392012-03-02 12:53:39 -05003311 for (retry = 0; retry < 5; retry++) {
3312 reg = FDI_RX_IIR(pipe);
3313 temp = I915_READ(reg);
3314 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3315 if (temp & FDI_RX_SYMBOL_LOCK) {
3316 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3317 DRM_DEBUG_KMS("FDI train 2 done.\n");
3318 break;
3319 }
3320 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003321 }
Sean Paulfa37d392012-03-02 12:53:39 -05003322 if (retry < 5)
3323 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003324 }
3325 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003326 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003327
3328 DRM_DEBUG_KMS("FDI train done.\n");
3329}
3330
Jesse Barnes357555c2011-04-28 15:09:55 -07003331/* Manual link training for Ivy Bridge A0 parts */
3332static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3333{
3334 struct drm_device *dev = crtc->dev;
3335 struct drm_i915_private *dev_priv = dev->dev_private;
3336 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3337 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003338 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003339
3340 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3341 for train result */
3342 reg = FDI_RX_IMR(pipe);
3343 temp = I915_READ(reg);
3344 temp &= ~FDI_RX_SYMBOL_LOCK;
3345 temp &= ~FDI_RX_BIT_LOCK;
3346 I915_WRITE(reg, temp);
3347
3348 POSTING_READ(reg);
3349 udelay(150);
3350
Daniel Vetter01a415f2012-10-27 15:58:40 +02003351 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3352 I915_READ(FDI_RX_IIR(pipe)));
3353
Jesse Barnes139ccd32013-08-19 11:04:55 -07003354 /* Try each vswing and preemphasis setting twice before moving on */
3355 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3356 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003357 reg = FDI_TX_CTL(pipe);
3358 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003359 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3360 temp &= ~FDI_TX_ENABLE;
3361 I915_WRITE(reg, temp);
3362
3363 reg = FDI_RX_CTL(pipe);
3364 temp = I915_READ(reg);
3365 temp &= ~FDI_LINK_TRAIN_AUTO;
3366 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3367 temp &= ~FDI_RX_ENABLE;
3368 I915_WRITE(reg, temp);
3369
3370 /* enable CPU FDI TX and PCH FDI RX */
3371 reg = FDI_TX_CTL(pipe);
3372 temp = I915_READ(reg);
3373 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003374 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003375 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003376 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003377 temp |= snb_b_fdi_train_param[j/2];
3378 temp |= FDI_COMPOSITE_SYNC;
3379 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3380
3381 I915_WRITE(FDI_RX_MISC(pipe),
3382 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3383
3384 reg = FDI_RX_CTL(pipe);
3385 temp = I915_READ(reg);
3386 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3387 temp |= FDI_COMPOSITE_SYNC;
3388 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3389
3390 POSTING_READ(reg);
3391 udelay(1); /* should be 0.5us */
3392
3393 for (i = 0; i < 4; i++) {
3394 reg = FDI_RX_IIR(pipe);
3395 temp = I915_READ(reg);
3396 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3397
3398 if (temp & FDI_RX_BIT_LOCK ||
3399 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3400 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3401 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3402 i);
3403 break;
3404 }
3405 udelay(1); /* should be 0.5us */
3406 }
3407 if (i == 4) {
3408 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3409 continue;
3410 }
3411
3412 /* Train 2 */
3413 reg = FDI_TX_CTL(pipe);
3414 temp = I915_READ(reg);
3415 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3416 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3417 I915_WRITE(reg, temp);
3418
3419 reg = FDI_RX_CTL(pipe);
3420 temp = I915_READ(reg);
3421 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3422 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003423 I915_WRITE(reg, temp);
3424
3425 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003426 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003427
Jesse Barnes139ccd32013-08-19 11:04:55 -07003428 for (i = 0; i < 4; i++) {
3429 reg = FDI_RX_IIR(pipe);
3430 temp = I915_READ(reg);
3431 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003432
Jesse Barnes139ccd32013-08-19 11:04:55 -07003433 if (temp & FDI_RX_SYMBOL_LOCK ||
3434 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3435 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3436 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3437 i);
3438 goto train_done;
3439 }
3440 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003441 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003442 if (i == 4)
3443 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003444 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003445
Jesse Barnes139ccd32013-08-19 11:04:55 -07003446train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003447 DRM_DEBUG_KMS("FDI train done.\n");
3448}
3449
Daniel Vetter88cefb62012-08-12 19:27:14 +02003450static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003451{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003452 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003453 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003454 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003455 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003456
Jesse Barnesc64e3112010-09-10 11:27:03 -07003457
Jesse Barnes0e23b992010-09-10 11:10:00 -07003458 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003459 reg = FDI_RX_CTL(pipe);
3460 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003461 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003462 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003463 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003464 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3465
3466 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003467 udelay(200);
3468
3469 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003470 temp = I915_READ(reg);
3471 I915_WRITE(reg, temp | FDI_PCDCLK);
3472
3473 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003474 udelay(200);
3475
Paulo Zanoni20749732012-11-23 15:30:38 -02003476 /* Enable CPU FDI TX PLL, always on for Ironlake */
3477 reg = FDI_TX_CTL(pipe);
3478 temp = I915_READ(reg);
3479 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3480 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003481
Paulo Zanoni20749732012-11-23 15:30:38 -02003482 POSTING_READ(reg);
3483 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003484 }
3485}
3486
Daniel Vetter88cefb62012-08-12 19:27:14 +02003487static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3488{
3489 struct drm_device *dev = intel_crtc->base.dev;
3490 struct drm_i915_private *dev_priv = dev->dev_private;
3491 int pipe = intel_crtc->pipe;
3492 u32 reg, temp;
3493
3494 /* Switch from PCDclk to Rawclk */
3495 reg = FDI_RX_CTL(pipe);
3496 temp = I915_READ(reg);
3497 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3498
3499 /* Disable CPU FDI TX PLL */
3500 reg = FDI_TX_CTL(pipe);
3501 temp = I915_READ(reg);
3502 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3503
3504 POSTING_READ(reg);
3505 udelay(100);
3506
3507 reg = FDI_RX_CTL(pipe);
3508 temp = I915_READ(reg);
3509 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3510
3511 /* Wait for the clocks to turn off. */
3512 POSTING_READ(reg);
3513 udelay(100);
3514}
3515
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003516static void ironlake_fdi_disable(struct drm_crtc *crtc)
3517{
3518 struct drm_device *dev = crtc->dev;
3519 struct drm_i915_private *dev_priv = dev->dev_private;
3520 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3521 int pipe = intel_crtc->pipe;
3522 u32 reg, temp;
3523
3524 /* disable CPU FDI tx and PCH FDI rx */
3525 reg = FDI_TX_CTL(pipe);
3526 temp = I915_READ(reg);
3527 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3528 POSTING_READ(reg);
3529
3530 reg = FDI_RX_CTL(pipe);
3531 temp = I915_READ(reg);
3532 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003533 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003534 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3535
3536 POSTING_READ(reg);
3537 udelay(100);
3538
3539 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003540 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003541 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003542
3543 /* still set train pattern 1 */
3544 reg = FDI_TX_CTL(pipe);
3545 temp = I915_READ(reg);
3546 temp &= ~FDI_LINK_TRAIN_NONE;
3547 temp |= FDI_LINK_TRAIN_PATTERN_1;
3548 I915_WRITE(reg, temp);
3549
3550 reg = FDI_RX_CTL(pipe);
3551 temp = I915_READ(reg);
3552 if (HAS_PCH_CPT(dev)) {
3553 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3554 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3555 } else {
3556 temp &= ~FDI_LINK_TRAIN_NONE;
3557 temp |= FDI_LINK_TRAIN_PATTERN_1;
3558 }
3559 /* BPC in FDI rx is consistent with that in PIPECONF */
3560 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003561 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003562 I915_WRITE(reg, temp);
3563
3564 POSTING_READ(reg);
3565 udelay(100);
3566}
3567
Chris Wilson5dce5b932014-01-20 10:17:36 +00003568bool intel_has_pending_fb_unpin(struct drm_device *dev)
3569{
3570 struct intel_crtc *crtc;
3571
3572 /* Note that we don't need to be called with mode_config.lock here
3573 * as our list of CRTC objects is static for the lifetime of the
3574 * device and so cannot disappear as we iterate. Similarly, we can
3575 * happily treat the predicates as racy, atomic checks as userspace
3576 * cannot claim and pin a new fb without at least acquring the
3577 * struct_mutex and so serialising with us.
3578 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003579 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003580 if (atomic_read(&crtc->unpin_work_count) == 0)
3581 continue;
3582
3583 if (crtc->unpin_work)
3584 intel_wait_for_vblank(dev, crtc->pipe);
3585
3586 return true;
3587 }
3588
3589 return false;
3590}
3591
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003592static void page_flip_completed(struct intel_crtc *intel_crtc)
3593{
3594 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3595 struct intel_unpin_work *work = intel_crtc->unpin_work;
3596
3597 /* ensure that the unpin work is consistent wrt ->pending. */
3598 smp_rmb();
3599 intel_crtc->unpin_work = NULL;
3600
3601 if (work->event)
3602 drm_send_vblank_event(intel_crtc->base.dev,
3603 intel_crtc->pipe,
3604 work->event);
3605
3606 drm_crtc_vblank_put(&intel_crtc->base);
3607
3608 wake_up_all(&dev_priv->pending_flip_queue);
3609 queue_work(dev_priv->wq, &work->work);
3610
3611 trace_i915_flip_complete(intel_crtc->plane,
3612 work->pending_flip_obj);
3613}
3614
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003615void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003616{
Chris Wilson0f911282012-04-17 10:05:38 +01003617 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003618 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003619
Daniel Vetter2c10d572012-12-20 21:24:07 +01003620 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003621 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3622 !intel_crtc_has_pending_flip(crtc),
3623 60*HZ) == 0)) {
3624 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003625
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003626 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003627 if (intel_crtc->unpin_work) {
3628 WARN_ONCE(1, "Removing stuck page flip\n");
3629 page_flip_completed(intel_crtc);
3630 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003631 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003632 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003633
Chris Wilson975d5682014-08-20 13:13:34 +01003634 if (crtc->primary->fb) {
3635 mutex_lock(&dev->struct_mutex);
3636 intel_finish_fb(crtc->primary->fb);
3637 mutex_unlock(&dev->struct_mutex);
3638 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003639}
3640
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003641/* Program iCLKIP clock to the desired frequency */
3642static void lpt_program_iclkip(struct drm_crtc *crtc)
3643{
3644 struct drm_device *dev = crtc->dev;
3645 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003646 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003647 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3648 u32 temp;
3649
Daniel Vetter09153002012-12-12 14:06:44 +01003650 mutex_lock(&dev_priv->dpio_lock);
3651
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003652 /* It is necessary to ungate the pixclk gate prior to programming
3653 * the divisors, and gate it back when it is done.
3654 */
3655 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3656
3657 /* Disable SSCCTL */
3658 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003659 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3660 SBI_SSCCTL_DISABLE,
3661 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003662
3663 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003664 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003665 auxdiv = 1;
3666 divsel = 0x41;
3667 phaseinc = 0x20;
3668 } else {
3669 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003670 * but the adjusted_mode->crtc_clock in in KHz. To get the
3671 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003672 * convert the virtual clock precision to KHz here for higher
3673 * precision.
3674 */
3675 u32 iclk_virtual_root_freq = 172800 * 1000;
3676 u32 iclk_pi_range = 64;
3677 u32 desired_divisor, msb_divisor_value, pi_value;
3678
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003679 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003680 msb_divisor_value = desired_divisor / iclk_pi_range;
3681 pi_value = desired_divisor % iclk_pi_range;
3682
3683 auxdiv = 0;
3684 divsel = msb_divisor_value - 2;
3685 phaseinc = pi_value;
3686 }
3687
3688 /* This should not happen with any sane values */
3689 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3690 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3691 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3692 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3693
3694 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 +03003695 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003696 auxdiv,
3697 divsel,
3698 phasedir,
3699 phaseinc);
3700
3701 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003702 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003703 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3704 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3705 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3706 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3707 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3708 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003709 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003710
3711 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003712 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003713 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3714 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003715 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003716
3717 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003718 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003719 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003720 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003721
3722 /* Wait for initialization time */
3723 udelay(24);
3724
3725 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003726
3727 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003728}
3729
Daniel Vetter275f01b22013-05-03 11:49:47 +02003730static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3731 enum pipe pch_transcoder)
3732{
3733 struct drm_device *dev = crtc->base.dev;
3734 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003735 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003736
3737 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3738 I915_READ(HTOTAL(cpu_transcoder)));
3739 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3740 I915_READ(HBLANK(cpu_transcoder)));
3741 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3742 I915_READ(HSYNC(cpu_transcoder)));
3743
3744 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3745 I915_READ(VTOTAL(cpu_transcoder)));
3746 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3747 I915_READ(VBLANK(cpu_transcoder)));
3748 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3749 I915_READ(VSYNC(cpu_transcoder)));
3750 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3751 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3752}
3753
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003754static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3755{
3756 struct drm_i915_private *dev_priv = dev->dev_private;
3757 uint32_t temp;
3758
3759 temp = I915_READ(SOUTH_CHICKEN1);
3760 if (temp & FDI_BC_BIFURCATION_SELECT)
3761 return;
3762
3763 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3764 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3765
3766 temp |= FDI_BC_BIFURCATION_SELECT;
3767 DRM_DEBUG_KMS("enabling fdi C rx\n");
3768 I915_WRITE(SOUTH_CHICKEN1, temp);
3769 POSTING_READ(SOUTH_CHICKEN1);
3770}
3771
3772static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3773{
3774 struct drm_device *dev = intel_crtc->base.dev;
3775 struct drm_i915_private *dev_priv = dev->dev_private;
3776
3777 switch (intel_crtc->pipe) {
3778 case PIPE_A:
3779 break;
3780 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003781 if (intel_crtc->config->fdi_lanes > 2)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003782 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3783 else
3784 cpt_enable_fdi_bc_bifurcation(dev);
3785
3786 break;
3787 case PIPE_C:
3788 cpt_enable_fdi_bc_bifurcation(dev);
3789
3790 break;
3791 default:
3792 BUG();
3793 }
3794}
3795
Jesse Barnesf67a5592011-01-05 10:31:48 -08003796/*
3797 * Enable PCH resources required for PCH ports:
3798 * - PCH PLLs
3799 * - FDI training & RX/TX
3800 * - update transcoder timings
3801 * - DP transcoding bits
3802 * - transcoder
3803 */
3804static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003805{
3806 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003807 struct drm_i915_private *dev_priv = dev->dev_private;
3808 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3809 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003810 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003811
Daniel Vetterab9412b2013-05-03 11:49:46 +02003812 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003813
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003814 if (IS_IVYBRIDGE(dev))
3815 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3816
Daniel Vettercd986ab2012-10-26 10:58:12 +02003817 /* Write the TU size bits before fdi link training, so that error
3818 * detection works. */
3819 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3820 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3821
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003822 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003823 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003824
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003825 /* We need to program the right clock selection before writing the pixel
3826 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003827 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003828 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003829
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003830 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003831 temp |= TRANS_DPLL_ENABLE(pipe);
3832 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003833 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003834 temp |= sel;
3835 else
3836 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003837 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003838 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003839
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003840 /* XXX: pch pll's can be enabled any time before we enable the PCH
3841 * transcoder, and we actually should do this to not upset any PCH
3842 * transcoder that already use the clock when we share it.
3843 *
3844 * Note that enable_shared_dpll tries to do the right thing, but
3845 * get_shared_dpll unconditionally resets the pll - we need that to have
3846 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02003847 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003848
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003849 /* set transcoder timing, panel must allow it */
3850 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003851 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003852
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003853 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003854
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003855 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003856 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003857 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003858 reg = TRANS_DP_CTL(pipe);
3859 temp = I915_READ(reg);
3860 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003861 TRANS_DP_SYNC_MASK |
3862 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003863 temp |= (TRANS_DP_OUTPUT_ENABLE |
3864 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003865 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003866
3867 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003868 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003869 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003870 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003871
3872 switch (intel_trans_dp_port_sel(crtc)) {
3873 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003874 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003875 break;
3876 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003877 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003878 break;
3879 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003880 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003881 break;
3882 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003883 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003884 }
3885
Chris Wilson5eddb702010-09-11 13:48:45 +01003886 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003887 }
3888
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003889 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003890}
3891
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003892static void lpt_pch_enable(struct drm_crtc *crtc)
3893{
3894 struct drm_device *dev = crtc->dev;
3895 struct drm_i915_private *dev_priv = dev->dev_private;
3896 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003897 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003898
Daniel Vetterab9412b2013-05-03 11:49:46 +02003899 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003900
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003901 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003902
Paulo Zanoni0540e482012-10-31 18:12:40 -02003903 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003904 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003905
Paulo Zanoni937bb612012-10-31 18:12:47 -02003906 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003907}
3908
Daniel Vetter716c2e52014-06-25 22:02:02 +03003909void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003910{
Daniel Vettere2b78262013-06-07 23:10:03 +02003911 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003912
3913 if (pll == NULL)
3914 return;
3915
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02003916 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02003917 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003918 return;
3919 }
3920
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02003921 pll->config.crtc_mask &= ~(1 << crtc->pipe);
3922 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003923 WARN_ON(pll->on);
3924 WARN_ON(pll->active);
3925 }
3926
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003927 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003928}
3929
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02003930struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
3931 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003932{
Daniel Vettere2b78262013-06-07 23:10:03 +02003933 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02003934 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02003935 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003936
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003937 if (HAS_PCH_IBX(dev_priv->dev)) {
3938 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003939 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003940 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003941
Daniel Vetter46edb022013-06-05 13:34:12 +02003942 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3943 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003944
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02003945 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003946
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003947 goto found;
3948 }
3949
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003950 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3951 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003952
3953 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02003954 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003955 continue;
3956
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02003957 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02003958 &pll->new_config->hw_state,
3959 sizeof(pll->new_config->hw_state)) == 0) {
3960 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02003961 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02003962 pll->new_config->crtc_mask,
3963 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003964 goto found;
3965 }
3966 }
3967
3968 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003969 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3970 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02003971 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003972 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3973 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003974 goto found;
3975 }
3976 }
3977
3978 return NULL;
3979
3980found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02003981 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02003982 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003983
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02003984 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003985 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3986 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003987
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02003988 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003989
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003990 return pll;
3991}
3992
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02003993/**
3994 * intel_shared_dpll_start_config - start a new PLL staged config
3995 * @dev_priv: DRM device
3996 * @clear_pipes: mask of pipes that will have their PLLs freed
3997 *
3998 * Starts a new PLL staged config, copying the current config but
3999 * releasing the references of pipes specified in clear_pipes.
4000 */
4001static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4002 unsigned clear_pipes)
4003{
4004 struct intel_shared_dpll *pll;
4005 enum intel_dpll_id i;
4006
4007 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4008 pll = &dev_priv->shared_dplls[i];
4009
4010 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4011 GFP_KERNEL);
4012 if (!pll->new_config)
4013 goto cleanup;
4014
4015 pll->new_config->crtc_mask &= ~clear_pipes;
4016 }
4017
4018 return 0;
4019
4020cleanup:
4021 while (--i >= 0) {
4022 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004023 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004024 pll->new_config = NULL;
4025 }
4026
4027 return -ENOMEM;
4028}
4029
4030static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4031{
4032 struct intel_shared_dpll *pll;
4033 enum intel_dpll_id i;
4034
4035 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4036 pll = &dev_priv->shared_dplls[i];
4037
4038 WARN_ON(pll->new_config == &pll->config);
4039
4040 pll->config = *pll->new_config;
4041 kfree(pll->new_config);
4042 pll->new_config = NULL;
4043 }
4044}
4045
4046static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4047{
4048 struct intel_shared_dpll *pll;
4049 enum intel_dpll_id i;
4050
4051 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4052 pll = &dev_priv->shared_dplls[i];
4053
4054 WARN_ON(pll->new_config == &pll->config);
4055
4056 kfree(pll->new_config);
4057 pll->new_config = NULL;
4058 }
4059}
4060
Daniel Vettera1520312013-05-03 11:49:50 +02004061static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004062{
4063 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004064 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004065 u32 temp;
4066
4067 temp = I915_READ(dslreg);
4068 udelay(500);
4069 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004070 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004071 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004072 }
4073}
4074
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004075static void skylake_pfit_enable(struct intel_crtc *crtc)
4076{
4077 struct drm_device *dev = crtc->base.dev;
4078 struct drm_i915_private *dev_priv = dev->dev_private;
4079 int pipe = crtc->pipe;
4080
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004081 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004082 I915_WRITE(PS_CTL(pipe), PS_ENABLE);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004083 I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4084 I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004085 }
4086}
4087
Jesse Barnesb074cec2013-04-25 12:55:02 -07004088static void ironlake_pfit_enable(struct intel_crtc *crtc)
4089{
4090 struct drm_device *dev = crtc->base.dev;
4091 struct drm_i915_private *dev_priv = dev->dev_private;
4092 int pipe = crtc->pipe;
4093
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004094 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004095 /* Force use of hard-coded filter coefficients
4096 * as some pre-programmed values are broken,
4097 * e.g. x201.
4098 */
4099 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4100 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4101 PF_PIPE_SEL_IVB(pipe));
4102 else
4103 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004104 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4105 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004106 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004107}
4108
Matt Roper4a3b8762014-12-23 10:41:51 -08004109static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004110{
4111 struct drm_device *dev = crtc->dev;
4112 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004113 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004114 struct intel_plane *intel_plane;
4115
Matt Roperaf2b6532014-04-01 15:22:32 -07004116 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4117 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004118 if (intel_plane->pipe == pipe)
4119 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004120 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004121}
4122
Matt Roper4a3b8762014-12-23 10:41:51 -08004123static void intel_disable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004124{
4125 struct drm_device *dev = crtc->dev;
4126 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004127 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004128 struct intel_plane *intel_plane;
4129
Matt Roperaf2b6532014-04-01 15:22:32 -07004130 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4131 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004132 if (intel_plane->pipe == pipe)
Matt Ropercf4c7c12014-12-04 10:27:42 -08004133 plane->funcs->disable_plane(plane);
Matt Roperaf2b6532014-04-01 15:22:32 -07004134 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004135}
4136
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004137void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004138{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004139 struct drm_device *dev = crtc->base.dev;
4140 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004141
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004142 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004143 return;
4144
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004145 /* We can only enable IPS after we enable a plane and wait for a vblank */
4146 intel_wait_for_vblank(dev, crtc->pipe);
4147
Paulo Zanonid77e4532013-09-24 13:52:55 -03004148 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004149 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004150 mutex_lock(&dev_priv->rps.hw_lock);
4151 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4152 mutex_unlock(&dev_priv->rps.hw_lock);
4153 /* Quoting Art Runyan: "its not safe to expect any particular
4154 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004155 * mailbox." Moreover, the mailbox may return a bogus state,
4156 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004157 */
4158 } else {
4159 I915_WRITE(IPS_CTL, IPS_ENABLE);
4160 /* The bit only becomes 1 in the next vblank, so this wait here
4161 * is essentially intel_wait_for_vblank. If we don't have this
4162 * and don't wait for vblanks until the end of crtc_enable, then
4163 * the HW state readout code will complain that the expected
4164 * IPS_CTL value is not the one we read. */
4165 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4166 DRM_ERROR("Timed out waiting for IPS enable\n");
4167 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004168}
4169
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004170void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004171{
4172 struct drm_device *dev = crtc->base.dev;
4173 struct drm_i915_private *dev_priv = dev->dev_private;
4174
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004175 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004176 return;
4177
4178 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004179 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004180 mutex_lock(&dev_priv->rps.hw_lock);
4181 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4182 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004183 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4184 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4185 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004186 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004187 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004188 POSTING_READ(IPS_CTL);
4189 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004190
4191 /* We need to wait for a vblank before we can disable the plane. */
4192 intel_wait_for_vblank(dev, crtc->pipe);
4193}
4194
4195/** Loads the palette/gamma unit for the CRTC with the prepared values */
4196static void intel_crtc_load_lut(struct drm_crtc *crtc)
4197{
4198 struct drm_device *dev = crtc->dev;
4199 struct drm_i915_private *dev_priv = dev->dev_private;
4200 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4201 enum pipe pipe = intel_crtc->pipe;
4202 int palreg = PALETTE(pipe);
4203 int i;
4204 bool reenable_ips = false;
4205
4206 /* The clocks have to be on to load the palette. */
4207 if (!crtc->enabled || !intel_crtc->active)
4208 return;
4209
4210 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004211 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004212 assert_dsi_pll_enabled(dev_priv);
4213 else
4214 assert_pll_enabled(dev_priv, pipe);
4215 }
4216
4217 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304218 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004219 palreg = LGC_PALETTE(pipe);
4220
4221 /* Workaround : Do not read or write the pipe palette/gamma data while
4222 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4223 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004224 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004225 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4226 GAMMA_MODE_MODE_SPLIT)) {
4227 hsw_disable_ips(intel_crtc);
4228 reenable_ips = true;
4229 }
4230
4231 for (i = 0; i < 256; i++) {
4232 I915_WRITE(palreg + 4 * i,
4233 (intel_crtc->lut_r[i] << 16) |
4234 (intel_crtc->lut_g[i] << 8) |
4235 intel_crtc->lut_b[i]);
4236 }
4237
4238 if (reenable_ips)
4239 hsw_enable_ips(intel_crtc);
4240}
4241
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004242static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4243{
4244 if (!enable && intel_crtc->overlay) {
4245 struct drm_device *dev = intel_crtc->base.dev;
4246 struct drm_i915_private *dev_priv = dev->dev_private;
4247
4248 mutex_lock(&dev->struct_mutex);
4249 dev_priv->mm.interruptible = false;
4250 (void) intel_overlay_switch_off(intel_crtc->overlay);
4251 dev_priv->mm.interruptible = true;
4252 mutex_unlock(&dev->struct_mutex);
4253 }
4254
4255 /* Let userspace switch the overlay on again. In most cases userspace
4256 * has to recompute where to put it anyway.
4257 */
4258}
4259
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004260static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004261{
4262 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004263 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4264 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004265
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004266 intel_enable_primary_hw_plane(crtc->primary, crtc);
Matt Roper4a3b8762014-12-23 10:41:51 -08004267 intel_enable_sprite_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004268 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004269 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004270
4271 hsw_enable_ips(intel_crtc);
4272
4273 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004274 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004275 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004276
4277 /*
4278 * FIXME: Once we grow proper nuclear flip support out of this we need
4279 * to compute the mask of flip planes precisely. For the time being
4280 * consider this a flip from a NULL plane.
4281 */
4282 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004283}
4284
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004285static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004286{
4287 struct drm_device *dev = crtc->dev;
4288 struct drm_i915_private *dev_priv = dev->dev_private;
4289 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4290 int pipe = intel_crtc->pipe;
4291 int plane = intel_crtc->plane;
4292
4293 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004294
4295 if (dev_priv->fbc.plane == plane)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004296 intel_fbc_disable(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004297
4298 hsw_disable_ips(intel_crtc);
4299
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004300 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004301 intel_crtc_update_cursor(crtc, false);
Matt Roper4a3b8762014-12-23 10:41:51 -08004302 intel_disable_sprite_planes(crtc);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004303 intel_disable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004304
Daniel Vetterf99d7062014-06-19 16:01:59 +02004305 /*
4306 * FIXME: Once we grow proper nuclear flip support out of this we need
4307 * to compute the mask of flip planes precisely. For the time being
4308 * consider this a flip to a NULL plane.
4309 */
4310 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004311}
4312
Jesse Barnesf67a5592011-01-05 10:31:48 -08004313static void ironlake_crtc_enable(struct drm_crtc *crtc)
4314{
4315 struct drm_device *dev = crtc->dev;
4316 struct drm_i915_private *dev_priv = dev->dev_private;
4317 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004318 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004319 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004320
Daniel Vetter08a48462012-07-02 11:43:47 +02004321 WARN_ON(!crtc->enabled);
4322
Jesse Barnesf67a5592011-01-05 10:31:48 -08004323 if (intel_crtc->active)
4324 return;
4325
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004326 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004327 intel_prepare_shared_dpll(intel_crtc);
4328
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004329 if (intel_crtc->config->has_dp_encoder)
Daniel Vetter29407aa2014-04-24 23:55:08 +02004330 intel_dp_set_m_n(intel_crtc);
4331
4332 intel_set_pipe_timings(intel_crtc);
4333
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004334 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004335 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004336 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004337 }
4338
4339 ironlake_set_pipeconf(crtc);
4340
Jesse Barnesf67a5592011-01-05 10:31:48 -08004341 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004342
Daniel Vettera72e4c92014-09-30 10:56:47 +02004343 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4344 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004345
Daniel Vetterf6736a12013-06-05 13:34:30 +02004346 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004347 if (encoder->pre_enable)
4348 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004349
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004350 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004351 /* Note: FDI PLL enabling _must_ be done before we enable the
4352 * cpu pipes, hence this is separate from all the other fdi/pch
4353 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004354 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004355 } else {
4356 assert_fdi_tx_disabled(dev_priv, pipe);
4357 assert_fdi_rx_disabled(dev_priv, pipe);
4358 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004359
Jesse Barnesb074cec2013-04-25 12:55:02 -07004360 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004361
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004362 /*
4363 * On ILK+ LUT must be loaded before the pipe is running but with
4364 * clocks enabled
4365 */
4366 intel_crtc_load_lut(crtc);
4367
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004368 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004369 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004370
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004371 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004372 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004373
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004374 assert_vblank_disabled(crtc);
4375 drm_crtc_vblank_on(crtc);
4376
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004377 for_each_encoder_on_crtc(dev, crtc, encoder)
4378 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004379
4380 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004381 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004382
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004383 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004384}
4385
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004386/* IPS only exists on ULT machines and is tied to pipe A. */
4387static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4388{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004389 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004390}
4391
Paulo Zanonie4916942013-09-20 16:21:19 -03004392/*
4393 * This implements the workaround described in the "notes" section of the mode
4394 * set sequence documentation. When going from no pipes or single pipe to
4395 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4396 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4397 */
4398static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4399{
4400 struct drm_device *dev = crtc->base.dev;
4401 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4402
4403 /* We want to get the other_active_crtc only if there's only 1 other
4404 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004405 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004406 if (!crtc_it->active || crtc_it == crtc)
4407 continue;
4408
4409 if (other_active_crtc)
4410 return;
4411
4412 other_active_crtc = crtc_it;
4413 }
4414 if (!other_active_crtc)
4415 return;
4416
4417 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4418 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4419}
4420
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004421static void haswell_crtc_enable(struct drm_crtc *crtc)
4422{
4423 struct drm_device *dev = crtc->dev;
4424 struct drm_i915_private *dev_priv = dev->dev_private;
4425 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4426 struct intel_encoder *encoder;
4427 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004428
4429 WARN_ON(!crtc->enabled);
4430
4431 if (intel_crtc->active)
4432 return;
4433
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004434 if (intel_crtc_to_shared_dpll(intel_crtc))
4435 intel_enable_shared_dpll(intel_crtc);
4436
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004437 if (intel_crtc->config->has_dp_encoder)
Daniel Vetter229fca92014-04-24 23:55:09 +02004438 intel_dp_set_m_n(intel_crtc);
4439
4440 intel_set_pipe_timings(intel_crtc);
4441
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004442 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4443 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4444 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004445 }
4446
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004447 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004448 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004449 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004450 }
4451
4452 haswell_set_pipeconf(crtc);
4453
4454 intel_set_pipe_csc(crtc);
4455
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004456 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004457
Daniel Vettera72e4c92014-09-30 10:56:47 +02004458 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004459 for_each_encoder_on_crtc(dev, crtc, encoder)
4460 if (encoder->pre_enable)
4461 encoder->pre_enable(encoder);
4462
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004463 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004464 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4465 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004466 dev_priv->display.fdi_link_train(crtc);
4467 }
4468
Paulo Zanoni1f544382012-10-24 11:32:00 -02004469 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004470
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004471 if (IS_SKYLAKE(dev))
4472 skylake_pfit_enable(intel_crtc);
4473 else
4474 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004475
4476 /*
4477 * On ILK+ LUT must be loaded before the pipe is running but with
4478 * clocks enabled
4479 */
4480 intel_crtc_load_lut(crtc);
4481
Paulo Zanoni1f544382012-10-24 11:32:00 -02004482 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004483 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004484
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004485 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004486 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004487
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004488 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004489 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004490
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004491 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004492 intel_ddi_set_vc_payload_alloc(crtc, true);
4493
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004494 assert_vblank_disabled(crtc);
4495 drm_crtc_vblank_on(crtc);
4496
Jani Nikula8807e552013-08-30 19:40:32 +03004497 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004498 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004499 intel_opregion_notify_encoder(encoder, true);
4500 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004501
Paulo Zanonie4916942013-09-20 16:21:19 -03004502 /* If we change the relative order between pipe/planes enabling, we need
4503 * to change the workaround. */
4504 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004505 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004506}
4507
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004508static void skylake_pfit_disable(struct intel_crtc *crtc)
4509{
4510 struct drm_device *dev = crtc->base.dev;
4511 struct drm_i915_private *dev_priv = dev->dev_private;
4512 int pipe = crtc->pipe;
4513
4514 /* To avoid upsetting the power well on haswell only disable the pfit if
4515 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004516 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004517 I915_WRITE(PS_CTL(pipe), 0);
4518 I915_WRITE(PS_WIN_POS(pipe), 0);
4519 I915_WRITE(PS_WIN_SZ(pipe), 0);
4520 }
4521}
4522
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004523static void ironlake_pfit_disable(struct intel_crtc *crtc)
4524{
4525 struct drm_device *dev = crtc->base.dev;
4526 struct drm_i915_private *dev_priv = dev->dev_private;
4527 int pipe = crtc->pipe;
4528
4529 /* To avoid upsetting the power well on haswell only disable the pfit if
4530 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004531 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004532 I915_WRITE(PF_CTL(pipe), 0);
4533 I915_WRITE(PF_WIN_POS(pipe), 0);
4534 I915_WRITE(PF_WIN_SZ(pipe), 0);
4535 }
4536}
4537
Jesse Barnes6be4a602010-09-10 10:26:01 -07004538static void ironlake_crtc_disable(struct drm_crtc *crtc)
4539{
4540 struct drm_device *dev = crtc->dev;
4541 struct drm_i915_private *dev_priv = dev->dev_private;
4542 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004543 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004544 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004545 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004546
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004547 if (!intel_crtc->active)
4548 return;
4549
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004550 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004551
Daniel Vetterea9d7582012-07-10 10:42:52 +02004552 for_each_encoder_on_crtc(dev, crtc, encoder)
4553 encoder->disable(encoder);
4554
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004555 drm_crtc_vblank_off(crtc);
4556 assert_vblank_disabled(crtc);
4557
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004558 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004559 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02004560
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004561 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004562
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004563 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004564
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004565 for_each_encoder_on_crtc(dev, crtc, encoder)
4566 if (encoder->post_disable)
4567 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004568
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004569 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004570 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004571
Daniel Vetterd925c592013-06-05 13:34:04 +02004572 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004573
Daniel Vetterd925c592013-06-05 13:34:04 +02004574 if (HAS_PCH_CPT(dev)) {
4575 /* disable TRANS_DP_CTL */
4576 reg = TRANS_DP_CTL(pipe);
4577 temp = I915_READ(reg);
4578 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4579 TRANS_DP_PORT_SEL_MASK);
4580 temp |= TRANS_DP_PORT_SEL_NONE;
4581 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004582
Daniel Vetterd925c592013-06-05 13:34:04 +02004583 /* disable DPLL_SEL */
4584 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004585 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004586 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004587 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004588
4589 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004590 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02004591
4592 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004593 }
4594
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004595 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004596 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004597
4598 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004599 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004600 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004601}
4602
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004603static void haswell_crtc_disable(struct drm_crtc *crtc)
4604{
4605 struct drm_device *dev = crtc->dev;
4606 struct drm_i915_private *dev_priv = dev->dev_private;
4607 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4608 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004609 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004610
4611 if (!intel_crtc->active)
4612 return;
4613
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004614 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03004615
Jani Nikula8807e552013-08-30 19:40:32 +03004616 for_each_encoder_on_crtc(dev, crtc, encoder) {
4617 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004618 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004619 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004620
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004621 drm_crtc_vblank_off(crtc);
4622 assert_vblank_disabled(crtc);
4623
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004624 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004625 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4626 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004627 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004628
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004629 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03004630 intel_ddi_set_vc_payload_alloc(crtc, false);
4631
Paulo Zanoniad80a812012-10-24 16:06:19 -02004632 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004633
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004634 if (IS_SKYLAKE(dev))
4635 skylake_pfit_disable(intel_crtc);
4636 else
4637 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004638
Paulo Zanoni1f544382012-10-24 11:32:00 -02004639 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004640
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004641 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02004642 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02004643 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02004644 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004645
Imre Deak97b040a2014-06-25 22:01:50 +03004646 for_each_encoder_on_crtc(dev, crtc, encoder)
4647 if (encoder->post_disable)
4648 encoder->post_disable(encoder);
4649
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004650 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004651 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004652
4653 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004654 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004655 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004656
4657 if (intel_crtc_to_shared_dpll(intel_crtc))
4658 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004659}
4660
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004661static void ironlake_crtc_off(struct drm_crtc *crtc)
4662{
4663 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004664 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004665}
4666
Paulo Zanoni6441ab52012-10-05 12:05:58 -03004667
Jesse Barnes2dd24552013-04-25 12:55:01 -07004668static void i9xx_pfit_enable(struct intel_crtc *crtc)
4669{
4670 struct drm_device *dev = crtc->base.dev;
4671 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004672 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07004673
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02004674 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07004675 return;
4676
Daniel Vetterc0b03412013-05-28 12:05:54 +02004677 /*
4678 * The panel fitter should only be adjusted whilst the pipe is disabled,
4679 * according to register description and PRM.
4680 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07004681 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4682 assert_pipe_disabled(dev_priv, crtc->pipe);
4683
Jesse Barnesb074cec2013-04-25 12:55:02 -07004684 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4685 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02004686
4687 /* Border color in case we don't scale up to the full screen. Black by
4688 * default, change to something else for debugging. */
4689 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07004690}
4691
Dave Airlied05410f2014-06-05 13:22:59 +10004692static enum intel_display_power_domain port_to_power_domain(enum port port)
4693{
4694 switch (port) {
4695 case PORT_A:
4696 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4697 case PORT_B:
4698 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4699 case PORT_C:
4700 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4701 case PORT_D:
4702 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4703 default:
4704 WARN_ON_ONCE(1);
4705 return POWER_DOMAIN_PORT_OTHER;
4706 }
4707}
4708
Imre Deak77d22dc2014-03-05 16:20:52 +02004709#define for_each_power_domain(domain, mask) \
4710 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4711 if ((1 << (domain)) & (mask))
4712
Imre Deak319be8a2014-03-04 19:22:57 +02004713enum intel_display_power_domain
4714intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02004715{
Imre Deak319be8a2014-03-04 19:22:57 +02004716 struct drm_device *dev = intel_encoder->base.dev;
4717 struct intel_digital_port *intel_dig_port;
4718
4719 switch (intel_encoder->type) {
4720 case INTEL_OUTPUT_UNKNOWN:
4721 /* Only DDI platforms should ever use this output type */
4722 WARN_ON_ONCE(!HAS_DDI(dev));
4723 case INTEL_OUTPUT_DISPLAYPORT:
4724 case INTEL_OUTPUT_HDMI:
4725 case INTEL_OUTPUT_EDP:
4726 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10004727 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10004728 case INTEL_OUTPUT_DP_MST:
4729 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4730 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02004731 case INTEL_OUTPUT_ANALOG:
4732 return POWER_DOMAIN_PORT_CRT;
4733 case INTEL_OUTPUT_DSI:
4734 return POWER_DOMAIN_PORT_DSI;
4735 default:
4736 return POWER_DOMAIN_PORT_OTHER;
4737 }
4738}
4739
4740static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4741{
4742 struct drm_device *dev = crtc->dev;
4743 struct intel_encoder *intel_encoder;
4744 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4745 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02004746 unsigned long mask;
4747 enum transcoder transcoder;
4748
4749 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4750
4751 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4752 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004753 if (intel_crtc->config->pch_pfit.enabled ||
4754 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02004755 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4756
Imre Deak319be8a2014-03-04 19:22:57 +02004757 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4758 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4759
Imre Deak77d22dc2014-03-05 16:20:52 +02004760 return mask;
4761}
4762
Imre Deak77d22dc2014-03-05 16:20:52 +02004763static void modeset_update_crtc_power_domains(struct drm_device *dev)
4764{
4765 struct drm_i915_private *dev_priv = dev->dev_private;
4766 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4767 struct intel_crtc *crtc;
4768
4769 /*
4770 * First get all needed power domains, then put all unneeded, to avoid
4771 * any unnecessary toggling of the power wells.
4772 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004773 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004774 enum intel_display_power_domain domain;
4775
4776 if (!crtc->base.enabled)
4777 continue;
4778
Imre Deak319be8a2014-03-04 19:22:57 +02004779 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02004780
4781 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4782 intel_display_power_get(dev_priv, domain);
4783 }
4784
Ville Syrjälä50f6e502014-11-06 14:49:12 +02004785 if (dev_priv->display.modeset_global_resources)
4786 dev_priv->display.modeset_global_resources(dev);
4787
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004788 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004789 enum intel_display_power_domain domain;
4790
4791 for_each_power_domain(domain, crtc->enabled_power_domains)
4792 intel_display_power_put(dev_priv, domain);
4793
4794 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4795 }
4796
4797 intel_display_set_init_power(dev_priv, false);
4798}
4799
Ville Syrjälädfcab172014-06-13 13:37:47 +03004800/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004801static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004802{
Jesse Barnes586f49d2013-11-04 16:06:59 -08004803 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08004804
Jesse Barnes586f49d2013-11-04 16:06:59 -08004805 /* Obtain SKU information */
4806 mutex_lock(&dev_priv->dpio_lock);
4807 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4808 CCK_FUSE_HPLL_FREQ_MASK;
4809 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004810
Ville Syrjälädfcab172014-06-13 13:37:47 +03004811 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004812}
4813
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004814static void vlv_update_cdclk(struct drm_device *dev)
4815{
4816 struct drm_i915_private *dev_priv = dev->dev_private;
4817
4818 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03004819 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004820 dev_priv->vlv_cdclk_freq);
4821
4822 /*
4823 * Program the gmbus_freq based on the cdclk frequency.
4824 * BSpec erroneously claims we should aim for 4MHz, but
4825 * in fact 1MHz is the correct frequency.
4826 */
Ville Syrjälä6be1e3d2014-10-16 20:52:31 +03004827 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004828}
4829
Jesse Barnes30a970c2013-11-04 13:48:12 -08004830/* Adjust CDclk dividers to allow high res or save power if possible */
4831static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4832{
4833 struct drm_i915_private *dev_priv = dev->dev_private;
4834 u32 val, cmd;
4835
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03004836 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02004837
Ville Syrjälädfcab172014-06-13 13:37:47 +03004838 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08004839 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03004840 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004841 cmd = 1;
4842 else
4843 cmd = 0;
4844
4845 mutex_lock(&dev_priv->rps.hw_lock);
4846 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4847 val &= ~DSPFREQGUAR_MASK;
4848 val |= (cmd << DSPFREQGUAR_SHIFT);
4849 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4850 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4851 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4852 50)) {
4853 DRM_ERROR("timed out waiting for CDclk change\n");
4854 }
4855 mutex_unlock(&dev_priv->rps.hw_lock);
4856
Ville Syrjälädfcab172014-06-13 13:37:47 +03004857 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03004858 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004859
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03004860 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004861
4862 mutex_lock(&dev_priv->dpio_lock);
4863 /* adjust cdclk divider */
4864 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03004865 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004866 val |= divider;
4867 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03004868
4869 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
4870 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
4871 50))
4872 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08004873 mutex_unlock(&dev_priv->dpio_lock);
4874 }
4875
4876 mutex_lock(&dev_priv->dpio_lock);
4877 /* adjust self-refresh exit latency value */
4878 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4879 val &= ~0x7f;
4880
4881 /*
4882 * For high bandwidth configs, we set a higher latency in the bunit
4883 * so that the core display fetch happens in time to avoid underruns.
4884 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03004885 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004886 val |= 4500 / 250; /* 4.5 usec */
4887 else
4888 val |= 3000 / 250; /* 3.0 usec */
4889 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4890 mutex_unlock(&dev_priv->dpio_lock);
4891
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004892 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004893}
4894
Ville Syrjälä383c5a62014-06-28 02:03:57 +03004895static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
4896{
4897 struct drm_i915_private *dev_priv = dev->dev_private;
4898 u32 val, cmd;
4899
4900 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
4901
4902 switch (cdclk) {
4903 case 400000:
4904 cmd = 3;
4905 break;
4906 case 333333:
4907 case 320000:
4908 cmd = 2;
4909 break;
4910 case 266667:
4911 cmd = 1;
4912 break;
4913 case 200000:
4914 cmd = 0;
4915 break;
4916 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01004917 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03004918 return;
4919 }
4920
4921 mutex_lock(&dev_priv->rps.hw_lock);
4922 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4923 val &= ~DSPFREQGUAR_MASK_CHV;
4924 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
4925 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4926 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4927 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
4928 50)) {
4929 DRM_ERROR("timed out waiting for CDclk change\n");
4930 }
4931 mutex_unlock(&dev_priv->rps.hw_lock);
4932
4933 vlv_update_cdclk(dev);
4934}
4935
Jesse Barnes30a970c2013-11-04 13:48:12 -08004936static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4937 int max_pixclk)
4938{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03004939 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004940
Ville Syrjäläd49a3402014-06-28 02:03:58 +03004941 /* FIXME: Punit isn't quite ready yet */
4942 if (IS_CHERRYVIEW(dev_priv->dev))
4943 return 400000;
4944
Jesse Barnes30a970c2013-11-04 13:48:12 -08004945 /*
4946 * Really only a few cases to deal with, as only 4 CDclks are supported:
4947 * 200MHz
4948 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004949 * 320/333MHz (depends on HPLL freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004950 * 400MHz
4951 * So we check to see whether we're above 90% of the lower bin and
4952 * adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03004953 *
4954 * We seem to get an unstable or solid color picture at 200MHz.
4955 * Not sure what's wrong. For now use 200MHz only when all pipes
4956 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08004957 */
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004958 if (max_pixclk > freq_320*9/10)
Ville Syrjälädfcab172014-06-13 13:37:47 +03004959 return 400000;
4960 else if (max_pixclk > 266667*9/10)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004961 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03004962 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03004963 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03004964 else
4965 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004966}
4967
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004968/* compute the max pixel clock for new configuration */
4969static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004970{
4971 struct drm_device *dev = dev_priv->dev;
4972 struct intel_crtc *intel_crtc;
4973 int max_pixclk = 0;
4974
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004975 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004976 if (intel_crtc->new_enabled)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004977 max_pixclk = max(max_pixclk,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02004978 intel_crtc->new_config->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004979 }
4980
4981 return max_pixclk;
4982}
4983
4984static void valleyview_modeset_global_pipes(struct drm_device *dev,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004985 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004986{
4987 struct drm_i915_private *dev_priv = dev->dev_private;
4988 struct intel_crtc *intel_crtc;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004989 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004990
Imre Deakd60c4472014-03-27 17:45:10 +02004991 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4992 dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004993 return;
4994
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004995 /* disable/enable all currently active pipes while we change cdclk */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004996 for_each_intel_crtc(dev, intel_crtc)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004997 if (intel_crtc->base.enabled)
4998 *prepare_pipes |= (1 << intel_crtc->pipe);
4999}
5000
5001static void valleyview_modeset_global_resources(struct drm_device *dev)
5002{
5003 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005004 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005005 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5006
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005007 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005008 /*
5009 * FIXME: We can end up here with all power domains off, yet
5010 * with a CDCLK frequency other than the minimum. To account
5011 * for this take the PIPE-A power domain, which covers the HW
5012 * blocks needed for the following programming. This can be
5013 * removed once it's guaranteed that we get here either with
5014 * the minimum CDCLK set, or the required power domains
5015 * enabled.
5016 */
5017 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5018
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005019 if (IS_CHERRYVIEW(dev))
5020 cherryview_set_cdclk(dev, req_cdclk);
5021 else
5022 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005023
5024 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005025 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005026}
5027
Jesse Barnes89b667f2013-04-18 14:51:36 -07005028static void valleyview_crtc_enable(struct drm_crtc *crtc)
5029{
5030 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005031 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005032 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5033 struct intel_encoder *encoder;
5034 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005035 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005036
5037 WARN_ON(!crtc->enabled);
5038
5039 if (intel_crtc->active)
5040 return;
5041
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005042 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305043
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005044 if (!is_dsi) {
5045 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005046 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005047 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005048 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005049 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005050
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005051 if (intel_crtc->config->has_dp_encoder)
Daniel Vetter5b18e572014-04-24 23:55:06 +02005052 intel_dp_set_m_n(intel_crtc);
5053
5054 intel_set_pipe_timings(intel_crtc);
5055
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005056 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5057 struct drm_i915_private *dev_priv = dev->dev_private;
5058
5059 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5060 I915_WRITE(CHV_CANVAS(pipe), 0);
5061 }
5062
Daniel Vetter5b18e572014-04-24 23:55:06 +02005063 i9xx_set_pipeconf(intel_crtc);
5064
Jesse Barnes89b667f2013-04-18 14:51:36 -07005065 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005066
Daniel Vettera72e4c92014-09-30 10:56:47 +02005067 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005068
Jesse Barnes89b667f2013-04-18 14:51:36 -07005069 for_each_encoder_on_crtc(dev, crtc, encoder)
5070 if (encoder->pre_pll_enable)
5071 encoder->pre_pll_enable(encoder);
5072
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005073 if (!is_dsi) {
5074 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005075 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005076 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005077 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005078 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005079
5080 for_each_encoder_on_crtc(dev, crtc, encoder)
5081 if (encoder->pre_enable)
5082 encoder->pre_enable(encoder);
5083
Jesse Barnes2dd24552013-04-25 12:55:01 -07005084 i9xx_pfit_enable(intel_crtc);
5085
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005086 intel_crtc_load_lut(crtc);
5087
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005088 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005089 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005090
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005091 assert_vblank_disabled(crtc);
5092 drm_crtc_vblank_on(crtc);
5093
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005094 for_each_encoder_on_crtc(dev, crtc, encoder)
5095 encoder->enable(encoder);
5096
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005097 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005098
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005099 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005100 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005101}
5102
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005103static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5104{
5105 struct drm_device *dev = crtc->base.dev;
5106 struct drm_i915_private *dev_priv = dev->dev_private;
5107
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005108 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5109 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005110}
5111
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005112static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005113{
5114 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005115 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005116 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005117 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005118 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005119
Daniel Vetter08a48462012-07-02 11:43:47 +02005120 WARN_ON(!crtc->enabled);
5121
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005122 if (intel_crtc->active)
5123 return;
5124
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005125 i9xx_set_pll_dividers(intel_crtc);
5126
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005127 if (intel_crtc->config->has_dp_encoder)
Daniel Vetter5b18e572014-04-24 23:55:06 +02005128 intel_dp_set_m_n(intel_crtc);
5129
5130 intel_set_pipe_timings(intel_crtc);
5131
Daniel Vetter5b18e572014-04-24 23:55:06 +02005132 i9xx_set_pipeconf(intel_crtc);
5133
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005134 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005135
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005136 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005137 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005138
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005139 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005140 if (encoder->pre_enable)
5141 encoder->pre_enable(encoder);
5142
Daniel Vetterf6736a12013-06-05 13:34:30 +02005143 i9xx_enable_pll(intel_crtc);
5144
Jesse Barnes2dd24552013-04-25 12:55:01 -07005145 i9xx_pfit_enable(intel_crtc);
5146
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005147 intel_crtc_load_lut(crtc);
5148
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005149 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005150 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005151
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005152 assert_vblank_disabled(crtc);
5153 drm_crtc_vblank_on(crtc);
5154
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005155 for_each_encoder_on_crtc(dev, crtc, encoder)
5156 encoder->enable(encoder);
5157
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005158 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005159
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005160 /*
5161 * Gen2 reports pipe underruns whenever all planes are disabled.
5162 * So don't enable underrun reporting before at least some planes
5163 * are enabled.
5164 * FIXME: Need to fix the logic to work when we turn off all planes
5165 * but leave the pipe running.
5166 */
5167 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005168 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005169
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005170 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005171 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005172}
5173
Daniel Vetter87476d62013-04-11 16:29:06 +02005174static void i9xx_pfit_disable(struct intel_crtc *crtc)
5175{
5176 struct drm_device *dev = crtc->base.dev;
5177 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02005178
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005179 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005180 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02005181
5182 assert_pipe_disabled(dev_priv, crtc->pipe);
5183
Daniel Vetter328d8e82013-05-08 10:36:31 +02005184 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5185 I915_READ(PFIT_CONTROL));
5186 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02005187}
5188
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005189static void i9xx_crtc_disable(struct drm_crtc *crtc)
5190{
5191 struct drm_device *dev = crtc->dev;
5192 struct drm_i915_private *dev_priv = dev->dev_private;
5193 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005194 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005195 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005196
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005197 if (!intel_crtc->active)
5198 return;
5199
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005200 /*
5201 * Gen2 reports pipe underruns whenever all planes are disabled.
5202 * So diasble underrun reporting before all the planes get disabled.
5203 * FIXME: Need to fix the logic to work when we turn off all planes
5204 * but leave the pipe running.
5205 */
5206 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005207 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005208
Imre Deak564ed192014-06-13 14:54:21 +03005209 /*
5210 * Vblank time updates from the shadow to live plane control register
5211 * are blocked if the memory self-refresh mode is active at that
5212 * moment. So to make sure the plane gets truly disabled, disable
5213 * first the self-refresh mode. The self-refresh enable bit in turn
5214 * will be checked/applied by the HW only at the next frame start
5215 * event which is after the vblank start event, so we need to have a
5216 * wait-for-vblank between disabling the plane and the pipe.
5217 */
5218 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005219 intel_crtc_disable_planes(crtc);
5220
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005221 /*
5222 * On gen2 planes are double buffered but the pipe isn't, so we must
5223 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03005224 * We also need to wait on all gmch platforms because of the
5225 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005226 */
Imre Deak564ed192014-06-13 14:54:21 +03005227 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005228
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005229 for_each_encoder_on_crtc(dev, crtc, encoder)
5230 encoder->disable(encoder);
5231
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005232 drm_crtc_vblank_off(crtc);
5233 assert_vblank_disabled(crtc);
5234
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005235 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005236
Daniel Vetter87476d62013-04-11 16:29:06 +02005237 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005238
Jesse Barnes89b667f2013-04-18 14:51:36 -07005239 for_each_encoder_on_crtc(dev, crtc, encoder)
5240 if (encoder->post_disable)
5241 encoder->post_disable(encoder);
5242
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005243 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005244 if (IS_CHERRYVIEW(dev))
5245 chv_disable_pll(dev_priv, pipe);
5246 else if (IS_VALLEYVIEW(dev))
5247 vlv_disable_pll(dev_priv, pipe);
5248 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03005249 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005250 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005251
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005252 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005253 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005254
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005255 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005256 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005257
Daniel Vetterefa96242014-04-24 23:55:02 +02005258 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005259 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02005260 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005261}
5262
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005263static void i9xx_crtc_off(struct drm_crtc *crtc)
5264{
5265}
5266
Borun Fub04c5bd2014-07-12 10:02:27 +05305267/* Master function to enable/disable CRTC and corresponding power wells */
5268void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01005269{
Chris Wilsoncdd59982010-09-08 16:30:16 +01005270 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005271 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005272 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005273 enum intel_display_power_domain domain;
5274 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005275
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005276 if (enable) {
5277 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005278 domains = get_crtc_power_domains(crtc);
5279 for_each_power_domain(domain, domains)
5280 intel_display_power_get(dev_priv, domain);
5281 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005282
5283 dev_priv->display.crtc_enable(crtc);
5284 }
5285 } else {
5286 if (intel_crtc->active) {
5287 dev_priv->display.crtc_disable(crtc);
5288
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005289 domains = intel_crtc->enabled_power_domains;
5290 for_each_power_domain(domain, domains)
5291 intel_display_power_put(dev_priv, domain);
5292 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005293 }
5294 }
Borun Fub04c5bd2014-07-12 10:02:27 +05305295}
5296
5297/**
5298 * Sets the power management mode of the pipe and plane.
5299 */
5300void intel_crtc_update_dpms(struct drm_crtc *crtc)
5301{
5302 struct drm_device *dev = crtc->dev;
5303 struct intel_encoder *intel_encoder;
5304 bool enable = false;
5305
5306 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5307 enable |= intel_encoder->connectors_active;
5308
5309 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005310}
5311
Daniel Vetter976f8a22012-07-08 22:34:21 +02005312static void intel_crtc_disable(struct drm_crtc *crtc)
5313{
5314 struct drm_device *dev = crtc->dev;
5315 struct drm_connector *connector;
5316 struct drm_i915_private *dev_priv = dev->dev_private;
5317
5318 /* crtc should still be enabled when we disable it. */
5319 WARN_ON(!crtc->enabled);
5320
5321 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005322 dev_priv->display.off(crtc);
5323
Gustavo Padovan455a6802014-12-01 15:40:11 -08005324 crtc->primary->funcs->disable_plane(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005325
5326 /* Update computed state. */
5327 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5328 if (!connector->encoder || !connector->encoder->crtc)
5329 continue;
5330
5331 if (connector->encoder->crtc != crtc)
5332 continue;
5333
5334 connector->dpms = DRM_MODE_DPMS_OFF;
5335 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01005336 }
5337}
5338
Chris Wilsonea5b2132010-08-04 13:50:23 +01005339void intel_encoder_destroy(struct drm_encoder *encoder)
5340{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005341 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01005342
Chris Wilsonea5b2132010-08-04 13:50:23 +01005343 drm_encoder_cleanup(encoder);
5344 kfree(intel_encoder);
5345}
5346
Damien Lespiau92373292013-08-08 22:28:57 +01005347/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005348 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5349 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01005350static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005351{
5352 if (mode == DRM_MODE_DPMS_ON) {
5353 encoder->connectors_active = true;
5354
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005355 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005356 } else {
5357 encoder->connectors_active = false;
5358
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005359 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005360 }
5361}
5362
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005363/* Cross check the actual hw state with our own modeset state tracking (and it's
5364 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02005365static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005366{
5367 if (connector->get_hw_state(connector)) {
5368 struct intel_encoder *encoder = connector->encoder;
5369 struct drm_crtc *crtc;
5370 bool encoder_enabled;
5371 enum pipe pipe;
5372
5373 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5374 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03005375 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005376
Dave Airlie0e32b392014-05-02 14:02:48 +10005377 /* there is no real hw state for MST connectors */
5378 if (connector->mst_port)
5379 return;
5380
Rob Clarke2c719b2014-12-15 13:56:32 -05005381 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005382 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05005383 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005384 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005385
Dave Airlie36cd7442014-05-02 13:44:18 +10005386 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05005387 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10005388 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005389
Dave Airlie36cd7442014-05-02 13:44:18 +10005390 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05005391 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
5392 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10005393 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005394
Dave Airlie36cd7442014-05-02 13:44:18 +10005395 crtc = encoder->base.crtc;
5396
Rob Clarke2c719b2014-12-15 13:56:32 -05005397 I915_STATE_WARN(!crtc->enabled, "crtc not enabled\n");
5398 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5399 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10005400 "encoder active on the wrong pipe\n");
5401 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005402 }
5403}
5404
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005405/* Even simpler default implementation, if there's really no special case to
5406 * consider. */
5407void intel_connector_dpms(struct drm_connector *connector, int mode)
5408{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005409 /* All the simple cases only support two dpms states. */
5410 if (mode != DRM_MODE_DPMS_ON)
5411 mode = DRM_MODE_DPMS_OFF;
5412
5413 if (mode == connector->dpms)
5414 return;
5415
5416 connector->dpms = mode;
5417
5418 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01005419 if (connector->encoder)
5420 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005421
Daniel Vetterb9805142012-08-31 17:37:33 +02005422 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005423}
5424
Daniel Vetterf0947c32012-07-02 13:10:34 +02005425/* Simple connector->get_hw_state implementation for encoders that support only
5426 * one connector and no cloning and hence the encoder state determines the state
5427 * of the connector. */
5428bool intel_connector_get_hw_state(struct intel_connector *connector)
5429{
Daniel Vetter24929352012-07-02 20:28:59 +02005430 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02005431 struct intel_encoder *encoder = connector->encoder;
5432
5433 return encoder->get_hw_state(encoder, &pipe);
5434}
5435
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005436static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005437 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005438{
5439 struct drm_i915_private *dev_priv = dev->dev_private;
5440 struct intel_crtc *pipe_B_crtc =
5441 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5442
5443 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5444 pipe_name(pipe), pipe_config->fdi_lanes);
5445 if (pipe_config->fdi_lanes > 4) {
5446 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5447 pipe_name(pipe), pipe_config->fdi_lanes);
5448 return false;
5449 }
5450
Paulo Zanonibafb6552013-11-02 21:07:44 -07005451 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005452 if (pipe_config->fdi_lanes > 2) {
5453 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5454 pipe_config->fdi_lanes);
5455 return false;
5456 } else {
5457 return true;
5458 }
5459 }
5460
5461 if (INTEL_INFO(dev)->num_pipes == 2)
5462 return true;
5463
5464 /* Ivybridge 3 pipe is really complicated */
5465 switch (pipe) {
5466 case PIPE_A:
5467 return true;
5468 case PIPE_B:
5469 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5470 pipe_config->fdi_lanes > 2) {
5471 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5472 pipe_name(pipe), pipe_config->fdi_lanes);
5473 return false;
5474 }
5475 return true;
5476 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01005477 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005478 pipe_B_crtc->config->fdi_lanes <= 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005479 if (pipe_config->fdi_lanes > 2) {
5480 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5481 pipe_name(pipe), pipe_config->fdi_lanes);
5482 return false;
5483 }
5484 } else {
5485 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5486 return false;
5487 }
5488 return true;
5489 default:
5490 BUG();
5491 }
5492}
5493
Daniel Vettere29c22c2013-02-21 00:00:16 +01005494#define RETRY 1
5495static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005496 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02005497{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005498 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02005499 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02005500 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01005501 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005502
Daniel Vettere29c22c2013-02-21 00:00:16 +01005503retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02005504 /* FDI is a binary signal running at ~2.7GHz, encoding
5505 * each output octet as 10 bits. The actual frequency
5506 * is stored as a divider into a 100MHz clock, and the
5507 * mode pixel clock is stored in units of 1KHz.
5508 * Hence the bw of each lane in terms of the mode signal
5509 * is:
5510 */
5511 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5512
Damien Lespiau241bfc32013-09-25 16:45:37 +01005513 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005514
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005515 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005516 pipe_config->pipe_bpp);
5517
5518 pipe_config->fdi_lanes = lane;
5519
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005520 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005521 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005522
Daniel Vettere29c22c2013-02-21 00:00:16 +01005523 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5524 intel_crtc->pipe, pipe_config);
5525 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5526 pipe_config->pipe_bpp -= 2*3;
5527 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5528 pipe_config->pipe_bpp);
5529 needs_recompute = true;
5530 pipe_config->bw_constrained = true;
5531
5532 goto retry;
5533 }
5534
5535 if (needs_recompute)
5536 return RETRY;
5537
5538 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005539}
5540
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005541static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005542 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005543{
Jani Nikulad330a952014-01-21 11:24:25 +02005544 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03005545 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07005546 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005547}
5548
Daniel Vettera43f6e02013-06-07 23:10:32 +02005549static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005550 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08005551{
Daniel Vettera43f6e02013-06-07 23:10:32 +02005552 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02005553 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02005554 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01005555
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005556 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005557 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005558 int clock_limit =
5559 dev_priv->display.get_display_clock_speed(dev);
5560
5561 /*
5562 * Enable pixel doubling when the dot clock
5563 * is > 90% of the (display) core speed.
5564 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03005565 * GDG double wide on either pipe,
5566 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005567 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03005568 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01005569 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005570 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005571 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005572 }
5573
Damien Lespiau241bfc32013-09-25 16:45:37 +01005574 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005575 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005576 }
Chris Wilson89749352010-09-12 18:25:19 +01005577
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005578 /*
5579 * Pipe horizontal size must be even in:
5580 * - DVO ganged mode
5581 * - LVDS dual channel mode
5582 * - Double wide pipe
5583 */
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005584 if ((intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005585 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5586 pipe_config->pipe_src_w &= ~1;
5587
Damien Lespiau8693a822013-05-03 18:48:11 +01005588 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5589 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03005590 */
5591 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5592 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005593 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03005594
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005595 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005596 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005597 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005598 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5599 * for lvds. */
5600 pipe_config->pipe_bpp = 8*3;
5601 }
5602
Damien Lespiauf5adf942013-06-24 18:29:34 +01005603 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005604 hsw_compute_ips_config(crtc, pipe_config);
5605
Daniel Vetter877d48d2013-04-19 11:24:43 +02005606 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02005607 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02005608
Daniel Vettere29c22c2013-02-21 00:00:16 +01005609 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005610}
5611
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005612static int valleyview_get_display_clock_speed(struct drm_device *dev)
5613{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005614 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005615 u32 val;
5616 int divider;
5617
Ville Syrjäläd49a3402014-06-28 02:03:58 +03005618 /* FIXME: Punit isn't quite ready yet */
5619 if (IS_CHERRYVIEW(dev))
5620 return 400000;
5621
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005622 if (dev_priv->hpll_freq == 0)
5623 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
5624
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005625 mutex_lock(&dev_priv->dpio_lock);
5626 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5627 mutex_unlock(&dev_priv->dpio_lock);
5628
5629 divider = val & DISPLAY_FREQUENCY_VALUES;
5630
Ville Syrjälä7d007f42014-06-13 13:37:53 +03005631 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5632 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5633 "cdclk change in progress\n");
5634
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005635 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005636}
5637
Jesse Barnese70236a2009-09-21 10:42:27 -07005638static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08005639{
Jesse Barnese70236a2009-09-21 10:42:27 -07005640 return 400000;
5641}
Jesse Barnes79e53942008-11-07 14:24:08 -08005642
Jesse Barnese70236a2009-09-21 10:42:27 -07005643static int i915_get_display_clock_speed(struct drm_device *dev)
5644{
5645 return 333000;
5646}
Jesse Barnes79e53942008-11-07 14:24:08 -08005647
Jesse Barnese70236a2009-09-21 10:42:27 -07005648static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5649{
5650 return 200000;
5651}
Jesse Barnes79e53942008-11-07 14:24:08 -08005652
Daniel Vetter257a7ff2013-07-26 08:35:42 +02005653static int pnv_get_display_clock_speed(struct drm_device *dev)
5654{
5655 u16 gcfgc = 0;
5656
5657 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5658
5659 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5660 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5661 return 267000;
5662 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5663 return 333000;
5664 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5665 return 444000;
5666 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5667 return 200000;
5668 default:
5669 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5670 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5671 return 133000;
5672 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5673 return 167000;
5674 }
5675}
5676
Jesse Barnese70236a2009-09-21 10:42:27 -07005677static int i915gm_get_display_clock_speed(struct drm_device *dev)
5678{
5679 u16 gcfgc = 0;
5680
5681 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5682
5683 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08005684 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07005685 else {
5686 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5687 case GC_DISPLAY_CLOCK_333_MHZ:
5688 return 333000;
5689 default:
5690 case GC_DISPLAY_CLOCK_190_200_MHZ:
5691 return 190000;
5692 }
5693 }
5694}
Jesse Barnes79e53942008-11-07 14:24:08 -08005695
Jesse Barnese70236a2009-09-21 10:42:27 -07005696static int i865_get_display_clock_speed(struct drm_device *dev)
5697{
5698 return 266000;
5699}
5700
5701static int i855_get_display_clock_speed(struct drm_device *dev)
5702{
5703 u16 hpllcc = 0;
5704 /* Assume that the hardware is in the high speed state. This
5705 * should be the default.
5706 */
5707 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5708 case GC_CLOCK_133_200:
5709 case GC_CLOCK_100_200:
5710 return 200000;
5711 case GC_CLOCK_166_250:
5712 return 250000;
5713 case GC_CLOCK_100_133:
5714 return 133000;
5715 }
5716
5717 /* Shouldn't happen */
5718 return 0;
5719}
5720
5721static int i830_get_display_clock_speed(struct drm_device *dev)
5722{
5723 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08005724}
5725
Zhenyu Wang2c072452009-06-05 15:38:42 +08005726static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005727intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005728{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005729 while (*num > DATA_LINK_M_N_MASK ||
5730 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08005731 *num >>= 1;
5732 *den >>= 1;
5733 }
5734}
5735
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005736static void compute_m_n(unsigned int m, unsigned int n,
5737 uint32_t *ret_m, uint32_t *ret_n)
5738{
5739 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5740 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5741 intel_reduce_m_n_ratio(ret_m, ret_n);
5742}
5743
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005744void
5745intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5746 int pixel_clock, int link_clock,
5747 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005748{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005749 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005750
5751 compute_m_n(bits_per_pixel * pixel_clock,
5752 link_clock * nlanes * 8,
5753 &m_n->gmch_m, &m_n->gmch_n);
5754
5755 compute_m_n(pixel_clock, link_clock,
5756 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005757}
5758
Chris Wilsona7615032011-01-12 17:04:08 +00005759static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5760{
Jani Nikulad330a952014-01-21 11:24:25 +02005761 if (i915.panel_use_ssc >= 0)
5762 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005763 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07005764 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00005765}
5766
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005767static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005768{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005769 struct drm_device *dev = crtc->base.dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005770 struct drm_i915_private *dev_priv = dev->dev_private;
5771 int refclk;
5772
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005773 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02005774 refclk = 100000;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02005775 } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005776 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005777 refclk = dev_priv->vbt.lvds_ssc_freq;
5778 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005779 } else if (!IS_GEN2(dev)) {
5780 refclk = 96000;
5781 } else {
5782 refclk = 48000;
5783 }
5784
5785 return refclk;
5786}
5787
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005788static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005789{
Daniel Vetter7df00d72013-05-21 21:54:55 +02005790 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005791}
Daniel Vetterf47709a2013-03-28 10:42:02 +01005792
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005793static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5794{
5795 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005796}
5797
Daniel Vetterf47709a2013-03-28 10:42:02 +01005798static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005799 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08005800 intel_clock_t *reduced_clock)
5801{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005802 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005803 u32 fp, fp2 = 0;
5804
5805 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005806 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005807 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005808 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005809 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005810 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005811 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005812 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005813 }
5814
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005815 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005816
Daniel Vetterf47709a2013-03-28 10:42:02 +01005817 crtc->lowfreq_avail = false;
Bob Paauwee1f234b2014-11-11 09:29:18 -08005818 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
Jani Nikulad330a952014-01-21 11:24:25 +02005819 reduced_clock && i915.powersave) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005820 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005821 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005822 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005823 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005824 }
5825}
5826
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005827static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5828 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07005829{
5830 u32 reg_val;
5831
5832 /*
5833 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5834 * and set it to a reasonable value instead.
5835 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005836 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005837 reg_val &= 0xffffff00;
5838 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005839 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005840
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005841 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005842 reg_val &= 0x8cffffff;
5843 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005844 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005845
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005846 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005847 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005848 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005849
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005850 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005851 reg_val &= 0x00ffffff;
5852 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005853 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005854}
5855
Daniel Vetterb5518422013-05-03 11:49:48 +02005856static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5857 struct intel_link_m_n *m_n)
5858{
5859 struct drm_device *dev = crtc->base.dev;
5860 struct drm_i915_private *dev_priv = dev->dev_private;
5861 int pipe = crtc->pipe;
5862
Daniel Vettere3b95f12013-05-03 11:49:49 +02005863 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5864 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5865 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5866 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005867}
5868
5869static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07005870 struct intel_link_m_n *m_n,
5871 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02005872{
5873 struct drm_device *dev = crtc->base.dev;
5874 struct drm_i915_private *dev_priv = dev->dev_private;
5875 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005876 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02005877
5878 if (INTEL_INFO(dev)->gen >= 5) {
5879 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5880 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5881 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5882 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07005883 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
5884 * for gen < 8) and if DRRS is supported (to make sure the
5885 * registers are not unnecessarily accessed).
5886 */
5887 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005888 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07005889 I915_WRITE(PIPE_DATA_M2(transcoder),
5890 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
5891 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
5892 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
5893 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
5894 }
Daniel Vetterb5518422013-05-03 11:49:48 +02005895 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02005896 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5897 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5898 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5899 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005900 }
5901}
5902
Vandana Kannanf769cd22014-08-05 07:51:22 -07005903void intel_dp_set_m_n(struct intel_crtc *crtc)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005904{
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005905 if (crtc->config->has_pch_encoder)
5906 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005907 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005908 intel_cpu_transcoder_set_m_n(crtc, &crtc->config->dp_m_n,
5909 &crtc->config->dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005910}
5911
Ville Syrjäläd288f652014-10-28 13:20:22 +02005912static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005913 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005914{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005915 u32 dpll, dpll_md;
5916
5917 /*
5918 * Enable DPIO clock input. We should never disable the reference
5919 * clock for pipe B, since VGA hotplug / manual detection depends
5920 * on it.
5921 */
5922 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5923 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5924 /* We should never disable this, set it here for state tracking */
5925 if (crtc->pipe == PIPE_B)
5926 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5927 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02005928 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005929
Ville Syrjäläd288f652014-10-28 13:20:22 +02005930 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005931 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02005932 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005933}
5934
Ville Syrjäläd288f652014-10-28 13:20:22 +02005935static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005936 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005937{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005938 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005939 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005940 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005941 u32 mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005942 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005943 u32 coreclk, reg_val;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005944
Daniel Vetter09153002012-12-12 14:06:44 +01005945 mutex_lock(&dev_priv->dpio_lock);
5946
Ville Syrjäläd288f652014-10-28 13:20:22 +02005947 bestn = pipe_config->dpll.n;
5948 bestm1 = pipe_config->dpll.m1;
5949 bestm2 = pipe_config->dpll.m2;
5950 bestp1 = pipe_config->dpll.p1;
5951 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005952
Jesse Barnes89b667f2013-04-18 14:51:36 -07005953 /* See eDP HDMI DPIO driver vbios notes doc */
5954
5955 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005956 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005957 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005958
5959 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005960 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005961
5962 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005963 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005964 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005965 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005966
5967 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005968 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005969
5970 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005971 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5972 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5973 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005974 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07005975
5976 /*
5977 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5978 * but we don't support that).
5979 * Note: don't use the DAC post divider as it seems unstable.
5980 */
5981 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005982 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005983
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005984 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005985 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005986
Jesse Barnes89b667f2013-04-18 14:51:36 -07005987 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02005988 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005989 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
5990 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005991 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03005992 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005993 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005994 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005995 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005996
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005997 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07005998 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005999 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006000 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006001 0x0df40000);
6002 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006003 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006004 0x0df70000);
6005 } else { /* HDMI or VGA */
6006 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006007 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006008 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006009 0x0df70000);
6010 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006011 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006012 0x0df40000);
6013 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07006014
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006015 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006016 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006017 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6018 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006019 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006020 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006021
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006022 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01006023 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07006024}
6025
Ville Syrjäläd288f652014-10-28 13:20:22 +02006026static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006027 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006028{
Ville Syrjäläd288f652014-10-28 13:20:22 +02006029 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006030 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6031 DPLL_VCO_ENABLE;
6032 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006033 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006034
Ville Syrjäläd288f652014-10-28 13:20:22 +02006035 pipe_config->dpll_hw_state.dpll_md =
6036 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006037}
6038
Ville Syrjäläd288f652014-10-28 13:20:22 +02006039static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006040 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006041{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006042 struct drm_device *dev = crtc->base.dev;
6043 struct drm_i915_private *dev_priv = dev->dev_private;
6044 int pipe = crtc->pipe;
6045 int dpll_reg = DPLL(crtc->pipe);
6046 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ville Syrjälä580d3812014-04-09 13:29:00 +03006047 u32 loopfilter, intcoeff;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006048 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
6049 int refclk;
6050
Ville Syrjäläd288f652014-10-28 13:20:22 +02006051 bestn = pipe_config->dpll.n;
6052 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6053 bestm1 = pipe_config->dpll.m1;
6054 bestm2 = pipe_config->dpll.m2 >> 22;
6055 bestp1 = pipe_config->dpll.p1;
6056 bestp2 = pipe_config->dpll.p2;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006057
6058 /*
6059 * Enable Refclk and SSC
6060 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006061 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02006062 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006063
6064 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006065
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006066 /* p1 and p2 divider */
6067 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6068 5 << DPIO_CHV_S1_DIV_SHIFT |
6069 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6070 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6071 1 << DPIO_CHV_K_DIV_SHIFT);
6072
6073 /* Feedback post-divider - m2 */
6074 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6075
6076 /* Feedback refclk divider - n and m1 */
6077 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6078 DPIO_CHV_M1_DIV_BY_2 |
6079 1 << DPIO_CHV_N_DIV_SHIFT);
6080
6081 /* M2 fraction division */
6082 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
6083
6084 /* M2 fraction division enable */
6085 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
6086 DPIO_CHV_FRAC_DIV_EN |
6087 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
6088
6089 /* Loop filter */
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006090 refclk = i9xx_get_refclk(crtc, 0);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006091 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
6092 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
6093 if (refclk == 100000)
6094 intcoeff = 11;
6095 else if (refclk == 38400)
6096 intcoeff = 10;
6097 else
6098 intcoeff = 9;
6099 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
6100 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6101
6102 /* AFC Recal */
6103 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6104 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6105 DPIO_AFC_RECAL);
6106
6107 mutex_unlock(&dev_priv->dpio_lock);
6108}
6109
Ville Syrjäläd288f652014-10-28 13:20:22 +02006110/**
6111 * vlv_force_pll_on - forcibly enable just the PLL
6112 * @dev_priv: i915 private structure
6113 * @pipe: pipe PLL to enable
6114 * @dpll: PLL configuration
6115 *
6116 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6117 * in cases where we need the PLL enabled even when @pipe is not going to
6118 * be enabled.
6119 */
6120void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
6121 const struct dpll *dpll)
6122{
6123 struct intel_crtc *crtc =
6124 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006125 struct intel_crtc_state pipe_config = {
Ville Syrjäläd288f652014-10-28 13:20:22 +02006126 .pixel_multiplier = 1,
6127 .dpll = *dpll,
6128 };
6129
6130 if (IS_CHERRYVIEW(dev)) {
6131 chv_update_pll(crtc, &pipe_config);
6132 chv_prepare_pll(crtc, &pipe_config);
6133 chv_enable_pll(crtc, &pipe_config);
6134 } else {
6135 vlv_update_pll(crtc, &pipe_config);
6136 vlv_prepare_pll(crtc, &pipe_config);
6137 vlv_enable_pll(crtc, &pipe_config);
6138 }
6139}
6140
6141/**
6142 * vlv_force_pll_off - forcibly disable just the PLL
6143 * @dev_priv: i915 private structure
6144 * @pipe: pipe PLL to disable
6145 *
6146 * Disable the PLL for @pipe. To be used in cases where we need
6147 * the PLL enabled even when @pipe is not going to be enabled.
6148 */
6149void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
6150{
6151 if (IS_CHERRYVIEW(dev))
6152 chv_disable_pll(to_i915(dev), pipe);
6153 else
6154 vlv_disable_pll(to_i915(dev), pipe);
6155}
6156
Daniel Vetterf47709a2013-03-28 10:42:02 +01006157static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006158 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01006159 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006160 int num_connectors)
6161{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006162 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006163 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006164 u32 dpll;
6165 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006166 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006167
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006168 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306169
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006170 is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
6171 intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006172
6173 dpll = DPLL_VGA_MODE_DIS;
6174
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006175 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006176 dpll |= DPLLB_MODE_LVDS;
6177 else
6178 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006179
Daniel Vetteref1b4602013-06-01 17:17:04 +02006180 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006181 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02006182 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006183 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02006184
6185 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006186 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006187
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006188 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006189 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006190
6191 /* compute bitmask from p1 value */
6192 if (IS_PINEVIEW(dev))
6193 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6194 else {
6195 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6196 if (IS_G4X(dev) && reduced_clock)
6197 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6198 }
6199 switch (clock->p2) {
6200 case 5:
6201 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6202 break;
6203 case 7:
6204 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6205 break;
6206 case 10:
6207 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6208 break;
6209 case 14:
6210 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6211 break;
6212 }
6213 if (INTEL_INFO(dev)->gen >= 4)
6214 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6215
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006216 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006217 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006218 else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006219 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6220 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6221 else
6222 dpll |= PLL_REF_INPUT_DREFCLK;
6223
6224 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006225 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006226
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006227 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006228 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02006229 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006230 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006231 }
6232}
6233
Daniel Vetterf47709a2013-03-28 10:42:02 +01006234static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006235 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01006236 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006237 int num_connectors)
6238{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006239 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006240 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006241 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006242 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006243
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006244 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306245
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006246 dpll = DPLL_VGA_MODE_DIS;
6247
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006248 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006249 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6250 } else {
6251 if (clock->p1 == 2)
6252 dpll |= PLL_P1_DIVIDE_BY_TWO;
6253 else
6254 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6255 if (clock->p2 == 4)
6256 dpll |= PLL_P2_DIVIDE_BY_4;
6257 }
6258
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006259 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02006260 dpll |= DPLL_DVO_2X_MODE;
6261
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006262 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006263 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6264 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6265 else
6266 dpll |= PLL_REF_INPUT_DREFCLK;
6267
6268 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006269 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006270}
6271
Daniel Vetter8a654f32013-06-01 17:16:22 +02006272static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006273{
6274 struct drm_device *dev = intel_crtc->base.dev;
6275 struct drm_i915_private *dev_priv = dev->dev_private;
6276 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006277 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02006278 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006279 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006280 uint32_t crtc_vtotal, crtc_vblank_end;
6281 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006282
6283 /* We need to be careful not to changed the adjusted mode, for otherwise
6284 * the hw state checker will get angry at the mismatch. */
6285 crtc_vtotal = adjusted_mode->crtc_vtotal;
6286 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006287
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006288 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006289 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006290 crtc_vtotal -= 1;
6291 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006292
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006293 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006294 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6295 else
6296 vsyncshift = adjusted_mode->crtc_hsync_start -
6297 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006298 if (vsyncshift < 0)
6299 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006300 }
6301
6302 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006303 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006304
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006305 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006306 (adjusted_mode->crtc_hdisplay - 1) |
6307 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006308 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006309 (adjusted_mode->crtc_hblank_start - 1) |
6310 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006311 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006312 (adjusted_mode->crtc_hsync_start - 1) |
6313 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6314
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006315 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006316 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006317 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006318 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006319 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006320 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006321 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006322 (adjusted_mode->crtc_vsync_start - 1) |
6323 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6324
Paulo Zanonib5e508d2012-10-24 11:34:43 -02006325 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6326 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6327 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6328 * bits. */
6329 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6330 (pipe == PIPE_B || pipe == PIPE_C))
6331 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6332
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006333 /* pipesrc controls the size that is scaled from, which should
6334 * always be the user's requested size.
6335 */
6336 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006337 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6338 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006339}
6340
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006341static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006342 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006343{
6344 struct drm_device *dev = crtc->base.dev;
6345 struct drm_i915_private *dev_priv = dev->dev_private;
6346 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6347 uint32_t tmp;
6348
6349 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006350 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6351 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006352 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006353 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6354 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006355 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006356 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6357 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006358
6359 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006360 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6361 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006362 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006363 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6364 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006365 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006366 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6367 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006368
6369 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006370 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6371 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
6372 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006373 }
6374
6375 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03006376 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6377 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6378
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006379 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
6380 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006381}
6382
Daniel Vetterf6a83282014-02-11 15:28:57 -08006383void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006384 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03006385{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006386 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
6387 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
6388 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
6389 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006390
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006391 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
6392 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
6393 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
6394 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006395
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006396 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006397
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006398 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
6399 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006400}
6401
Daniel Vetter84b046f2013-02-19 18:48:54 +01006402static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6403{
6404 struct drm_device *dev = intel_crtc->base.dev;
6405 struct drm_i915_private *dev_priv = dev->dev_private;
6406 uint32_t pipeconf;
6407
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006408 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006409
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03006410 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6411 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6412 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02006413
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006414 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006415 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006416
Daniel Vetterff9ce462013-04-24 14:57:17 +02006417 /* only g4x and later have fancy bpc/dither controls */
6418 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02006419 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006420 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02006421 pipeconf |= PIPECONF_DITHER_EN |
6422 PIPECONF_DITHER_TYPE_SP;
6423
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006424 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02006425 case 18:
6426 pipeconf |= PIPECONF_6BPC;
6427 break;
6428 case 24:
6429 pipeconf |= PIPECONF_8BPC;
6430 break;
6431 case 30:
6432 pipeconf |= PIPECONF_10BPC;
6433 break;
6434 default:
6435 /* Case prevented by intel_choose_pipe_bpp_dither. */
6436 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01006437 }
6438 }
6439
6440 if (HAS_PIPE_CXSR(dev)) {
6441 if (intel_crtc->lowfreq_avail) {
6442 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6443 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6444 } else {
6445 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01006446 }
6447 }
6448
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006449 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006450 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006451 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006452 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6453 else
6454 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6455 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01006456 pipeconf |= PIPECONF_PROGRESSIVE;
6457
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006458 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006459 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03006460
Daniel Vetter84b046f2013-02-19 18:48:54 +01006461 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6462 POSTING_READ(PIPECONF(intel_crtc->pipe));
6463}
6464
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006465static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
6466 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08006467{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006468 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08006469 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07006470 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07006471 intel_clock_t clock, reduced_clock;
Daniel Vettera16af7212013-04-30 14:01:44 +02006472 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006473 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01006474 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08006475 const intel_limit_t *limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08006476
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006477 for_each_intel_encoder(dev, encoder) {
6478 if (encoder->new_crtc != crtc)
6479 continue;
6480
Chris Wilson5eddb702010-09-11 13:48:45 +01006481 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006482 case INTEL_OUTPUT_LVDS:
6483 is_lvds = true;
6484 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006485 case INTEL_OUTPUT_DSI:
6486 is_dsi = true;
6487 break;
Paulo Zanoni6847d712014-10-27 17:47:52 -02006488 default:
6489 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006490 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006491
Eric Anholtc751ce42010-03-25 11:48:48 -07006492 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08006493 }
6494
Jani Nikulaf2335332013-09-13 11:03:09 +03006495 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02006496 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006497
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006498 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006499 refclk = i9xx_get_refclk(crtc, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03006500
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006501 /*
6502 * Returns a set of divisors for the desired target clock with
6503 * the given refclk, or FALSE. The returned values represent
6504 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6505 * 2) / p1 / p2.
6506 */
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006507 limit = intel_limit(crtc, refclk);
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006508 ok = dev_priv->display.find_dpll(limit, crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006509 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006510 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03006511 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006512 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6513 return -EINVAL;
6514 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006515
Jani Nikulaf2335332013-09-13 11:03:09 +03006516 if (is_lvds && dev_priv->lvds_downclock_avail) {
6517 /*
6518 * Ensure we match the reduced clock's P to the target
6519 * clock. If the clocks don't match, we can't switch
6520 * the display clock by using the FP0/FP1. In such case
6521 * we will disable the LVDS downclock feature.
6522 */
6523 has_reduced_clock =
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006524 dev_priv->display.find_dpll(limit, crtc,
Jani Nikulaf2335332013-09-13 11:03:09 +03006525 dev_priv->lvds_downclock,
6526 refclk, &clock,
6527 &reduced_clock);
6528 }
6529 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006530 crtc_state->dpll.n = clock.n;
6531 crtc_state->dpll.m1 = clock.m1;
6532 crtc_state->dpll.m2 = clock.m2;
6533 crtc_state->dpll.p1 = clock.p1;
6534 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006535 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006536
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006537 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006538 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306539 has_reduced_clock ? &reduced_clock : NULL,
6540 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006541 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006542 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006543 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006544 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006545 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006546 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006547 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02006548 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006549 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006550
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02006551 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07006552}
6553
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006554static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006555 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006556{
6557 struct drm_device *dev = crtc->base.dev;
6558 struct drm_i915_private *dev_priv = dev->dev_private;
6559 uint32_t tmp;
6560
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02006561 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6562 return;
6563
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006564 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02006565 if (!(tmp & PFIT_ENABLE))
6566 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006567
Daniel Vetter06922822013-07-11 13:35:40 +02006568 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006569 if (INTEL_INFO(dev)->gen < 4) {
6570 if (crtc->pipe != PIPE_B)
6571 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006572 } else {
6573 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6574 return;
6575 }
6576
Daniel Vetter06922822013-07-11 13:35:40 +02006577 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006578 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6579 if (INTEL_INFO(dev)->gen < 5)
6580 pipe_config->gmch_pfit.lvds_border_bits =
6581 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6582}
6583
Jesse Barnesacbec812013-09-20 11:29:32 -07006584static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006585 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07006586{
6587 struct drm_device *dev = crtc->base.dev;
6588 struct drm_i915_private *dev_priv = dev->dev_private;
6589 int pipe = pipe_config->cpu_transcoder;
6590 intel_clock_t clock;
6591 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07006592 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07006593
Shobhit Kumarf573de52014-07-30 20:32:37 +05306594 /* In case of MIPI DPLL will not even be used */
6595 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6596 return;
6597
Jesse Barnesacbec812013-09-20 11:29:32 -07006598 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006599 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07006600 mutex_unlock(&dev_priv->dpio_lock);
6601
6602 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6603 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6604 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6605 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6606 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6607
Ville Syrjäläf6466282013-10-14 14:50:31 +03006608 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07006609
Ville Syrjäläf6466282013-10-14 14:50:31 +03006610 /* clock.dot is the fast clock */
6611 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07006612}
6613
Damien Lespiau5724dbd2015-01-20 12:51:52 +00006614static void
6615i9xx_get_initial_plane_config(struct intel_crtc *crtc,
6616 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006617{
6618 struct drm_device *dev = crtc->base.dev;
6619 struct drm_i915_private *dev_priv = dev->dev_private;
6620 u32 val, base, offset;
6621 int pipe = crtc->pipe, plane = crtc->plane;
6622 int fourcc, pixel_format;
6623 int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006624 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00006625 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006626
Damien Lespiau7f0801e2015-02-05 19:35:13 +00006627 val = I915_READ(DSPCNTR(plane));
6628 if (!(val & DISPLAY_PLANE_ENABLE))
6629 return;
6630
Damien Lespiaud9806c92015-01-21 14:07:19 +00006631 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00006632 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006633 DRM_DEBUG_KMS("failed to alloc fb\n");
6634 return;
6635 }
6636
Damien Lespiau1b842c82015-01-21 13:50:54 +00006637 fb = &intel_fb->base;
6638
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006639 if (INTEL_INFO(dev)->gen >= 4)
6640 if (val & DISPPLANE_TILED)
Damien Lespiau49af4492015-01-20 12:51:44 +00006641 plane_config->tiling = I915_TILING_X;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006642
6643 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00006644 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006645 fb->pixel_format = fourcc;
6646 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006647
6648 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00006649 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006650 offset = I915_READ(DSPTILEOFF(plane));
6651 else
6652 offset = I915_READ(DSPLINOFF(plane));
6653 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6654 } else {
6655 base = I915_READ(DSPADDR(plane));
6656 }
6657 plane_config->base = base;
6658
6659 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006660 fb->width = ((val >> 16) & 0xfff) + 1;
6661 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006662
6663 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006664 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006665
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006666 aligned_height = intel_fb_align_height(dev, fb->height,
Damien Lespiauec2c9812015-01-20 12:51:45 +00006667 plane_config->tiling);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006668
Daniel Vetterf37b5c22015-02-10 23:12:27 +01006669 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006670
Damien Lespiau2844a922015-01-20 12:51:48 +00006671 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
6672 pipe_name(pipe), plane, fb->width, fb->height,
6673 fb->bits_per_pixel, base, fb->pitches[0],
6674 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006675
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006676 crtc->base.primary->fb = fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006677}
6678
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006679static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006680 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006681{
6682 struct drm_device *dev = crtc->base.dev;
6683 struct drm_i915_private *dev_priv = dev->dev_private;
6684 int pipe = pipe_config->cpu_transcoder;
6685 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6686 intel_clock_t clock;
6687 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6688 int refclk = 100000;
6689
6690 mutex_lock(&dev_priv->dpio_lock);
6691 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6692 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6693 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6694 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6695 mutex_unlock(&dev_priv->dpio_lock);
6696
6697 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6698 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6699 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6700 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6701 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6702
6703 chv_clock(refclk, &clock);
6704
6705 /* clock.dot is the fast clock */
6706 pipe_config->port_clock = clock.dot / 5;
6707}
6708
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006709static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006710 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006711{
6712 struct drm_device *dev = crtc->base.dev;
6713 struct drm_i915_private *dev_priv = dev->dev_private;
6714 uint32_t tmp;
6715
Daniel Vetterf458ebb2014-09-30 10:56:39 +02006716 if (!intel_display_power_is_enabled(dev_priv,
6717 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02006718 return false;
6719
Daniel Vettere143a212013-07-04 12:01:15 +02006720 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006721 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006722
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006723 tmp = I915_READ(PIPECONF(crtc->pipe));
6724 if (!(tmp & PIPECONF_ENABLE))
6725 return false;
6726
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006727 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6728 switch (tmp & PIPECONF_BPC_MASK) {
6729 case PIPECONF_6BPC:
6730 pipe_config->pipe_bpp = 18;
6731 break;
6732 case PIPECONF_8BPC:
6733 pipe_config->pipe_bpp = 24;
6734 break;
6735 case PIPECONF_10BPC:
6736 pipe_config->pipe_bpp = 30;
6737 break;
6738 default:
6739 break;
6740 }
6741 }
6742
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02006743 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6744 pipe_config->limited_color_range = true;
6745
Ville Syrjälä282740f2013-09-04 18:30:03 +03006746 if (INTEL_INFO(dev)->gen < 4)
6747 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6748
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006749 intel_get_pipe_timings(crtc, pipe_config);
6750
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006751 i9xx_get_pfit_config(crtc, pipe_config);
6752
Daniel Vetter6c49f242013-06-06 12:45:25 +02006753 if (INTEL_INFO(dev)->gen >= 4) {
6754 tmp = I915_READ(DPLL_MD(crtc->pipe));
6755 pipe_config->pixel_multiplier =
6756 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6757 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006758 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02006759 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6760 tmp = I915_READ(DPLL(crtc->pipe));
6761 pipe_config->pixel_multiplier =
6762 ((tmp & SDVO_MULTIPLIER_MASK)
6763 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6764 } else {
6765 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6766 * port and will be fixed up in the encoder->get_config
6767 * function. */
6768 pipe_config->pixel_multiplier = 1;
6769 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006770 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6771 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006772 /*
6773 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
6774 * on 830. Filter it out here so that we don't
6775 * report errors due to that.
6776 */
6777 if (IS_I830(dev))
6778 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
6779
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006780 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6781 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03006782 } else {
6783 /* Mask out read-only status bits. */
6784 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6785 DPLL_PORTC_READY_MASK |
6786 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006787 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02006788
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006789 if (IS_CHERRYVIEW(dev))
6790 chv_crtc_clock_get(crtc, pipe_config);
6791 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07006792 vlv_crtc_clock_get(crtc, pipe_config);
6793 else
6794 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03006795
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006796 return true;
6797}
6798
Paulo Zanonidde86e22012-12-01 12:04:25 -02006799static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07006800{
6801 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006802 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006803 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006804 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006805 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006806 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07006807 bool has_ck505 = false;
6808 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006809
6810 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01006811 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07006812 switch (encoder->type) {
6813 case INTEL_OUTPUT_LVDS:
6814 has_panel = true;
6815 has_lvds = true;
6816 break;
6817 case INTEL_OUTPUT_EDP:
6818 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03006819 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07006820 has_cpu_edp = true;
6821 break;
Paulo Zanoni6847d712014-10-27 17:47:52 -02006822 default:
6823 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006824 }
6825 }
6826
Keith Packard99eb6a02011-09-26 14:29:12 -07006827 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006828 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07006829 can_ssc = has_ck505;
6830 } else {
6831 has_ck505 = false;
6832 can_ssc = true;
6833 }
6834
Imre Deak2de69052013-05-08 13:14:04 +03006835 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6836 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006837
6838 /* Ironlake: try to setup display ref clock before DPLL
6839 * enabling. This is only under driver's control after
6840 * PCH B stepping, previous chipset stepping should be
6841 * ignoring this setting.
6842 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006843 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006844
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006845 /* As we must carefully and slowly disable/enable each source in turn,
6846 * compute the final state we want first and check if we need to
6847 * make any changes at all.
6848 */
6849 final = val;
6850 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07006851 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006852 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07006853 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006854 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6855
6856 final &= ~DREF_SSC_SOURCE_MASK;
6857 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6858 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006859
Keith Packard199e5d72011-09-22 12:01:57 -07006860 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006861 final |= DREF_SSC_SOURCE_ENABLE;
6862
6863 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6864 final |= DREF_SSC1_ENABLE;
6865
6866 if (has_cpu_edp) {
6867 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6868 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6869 else
6870 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6871 } else
6872 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6873 } else {
6874 final |= DREF_SSC_SOURCE_DISABLE;
6875 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6876 }
6877
6878 if (final == val)
6879 return;
6880
6881 /* Always enable nonspread source */
6882 val &= ~DREF_NONSPREAD_SOURCE_MASK;
6883
6884 if (has_ck505)
6885 val |= DREF_NONSPREAD_CK505_ENABLE;
6886 else
6887 val |= DREF_NONSPREAD_SOURCE_ENABLE;
6888
6889 if (has_panel) {
6890 val &= ~DREF_SSC_SOURCE_MASK;
6891 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006892
Keith Packard199e5d72011-09-22 12:01:57 -07006893 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07006894 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006895 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006896 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02006897 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006898 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006899
6900 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006901 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006902 POSTING_READ(PCH_DREF_CONTROL);
6903 udelay(200);
6904
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006905 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006906
6907 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07006908 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07006909 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006910 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006911 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02006912 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006913 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07006914 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006915 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006916
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006917 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006918 POSTING_READ(PCH_DREF_CONTROL);
6919 udelay(200);
6920 } else {
6921 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6922
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006923 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07006924
6925 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006926 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006927
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006928 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006929 POSTING_READ(PCH_DREF_CONTROL);
6930 udelay(200);
6931
6932 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006933 val &= ~DREF_SSC_SOURCE_MASK;
6934 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006935
6936 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006937 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006938
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006939 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006940 POSTING_READ(PCH_DREF_CONTROL);
6941 udelay(200);
6942 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006943
6944 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006945}
6946
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006947static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02006948{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006949 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006950
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006951 tmp = I915_READ(SOUTH_CHICKEN2);
6952 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6953 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006954
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006955 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6956 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6957 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02006958
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006959 tmp = I915_READ(SOUTH_CHICKEN2);
6960 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6961 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006962
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006963 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6964 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6965 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006966}
6967
6968/* WaMPhyProgramming:hsw */
6969static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6970{
6971 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006972
6973 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6974 tmp &= ~(0xFF << 24);
6975 tmp |= (0x12 << 24);
6976 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6977
Paulo Zanonidde86e22012-12-01 12:04:25 -02006978 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6979 tmp |= (1 << 11);
6980 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6981
6982 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6983 tmp |= (1 << 11);
6984 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6985
Paulo Zanonidde86e22012-12-01 12:04:25 -02006986 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6987 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6988 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6989
6990 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6991 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6992 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6993
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006994 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6995 tmp &= ~(7 << 13);
6996 tmp |= (5 << 13);
6997 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006998
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006999 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7000 tmp &= ~(7 << 13);
7001 tmp |= (5 << 13);
7002 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007003
7004 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7005 tmp &= ~0xFF;
7006 tmp |= 0x1C;
7007 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7008
7009 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7010 tmp &= ~0xFF;
7011 tmp |= 0x1C;
7012 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7013
7014 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7015 tmp &= ~(0xFF << 16);
7016 tmp |= (0x1C << 16);
7017 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7018
7019 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7020 tmp &= ~(0xFF << 16);
7021 tmp |= (0x1C << 16);
7022 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7023
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007024 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7025 tmp |= (1 << 27);
7026 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007027
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007028 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7029 tmp |= (1 << 27);
7030 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007031
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007032 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7033 tmp &= ~(0xF << 28);
7034 tmp |= (4 << 28);
7035 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007036
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007037 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7038 tmp &= ~(0xF << 28);
7039 tmp |= (4 << 28);
7040 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007041}
7042
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007043/* Implements 3 different sequences from BSpec chapter "Display iCLK
7044 * Programming" based on the parameters passed:
7045 * - Sequence to enable CLKOUT_DP
7046 * - Sequence to enable CLKOUT_DP without spread
7047 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7048 */
7049static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
7050 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007051{
7052 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007053 uint32_t reg, tmp;
7054
7055 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7056 with_spread = true;
7057 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
7058 with_fdi, "LP PCH doesn't have FDI\n"))
7059 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007060
7061 mutex_lock(&dev_priv->dpio_lock);
7062
7063 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7064 tmp &= ~SBI_SSCCTL_DISABLE;
7065 tmp |= SBI_SSCCTL_PATHALT;
7066 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7067
7068 udelay(24);
7069
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007070 if (with_spread) {
7071 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7072 tmp &= ~SBI_SSCCTL_PATHALT;
7073 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007074
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007075 if (with_fdi) {
7076 lpt_reset_fdi_mphy(dev_priv);
7077 lpt_program_fdi_mphy(dev_priv);
7078 }
7079 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02007080
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007081 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7082 SBI_GEN0 : SBI_DBUFF0;
7083 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7084 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7085 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01007086
7087 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007088}
7089
Paulo Zanoni47701c32013-07-23 11:19:25 -03007090/* Sequence to disable CLKOUT_DP */
7091static void lpt_disable_clkout_dp(struct drm_device *dev)
7092{
7093 struct drm_i915_private *dev_priv = dev->dev_private;
7094 uint32_t reg, tmp;
7095
7096 mutex_lock(&dev_priv->dpio_lock);
7097
7098 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7099 SBI_GEN0 : SBI_DBUFF0;
7100 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7101 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7102 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7103
7104 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7105 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7106 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7107 tmp |= SBI_SSCCTL_PATHALT;
7108 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7109 udelay(32);
7110 }
7111 tmp |= SBI_SSCCTL_DISABLE;
7112 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7113 }
7114
7115 mutex_unlock(&dev_priv->dpio_lock);
7116}
7117
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007118static void lpt_init_pch_refclk(struct drm_device *dev)
7119{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007120 struct intel_encoder *encoder;
7121 bool has_vga = false;
7122
Damien Lespiaub2784e12014-08-05 11:29:37 +01007123 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007124 switch (encoder->type) {
7125 case INTEL_OUTPUT_ANALOG:
7126 has_vga = true;
7127 break;
Paulo Zanoni6847d712014-10-27 17:47:52 -02007128 default:
7129 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007130 }
7131 }
7132
Paulo Zanoni47701c32013-07-23 11:19:25 -03007133 if (has_vga)
7134 lpt_enable_clkout_dp(dev, true, true);
7135 else
7136 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007137}
7138
Paulo Zanonidde86e22012-12-01 12:04:25 -02007139/*
7140 * Initialize reference clocks when the driver loads
7141 */
7142void intel_init_pch_refclk(struct drm_device *dev)
7143{
7144 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7145 ironlake_init_pch_refclk(dev);
7146 else if (HAS_PCH_LPT(dev))
7147 lpt_init_pch_refclk(dev);
7148}
7149
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007150static int ironlake_get_refclk(struct drm_crtc *crtc)
7151{
7152 struct drm_device *dev = crtc->dev;
7153 struct drm_i915_private *dev_priv = dev->dev_private;
7154 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007155 int num_connectors = 0;
7156 bool is_lvds = false;
7157
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007158 for_each_intel_encoder(dev, encoder) {
7159 if (encoder->new_crtc != to_intel_crtc(crtc))
7160 continue;
7161
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007162 switch (encoder->type) {
7163 case INTEL_OUTPUT_LVDS:
7164 is_lvds = true;
7165 break;
Paulo Zanoni6847d712014-10-27 17:47:52 -02007166 default:
7167 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007168 }
7169 num_connectors++;
7170 }
7171
7172 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007173 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007174 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007175 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007176 }
7177
7178 return 120000;
7179}
7180
Daniel Vetter6ff93602013-04-19 11:24:36 +02007181static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03007182{
7183 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
7184 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7185 int pipe = intel_crtc->pipe;
7186 uint32_t val;
7187
Daniel Vetter78114072013-06-13 00:54:57 +02007188 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03007189
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007190 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03007191 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007192 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007193 break;
7194 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007195 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007196 break;
7197 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007198 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007199 break;
7200 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007201 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007202 break;
7203 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03007204 /* Case prevented by intel_choose_pipe_bpp_dither. */
7205 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03007206 }
7207
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007208 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03007209 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7210
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007211 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03007212 val |= PIPECONF_INTERLACED_ILK;
7213 else
7214 val |= PIPECONF_PROGRESSIVE;
7215
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007216 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007217 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007218
Paulo Zanonic8203562012-09-12 10:06:29 -03007219 I915_WRITE(PIPECONF(pipe), val);
7220 POSTING_READ(PIPECONF(pipe));
7221}
7222
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007223/*
7224 * Set up the pipe CSC unit.
7225 *
7226 * Currently only full range RGB to limited range RGB conversion
7227 * is supported, but eventually this should handle various
7228 * RGB<->YCbCr scenarios as well.
7229 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01007230static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007231{
7232 struct drm_device *dev = crtc->dev;
7233 struct drm_i915_private *dev_priv = dev->dev_private;
7234 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7235 int pipe = intel_crtc->pipe;
7236 uint16_t coeff = 0x7800; /* 1.0 */
7237
7238 /*
7239 * TODO: Check what kind of values actually come out of the pipe
7240 * with these coeff/postoff values and adjust to get the best
7241 * accuracy. Perhaps we even need to take the bpc value into
7242 * consideration.
7243 */
7244
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007245 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007246 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
7247
7248 /*
7249 * GY/GU and RY/RU should be the other way around according
7250 * to BSpec, but reality doesn't agree. Just set them up in
7251 * a way that results in the correct picture.
7252 */
7253 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
7254 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
7255
7256 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
7257 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
7258
7259 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
7260 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
7261
7262 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
7263 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
7264 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
7265
7266 if (INTEL_INFO(dev)->gen > 6) {
7267 uint16_t postoff = 0;
7268
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007269 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02007270 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007271
7272 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
7273 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
7274 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
7275
7276 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
7277 } else {
7278 uint32_t mode = CSC_MODE_YUV_TO_RGB;
7279
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007280 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007281 mode |= CSC_BLACK_SCREEN_OFFSET;
7282
7283 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
7284 }
7285}
7286
Daniel Vetter6ff93602013-04-19 11:24:36 +02007287static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007288{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007289 struct drm_device *dev = crtc->dev;
7290 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007291 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007292 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007293 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007294 uint32_t val;
7295
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007296 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007297
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007298 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007299 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7300
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007301 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007302 val |= PIPECONF_INTERLACED_ILK;
7303 else
7304 val |= PIPECONF_PROGRESSIVE;
7305
Paulo Zanoni702e7a52012-10-23 18:29:59 -02007306 I915_WRITE(PIPECONF(cpu_transcoder), val);
7307 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007308
7309 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
7310 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007311
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05307312 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007313 val = 0;
7314
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007315 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007316 case 18:
7317 val |= PIPEMISC_DITHER_6_BPC;
7318 break;
7319 case 24:
7320 val |= PIPEMISC_DITHER_8_BPC;
7321 break;
7322 case 30:
7323 val |= PIPEMISC_DITHER_10_BPC;
7324 break;
7325 case 36:
7326 val |= PIPEMISC_DITHER_12_BPC;
7327 break;
7328 default:
7329 /* Case prevented by pipe_config_set_bpp. */
7330 BUG();
7331 }
7332
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007333 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007334 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7335
7336 I915_WRITE(PIPEMISC(pipe), val);
7337 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007338}
7339
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007340static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007341 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007342 intel_clock_t *clock,
7343 bool *has_reduced_clock,
7344 intel_clock_t *reduced_clock)
7345{
7346 struct drm_device *dev = crtc->dev;
7347 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03007348 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007349 int refclk;
7350 const intel_limit_t *limit;
Daniel Vettera16af7212013-04-30 14:01:44 +02007351 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007352
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007353 is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007354
7355 refclk = ironlake_get_refclk(crtc);
7356
7357 /*
7358 * Returns a set of divisors for the desired target clock with the given
7359 * refclk, or FALSE. The returned values represent the clock equation:
7360 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7361 */
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007362 limit = intel_limit(intel_crtc, refclk);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03007363 ret = dev_priv->display.find_dpll(limit, intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007364 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007365 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007366 if (!ret)
7367 return false;
7368
7369 if (is_lvds && dev_priv->lvds_downclock_avail) {
7370 /*
7371 * Ensure we match the reduced clock's P to the target clock.
7372 * If the clocks don't match, we can't switch the display clock
7373 * by using the FP0/FP1. In such case we will disable the LVDS
7374 * downclock feature.
7375 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02007376 *has_reduced_clock =
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03007377 dev_priv->display.find_dpll(limit, intel_crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007378 dev_priv->lvds_downclock,
7379 refclk, clock,
7380 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007381 }
7382
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007383 return true;
7384}
7385
Paulo Zanonid4b19312012-11-29 11:29:32 -02007386int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7387{
7388 /*
7389 * Account for spread spectrum to avoid
7390 * oversubscribing the link. Max center spread
7391 * is 2.5%; use 5% for safety's sake.
7392 */
7393 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02007394 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02007395}
7396
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007397static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02007398{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007399 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03007400}
7401
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007402static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007403 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007404 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007405 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007406{
7407 struct drm_crtc *crtc = &intel_crtc->base;
7408 struct drm_device *dev = crtc->dev;
7409 struct drm_i915_private *dev_priv = dev->dev_private;
7410 struct intel_encoder *intel_encoder;
7411 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007412 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02007413 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007414
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007415 for_each_intel_encoder(dev, intel_encoder) {
7416 if (intel_encoder->new_crtc != to_intel_crtc(crtc))
7417 continue;
7418
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007419 switch (intel_encoder->type) {
7420 case INTEL_OUTPUT_LVDS:
7421 is_lvds = true;
7422 break;
7423 case INTEL_OUTPUT_SDVO:
7424 case INTEL_OUTPUT_HDMI:
7425 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007426 break;
Paulo Zanoni6847d712014-10-27 17:47:52 -02007427 default:
7428 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007429 }
7430
7431 num_connectors++;
7432 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007433
Chris Wilsonc1858122010-12-03 21:35:48 +00007434 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07007435 factor = 21;
7436 if (is_lvds) {
7437 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007438 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02007439 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07007440 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007441 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07007442 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00007443
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007444 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02007445 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00007446
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007447 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7448 *fp2 |= FP_CB_TUNE;
7449
Chris Wilson5eddb702010-09-11 13:48:45 +01007450 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08007451
Eric Anholta07d6782011-03-30 13:01:08 -07007452 if (is_lvds)
7453 dpll |= DPLLB_MODE_LVDS;
7454 else
7455 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007456
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007457 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007458 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007459
7460 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007461 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007462 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007463 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08007464
Eric Anholta07d6782011-03-30 13:01:08 -07007465 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007466 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007467 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007468 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007469
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007470 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07007471 case 5:
7472 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7473 break;
7474 case 7:
7475 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7476 break;
7477 case 10:
7478 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7479 break;
7480 case 14:
7481 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7482 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007483 }
7484
Daniel Vetterb4c09f32013-04-30 14:01:42 +02007485 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007486 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08007487 else
7488 dpll |= PLL_REF_INPUT_DREFCLK;
7489
Daniel Vetter959e16d2013-06-05 13:34:21 +02007490 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007491}
7492
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007493static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
7494 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007495{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007496 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007497 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007498 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03007499 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01007500 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007501 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007502
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007503 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08007504
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007505 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7506 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
7507
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007508 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007509 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007510 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007511 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7512 return -EINVAL;
7513 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01007514 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007515 if (!crtc_state->clock_set) {
7516 crtc_state->dpll.n = clock.n;
7517 crtc_state->dpll.m1 = clock.m1;
7518 crtc_state->dpll.m2 = clock.m2;
7519 crtc_state->dpll.p1 = clock.p1;
7520 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007521 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007522
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007523 /* 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 +02007524 if (crtc_state->has_pch_encoder) {
7525 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007526 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007527 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007528
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007529 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007530 &fp, &reduced_clock,
7531 has_reduced_clock ? &fp2 : NULL);
7532
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007533 crtc_state->dpll_hw_state.dpll = dpll;
7534 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007535 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007536 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007537 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007538 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007539
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007540 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007541 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03007542 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007543 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07007544 return -EINVAL;
7545 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02007546 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007547
Jani Nikulad330a952014-01-21 11:24:25 +02007548 if (is_lvds && has_reduced_clock && i915.powersave)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007549 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02007550 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007551 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007552
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007553 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007554}
7555
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007556static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7557 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02007558{
7559 struct drm_device *dev = crtc->base.dev;
7560 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007561 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02007562
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007563 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7564 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7565 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7566 & ~TU_SIZE_MASK;
7567 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7568 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7569 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7570}
7571
7572static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7573 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007574 struct intel_link_m_n *m_n,
7575 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007576{
7577 struct drm_device *dev = crtc->base.dev;
7578 struct drm_i915_private *dev_priv = dev->dev_private;
7579 enum pipe pipe = crtc->pipe;
7580
7581 if (INTEL_INFO(dev)->gen >= 5) {
7582 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7583 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7584 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7585 & ~TU_SIZE_MASK;
7586 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7587 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7588 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007589 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7590 * gen < 8) and if DRRS is supported (to make sure the
7591 * registers are not unnecessarily read).
7592 */
7593 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007594 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007595 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7596 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7597 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7598 & ~TU_SIZE_MASK;
7599 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7600 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7601 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7602 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007603 } else {
7604 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7605 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7606 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7607 & ~TU_SIZE_MASK;
7608 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7609 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7610 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7611 }
7612}
7613
7614void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007615 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007616{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007617 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007618 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7619 else
7620 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007621 &pipe_config->dp_m_n,
7622 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007623}
7624
Daniel Vetter72419202013-04-04 13:28:53 +02007625static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007626 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02007627{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007628 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007629 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02007630}
7631
Jesse Barnesbd2e2442014-11-13 17:51:47 +00007632static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007633 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00007634{
7635 struct drm_device *dev = crtc->base.dev;
7636 struct drm_i915_private *dev_priv = dev->dev_private;
7637 uint32_t tmp;
7638
7639 tmp = I915_READ(PS_CTL(crtc->pipe));
7640
7641 if (tmp & PS_ENABLE) {
7642 pipe_config->pch_pfit.enabled = true;
7643 pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
7644 pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
7645 }
7646}
7647
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007648static void
7649skylake_get_initial_plane_config(struct intel_crtc *crtc,
7650 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007651{
7652 struct drm_device *dev = crtc->base.dev;
7653 struct drm_i915_private *dev_priv = dev->dev_private;
7654 u32 val, base, offset, stride_mult;
7655 int pipe = crtc->pipe;
7656 int fourcc, pixel_format;
7657 int aligned_height;
7658 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007659 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007660
Damien Lespiaud9806c92015-01-21 14:07:19 +00007661 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007662 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007663 DRM_DEBUG_KMS("failed to alloc fb\n");
7664 return;
7665 }
7666
Damien Lespiau1b842c82015-01-21 13:50:54 +00007667 fb = &intel_fb->base;
7668
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007669 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau7f0801e2015-02-05 19:35:13 +00007670 if (!(val & PLANE_CTL_ENABLE))
7671 goto error;
7672
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007673 if (val & PLANE_CTL_TILED_MASK)
7674 plane_config->tiling = I915_TILING_X;
7675
7676 pixel_format = val & PLANE_CTL_FORMAT_MASK;
7677 fourcc = skl_format_to_fourcc(pixel_format,
7678 val & PLANE_CTL_ORDER_RGBX,
7679 val & PLANE_CTL_ALPHA_MASK);
7680 fb->pixel_format = fourcc;
7681 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
7682
7683 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
7684 plane_config->base = base;
7685
7686 offset = I915_READ(PLANE_OFFSET(pipe, 0));
7687
7688 val = I915_READ(PLANE_SIZE(pipe, 0));
7689 fb->height = ((val >> 16) & 0xfff) + 1;
7690 fb->width = ((val >> 0) & 0x1fff) + 1;
7691
7692 val = I915_READ(PLANE_STRIDE(pipe, 0));
7693 switch (plane_config->tiling) {
7694 case I915_TILING_NONE:
7695 stride_mult = 64;
7696 break;
7697 case I915_TILING_X:
7698 stride_mult = 512;
7699 break;
7700 default:
7701 MISSING_CASE(plane_config->tiling);
7702 goto error;
7703 }
7704 fb->pitches[0] = (val & 0x3ff) * stride_mult;
7705
7706 aligned_height = intel_fb_align_height(dev, fb->height,
7707 plane_config->tiling);
7708
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007709 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007710
7711 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7712 pipe_name(pipe), fb->width, fb->height,
7713 fb->bits_per_pixel, base, fb->pitches[0],
7714 plane_config->size);
7715
7716 crtc->base.primary->fb = fb;
7717 return;
7718
7719error:
7720 kfree(fb);
7721}
7722
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007723static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007724 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007725{
7726 struct drm_device *dev = crtc->base.dev;
7727 struct drm_i915_private *dev_priv = dev->dev_private;
7728 uint32_t tmp;
7729
7730 tmp = I915_READ(PF_CTL(crtc->pipe));
7731
7732 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01007733 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007734 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7735 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02007736
7737 /* We currently do not free assignements of panel fitters on
7738 * ivb/hsw (since we don't use the higher upscaling modes which
7739 * differentiates them) so just WARN about this case for now. */
7740 if (IS_GEN7(dev)) {
7741 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7742 PF_PIPE_SEL_IVB(crtc->pipe));
7743 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007744 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007745}
7746
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007747static void
7748ironlake_get_initial_plane_config(struct intel_crtc *crtc,
7749 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007750{
7751 struct drm_device *dev = crtc->base.dev;
7752 struct drm_i915_private *dev_priv = dev->dev_private;
7753 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00007754 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007755 int fourcc, pixel_format;
7756 int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007757 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007758 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007759
Damien Lespiau7f0801e2015-02-05 19:35:13 +00007760 val = I915_READ(DSPCNTR(pipe));
7761 if (!(val & DISPLAY_PLANE_ENABLE))
7762 return;
7763
Damien Lespiaud9806c92015-01-21 14:07:19 +00007764 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007765 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007766 DRM_DEBUG_KMS("failed to alloc fb\n");
7767 return;
7768 }
7769
Damien Lespiau1b842c82015-01-21 13:50:54 +00007770 fb = &intel_fb->base;
7771
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007772 if (INTEL_INFO(dev)->gen >= 4)
7773 if (val & DISPPLANE_TILED)
Damien Lespiau49af4492015-01-20 12:51:44 +00007774 plane_config->tiling = I915_TILING_X;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007775
7776 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007777 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007778 fb->pixel_format = fourcc;
7779 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007780
Damien Lespiauaeee5a42015-01-20 12:51:47 +00007781 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007782 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00007783 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007784 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00007785 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00007786 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007787 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00007788 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007789 }
7790 plane_config->base = base;
7791
7792 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007793 fb->width = ((val >> 16) & 0xfff) + 1;
7794 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007795
7796 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007797 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007798
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007799 aligned_height = intel_fb_align_height(dev, fb->height,
Damien Lespiauec2c9812015-01-20 12:51:45 +00007800 plane_config->tiling);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007801
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007802 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007803
Damien Lespiau2844a922015-01-20 12:51:48 +00007804 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7805 pipe_name(pipe), fb->width, fb->height,
7806 fb->bits_per_pixel, base, fb->pitches[0],
7807 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007808
7809 crtc->base.primary->fb = fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007810}
7811
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007812static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007813 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007814{
7815 struct drm_device *dev = crtc->base.dev;
7816 struct drm_i915_private *dev_priv = dev->dev_private;
7817 uint32_t tmp;
7818
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007819 if (!intel_display_power_is_enabled(dev_priv,
7820 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03007821 return false;
7822
Daniel Vettere143a212013-07-04 12:01:15 +02007823 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007824 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007825
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007826 tmp = I915_READ(PIPECONF(crtc->pipe));
7827 if (!(tmp & PIPECONF_ENABLE))
7828 return false;
7829
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007830 switch (tmp & PIPECONF_BPC_MASK) {
7831 case PIPECONF_6BPC:
7832 pipe_config->pipe_bpp = 18;
7833 break;
7834 case PIPECONF_8BPC:
7835 pipe_config->pipe_bpp = 24;
7836 break;
7837 case PIPECONF_10BPC:
7838 pipe_config->pipe_bpp = 30;
7839 break;
7840 case PIPECONF_12BPC:
7841 pipe_config->pipe_bpp = 36;
7842 break;
7843 default:
7844 break;
7845 }
7846
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007847 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7848 pipe_config->limited_color_range = true;
7849
Daniel Vetterab9412b2013-05-03 11:49:46 +02007850 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02007851 struct intel_shared_dpll *pll;
7852
Daniel Vetter88adfff2013-03-28 10:42:01 +01007853 pipe_config->has_pch_encoder = true;
7854
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007855 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7856 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7857 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02007858
7859 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007860
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007861 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02007862 pipe_config->shared_dpll =
7863 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007864 } else {
7865 tmp = I915_READ(PCH_DPLL_SEL);
7866 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7867 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7868 else
7869 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7870 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02007871
7872 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7873
7874 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7875 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02007876
7877 tmp = pipe_config->dpll_hw_state.dpll;
7878 pipe_config->pixel_multiplier =
7879 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7880 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03007881
7882 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007883 } else {
7884 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007885 }
7886
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007887 intel_get_pipe_timings(crtc, pipe_config);
7888
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007889 ironlake_get_pfit_config(crtc, pipe_config);
7890
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007891 return true;
7892}
7893
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007894static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7895{
7896 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007897 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007898
Damien Lespiaud3fcc802014-05-13 23:32:22 +01007899 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05007900 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007901 pipe_name(crtc->pipe));
7902
Rob Clarke2c719b2014-12-15 13:56:32 -05007903 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
7904 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
7905 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
7906 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
7907 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7908 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007909 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03007910 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05007911 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03007912 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05007913 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007914 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05007915 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007916 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05007917 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007918
Paulo Zanoni9926ada2014-04-01 19:39:47 -03007919 /*
7920 * In theory we can still leave IRQs enabled, as long as only the HPD
7921 * interrupts remain enabled. We used to check for that, but since it's
7922 * gen-specific and since we only disable LCPLL after we fully disable
7923 * the interrupts, the check below should be enough.
7924 */
Rob Clarke2c719b2014-12-15 13:56:32 -05007925 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007926}
7927
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007928static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
7929{
7930 struct drm_device *dev = dev_priv->dev;
7931
7932 if (IS_HASWELL(dev))
7933 return I915_READ(D_COMP_HSW);
7934 else
7935 return I915_READ(D_COMP_BDW);
7936}
7937
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007938static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7939{
7940 struct drm_device *dev = dev_priv->dev;
7941
7942 if (IS_HASWELL(dev)) {
7943 mutex_lock(&dev_priv->rps.hw_lock);
7944 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7945 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03007946 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007947 mutex_unlock(&dev_priv->rps.hw_lock);
7948 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007949 I915_WRITE(D_COMP_BDW, val);
7950 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007951 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007952}
7953
7954/*
7955 * This function implements pieces of two sequences from BSpec:
7956 * - Sequence for display software to disable LCPLL
7957 * - Sequence for display software to allow package C8+
7958 * The steps implemented here are just the steps that actually touch the LCPLL
7959 * register. Callers should take care of disabling all the display engine
7960 * functions, doing the mode unset, fixing interrupts, etc.
7961 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007962static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7963 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007964{
7965 uint32_t val;
7966
7967 assert_can_disable_lcpll(dev_priv);
7968
7969 val = I915_READ(LCPLL_CTL);
7970
7971 if (switch_to_fclk) {
7972 val |= LCPLL_CD_SOURCE_FCLK;
7973 I915_WRITE(LCPLL_CTL, val);
7974
7975 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7976 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7977 DRM_ERROR("Switching to FCLK failed\n");
7978
7979 val = I915_READ(LCPLL_CTL);
7980 }
7981
7982 val |= LCPLL_PLL_DISABLE;
7983 I915_WRITE(LCPLL_CTL, val);
7984 POSTING_READ(LCPLL_CTL);
7985
7986 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7987 DRM_ERROR("LCPLL still locked\n");
7988
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007989 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007990 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007991 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007992 ndelay(100);
7993
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007994 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
7995 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007996 DRM_ERROR("D_COMP RCOMP still in progress\n");
7997
7998 if (allow_power_down) {
7999 val = I915_READ(LCPLL_CTL);
8000 val |= LCPLL_POWER_DOWN_ALLOW;
8001 I915_WRITE(LCPLL_CTL, val);
8002 POSTING_READ(LCPLL_CTL);
8003 }
8004}
8005
8006/*
8007 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8008 * source.
8009 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008010static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008011{
8012 uint32_t val;
8013
8014 val = I915_READ(LCPLL_CTL);
8015
8016 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8017 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8018 return;
8019
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008020 /*
8021 * Make sure we're not on PC8 state before disabling PC8, otherwise
8022 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008023 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02008024 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03008025
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008026 if (val & LCPLL_POWER_DOWN_ALLOW) {
8027 val &= ~LCPLL_POWER_DOWN_ALLOW;
8028 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02008029 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008030 }
8031
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008032 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008033 val |= D_COMP_COMP_FORCE;
8034 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008035 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008036
8037 val = I915_READ(LCPLL_CTL);
8038 val &= ~LCPLL_PLL_DISABLE;
8039 I915_WRITE(LCPLL_CTL, val);
8040
8041 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
8042 DRM_ERROR("LCPLL not locked yet\n");
8043
8044 if (val & LCPLL_CD_SOURCE_FCLK) {
8045 val = I915_READ(LCPLL_CTL);
8046 val &= ~LCPLL_CD_SOURCE_FCLK;
8047 I915_WRITE(LCPLL_CTL, val);
8048
8049 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
8050 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
8051 DRM_ERROR("Switching back to LCPLL failed\n");
8052 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03008053
Mika Kuoppala59bad942015-01-16 11:34:40 +02008054 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008055}
8056
Paulo Zanoni765dab62014-03-07 20:08:18 -03008057/*
8058 * Package states C8 and deeper are really deep PC states that can only be
8059 * reached when all the devices on the system allow it, so even if the graphics
8060 * device allows PC8+, it doesn't mean the system will actually get to these
8061 * states. Our driver only allows PC8+ when going into runtime PM.
8062 *
8063 * The requirements for PC8+ are that all the outputs are disabled, the power
8064 * well is disabled and most interrupts are disabled, and these are also
8065 * requirements for runtime PM. When these conditions are met, we manually do
8066 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8067 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8068 * hang the machine.
8069 *
8070 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8071 * the state of some registers, so when we come back from PC8+ we need to
8072 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8073 * need to take care of the registers kept by RC6. Notice that this happens even
8074 * if we don't put the device in PCI D3 state (which is what currently happens
8075 * because of the runtime PM support).
8076 *
8077 * For more, read "Display Sequences for Package C8" on the hardware
8078 * documentation.
8079 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008080void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008081{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008082 struct drm_device *dev = dev_priv->dev;
8083 uint32_t val;
8084
Paulo Zanonic67a4702013-08-19 13:18:09 -03008085 DRM_DEBUG_KMS("Enabling package C8+\n");
8086
Paulo Zanonic67a4702013-08-19 13:18:09 -03008087 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8088 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8089 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8090 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8091 }
8092
8093 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008094 hsw_disable_lcpll(dev_priv, true, true);
8095}
8096
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008097void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008098{
8099 struct drm_device *dev = dev_priv->dev;
8100 uint32_t val;
8101
Paulo Zanonic67a4702013-08-19 13:18:09 -03008102 DRM_DEBUG_KMS("Disabling package C8+\n");
8103
8104 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008105 lpt_init_pch_refclk(dev);
8106
8107 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8108 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8109 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8110 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8111 }
8112
8113 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008114}
8115
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008116static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8117 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008118{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008119 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008120 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03008121
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008122 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02008123
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008124 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008125}
8126
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008127static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8128 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008129 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008130{
Damien Lespiau3148ade2014-11-21 16:14:56 +00008131 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008132
8133 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
8134 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
8135
8136 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00008137 case SKL_DPLL0:
8138 /*
8139 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
8140 * of the shared DPLL framework and thus needs to be read out
8141 * separately
8142 */
8143 dpll_ctl1 = I915_READ(DPLL_CTRL1);
8144 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
8145 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008146 case SKL_DPLL1:
8147 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
8148 break;
8149 case SKL_DPLL2:
8150 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
8151 break;
8152 case SKL_DPLL3:
8153 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
8154 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008155 }
8156}
8157
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008158static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8159 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008160 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008161{
8162 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8163
8164 switch (pipe_config->ddi_pll_sel) {
8165 case PORT_CLK_SEL_WRPLL1:
8166 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
8167 break;
8168 case PORT_CLK_SEL_WRPLL2:
8169 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
8170 break;
8171 }
8172}
8173
Daniel Vetter26804af2014-06-25 22:01:55 +03008174static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008175 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03008176{
8177 struct drm_device *dev = crtc->base.dev;
8178 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03008179 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03008180 enum port port;
8181 uint32_t tmp;
8182
8183 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
8184
8185 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
8186
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008187 if (IS_SKYLAKE(dev))
8188 skylake_get_ddi_pll(dev_priv, port, pipe_config);
8189 else
8190 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03008191
Daniel Vetterd452c5b2014-07-04 11:27:39 -03008192 if (pipe_config->shared_dpll >= 0) {
8193 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8194
8195 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8196 &pipe_config->dpll_hw_state));
8197 }
8198
Daniel Vetter26804af2014-06-25 22:01:55 +03008199 /*
8200 * Haswell has only FDI/PCH transcoder A. It is which is connected to
8201 * DDI E. So just check whether this pipe is wired to DDI E and whether
8202 * the PCH transcoder is on.
8203 */
Damien Lespiauca370452013-12-03 13:56:24 +00008204 if (INTEL_INFO(dev)->gen < 9 &&
8205 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03008206 pipe_config->has_pch_encoder = true;
8207
8208 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
8209 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8210 FDI_DP_PORT_WIDTH_SHIFT) + 1;
8211
8212 ironlake_get_fdi_m_n_config(crtc, pipe_config);
8213 }
8214}
8215
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008216static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008217 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008218{
8219 struct drm_device *dev = crtc->base.dev;
8220 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008221 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008222 uint32_t tmp;
8223
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008224 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02008225 POWER_DOMAIN_PIPE(crtc->pipe)))
8226 return false;
8227
Daniel Vettere143a212013-07-04 12:01:15 +02008228 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008229 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
8230
Daniel Vettereccb1402013-05-22 00:50:22 +02008231 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8232 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8233 enum pipe trans_edp_pipe;
8234 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8235 default:
8236 WARN(1, "unknown pipe linked to edp transcoder\n");
8237 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8238 case TRANS_DDI_EDP_INPUT_A_ON:
8239 trans_edp_pipe = PIPE_A;
8240 break;
8241 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8242 trans_edp_pipe = PIPE_B;
8243 break;
8244 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8245 trans_edp_pipe = PIPE_C;
8246 break;
8247 }
8248
8249 if (trans_edp_pipe == crtc->pipe)
8250 pipe_config->cpu_transcoder = TRANSCODER_EDP;
8251 }
8252
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008253 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02008254 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03008255 return false;
8256
Daniel Vettereccb1402013-05-22 00:50:22 +02008257 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008258 if (!(tmp & PIPECONF_ENABLE))
8259 return false;
8260
Daniel Vetter26804af2014-06-25 22:01:55 +03008261 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008262
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008263 intel_get_pipe_timings(crtc, pipe_config);
8264
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008265 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008266 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
8267 if (IS_SKYLAKE(dev))
8268 skylake_get_pfit_config(crtc, pipe_config);
8269 else
8270 ironlake_get_pfit_config(crtc, pipe_config);
8271 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01008272
Jesse Barnese59150d2014-01-07 13:30:45 -08008273 if (IS_HASWELL(dev))
8274 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
8275 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008276
Clint Taylorebb69c92014-09-30 10:30:22 -07008277 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
8278 pipe_config->pixel_multiplier =
8279 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
8280 } else {
8281 pipe_config->pixel_multiplier = 1;
8282 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008283
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008284 return true;
8285}
8286
Chris Wilson560b85b2010-08-07 11:01:38 +01008287static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8288{
8289 struct drm_device *dev = crtc->dev;
8290 struct drm_i915_private *dev_priv = dev->dev_private;
8291 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03008292 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01008293
Ville Syrjälädc41c152014-08-13 11:57:05 +03008294 if (base) {
8295 unsigned int width = intel_crtc->cursor_width;
8296 unsigned int height = intel_crtc->cursor_height;
8297 unsigned int stride = roundup_pow_of_two(width) * 4;
8298
8299 switch (stride) {
8300 default:
8301 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8302 width, stride);
8303 stride = 256;
8304 /* fallthrough */
8305 case 256:
8306 case 512:
8307 case 1024:
8308 case 2048:
8309 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008310 }
8311
Ville Syrjälädc41c152014-08-13 11:57:05 +03008312 cntl |= CURSOR_ENABLE |
8313 CURSOR_GAMMA_ENABLE |
8314 CURSOR_FORMAT_ARGB |
8315 CURSOR_STRIDE(stride);
8316
8317 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008318 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008319
Ville Syrjälädc41c152014-08-13 11:57:05 +03008320 if (intel_crtc->cursor_cntl != 0 &&
8321 (intel_crtc->cursor_base != base ||
8322 intel_crtc->cursor_size != size ||
8323 intel_crtc->cursor_cntl != cntl)) {
8324 /* On these chipsets we can only modify the base/size/stride
8325 * whilst the cursor is disabled.
8326 */
8327 I915_WRITE(_CURACNTR, 0);
8328 POSTING_READ(_CURACNTR);
8329 intel_crtc->cursor_cntl = 0;
8330 }
8331
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008332 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03008333 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008334 intel_crtc->cursor_base = base;
8335 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03008336
8337 if (intel_crtc->cursor_size != size) {
8338 I915_WRITE(CURSIZE, size);
8339 intel_crtc->cursor_size = size;
8340 }
8341
Chris Wilson4b0e3332014-05-30 16:35:26 +03008342 if (intel_crtc->cursor_cntl != cntl) {
8343 I915_WRITE(_CURACNTR, cntl);
8344 POSTING_READ(_CURACNTR);
8345 intel_crtc->cursor_cntl = cntl;
8346 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008347}
8348
8349static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
8350{
8351 struct drm_device *dev = crtc->dev;
8352 struct drm_i915_private *dev_priv = dev->dev_private;
8353 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8354 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008355 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01008356
Chris Wilson4b0e3332014-05-30 16:35:26 +03008357 cntl = 0;
8358 if (base) {
8359 cntl = MCURSOR_GAMMA_ENABLE;
8360 switch (intel_crtc->cursor_width) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308361 case 64:
8362 cntl |= CURSOR_MODE_64_ARGB_AX;
8363 break;
8364 case 128:
8365 cntl |= CURSOR_MODE_128_ARGB_AX;
8366 break;
8367 case 256:
8368 cntl |= CURSOR_MODE_256_ARGB_AX;
8369 break;
8370 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01008371 MISSING_CASE(intel_crtc->cursor_width);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308372 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01008373 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008374 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03008375
8376 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8377 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01008378 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008379
Matt Roper8e7d6882015-01-21 16:35:41 -08008380 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008381 cntl |= CURSOR_ROTATE_180;
8382
Chris Wilson4b0e3332014-05-30 16:35:26 +03008383 if (intel_crtc->cursor_cntl != cntl) {
8384 I915_WRITE(CURCNTR(pipe), cntl);
8385 POSTING_READ(CURCNTR(pipe));
8386 intel_crtc->cursor_cntl = cntl;
8387 }
8388
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008389 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008390 I915_WRITE(CURBASE(pipe), base);
8391 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008392
8393 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008394}
8395
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008396/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01008397static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8398 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008399{
8400 struct drm_device *dev = crtc->dev;
8401 struct drm_i915_private *dev_priv = dev->dev_private;
8402 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8403 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07008404 int x = crtc->cursor_x;
8405 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008406 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008407
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008408 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008409 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008410
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008411 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008412 base = 0;
8413
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008414 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008415 base = 0;
8416
8417 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008418 if (x + intel_crtc->cursor_width <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008419 base = 0;
8420
8421 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8422 x = -x;
8423 }
8424 pos |= x << CURSOR_X_SHIFT;
8425
8426 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008427 if (y + intel_crtc->cursor_height <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008428 base = 0;
8429
8430 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8431 y = -y;
8432 }
8433 pos |= y << CURSOR_Y_SHIFT;
8434
Chris Wilson4b0e3332014-05-30 16:35:26 +03008435 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008436 return;
8437
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008438 I915_WRITE(CURPOS(pipe), pos);
8439
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008440 /* ILK+ do this automagically */
8441 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08008442 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008443 base += (intel_crtc->cursor_height *
8444 intel_crtc->cursor_width - 1) * 4;
8445 }
8446
Ville Syrjälä8ac54662014-08-12 19:39:54 +03008447 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008448 i845_update_cursor(crtc, base);
8449 else
8450 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008451}
8452
Ville Syrjälädc41c152014-08-13 11:57:05 +03008453static bool cursor_size_ok(struct drm_device *dev,
8454 uint32_t width, uint32_t height)
8455{
8456 if (width == 0 || height == 0)
8457 return false;
8458
8459 /*
8460 * 845g/865g are special in that they are only limited by
8461 * the width of their cursors, the height is arbitrary up to
8462 * the precision of the register. Everything else requires
8463 * square cursors, limited to a few power-of-two sizes.
8464 */
8465 if (IS_845G(dev) || IS_I865G(dev)) {
8466 if ((width & 63) != 0)
8467 return false;
8468
8469 if (width > (IS_845G(dev) ? 64 : 512))
8470 return false;
8471
8472 if (height > 1023)
8473 return false;
8474 } else {
8475 switch (width | height) {
8476 case 256:
8477 case 128:
8478 if (IS_GEN2(dev))
8479 return false;
8480 case 64:
8481 break;
8482 default:
8483 return false;
8484 }
8485 }
8486
8487 return true;
8488}
8489
Jesse Barnes79e53942008-11-07 14:24:08 -08008490static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01008491 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08008492{
James Simmons72034252010-08-03 01:33:19 +01008493 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08008494 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008495
James Simmons72034252010-08-03 01:33:19 +01008496 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008497 intel_crtc->lut_r[i] = red[i] >> 8;
8498 intel_crtc->lut_g[i] = green[i] >> 8;
8499 intel_crtc->lut_b[i] = blue[i] >> 8;
8500 }
8501
8502 intel_crtc_load_lut(crtc);
8503}
8504
Jesse Barnes79e53942008-11-07 14:24:08 -08008505/* VESA 640x480x72Hz mode to set on the pipe */
8506static struct drm_display_mode load_detect_mode = {
8507 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8508 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8509};
8510
Daniel Vettera8bb6812014-02-10 18:00:39 +01008511struct drm_framebuffer *
8512__intel_framebuffer_create(struct drm_device *dev,
8513 struct drm_mode_fb_cmd2 *mode_cmd,
8514 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01008515{
8516 struct intel_framebuffer *intel_fb;
8517 int ret;
8518
8519 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8520 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03008521 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01008522 return ERR_PTR(-ENOMEM);
8523 }
8524
8525 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008526 if (ret)
8527 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01008528
8529 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008530err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03008531 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008532 kfree(intel_fb);
8533
8534 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01008535}
8536
Daniel Vetterb5ea6422014-03-02 21:18:00 +01008537static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01008538intel_framebuffer_create(struct drm_device *dev,
8539 struct drm_mode_fb_cmd2 *mode_cmd,
8540 struct drm_i915_gem_object *obj)
8541{
8542 struct drm_framebuffer *fb;
8543 int ret;
8544
8545 ret = i915_mutex_lock_interruptible(dev);
8546 if (ret)
8547 return ERR_PTR(ret);
8548 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8549 mutex_unlock(&dev->struct_mutex);
8550
8551 return fb;
8552}
8553
Chris Wilsond2dff872011-04-19 08:36:26 +01008554static u32
8555intel_framebuffer_pitch_for_width(int width, int bpp)
8556{
8557 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8558 return ALIGN(pitch, 64);
8559}
8560
8561static u32
8562intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8563{
8564 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02008565 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01008566}
8567
8568static struct drm_framebuffer *
8569intel_framebuffer_create_for_mode(struct drm_device *dev,
8570 struct drm_display_mode *mode,
8571 int depth, int bpp)
8572{
8573 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00008574 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01008575
8576 obj = i915_gem_alloc_object(dev,
8577 intel_framebuffer_size_for_mode(mode, bpp));
8578 if (obj == NULL)
8579 return ERR_PTR(-ENOMEM);
8580
8581 mode_cmd.width = mode->hdisplay;
8582 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008583 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8584 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00008585 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01008586
8587 return intel_framebuffer_create(dev, &mode_cmd, obj);
8588}
8589
8590static struct drm_framebuffer *
8591mode_fits_in_fbdev(struct drm_device *dev,
8592 struct drm_display_mode *mode)
8593{
Daniel Vetter4520f532013-10-09 09:18:51 +02008594#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01008595 struct drm_i915_private *dev_priv = dev->dev_private;
8596 struct drm_i915_gem_object *obj;
8597 struct drm_framebuffer *fb;
8598
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008599 if (!dev_priv->fbdev)
8600 return NULL;
8601
8602 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01008603 return NULL;
8604
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008605 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008606 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01008607
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008608 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008609 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8610 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01008611 return NULL;
8612
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008613 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01008614 return NULL;
8615
8616 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02008617#else
8618 return NULL;
8619#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01008620}
8621
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008622bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01008623 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05008624 struct intel_load_detect_pipe *old,
8625 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08008626{
8627 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008628 struct intel_encoder *intel_encoder =
8629 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08008630 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008631 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08008632 struct drm_crtc *crtc = NULL;
8633 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02008634 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05008635 struct drm_mode_config *config = &dev->mode_config;
8636 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008637
Chris Wilsond2dff872011-04-19 08:36:26 +01008638 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008639 connector->base.id, connector->name,
Jani Nikula8e329a02014-06-03 14:56:21 +03008640 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008641
Rob Clark51fd3712013-11-19 12:10:12 -05008642retry:
8643 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8644 if (ret)
8645 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02008646
Jesse Barnes79e53942008-11-07 14:24:08 -08008647 /*
8648 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01008649 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008650 * - if the connector already has an assigned crtc, use it (but make
8651 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01008652 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008653 * - try to find the first unused crtc that can drive this connector,
8654 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08008655 */
8656
8657 /* See if we already have a CRTC for this connector */
8658 if (encoder->crtc) {
8659 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01008660
Rob Clark51fd3712013-11-19 12:10:12 -05008661 ret = drm_modeset_lock(&crtc->mutex, ctx);
8662 if (ret)
8663 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01008664 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
8665 if (ret)
8666 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01008667
Daniel Vetter24218aa2012-08-12 19:27:11 +02008668 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008669 old->load_detect_temp = false;
8670
8671 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008672 if (connector->dpms != DRM_MODE_DPMS_ON)
8673 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01008674
Chris Wilson71731882011-04-19 23:10:58 +01008675 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08008676 }
8677
8678 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008679 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008680 i++;
8681 if (!(encoder->possible_crtcs & (1 << i)))
8682 continue;
Ville Syrjäläa4592492014-08-11 13:15:36 +03008683 if (possible_crtc->enabled)
8684 continue;
8685 /* This can occur when applying the pipe A quirk on resume. */
8686 if (to_intel_crtc(possible_crtc)->new_enabled)
8687 continue;
8688
8689 crtc = possible_crtc;
8690 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008691 }
8692
8693 /*
8694 * If we didn't find an unused CRTC, don't use any.
8695 */
8696 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01008697 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05008698 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08008699 }
8700
Rob Clark51fd3712013-11-19 12:10:12 -05008701 ret = drm_modeset_lock(&crtc->mutex, ctx);
8702 if (ret)
8703 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01008704 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
8705 if (ret)
8706 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02008707 intel_encoder->new_crtc = to_intel_crtc(crtc);
8708 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008709
8710 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008711 intel_crtc->new_enabled = true;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008712 intel_crtc->new_config = intel_crtc->config;
Daniel Vetter24218aa2012-08-12 19:27:11 +02008713 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008714 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01008715 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08008716
Chris Wilson64927112011-04-20 07:25:26 +01008717 if (!mode)
8718 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08008719
Chris Wilsond2dff872011-04-19 08:36:26 +01008720 /* We need a framebuffer large enough to accommodate all accesses
8721 * that the plane may generate whilst we perform load detection.
8722 * We can not rely on the fbcon either being present (we get called
8723 * during its initialisation to detect all boot displays, or it may
8724 * not even exist) or that it is large enough to satisfy the
8725 * requested mode.
8726 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02008727 fb = mode_fits_in_fbdev(dev, mode);
8728 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008729 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008730 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8731 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01008732 } else
8733 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008734 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008735 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008736 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008737 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008738
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008739 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01008740 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01008741 if (old->release_fb)
8742 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008743 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008744 }
Daniel Vetter9128b042015-03-03 17:31:21 +01008745 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +01008746
Jesse Barnes79e53942008-11-07 14:24:08 -08008747 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008748 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01008749 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008750
8751 fail:
8752 intel_crtc->new_enabled = crtc->enabled;
8753 if (intel_crtc->new_enabled)
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008754 intel_crtc->new_config = intel_crtc->config;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008755 else
8756 intel_crtc->new_config = NULL;
Rob Clark51fd3712013-11-19 12:10:12 -05008757fail_unlock:
8758 if (ret == -EDEADLK) {
8759 drm_modeset_backoff(ctx);
8760 goto retry;
8761 }
8762
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008763 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08008764}
8765
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008766void intel_release_load_detect_pipe(struct drm_connector *connector,
Ville Syrjälä208bf9f2014-08-11 13:15:35 +03008767 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08008768{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008769 struct intel_encoder *intel_encoder =
8770 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01008771 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01008772 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008773 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008774
Chris Wilsond2dff872011-04-19 08:36:26 +01008775 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008776 connector->base.id, connector->name,
Jani Nikula8e329a02014-06-03 14:56:21 +03008777 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008778
Chris Wilson8261b192011-04-19 23:18:09 +01008779 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02008780 to_intel_connector(connector)->new_encoder = NULL;
8781 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008782 intel_crtc->new_enabled = false;
8783 intel_crtc->new_config = NULL;
Daniel Vetterfc303102012-07-09 10:40:58 +02008784 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01008785
Daniel Vetter36206362012-12-10 20:42:17 +01008786 if (old->release_fb) {
8787 drm_framebuffer_unregister_private(old->release_fb);
8788 drm_framebuffer_unreference(old->release_fb);
8789 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008790
Chris Wilson0622a532011-04-21 09:32:11 +01008791 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008792 }
8793
Eric Anholtc751ce42010-03-25 11:48:48 -07008794 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008795 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8796 connector->funcs->dpms(connector, old->dpms_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008797}
8798
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008799static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008800 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008801{
8802 struct drm_i915_private *dev_priv = dev->dev_private;
8803 u32 dpll = pipe_config->dpll_hw_state.dpll;
8804
8805 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008806 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008807 else if (HAS_PCH_SPLIT(dev))
8808 return 120000;
8809 else if (!IS_GEN2(dev))
8810 return 96000;
8811 else
8812 return 48000;
8813}
8814
Jesse Barnes79e53942008-11-07 14:24:08 -08008815/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008816static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008817 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08008818{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008819 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008820 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008821 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008822 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008823 u32 fp;
8824 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008825 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08008826
8827 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03008828 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008829 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03008830 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008831
8832 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008833 if (IS_PINEVIEW(dev)) {
8834 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8835 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08008836 } else {
8837 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8838 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8839 }
8840
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008841 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008842 if (IS_PINEVIEW(dev))
8843 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8844 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08008845 else
8846 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08008847 DPLL_FPA01_P1_POST_DIV_SHIFT);
8848
8849 switch (dpll & DPLL_MODE_MASK) {
8850 case DPLLB_MODE_DAC_SERIAL:
8851 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8852 5 : 10;
8853 break;
8854 case DPLLB_MODE_LVDS:
8855 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8856 7 : 14;
8857 break;
8858 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08008859 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08008860 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008861 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008862 }
8863
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008864 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008865 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008866 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008867 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008868 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02008869 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008870 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08008871
8872 if (is_lvds) {
8873 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8874 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008875
8876 if (lvds & LVDS_CLKB_POWER_UP)
8877 clock.p2 = 7;
8878 else
8879 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08008880 } else {
8881 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8882 clock.p1 = 2;
8883 else {
8884 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8885 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8886 }
8887 if (dpll & PLL_P2_DIVIDE_BY_4)
8888 clock.p2 = 4;
8889 else
8890 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08008891 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008892
8893 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008894 }
8895
Ville Syrjälä18442d02013-09-13 16:00:08 +03008896 /*
8897 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01008898 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03008899 * encoder's get_config() function.
8900 */
8901 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008902}
8903
Ville Syrjälä6878da02013-09-13 15:59:11 +03008904int intel_dotclock_calculate(int link_freq,
8905 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008906{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008907 /*
8908 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008909 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008910 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008911 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008912 *
8913 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008914 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08008915 */
8916
Ville Syrjälä6878da02013-09-13 15:59:11 +03008917 if (!m_n->link_n)
8918 return 0;
8919
8920 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8921}
8922
Ville Syrjälä18442d02013-09-13 16:00:08 +03008923static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008924 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03008925{
8926 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008927
8928 /* read out port_clock from the DPLL */
8929 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03008930
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008931 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03008932 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01008933 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03008934 * agree once we know their relationship in the encoder's
8935 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008936 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008937 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03008938 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8939 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08008940}
8941
8942/** Returns the currently programmed mode of the given pipe. */
8943struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8944 struct drm_crtc *crtc)
8945{
Jesse Barnes548f2452011-02-17 10:40:53 -08008946 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008947 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008948 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008949 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008950 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008951 int htot = I915_READ(HTOTAL(cpu_transcoder));
8952 int hsync = I915_READ(HSYNC(cpu_transcoder));
8953 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8954 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03008955 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08008956
8957 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8958 if (!mode)
8959 return NULL;
8960
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008961 /*
8962 * Construct a pipe_config sufficient for getting the clock info
8963 * back out of crtc_clock_get.
8964 *
8965 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8966 * to use a real value here instead.
8967 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03008968 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008969 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008970 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8971 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8972 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008973 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8974
Ville Syrjälä773ae032013-09-23 17:48:20 +03008975 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08008976 mode->hdisplay = (htot & 0xffff) + 1;
8977 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8978 mode->hsync_start = (hsync & 0xffff) + 1;
8979 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8980 mode->vdisplay = (vtot & 0xffff) + 1;
8981 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8982 mode->vsync_start = (vsync & 0xffff) + 1;
8983 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8984
8985 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008986
8987 return mode;
8988}
8989
Jesse Barnes652c3932009-08-17 13:31:43 -07008990static void intel_decrease_pllclock(struct drm_crtc *crtc)
8991{
8992 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03008993 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07008994 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07008995
Sonika Jindalbaff2962014-07-22 11:16:35 +05308996 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008997 return;
8998
8999 if (!dev_priv->lvds_downclock_avail)
9000 return;
9001
9002 /*
9003 * Since this is called by a timer, we should never get here in
9004 * the manual case.
9005 */
9006 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01009007 int pipe = intel_crtc->pipe;
9008 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02009009 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01009010
Zhao Yakui44d98a62009-10-09 11:39:40 +08009011 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009012
Sean Paul8ac5a6d2012-02-13 13:14:51 -05009013 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009014
Chris Wilson074b5e12012-05-02 12:07:06 +01009015 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07009016 dpll |= DISPLAY_RATE_SELECT_FPA1;
9017 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009018 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009019 dpll = I915_READ(dpll_reg);
9020 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08009021 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009022 }
9023
9024}
9025
Chris Wilsonf047e392012-07-21 12:31:41 +01009026void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07009027{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009028 struct drm_i915_private *dev_priv = dev->dev_private;
9029
Chris Wilsonf62a0072014-02-21 17:55:39 +00009030 if (dev_priv->mm.busy)
9031 return;
9032
Paulo Zanoni43694d62014-03-07 20:08:08 -03009033 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009034 i915_update_gfx_val(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +00009035 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +01009036}
9037
9038void intel_mark_idle(struct drm_device *dev)
9039{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009040 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00009041 struct drm_crtc *crtc;
9042
Chris Wilsonf62a0072014-02-21 17:55:39 +00009043 if (!dev_priv->mm.busy)
9044 return;
9045
9046 dev_priv->mm.busy = false;
9047
Jani Nikulad330a952014-01-21 11:24:25 +02009048 if (!i915.powersave)
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03009049 goto out;
Chris Wilson725a5b52013-01-08 11:02:57 +00009050
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009051 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07009052 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +00009053 continue;
9054
9055 intel_decrease_pllclock(crtc);
9056 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009057
Damien Lespiau3d13ef22014-02-07 19:12:47 +00009058 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009059 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03009060
9061out:
Paulo Zanoni43694d62014-03-07 20:08:08 -03009062 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01009063}
9064
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009065static void intel_crtc_set_state(struct intel_crtc *crtc,
9066 struct intel_crtc_state *crtc_state)
9067{
9068 kfree(crtc->config);
9069 crtc->config = crtc_state;
Ander Conselvan de Oliveira16f3f652015-01-15 14:55:27 +02009070 crtc->base.state = &crtc_state->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009071}
9072
Jesse Barnes79e53942008-11-07 14:24:08 -08009073static void intel_crtc_destroy(struct drm_crtc *crtc)
9074{
9075 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009076 struct drm_device *dev = crtc->dev;
9077 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +02009078
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009079 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009080 work = intel_crtc->unpin_work;
9081 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009082 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009083
9084 if (work) {
9085 cancel_work_sync(&work->work);
9086 kfree(work);
9087 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009088
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009089 intel_crtc_set_state(intel_crtc, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08009090 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009091
Jesse Barnes79e53942008-11-07 14:24:08 -08009092 kfree(intel_crtc);
9093}
9094
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009095static void intel_unpin_work_fn(struct work_struct *__work)
9096{
9097 struct intel_unpin_work *work =
9098 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009099 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +02009100 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009101
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009102 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01009103 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00009104 drm_gem_object_unreference(&work->pending_flip_obj->base);
9105 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00009106
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02009107 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +00009108
9109 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +00009110 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009111 mutex_unlock(&dev->struct_mutex);
9112
Daniel Vetterf99d7062014-06-19 16:01:59 +02009113 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
9114
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009115 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9116 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9117
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009118 kfree(work);
9119}
9120
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009121static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01009122 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009123{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009124 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9125 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009126 unsigned long flags;
9127
9128 /* Ignore early vblank irqs */
9129 if (intel_crtc == NULL)
9130 return;
9131
Daniel Vetterf3260382014-09-15 14:55:23 +02009132 /*
9133 * This is called both by irq handlers and the reset code (to complete
9134 * lost pageflips) so needs the full irqsave spinlocks.
9135 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009136 spin_lock_irqsave(&dev->event_lock, flags);
9137 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00009138
9139 /* Ensure we don't miss a work->pending update ... */
9140 smp_rmb();
9141
9142 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009143 spin_unlock_irqrestore(&dev->event_lock, flags);
9144 return;
9145 }
9146
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009147 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01009148
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009149 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009150}
9151
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009152void intel_finish_page_flip(struct drm_device *dev, int pipe)
9153{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009154 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009155 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9156
Mario Kleiner49b14a52010-12-09 07:00:07 +01009157 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009158}
9159
9160void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9161{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009162 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009163 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9164
Mario Kleiner49b14a52010-12-09 07:00:07 +01009165 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009166}
9167
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009168/* Is 'a' after or equal to 'b'? */
9169static bool g4x_flip_count_after_eq(u32 a, u32 b)
9170{
9171 return !((a - b) & 0x80000000);
9172}
9173
9174static bool page_flip_finished(struct intel_crtc *crtc)
9175{
9176 struct drm_device *dev = crtc->base.dev;
9177 struct drm_i915_private *dev_priv = dev->dev_private;
9178
Ville Syrjäläbdfa7542014-05-27 21:33:09 +03009179 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
9180 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
9181 return true;
9182
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009183 /*
9184 * The relevant registers doen't exist on pre-ctg.
9185 * As the flip done interrupt doesn't trigger for mmio
9186 * flips on gmch platforms, a flip count check isn't
9187 * really needed there. But since ctg has the registers,
9188 * include it in the check anyway.
9189 */
9190 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9191 return true;
9192
9193 /*
9194 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9195 * used the same base address. In that case the mmio flip might
9196 * have completed, but the CS hasn't even executed the flip yet.
9197 *
9198 * A flip count check isn't enough as the CS might have updated
9199 * the base address just after start of vblank, but before we
9200 * managed to process the interrupt. This means we'd complete the
9201 * CS flip too soon.
9202 *
9203 * Combining both checks should get us a good enough result. It may
9204 * still happen that the CS flip has been executed, but has not
9205 * yet actually completed. But in case the base address is the same
9206 * anyway, we don't really care.
9207 */
9208 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9209 crtc->unpin_work->gtt_offset &&
9210 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9211 crtc->unpin_work->flip_count);
9212}
9213
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009214void intel_prepare_page_flip(struct drm_device *dev, int plane)
9215{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009216 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009217 struct intel_crtc *intel_crtc =
9218 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9219 unsigned long flags;
9220
Daniel Vetterf3260382014-09-15 14:55:23 +02009221
9222 /*
9223 * This is called both by irq handlers and the reset code (to complete
9224 * lost pageflips) so needs the full irqsave spinlocks.
9225 *
9226 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +00009227 * generate a page-flip completion irq, i.e. every modeset
9228 * is also accompanied by a spurious intel_prepare_page_flip().
9229 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009230 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009231 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +00009232 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009233 spin_unlock_irqrestore(&dev->event_lock, flags);
9234}
9235
Robin Schroereba905b2014-05-18 02:24:50 +02009236static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +00009237{
9238 /* Ensure that the work item is consistent when activating it ... */
9239 smp_wmb();
9240 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9241 /* and that it is marked active as soon as the irq could fire. */
9242 smp_wmb();
9243}
9244
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009245static int intel_gen2_queue_flip(struct drm_device *dev,
9246 struct drm_crtc *crtc,
9247 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009248 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009249 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009250 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009251{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009252 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009253 u32 flip_mask;
9254 int ret;
9255
Daniel Vetter6d90c952012-04-26 23:28:05 +02009256 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009257 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009258 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009259
9260 /* Can't queue multiple flips, so wait for the previous
9261 * one to finish before executing the next.
9262 */
9263 if (intel_crtc->plane)
9264 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9265 else
9266 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009267 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9268 intel_ring_emit(ring, MI_NOOP);
9269 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9270 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9271 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009272 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009273 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00009274
9275 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009276 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009277 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009278}
9279
9280static int intel_gen3_queue_flip(struct drm_device *dev,
9281 struct drm_crtc *crtc,
9282 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009283 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009284 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009285 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009286{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009287 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009288 u32 flip_mask;
9289 int ret;
9290
Daniel Vetter6d90c952012-04-26 23:28:05 +02009291 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009292 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009293 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009294
9295 if (intel_crtc->plane)
9296 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9297 else
9298 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009299 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9300 intel_ring_emit(ring, MI_NOOP);
9301 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9302 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9303 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009304 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009305 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009306
Chris Wilsone7d841c2012-12-03 11:36:30 +00009307 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009308 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009309 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009310}
9311
9312static int intel_gen4_queue_flip(struct drm_device *dev,
9313 struct drm_crtc *crtc,
9314 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009315 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009316 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009317 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009318{
9319 struct drm_i915_private *dev_priv = dev->dev_private;
9320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9321 uint32_t pf, pipesrc;
9322 int ret;
9323
Daniel Vetter6d90c952012-04-26 23:28:05 +02009324 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009325 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009326 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009327
9328 /* i965+ uses the linear or tiled offsets from the
9329 * Display Registers (which do not change across a page-flip)
9330 * so we need only reprogram the base address.
9331 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02009332 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9333 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9334 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009335 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +02009336 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009337
9338 /* XXX Enabling the panel-fitter across page-flip is so far
9339 * untested on non-native modes, so ignore it for now.
9340 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9341 */
9342 pf = 0;
9343 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009344 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009345
9346 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009347 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009348 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009349}
9350
9351static int intel_gen6_queue_flip(struct drm_device *dev,
9352 struct drm_crtc *crtc,
9353 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009354 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009355 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009356 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009357{
9358 struct drm_i915_private *dev_priv = dev->dev_private;
9359 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9360 uint32_t pf, pipesrc;
9361 int ret;
9362
Daniel Vetter6d90c952012-04-26 23:28:05 +02009363 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009364 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009365 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009366
Daniel Vetter6d90c952012-04-26 23:28:05 +02009367 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9368 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9369 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009370 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009371
Chris Wilson99d9acd2012-04-17 20:37:00 +01009372 /* Contrary to the suggestions in the documentation,
9373 * "Enable Panel Fitter" does not seem to be required when page
9374 * flipping with a non-native mode, and worse causes a normal
9375 * modeset to fail.
9376 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9377 */
9378 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009379 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009380 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009381
9382 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009383 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009384 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009385}
9386
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009387static int intel_gen7_queue_flip(struct drm_device *dev,
9388 struct drm_crtc *crtc,
9389 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009390 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009391 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009392 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009393{
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009394 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009395 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01009396 int len, ret;
9397
Robin Schroereba905b2014-05-18 02:24:50 +02009398 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009399 case PLANE_A:
9400 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9401 break;
9402 case PLANE_B:
9403 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9404 break;
9405 case PLANE_C:
9406 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9407 break;
9408 default:
9409 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009410 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009411 }
9412
Chris Wilsonffe74d72013-08-26 20:58:12 +01009413 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +01009414 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +01009415 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +01009416 /*
9417 * On Gen 8, SRM is now taking an extra dword to accommodate
9418 * 48bits addresses, and we need a NOOP for the batch size to
9419 * stay even.
9420 */
9421 if (IS_GEN8(dev))
9422 len += 2;
9423 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009424
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009425 /*
9426 * BSpec MI_DISPLAY_FLIP for IVB:
9427 * "The full packet must be contained within the same cache line."
9428 *
9429 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9430 * cacheline, if we ever start emitting more commands before
9431 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9432 * then do the cacheline alignment, and finally emit the
9433 * MI_DISPLAY_FLIP.
9434 */
9435 ret = intel_ring_cacheline_align(ring);
9436 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009437 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009438
Chris Wilsonffe74d72013-08-26 20:58:12 +01009439 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009440 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009441 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009442
Chris Wilsonffe74d72013-08-26 20:58:12 +01009443 /* Unmask the flip-done completion message. Note that the bspec says that
9444 * we should do this for both the BCS and RCS, and that we must not unmask
9445 * more than one flip event at any time (or ensure that one flip message
9446 * can be sent by waiting for flip-done prior to queueing new flips).
9447 * Experimentation says that BCS works despite DERRMR masking all
9448 * flip-done completion events and that unmasking all planes at once
9449 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9450 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9451 */
9452 if (ring->id == RCS) {
9453 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9454 intel_ring_emit(ring, DERRMR);
9455 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9456 DERRMR_PIPEB_PRI_FLIP_DONE |
9457 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +01009458 if (IS_GEN8(dev))
9459 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9460 MI_SRM_LRM_GLOBAL_GTT);
9461 else
9462 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9463 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +01009464 intel_ring_emit(ring, DERRMR);
9465 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +01009466 if (IS_GEN8(dev)) {
9467 intel_ring_emit(ring, 0);
9468 intel_ring_emit(ring, MI_NOOP);
9469 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009470 }
9471
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009472 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009473 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009474 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009475 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00009476
9477 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009478 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009479 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009480}
9481
Sourab Gupta84c33a62014-06-02 16:47:17 +05309482static bool use_mmio_flip(struct intel_engine_cs *ring,
9483 struct drm_i915_gem_object *obj)
9484{
9485 /*
9486 * This is not being used for older platforms, because
9487 * non-availability of flip done interrupt forces us to use
9488 * CS flips. Older platforms derive flip done using some clever
9489 * tricks involving the flip_pending status bits and vblank irqs.
9490 * So using MMIO flips there would disrupt this mechanism.
9491 */
9492
Chris Wilson8e09bf82014-07-08 10:40:30 +01009493 if (ring == NULL)
9494 return true;
9495
Sourab Gupta84c33a62014-06-02 16:47:17 +05309496 if (INTEL_INFO(ring->dev)->gen < 5)
9497 return false;
9498
9499 if (i915.use_mmio_flip < 0)
9500 return false;
9501 else if (i915.use_mmio_flip > 0)
9502 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +01009503 else if (i915.enable_execlists)
9504 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +05309505 else
John Harrison41c52412014-11-24 18:49:43 +00009506 return ring != i915_gem_request_get_ring(obj->last_read_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309507}
9508
Damien Lespiauff944562014-11-20 14:58:16 +00009509static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
9510{
9511 struct drm_device *dev = intel_crtc->base.dev;
9512 struct drm_i915_private *dev_priv = dev->dev_private;
9513 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
9514 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
9515 struct drm_i915_gem_object *obj = intel_fb->obj;
9516 const enum pipe pipe = intel_crtc->pipe;
9517 u32 ctl, stride;
9518
9519 ctl = I915_READ(PLANE_CTL(pipe, 0));
9520 ctl &= ~PLANE_CTL_TILED_MASK;
9521 if (obj->tiling_mode == I915_TILING_X)
9522 ctl |= PLANE_CTL_TILED_X;
9523
9524 /*
9525 * The stride is either expressed as a multiple of 64 bytes chunks for
9526 * linear buffers or in number of tiles for tiled buffers.
9527 */
9528 stride = fb->pitches[0] >> 6;
9529 if (obj->tiling_mode == I915_TILING_X)
9530 stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
9531
9532 /*
9533 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
9534 * PLANE_SURF updates, the update is then guaranteed to be atomic.
9535 */
9536 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
9537 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
9538
9539 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
9540 POSTING_READ(PLANE_SURF(pipe, 0));
9541}
9542
9543static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +05309544{
9545 struct drm_device *dev = intel_crtc->base.dev;
9546 struct drm_i915_private *dev_priv = dev->dev_private;
9547 struct intel_framebuffer *intel_fb =
9548 to_intel_framebuffer(intel_crtc->base.primary->fb);
9549 struct drm_i915_gem_object *obj = intel_fb->obj;
9550 u32 dspcntr;
9551 u32 reg;
9552
Sourab Gupta84c33a62014-06-02 16:47:17 +05309553 reg = DSPCNTR(intel_crtc->plane);
9554 dspcntr = I915_READ(reg);
9555
Damien Lespiauc5d97472014-10-25 00:11:11 +01009556 if (obj->tiling_mode != I915_TILING_NONE)
9557 dspcntr |= DISPPLANE_TILED;
9558 else
9559 dspcntr &= ~DISPPLANE_TILED;
9560
Sourab Gupta84c33a62014-06-02 16:47:17 +05309561 I915_WRITE(reg, dspcntr);
9562
9563 I915_WRITE(DSPSURF(intel_crtc->plane),
9564 intel_crtc->unpin_work->gtt_offset);
9565 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +02009566
Damien Lespiauff944562014-11-20 14:58:16 +00009567}
9568
9569/*
9570 * XXX: This is the temporary way to update the plane registers until we get
9571 * around to using the usual plane update functions for MMIO flips
9572 */
9573static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9574{
9575 struct drm_device *dev = intel_crtc->base.dev;
9576 bool atomic_update;
9577 u32 start_vbl_count;
9578
9579 intel_mark_page_flip_active(intel_crtc);
9580
9581 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
9582
9583 if (INTEL_INFO(dev)->gen >= 9)
9584 skl_do_mmio_flip(intel_crtc);
9585 else
9586 /* use_mmio_flip() retricts MMIO flips to ilk+ */
9587 ilk_do_mmio_flip(intel_crtc);
9588
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +02009589 if (atomic_update)
9590 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309591}
9592
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +02009593static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +05309594{
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009595 struct intel_crtc *crtc =
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +02009596 container_of(work, struct intel_crtc, mmio_flip.work);
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009597 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +05309598
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009599 mmio_flip = &crtc->mmio_flip;
9600 if (mmio_flip->req)
John Harrison9c654812014-11-24 18:49:35 +00009601 WARN_ON(__i915_wait_request(mmio_flip->req,
9602 crtc->reset_counter,
9603 false, NULL, NULL) != 0);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309604
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009605 intel_do_mmio_flip(crtc);
9606 if (mmio_flip->req) {
9607 mutex_lock(&crtc->base.dev->struct_mutex);
John Harrison146d84f2014-12-05 13:49:33 +00009608 i915_gem_request_assign(&mmio_flip->req, NULL);
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009609 mutex_unlock(&crtc->base.dev->struct_mutex);
9610 }
Sourab Gupta84c33a62014-06-02 16:47:17 +05309611}
9612
9613static int intel_queue_mmio_flip(struct drm_device *dev,
9614 struct drm_crtc *crtc,
9615 struct drm_framebuffer *fb,
9616 struct drm_i915_gem_object *obj,
9617 struct intel_engine_cs *ring,
9618 uint32_t flags)
9619{
Sourab Gupta84c33a62014-06-02 16:47:17 +05309620 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309621
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009622 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
9623 obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309624
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +02009625 schedule_work(&intel_crtc->mmio_flip.work);
9626
Sourab Gupta84c33a62014-06-02 16:47:17 +05309627 return 0;
9628}
9629
Damien Lespiau830c81d2014-11-13 17:51:46 +00009630static int intel_gen9_queue_flip(struct drm_device *dev,
9631 struct drm_crtc *crtc,
9632 struct drm_framebuffer *fb,
9633 struct drm_i915_gem_object *obj,
9634 struct intel_engine_cs *ring,
9635 uint32_t flags)
9636{
9637 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9638 uint32_t plane = 0, stride;
9639 int ret;
9640
9641 switch(intel_crtc->pipe) {
9642 case PIPE_A:
9643 plane = MI_DISPLAY_FLIP_SKL_PLANE_1_A;
9644 break;
9645 case PIPE_B:
9646 plane = MI_DISPLAY_FLIP_SKL_PLANE_1_B;
9647 break;
9648 case PIPE_C:
9649 plane = MI_DISPLAY_FLIP_SKL_PLANE_1_C;
9650 break;
9651 default:
9652 WARN_ONCE(1, "unknown plane in flip command\n");
9653 return -ENODEV;
9654 }
9655
9656 switch (obj->tiling_mode) {
9657 case I915_TILING_NONE:
9658 stride = fb->pitches[0] >> 6;
9659 break;
9660 case I915_TILING_X:
9661 stride = fb->pitches[0] >> 9;
9662 break;
9663 default:
9664 WARN_ONCE(1, "unknown tiling in flip command\n");
9665 return -ENODEV;
9666 }
9667
9668 ret = intel_ring_begin(ring, 10);
9669 if (ret)
9670 return ret;
9671
9672 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9673 intel_ring_emit(ring, DERRMR);
9674 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9675 DERRMR_PIPEB_PRI_FLIP_DONE |
9676 DERRMR_PIPEC_PRI_FLIP_DONE));
9677 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9678 MI_SRM_LRM_GLOBAL_GTT);
9679 intel_ring_emit(ring, DERRMR);
9680 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
9681 intel_ring_emit(ring, 0);
9682
9683 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane);
9684 intel_ring_emit(ring, stride << 6 | obj->tiling_mode);
9685 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
9686
9687 intel_mark_page_flip_active(intel_crtc);
9688 __intel_ring_advance(ring);
9689
9690 return 0;
9691}
9692
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009693static int intel_default_queue_flip(struct drm_device *dev,
9694 struct drm_crtc *crtc,
9695 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009696 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009697 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009698 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009699{
9700 return -ENODEV;
9701}
9702
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009703static bool __intel_pageflip_stall_check(struct drm_device *dev,
9704 struct drm_crtc *crtc)
9705{
9706 struct drm_i915_private *dev_priv = dev->dev_private;
9707 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9708 struct intel_unpin_work *work = intel_crtc->unpin_work;
9709 u32 addr;
9710
9711 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
9712 return true;
9713
9714 if (!work->enable_stall_check)
9715 return false;
9716
9717 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +01009718 if (work->flip_queued_req &&
9719 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009720 return false;
9721
9722 work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9723 }
9724
9725 if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
9726 return false;
9727
9728 /* Potential stall - if we see that the flip has happened,
9729 * assume a missed interrupt. */
9730 if (INTEL_INFO(dev)->gen >= 4)
9731 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
9732 else
9733 addr = I915_READ(DSPADDR(intel_crtc->plane));
9734
9735 /* There is a potential issue here with a false positive after a flip
9736 * to the same address. We could address this by checking for a
9737 * non-incrementing frame counter.
9738 */
9739 return addr == work->gtt_offset;
9740}
9741
9742void intel_check_page_flip(struct drm_device *dev, int pipe)
9743{
9744 struct drm_i915_private *dev_priv = dev->dev_private;
9745 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9746 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterf3260382014-09-15 14:55:23 +02009747
Dave Gordon6c51d462015-03-06 15:34:26 +00009748 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009749
9750 if (crtc == NULL)
9751 return;
9752
Daniel Vetterf3260382014-09-15 14:55:23 +02009753 spin_lock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009754 if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
9755 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
9756 intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
9757 page_flip_completed(intel_crtc);
9758 }
Daniel Vetterf3260382014-09-15 14:55:23 +02009759 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009760}
9761
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009762static int intel_crtc_page_flip(struct drm_crtc *crtc,
9763 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009764 struct drm_pending_vblank_event *event,
9765 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009766{
9767 struct drm_device *dev = crtc->dev;
9768 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -07009769 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -07009770 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009771 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -08009772 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +02009773 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009774 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009775 struct intel_engine_cs *ring;
Chris Wilson52e68632010-08-08 10:15:59 +01009776 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009777
Matt Roper2ff8fde2014-07-08 07:50:07 -07009778 /*
9779 * drm_mode_page_flip_ioctl() should already catch this, but double
9780 * check to be safe. In the future we may enable pageflipping from
9781 * a disabled primary plane.
9782 */
9783 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
9784 return -EBUSY;
9785
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009786 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -07009787 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009788 return -EINVAL;
9789
9790 /*
9791 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9792 * Note that pitch changes could also affect these register.
9793 */
9794 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -07009795 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9796 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009797 return -EINVAL;
9798
Chris Wilsonf900db42014-02-20 09:26:13 +00009799 if (i915_terminally_wedged(&dev_priv->gpu_error))
9800 goto out_hang;
9801
Daniel Vetterb14c5672013-09-19 12:18:32 +02009802 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009803 if (work == NULL)
9804 return -ENOMEM;
9805
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009806 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009807 work->crtc = crtc;
Matt Roper2ff8fde2014-07-08 07:50:07 -07009808 work->old_fb_obj = intel_fb_obj(old_fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009809 INIT_WORK(&work->work, intel_unpin_work_fn);
9810
Daniel Vetter87b6b102014-05-15 15:33:46 +02009811 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009812 if (ret)
9813 goto free_work;
9814
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009815 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009816 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009817 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009818 /* Before declaring the flip queue wedged, check if
9819 * the hardware completed the operation behind our backs.
9820 */
9821 if (__intel_pageflip_stall_check(dev, crtc)) {
9822 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
9823 page_flip_completed(intel_crtc);
9824 } else {
9825 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009826 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +01009827
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009828 drm_crtc_vblank_put(crtc);
9829 kfree(work);
9830 return -EBUSY;
9831 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009832 }
9833 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009834 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009835
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009836 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9837 flush_workqueue(dev_priv->wq);
9838
Chris Wilson79158102012-05-23 11:13:58 +01009839 ret = i915_mutex_lock_interruptible(dev);
9840 if (ret)
9841 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009842
Jesse Barnes75dfca82010-02-10 15:09:44 -08009843 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00009844 drm_gem_object_reference(&work->old_fb_obj->base);
9845 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009846
Matt Roperf4510a22014-04-01 15:22:40 -07009847 crtc->primary->fb = fb;
Xi Ruoyao319c1d42015-03-12 20:16:32 +08009848 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +01009849
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009850 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009851
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009852 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02009853 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009854
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009855 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +02009856 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009857
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009858 if (IS_VALLEYVIEW(dev)) {
9859 ring = &dev_priv->ring[BCS];
Chris Wilson8e09bf82014-07-08 10:40:30 +01009860 if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
9861 /* vlv: DISPLAY_FLIP fails to change tiling */
9862 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +00009863 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +01009864 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009865 } else if (INTEL_INFO(dev)->gen >= 7) {
John Harrison41c52412014-11-24 18:49:43 +00009866 ring = i915_gem_request_get_ring(obj->last_read_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009867 if (ring == NULL || ring->id != RCS)
9868 ring = &dev_priv->ring[BCS];
9869 } else {
9870 ring = &dev_priv->ring[RCS];
9871 }
9872
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00009873 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009874 if (ret)
9875 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009876
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009877 work->gtt_offset =
9878 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9879
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009880 if (use_mmio_flip(ring, obj)) {
Sourab Gupta84c33a62014-06-02 16:47:17 +05309881 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
9882 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009883 if (ret)
9884 goto cleanup_unpin;
9885
John Harrisonf06cc1b2014-11-24 18:49:37 +00009886 i915_gem_request_assign(&work->flip_queued_req,
9887 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009888 } else {
Sourab Gupta84c33a62014-06-02 16:47:17 +05309889 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009890 page_flip_flags);
9891 if (ret)
9892 goto cleanup_unpin;
9893
John Harrisonf06cc1b2014-11-24 18:49:37 +00009894 i915_gem_request_assign(&work->flip_queued_req,
9895 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009896 }
9897
9898 work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9899 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009900
Daniel Vettera071fa02014-06-18 23:28:09 +02009901 i915_gem_track_fb(work->old_fb_obj, obj,
9902 INTEL_FRONTBUFFER_PRIMARY(pipe));
9903
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02009904 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +02009905 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009906 mutex_unlock(&dev->struct_mutex);
9907
Jesse Barnese5510fa2010-07-01 16:48:37 -07009908 trace_i915_flip_request(intel_crtc->plane, obj);
9909
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009910 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01009911
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009912cleanup_unpin:
9913 intel_unpin_fb_obj(obj);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009914cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009915 atomic_dec(&intel_crtc->unpin_work_count);
Matt Roperf4510a22014-04-01 15:22:40 -07009916 crtc->primary->fb = old_fb;
Xi Ruoyao319c1d42015-03-12 20:16:32 +08009917 update_state_fb(crtc->primary);
Chris Wilson05394f32010-11-08 19:18:58 +00009918 drm_gem_object_unreference(&work->old_fb_obj->base);
9919 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01009920 mutex_unlock(&dev->struct_mutex);
9921
Chris Wilson79158102012-05-23 11:13:58 +01009922cleanup:
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009923 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +01009924 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009925 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +01009926
Daniel Vetter87b6b102014-05-15 15:33:46 +02009927 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009928free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01009929 kfree(work);
9930
Chris Wilsonf900db42014-02-20 09:26:13 +00009931 if (ret == -EIO) {
9932out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -08009933 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +01009934 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009935 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +02009936 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009937 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +01009938 }
Chris Wilsonf900db42014-02-20 09:26:13 +00009939 }
Chris Wilson96b099f2010-06-07 14:03:04 +01009940 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009941}
9942
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009943static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009944 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9945 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -08009946 .atomic_begin = intel_begin_crtc_commit,
9947 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009948};
9949
Daniel Vetter9a935852012-07-05 22:34:27 +02009950/**
9951 * intel_modeset_update_staged_output_state
9952 *
9953 * Updates the staged output configuration state, e.g. after we've read out the
9954 * current hw state.
9955 */
9956static void intel_modeset_update_staged_output_state(struct drm_device *dev)
9957{
Ville Syrjälä76688512014-01-10 11:28:06 +02009958 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009959 struct intel_encoder *encoder;
9960 struct intel_connector *connector;
9961
9962 list_for_each_entry(connector, &dev->mode_config.connector_list,
9963 base.head) {
9964 connector->new_encoder =
9965 to_intel_encoder(connector->base.encoder);
9966 }
9967
Damien Lespiaub2784e12014-08-05 11:29:37 +01009968 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009969 encoder->new_crtc =
9970 to_intel_crtc(encoder->base.crtc);
9971 }
Ville Syrjälä76688512014-01-10 11:28:06 +02009972
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009973 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009974 crtc->new_enabled = crtc->base.enabled;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +02009975
9976 if (crtc->new_enabled)
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009977 crtc->new_config = crtc->config;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +02009978 else
9979 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +02009980 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009981}
9982
9983/**
9984 * intel_modeset_commit_output_state
9985 *
9986 * This function copies the stage display pipe configuration to the real one.
9987 */
9988static void intel_modeset_commit_output_state(struct drm_device *dev)
9989{
Ville Syrjälä76688512014-01-10 11:28:06 +02009990 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009991 struct intel_encoder *encoder;
9992 struct intel_connector *connector;
9993
9994 list_for_each_entry(connector, &dev->mode_config.connector_list,
9995 base.head) {
9996 connector->base.encoder = &connector->new_encoder->base;
9997 }
9998
Damien Lespiaub2784e12014-08-05 11:29:37 +01009999 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010000 encoder->base.crtc = &encoder->new_crtc->base;
10001 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010002
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010003 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010004 crtc->base.enabled = crtc->new_enabled;
10005 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010006}
10007
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010008static void
Robin Schroereba905b2014-05-18 02:24:50 +020010009connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010010 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010011{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010012 int bpp = pipe_config->pipe_bpp;
10013
10014 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10015 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010016 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010017
10018 /* Don't use an invalid EDID bpc value */
10019 if (connector->base.display_info.bpc &&
10020 connector->base.display_info.bpc * 3 < bpp) {
10021 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10022 bpp, connector->base.display_info.bpc*3);
10023 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10024 }
10025
10026 /* Clamp bpp to 8 on screens without EDID 1.4 */
10027 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10028 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10029 bpp);
10030 pipe_config->pipe_bpp = 24;
10031 }
10032}
10033
10034static int
10035compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10036 struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010037 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010038{
10039 struct drm_device *dev = crtc->base.dev;
10040 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010041 int bpp;
10042
Daniel Vetterd42264b2013-03-28 16:38:08 +010010043 switch (fb->pixel_format) {
10044 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010045 bpp = 8*3; /* since we go through a colormap */
10046 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010047 case DRM_FORMAT_XRGB1555:
10048 case DRM_FORMAT_ARGB1555:
10049 /* checked in intel_framebuffer_init already */
10050 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10051 return -EINVAL;
10052 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010053 bpp = 6*3; /* min is 18bpp */
10054 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010055 case DRM_FORMAT_XBGR8888:
10056 case DRM_FORMAT_ABGR8888:
10057 /* checked in intel_framebuffer_init already */
10058 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10059 return -EINVAL;
10060 case DRM_FORMAT_XRGB8888:
10061 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010062 bpp = 8*3;
10063 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010064 case DRM_FORMAT_XRGB2101010:
10065 case DRM_FORMAT_ARGB2101010:
10066 case DRM_FORMAT_XBGR2101010:
10067 case DRM_FORMAT_ABGR2101010:
10068 /* checked in intel_framebuffer_init already */
10069 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +010010070 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010071 bpp = 10*3;
10072 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +010010073 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010074 default:
10075 DRM_DEBUG_KMS("unsupported depth\n");
10076 return -EINVAL;
10077 }
10078
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010079 pipe_config->pipe_bpp = bpp;
10080
10081 /* Clamp display bpp to EDID value */
10082 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010083 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +020010084 if (!connector->new_encoder ||
10085 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010086 continue;
10087
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010088 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010089 }
10090
10091 return bpp;
10092}
10093
Daniel Vetter644db712013-09-19 14:53:58 +020010094static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10095{
10096 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10097 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010010098 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020010099 mode->crtc_hdisplay, mode->crtc_hsync_start,
10100 mode->crtc_hsync_end, mode->crtc_htotal,
10101 mode->crtc_vdisplay, mode->crtc_vsync_start,
10102 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10103}
10104
Daniel Vetterc0b03412013-05-28 12:05:54 +020010105static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010106 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020010107 const char *context)
10108{
10109 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10110 context, pipe_name(crtc->pipe));
10111
10112 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10113 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10114 pipe_config->pipe_bpp, pipe_config->dither);
10115 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10116 pipe_config->has_pch_encoder,
10117 pipe_config->fdi_lanes,
10118 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10119 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10120 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010121 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10122 pipe_config->has_dp_encoder,
10123 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10124 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10125 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010126
10127 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10128 pipe_config->has_dp_encoder,
10129 pipe_config->dp_m2_n2.gmch_m,
10130 pipe_config->dp_m2_n2.gmch_n,
10131 pipe_config->dp_m2_n2.link_m,
10132 pipe_config->dp_m2_n2.link_n,
10133 pipe_config->dp_m2_n2.tu);
10134
Daniel Vetter55072d12014-11-20 16:10:28 +010010135 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
10136 pipe_config->has_audio,
10137 pipe_config->has_infoframe);
10138
Daniel Vetterc0b03412013-05-28 12:05:54 +020010139 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010140 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010141 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010142 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
10143 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030010144 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010145 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10146 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010147 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10148 pipe_config->gmch_pfit.control,
10149 pipe_config->gmch_pfit.pgm_ratios,
10150 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010151 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020010152 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010153 pipe_config->pch_pfit.size,
10154 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010155 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030010156 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010157}
10158
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010159static bool encoders_cloneable(const struct intel_encoder *a,
10160 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010161{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010162 /* masks could be asymmetric, so check both ways */
10163 return a == b || (a->cloneable & (1 << b->type) &&
10164 b->cloneable & (1 << a->type));
10165}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010166
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010167static bool check_single_encoder_cloning(struct intel_crtc *crtc,
10168 struct intel_encoder *encoder)
10169{
10170 struct drm_device *dev = crtc->base.dev;
10171 struct intel_encoder *source_encoder;
10172
Damien Lespiaub2784e12014-08-05 11:29:37 +010010173 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010174 if (source_encoder->new_crtc != crtc)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010175 continue;
10176
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010177 if (!encoders_cloneable(encoder, source_encoder))
10178 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010179 }
10180
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010181 return true;
10182}
10183
10184static bool check_encoder_cloning(struct intel_crtc *crtc)
10185{
10186 struct drm_device *dev = crtc->base.dev;
10187 struct intel_encoder *encoder;
10188
Damien Lespiaub2784e12014-08-05 11:29:37 +010010189 for_each_intel_encoder(dev, encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010190 if (encoder->new_crtc != crtc)
10191 continue;
10192
10193 if (!check_single_encoder_cloning(crtc, encoder))
10194 return false;
10195 }
10196
10197 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010198}
10199
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010200static bool check_digital_port_conflicts(struct drm_device *dev)
10201{
10202 struct intel_connector *connector;
10203 unsigned int used_ports = 0;
10204
10205 /*
10206 * Walk the connector list instead of the encoder
10207 * list to detect the problem on ddi platforms
10208 * where there's just one encoder per digital port.
10209 */
10210 list_for_each_entry(connector,
10211 &dev->mode_config.connector_list, base.head) {
10212 struct intel_encoder *encoder = connector->new_encoder;
10213
10214 if (!encoder)
10215 continue;
10216
10217 WARN_ON(!encoder->new_crtc);
10218
10219 switch (encoder->type) {
10220 unsigned int port_mask;
10221 case INTEL_OUTPUT_UNKNOWN:
10222 if (WARN_ON(!HAS_DDI(dev)))
10223 break;
10224 case INTEL_OUTPUT_DISPLAYPORT:
10225 case INTEL_OUTPUT_HDMI:
10226 case INTEL_OUTPUT_EDP:
10227 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
10228
10229 /* the same port mustn't appear more than once */
10230 if (used_ports & port_mask)
10231 return false;
10232
10233 used_ports |= port_mask;
10234 default:
10235 break;
10236 }
10237 }
10238
10239 return true;
10240}
10241
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010242static struct intel_crtc_state *
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010243intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010244 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010245 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +020010246{
10247 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +020010248 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010249 struct intel_crtc_state *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +010010250 int plane_bpp, ret = -EINVAL;
10251 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020010252
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010253 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010254 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10255 return ERR_PTR(-EINVAL);
10256 }
10257
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010258 if (!check_digital_port_conflicts(dev)) {
10259 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
10260 return ERR_PTR(-EINVAL);
10261 }
10262
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010263 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10264 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020010265 return ERR_PTR(-ENOMEM);
10266
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010267 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
10268 drm_mode_copy(&pipe_config->base.mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010269
Daniel Vettere143a212013-07-04 12:01:15 +020010270 pipe_config->cpu_transcoder =
10271 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010272 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010273
Imre Deak2960bc92013-07-30 13:36:32 +030010274 /*
10275 * Sanitize sync polarity flags based on requested ones. If neither
10276 * positive or negative polarity is requested, treat this as meaning
10277 * negative polarity.
10278 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010279 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030010280 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010281 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030010282
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010283 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030010284 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010285 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030010286
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010287 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10288 * plane pixel format and any sink constraints into account. Returns the
10289 * source plane bpp so that dithering can be selected on mismatches
10290 * after encoders and crtc also have had their say. */
10291 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10292 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010293 if (plane_bpp < 0)
10294 goto fail;
10295
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030010296 /*
10297 * Determine the real pipe dimensions. Note that stereo modes can
10298 * increase the actual pipe size due to the frame doubling and
10299 * insertion of additional space for blanks between the frame. This
10300 * is stored in the crtc timings. We use the requested mode to do this
10301 * computation to clearly distinguish it from the adjusted mode, which
10302 * can be changed by the connectors in the below retry loop.
10303 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010304 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080010305 &pipe_config->pipe_src_w,
10306 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030010307
Daniel Vettere29c22c2013-02-21 00:00:16 +010010308encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020010309 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020010310 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020010311 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010312
Daniel Vetter135c81b2013-07-21 21:37:09 +020010313 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010314 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
10315 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020010316
Daniel Vetter7758a112012-07-08 19:40:39 +020010317 /* Pass our mode to the connectors and the CRTC to give them a chance to
10318 * adjust it according to limitations or connector properties, and also
10319 * a chance to reject the mode entirely.
10320 */
Damien Lespiaub2784e12014-08-05 11:29:37 +010010321 for_each_intel_encoder(dev, encoder) {
Daniel Vetter7758a112012-07-08 19:40:39 +020010322
10323 if (&encoder->new_crtc->base != crtc)
10324 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +010010325
Daniel Vetterefea6e82013-07-21 21:36:59 +020010326 if (!(encoder->compute_config(encoder, pipe_config))) {
10327 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020010328 goto fail;
10329 }
10330 }
10331
Daniel Vetterff9a6752013-06-01 17:16:21 +020010332 /* Set default port clock if not overwritten by the encoder. Needs to be
10333 * done afterwards in case the encoder adjusts the mode. */
10334 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010335 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010010336 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010337
Daniel Vettera43f6e02013-06-07 23:10:32 +020010338 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010010339 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020010340 DRM_DEBUG_KMS("CRTC fixup failed\n");
10341 goto fail;
10342 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010010343
10344 if (ret == RETRY) {
10345 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10346 ret = -EINVAL;
10347 goto fail;
10348 }
10349
10350 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10351 retry = false;
10352 goto encoder_retry;
10353 }
10354
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010355 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10356 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10357 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10358
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010359 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020010360fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010361 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010010362 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +020010363}
10364
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010365/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10366 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10367static void
10368intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10369 unsigned *prepare_pipes, unsigned *disable_pipes)
10370{
10371 struct intel_crtc *intel_crtc;
10372 struct drm_device *dev = crtc->dev;
10373 struct intel_encoder *encoder;
10374 struct intel_connector *connector;
10375 struct drm_crtc *tmp_crtc;
10376
10377 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
10378
10379 /* Check which crtcs have changed outputs connected to them, these need
10380 * to be part of the prepare_pipes mask. We don't (yet) support global
10381 * modeset across multiple crtcs, so modeset_pipes will only have one
10382 * bit set at most. */
10383 list_for_each_entry(connector, &dev->mode_config.connector_list,
10384 base.head) {
10385 if (connector->base.encoder == &connector->new_encoder->base)
10386 continue;
10387
10388 if (connector->base.encoder) {
10389 tmp_crtc = connector->base.encoder->crtc;
10390
10391 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10392 }
10393
10394 if (connector->new_encoder)
10395 *prepare_pipes |=
10396 1 << connector->new_encoder->new_crtc->pipe;
10397 }
10398
Damien Lespiaub2784e12014-08-05 11:29:37 +010010399 for_each_intel_encoder(dev, encoder) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010400 if (encoder->base.crtc == &encoder->new_crtc->base)
10401 continue;
10402
10403 if (encoder->base.crtc) {
10404 tmp_crtc = encoder->base.crtc;
10405
10406 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10407 }
10408
10409 if (encoder->new_crtc)
10410 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
10411 }
10412
Ville Syrjälä76688512014-01-10 11:28:06 +020010413 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010414 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010415 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010416 continue;
10417
Ville Syrjälä76688512014-01-10 11:28:06 +020010418 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010419 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +020010420 else
10421 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010422 }
10423
10424
10425 /* set_mode is also used to update properties on life display pipes. */
10426 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +020010427 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010428 *prepare_pipes |= 1 << intel_crtc->pipe;
10429
Daniel Vetterb6c51642013-04-12 18:48:43 +020010430 /*
10431 * For simplicity do a full modeset on any pipe where the output routing
10432 * changed. We could be more clever, but that would require us to be
10433 * more careful with calling the relevant encoder->mode_set functions.
10434 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010435 if (*prepare_pipes)
10436 *modeset_pipes = *prepare_pipes;
10437
10438 /* ... and mask these out. */
10439 *modeset_pipes &= ~(*disable_pipes);
10440 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020010441
10442 /*
10443 * HACK: We don't (yet) fully support global modesets. intel_set_config
10444 * obies this rule, but the modeset restore mode of
10445 * intel_modeset_setup_hw_state does not.
10446 */
10447 *modeset_pipes &= 1 << intel_crtc->pipe;
10448 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020010449
10450 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10451 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010452}
10453
Daniel Vetterea9d7582012-07-10 10:42:52 +020010454static bool intel_crtc_in_use(struct drm_crtc *crtc)
10455{
10456 struct drm_encoder *encoder;
10457 struct drm_device *dev = crtc->dev;
10458
10459 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10460 if (encoder->crtc == crtc)
10461 return true;
10462
10463 return false;
10464}
10465
10466static void
10467intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10468{
Daniel Vetterba41c0de2014-11-03 15:04:55 +010010469 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020010470 struct intel_encoder *intel_encoder;
10471 struct intel_crtc *intel_crtc;
10472 struct drm_connector *connector;
10473
Daniel Vetterba41c0de2014-11-03 15:04:55 +010010474 intel_shared_dpll_commit(dev_priv);
10475
Damien Lespiaub2784e12014-08-05 11:29:37 +010010476 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020010477 if (!intel_encoder->base.crtc)
10478 continue;
10479
10480 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10481
10482 if (prepare_pipes & (1 << intel_crtc->pipe))
10483 intel_encoder->connectors_active = false;
10484 }
10485
10486 intel_modeset_commit_output_state(dev);
10487
Ville Syrjälä76688512014-01-10 11:28:06 +020010488 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010489 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010490 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010491 WARN_ON(intel_crtc->new_config &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010492 intel_crtc->new_config != intel_crtc->config);
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010493 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010494 }
10495
10496 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10497 if (!connector->encoder || !connector->encoder->crtc)
10498 continue;
10499
10500 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10501
10502 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020010503 struct drm_property *dpms_property =
10504 dev->mode_config.dpms_property;
10505
Daniel Vetterea9d7582012-07-10 10:42:52 +020010506 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050010507 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020010508 dpms_property,
10509 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010510
10511 intel_encoder = to_intel_encoder(connector->encoder);
10512 intel_encoder->connectors_active = true;
10513 }
10514 }
10515
10516}
10517
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010518static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010519{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010520 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010521
10522 if (clock1 == clock2)
10523 return true;
10524
10525 if (!clock1 || !clock2)
10526 return false;
10527
10528 diff = abs(clock1 - clock2);
10529
10530 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10531 return true;
10532
10533 return false;
10534}
10535
Daniel Vetter25c5b262012-07-08 22:08:04 +020010536#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10537 list_for_each_entry((intel_crtc), \
10538 &(dev)->mode_config.crtc_list, \
10539 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020010540 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020010541
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010542static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010543intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010544 struct intel_crtc_state *current_config,
10545 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010546{
Daniel Vetter66e985c2013-06-05 13:34:20 +020010547#define PIPE_CONF_CHECK_X(name) \
10548 if (current_config->name != pipe_config->name) { \
10549 DRM_ERROR("mismatch in " #name " " \
10550 "(expected 0x%08x, found 0x%08x)\n", \
10551 current_config->name, \
10552 pipe_config->name); \
10553 return false; \
10554 }
10555
Daniel Vetter08a24032013-04-19 11:25:34 +020010556#define PIPE_CONF_CHECK_I(name) \
10557 if (current_config->name != pipe_config->name) { \
10558 DRM_ERROR("mismatch in " #name " " \
10559 "(expected %i, found %i)\n", \
10560 current_config->name, \
10561 pipe_config->name); \
10562 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010010563 }
10564
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010565/* This is required for BDW+ where there is only one set of registers for
10566 * switching between high and low RR.
10567 * This macro can be used whenever a comparison has to be made between one
10568 * hw state and multiple sw state variables.
10569 */
10570#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10571 if ((current_config->name != pipe_config->name) && \
10572 (current_config->alt_name != pipe_config->name)) { \
10573 DRM_ERROR("mismatch in " #name " " \
10574 "(expected %i or %i, found %i)\n", \
10575 current_config->name, \
10576 current_config->alt_name, \
10577 pipe_config->name); \
10578 return false; \
10579 }
10580
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010581#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10582 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070010583 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010584 "(expected %i, found %i)\n", \
10585 current_config->name & (mask), \
10586 pipe_config->name & (mask)); \
10587 return false; \
10588 }
10589
Ville Syrjälä5e550652013-09-06 23:29:07 +030010590#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10591 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10592 DRM_ERROR("mismatch in " #name " " \
10593 "(expected %i, found %i)\n", \
10594 current_config->name, \
10595 pipe_config->name); \
10596 return false; \
10597 }
10598
Daniel Vetterbb760062013-06-06 14:55:52 +020010599#define PIPE_CONF_QUIRK(quirk) \
10600 ((current_config->quirks | pipe_config->quirks) & (quirk))
10601
Daniel Vettereccb1402013-05-22 00:50:22 +020010602 PIPE_CONF_CHECK_I(cpu_transcoder);
10603
Daniel Vetter08a24032013-04-19 11:25:34 +020010604 PIPE_CONF_CHECK_I(has_pch_encoder);
10605 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020010606 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10607 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10608 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10609 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10610 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020010611
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010612 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010613
10614 if (INTEL_INFO(dev)->gen < 8) {
10615 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10616 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10617 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10618 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10619 PIPE_CONF_CHECK_I(dp_m_n.tu);
10620
10621 if (current_config->has_drrs) {
10622 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
10623 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
10624 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
10625 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
10626 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
10627 }
10628 } else {
10629 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
10630 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
10631 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
10632 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
10633 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
10634 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010635
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010636 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
10637 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
10638 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
10639 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
10640 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
10641 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010642
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010643 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
10644 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
10645 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
10646 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
10647 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
10648 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010649
Daniel Vetterc93f54c2013-06-27 19:47:19 +020010650 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020010651 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020010652 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10653 IS_VALLEYVIEW(dev))
10654 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080010655 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020010656
Daniel Vetter9ed109a2014-04-24 23:54:52 +020010657 PIPE_CONF_CHECK_I(has_audio);
10658
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010659 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010660 DRM_MODE_FLAG_INTERLACE);
10661
Daniel Vetterbb760062013-06-06 14:55:52 +020010662 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010663 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020010664 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010665 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020010666 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010667 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020010668 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010669 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020010670 DRM_MODE_FLAG_NVSYNC);
10671 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010672
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010673 PIPE_CONF_CHECK_I(pipe_src_w);
10674 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010675
Daniel Vetter99535992014-04-13 12:00:33 +020010676 /*
10677 * FIXME: BIOS likes to set up a cloned config with lvds+external
10678 * screen. Since we don't yet re-compute the pipe config when moving
10679 * just the lvds port away to another pipe the sw tracking won't match.
10680 *
10681 * Proper atomic modesets with recomputed global state will fix this.
10682 * Until then just don't check gmch state for inherited modes.
10683 */
10684 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10685 PIPE_CONF_CHECK_I(gmch_pfit.control);
10686 /* pfit ratios are autocomputed by the hw on gen4+ */
10687 if (INTEL_INFO(dev)->gen < 4)
10688 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10689 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10690 }
10691
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010692 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10693 if (current_config->pch_pfit.enabled) {
10694 PIPE_CONF_CHECK_I(pch_pfit.pos);
10695 PIPE_CONF_CHECK_I(pch_pfit.size);
10696 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010697
Jesse Barnese59150d2014-01-07 13:30:45 -080010698 /* BDW+ don't expose a synchronous way to read the state */
10699 if (IS_HASWELL(dev))
10700 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010701
Ville Syrjälä282740f2013-09-04 18:30:03 +030010702 PIPE_CONF_CHECK_I(double_wide);
10703
Daniel Vetter26804af2014-06-25 22:01:55 +030010704 PIPE_CONF_CHECK_X(ddi_pll_sel);
10705
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010706 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010707 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020010708 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010709 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10710 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010711 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000010712 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
10713 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
10714 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010715
Ville Syrjälä42571ae2013-09-06 23:29:00 +030010716 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10717 PIPE_CONF_CHECK_I(pipe_bpp);
10718
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010719 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080010720 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030010721
Daniel Vetter66e985c2013-06-05 13:34:20 +020010722#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020010723#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010724#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010725#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030010726#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020010727#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010728
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010729 return true;
10730}
10731
Damien Lespiau08db6652014-11-04 17:06:52 +000010732static void check_wm_state(struct drm_device *dev)
10733{
10734 struct drm_i915_private *dev_priv = dev->dev_private;
10735 struct skl_ddb_allocation hw_ddb, *sw_ddb;
10736 struct intel_crtc *intel_crtc;
10737 int plane;
10738
10739 if (INTEL_INFO(dev)->gen < 9)
10740 return;
10741
10742 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
10743 sw_ddb = &dev_priv->wm.skl_hw.ddb;
10744
10745 for_each_intel_crtc(dev, intel_crtc) {
10746 struct skl_ddb_entry *hw_entry, *sw_entry;
10747 const enum pipe pipe = intel_crtc->pipe;
10748
10749 if (!intel_crtc->active)
10750 continue;
10751
10752 /* planes */
10753 for_each_plane(pipe, plane) {
10754 hw_entry = &hw_ddb.plane[pipe][plane];
10755 sw_entry = &sw_ddb->plane[pipe][plane];
10756
10757 if (skl_ddb_entry_equal(hw_entry, sw_entry))
10758 continue;
10759
10760 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
10761 "(expected (%u,%u), found (%u,%u))\n",
10762 pipe_name(pipe), plane + 1,
10763 sw_entry->start, sw_entry->end,
10764 hw_entry->start, hw_entry->end);
10765 }
10766
10767 /* cursor */
10768 hw_entry = &hw_ddb.cursor[pipe];
10769 sw_entry = &sw_ddb->cursor[pipe];
10770
10771 if (skl_ddb_entry_equal(hw_entry, sw_entry))
10772 continue;
10773
10774 DRM_ERROR("mismatch in DDB state pipe %c cursor "
10775 "(expected (%u,%u), found (%u,%u))\n",
10776 pipe_name(pipe),
10777 sw_entry->start, sw_entry->end,
10778 hw_entry->start, hw_entry->end);
10779 }
10780}
10781
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010782static void
10783check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010784{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010785 struct intel_connector *connector;
10786
10787 list_for_each_entry(connector, &dev->mode_config.connector_list,
10788 base.head) {
10789 /* This also checks the encoder/connector hw state with the
10790 * ->get_hw_state callbacks. */
10791 intel_connector_check_state(connector);
10792
Rob Clarke2c719b2014-12-15 13:56:32 -050010793 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010794 "connector's staged encoder doesn't match current encoder\n");
10795 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010796}
10797
10798static void
10799check_encoder_state(struct drm_device *dev)
10800{
10801 struct intel_encoder *encoder;
10802 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010803
Damien Lespiaub2784e12014-08-05 11:29:37 +010010804 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010805 bool enabled = false;
10806 bool active = false;
10807 enum pipe pipe, tracked_pipe;
10808
10809 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10810 encoder->base.base.id,
Jani Nikula8e329a02014-06-03 14:56:21 +030010811 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010812
Rob Clarke2c719b2014-12-15 13:56:32 -050010813 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010814 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010815 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010816 "encoder's active_connectors set, but no crtc\n");
10817
10818 list_for_each_entry(connector, &dev->mode_config.connector_list,
10819 base.head) {
10820 if (connector->base.encoder != &encoder->base)
10821 continue;
10822 enabled = true;
10823 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10824 active = true;
10825 }
Dave Airlie0e32b392014-05-02 14:02:48 +100010826 /*
10827 * for MST connectors if we unplug the connector is gone
10828 * away but the encoder is still connected to a crtc
10829 * until a modeset happens in response to the hotplug.
10830 */
10831 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
10832 continue;
10833
Rob Clarke2c719b2014-12-15 13:56:32 -050010834 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010835 "encoder's enabled state mismatch "
10836 "(expected %i, found %i)\n",
10837 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050010838 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010839 "active encoder with no crtc\n");
10840
Rob Clarke2c719b2014-12-15 13:56:32 -050010841 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010842 "encoder's computed active state doesn't match tracked active state "
10843 "(expected %i, found %i)\n", active, encoder->connectors_active);
10844
10845 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050010846 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010847 "encoder's hw state doesn't match sw tracking "
10848 "(expected %i, found %i)\n",
10849 encoder->connectors_active, active);
10850
10851 if (!encoder->base.crtc)
10852 continue;
10853
10854 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050010855 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010856 "active encoder's pipe doesn't match"
10857 "(expected %i, found %i)\n",
10858 tracked_pipe, pipe);
10859
10860 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010861}
10862
10863static void
10864check_crtc_state(struct drm_device *dev)
10865{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010866 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010867 struct intel_crtc *crtc;
10868 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010869 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010870
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010871 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010872 bool enabled = false;
10873 bool active = false;
10874
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010875 memset(&pipe_config, 0, sizeof(pipe_config));
10876
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010877 DRM_DEBUG_KMS("[CRTC:%d]\n",
10878 crtc->base.base.id);
10879
Rob Clarke2c719b2014-12-15 13:56:32 -050010880 I915_STATE_WARN(crtc->active && !crtc->base.enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010881 "active crtc, but not enabled in sw tracking\n");
10882
Damien Lespiaub2784e12014-08-05 11:29:37 +010010883 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010884 if (encoder->base.crtc != &crtc->base)
10885 continue;
10886 enabled = true;
10887 if (encoder->connectors_active)
10888 active = true;
10889 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010890
Rob Clarke2c719b2014-12-15 13:56:32 -050010891 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010892 "crtc's computed active state doesn't match tracked active state "
10893 "(expected %i, found %i)\n", active, crtc->active);
Rob Clarke2c719b2014-12-15 13:56:32 -050010894 I915_STATE_WARN(enabled != crtc->base.enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010895 "crtc's computed enabled state doesn't match tracked enabled state "
10896 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10897
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010898 active = dev_priv->display.get_pipe_config(crtc,
10899 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020010900
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030010901 /* hw state is inconsistent with the pipe quirk */
10902 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
10903 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020010904 active = crtc->active;
10905
Damien Lespiaub2784e12014-08-05 11:29:37 +010010906 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030010907 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020010908 if (encoder->base.crtc != &crtc->base)
10909 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010010910 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020010911 encoder->get_config(encoder, &pipe_config);
10912 }
10913
Rob Clarke2c719b2014-12-15 13:56:32 -050010914 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010915 "crtc active state doesn't match with hw state "
10916 "(expected %i, found %i)\n", crtc->active, active);
10917
Daniel Vetterc0b03412013-05-28 12:05:54 +020010918 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010919 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050010920 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020010921 intel_dump_pipe_config(crtc, &pipe_config,
10922 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010923 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020010924 "[sw state]");
10925 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010926 }
10927}
10928
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010929static void
10930check_shared_dpll_state(struct drm_device *dev)
10931{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010932 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010933 struct intel_crtc *crtc;
10934 struct intel_dpll_hw_state dpll_hw_state;
10935 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020010936
10937 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10938 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10939 int enabled_crtcs = 0, active_crtcs = 0;
10940 bool active;
10941
10942 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10943
10944 DRM_DEBUG_KMS("%s\n", pll->name);
10945
10946 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10947
Rob Clarke2c719b2014-12-15 13:56:32 -050010948 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020010949 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020010950 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050010951 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020010952 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010953 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020010954 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010955 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020010956 "pll on state mismatch (expected %i, found %i)\n",
10957 pll->on, active);
10958
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010959 for_each_intel_crtc(dev, crtc) {
Daniel Vetter53589012013-06-05 13:34:16 +020010960 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10961 enabled_crtcs++;
10962 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10963 active_crtcs++;
10964 }
Rob Clarke2c719b2014-12-15 13:56:32 -050010965 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020010966 "pll active crtcs mismatch (expected %i, found %i)\n",
10967 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050010968 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020010969 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020010970 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010971
Rob Clarke2c719b2014-12-15 13:56:32 -050010972 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020010973 sizeof(dpll_hw_state)),
10974 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020010975 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010976}
10977
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010978void
10979intel_modeset_check_state(struct drm_device *dev)
10980{
Damien Lespiau08db6652014-11-04 17:06:52 +000010981 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010982 check_connector_state(dev);
10983 check_encoder_state(dev);
10984 check_crtc_state(dev);
10985 check_shared_dpll_state(dev);
10986}
10987
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010988void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030010989 int dotclock)
10990{
10991 /*
10992 * FDI already provided one idea for the dotclock.
10993 * Yell if the encoder disagrees.
10994 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010995 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010996 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010997 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010998}
10999
Ville Syrjälä80715b22014-05-15 20:23:23 +030011000static void update_scanline_offset(struct intel_crtc *crtc)
11001{
11002 struct drm_device *dev = crtc->base.dev;
11003
11004 /*
11005 * The scanline counter increments at the leading edge of hsync.
11006 *
11007 * On most platforms it starts counting from vtotal-1 on the
11008 * first active line. That means the scanline counter value is
11009 * always one less than what we would expect. Ie. just after
11010 * start of vblank, which also occurs at start of hsync (on the
11011 * last active line), the scanline counter will read vblank_start-1.
11012 *
11013 * On gen2 the scanline counter starts counting from 1 instead
11014 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11015 * to keep the value positive), instead of adding one.
11016 *
11017 * On HSW+ the behaviour of the scanline counter depends on the output
11018 * type. For DP ports it behaves like most other platforms, but on HDMI
11019 * there's an extra 1 line difference. So we need to add two instead of
11020 * one to the value.
11021 */
11022 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011023 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030011024 int vtotal;
11025
11026 vtotal = mode->crtc_vtotal;
11027 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
11028 vtotal /= 2;
11029
11030 crtc->scanline_offset = vtotal - 1;
11031 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030011032 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030011033 crtc->scanline_offset = 2;
11034 } else
11035 crtc->scanline_offset = 1;
11036}
11037
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011038static struct intel_crtc_state *
Jesse Barnes7f271262014-11-05 14:26:06 -080011039intel_modeset_compute_config(struct drm_crtc *crtc,
11040 struct drm_display_mode *mode,
11041 struct drm_framebuffer *fb,
11042 unsigned *modeset_pipes,
11043 unsigned *prepare_pipes,
11044 unsigned *disable_pipes)
11045{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011046 struct intel_crtc_state *pipe_config = NULL;
Jesse Barnes7f271262014-11-05 14:26:06 -080011047
11048 intel_modeset_affected_pipes(crtc, modeset_pipes,
11049 prepare_pipes, disable_pipes);
11050
11051 if ((*modeset_pipes) == 0)
11052 goto out;
11053
11054 /*
11055 * Note this needs changes when we start tracking multiple modes
11056 * and crtcs. At that point we'll need to compute the whole config
11057 * (i.e. one pipe_config for each crtc) rather than just the one
11058 * for this crtc.
11059 */
11060 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
11061 if (IS_ERR(pipe_config)) {
11062 goto out;
11063 }
11064 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
11065 "[modeset]");
Jesse Barnes7f271262014-11-05 14:26:06 -080011066
11067out:
11068 return pipe_config;
11069}
11070
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011071static int __intel_set_mode_setup_plls(struct drm_device *dev,
11072 unsigned modeset_pipes,
11073 unsigned disable_pipes)
11074{
11075 struct drm_i915_private *dev_priv = to_i915(dev);
11076 unsigned clear_pipes = modeset_pipes | disable_pipes;
11077 struct intel_crtc *intel_crtc;
11078 int ret = 0;
11079
11080 if (!dev_priv->display.crtc_compute_clock)
11081 return 0;
11082
11083 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
11084 if (ret)
11085 goto done;
11086
11087 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
11088 struct intel_crtc_state *state = intel_crtc->new_config;
11089 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11090 state);
11091 if (ret) {
11092 intel_shared_dpll_abort_config(dev_priv);
11093 goto done;
11094 }
11095 }
11096
11097done:
11098 return ret;
11099}
11100
Daniel Vetterf30da182013-04-11 20:22:50 +020011101static int __intel_set_mode(struct drm_crtc *crtc,
11102 struct drm_display_mode *mode,
Jesse Barnes7f271262014-11-05 14:26:06 -080011103 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011104 struct intel_crtc_state *pipe_config,
Jesse Barnes7f271262014-11-05 14:26:06 -080011105 unsigned modeset_pipes,
11106 unsigned prepare_pipes,
11107 unsigned disable_pipes)
Daniel Vettera6778b32012-07-02 09:56:42 +020011108{
11109 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030011110 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011111 struct drm_display_mode *saved_mode;
Daniel Vetter25c5b262012-07-08 22:08:04 +020011112 struct intel_crtc *intel_crtc;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011113 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020011114
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011115 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011116 if (!saved_mode)
11117 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020011118
Tim Gardner3ac18232012-12-07 07:54:26 -070011119 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020011120
Ville Syrjäläb9950a12014-11-21 21:00:36 +020011121 if (modeset_pipes)
11122 to_intel_crtc(crtc)->new_config = pipe_config;
11123
Jesse Barnes30a970c2013-11-04 13:48:12 -080011124 /*
11125 * See if the config requires any additional preparation, e.g.
11126 * to adjust global state with pipes off. We need to do this
11127 * here so we can get the modeset_pipe updated config for the new
11128 * mode set on this crtc. For other crtcs we need to use the
11129 * adjusted_mode bits in the crtc directly.
11130 */
Ville Syrjäläc164f832013-11-05 22:34:12 +020011131 if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +020011132 valleyview_modeset_global_pipes(dev, &prepare_pipes);
Jesse Barnes30a970c2013-11-04 13:48:12 -080011133
Ville Syrjäläc164f832013-11-05 22:34:12 +020011134 /* may have added more to prepare_pipes than we should */
11135 prepare_pipes &= ~disable_pipes;
11136 }
11137
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011138 ret = __intel_set_mode_setup_plls(dev, modeset_pipes, disable_pipes);
11139 if (ret)
11140 goto done;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +020011141
Daniel Vetter460da9162013-03-27 00:44:51 +010011142 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
11143 intel_crtc_disable(&intel_crtc->base);
11144
Daniel Vetterea9d7582012-07-10 10:42:52 +020011145 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11146 if (intel_crtc->base.enabled)
11147 dev_priv->display.crtc_disable(&intel_crtc->base);
11148 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011149
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020011150 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
11151 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f271262014-11-05 14:26:06 -080011152 *
11153 * Note we'll need to fix this up when we start tracking multiple
11154 * pipes; here we assume a single modeset_pipe and only track the
11155 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020011156 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011157 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020011158 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011159 /* mode_set/enable/disable functions rely on a correct pipe
11160 * config. */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020011161 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020011162
11163 /*
11164 * Calculate and store various constants which
11165 * are later needed by vblank and swap-completion
11166 * timestamping. They are derived from true hwmode.
11167 */
11168 drm_calc_timestamping_constants(crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011169 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011170 }
Daniel Vetter7758a112012-07-08 19:40:39 +020011171
Daniel Vetterea9d7582012-07-10 10:42:52 +020011172 /* Only after disabling all output pipelines that will be changed can we
11173 * update the the output configuration. */
11174 intel_modeset_update_state(dev, prepare_pipes);
11175
Ville Syrjälä50f6e502014-11-06 14:49:12 +020011176 modeset_update_crtc_power_domains(dev);
Daniel Vetter47fab732012-10-26 10:58:18 +020011177
Daniel Vettera6778b32012-07-02 09:56:42 +020011178 /* Set up the DPLL and any encoders state that needs to adjust or depend
11179 * on the DPLL.
11180 */
Daniel Vetter25c5b262012-07-08 22:08:04 +020011181 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Gustavo Padovan455a6802014-12-01 15:40:11 -080011182 struct drm_plane *primary = intel_crtc->base.primary;
11183 int vdisplay, hdisplay;
Daniel Vetter4c107942014-04-24 23:55:05 +020011184
Gustavo Padovan455a6802014-12-01 15:40:11 -080011185 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
11186 ret = primary->funcs->update_plane(primary, &intel_crtc->base,
11187 fb, 0, 0,
11188 hdisplay, vdisplay,
11189 x << 16, y << 16,
11190 hdisplay << 16, vdisplay << 16);
Daniel Vettera6778b32012-07-02 09:56:42 +020011191 }
11192
11193 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030011194 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11195 update_scanline_offset(intel_crtc);
11196
Daniel Vetter25c5b262012-07-08 22:08:04 +020011197 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030011198 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011199
Daniel Vettera6778b32012-07-02 09:56:42 +020011200 /* FIXME: add subpixel order */
11201done:
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011202 if (ret && crtc->enabled)
Tim Gardner3ac18232012-12-07 07:54:26 -070011203 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020011204
Tim Gardner3ac18232012-12-07 07:54:26 -070011205 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020011206 return ret;
11207}
11208
Jesse Barnes7f271262014-11-05 14:26:06 -080011209static int intel_set_mode_pipes(struct drm_crtc *crtc,
11210 struct drm_display_mode *mode,
11211 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011212 struct intel_crtc_state *pipe_config,
Jesse Barnes7f271262014-11-05 14:26:06 -080011213 unsigned modeset_pipes,
11214 unsigned prepare_pipes,
11215 unsigned disable_pipes)
11216{
11217 int ret;
11218
11219 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
11220 prepare_pipes, disable_pipes);
11221
11222 if (ret == 0)
11223 intel_modeset_check_state(crtc->dev);
11224
11225 return ret;
11226}
11227
Damien Lespiaue7457a92013-08-08 22:28:59 +010011228static int intel_set_mode(struct drm_crtc *crtc,
11229 struct drm_display_mode *mode,
11230 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +020011231{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011232 struct intel_crtc_state *pipe_config;
Jesse Barnes7f271262014-11-05 14:26:06 -080011233 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Daniel Vetterf30da182013-04-11 20:22:50 +020011234
Jesse Barnes7f271262014-11-05 14:26:06 -080011235 pipe_config = intel_modeset_compute_config(crtc, mode, fb,
11236 &modeset_pipes,
11237 &prepare_pipes,
11238 &disable_pipes);
Daniel Vetterf30da182013-04-11 20:22:50 +020011239
Jesse Barnes7f271262014-11-05 14:26:06 -080011240 if (IS_ERR(pipe_config))
11241 return PTR_ERR(pipe_config);
Daniel Vetterf30da182013-04-11 20:22:50 +020011242
Jesse Barnes7f271262014-11-05 14:26:06 -080011243 return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
11244 modeset_pipes, prepare_pipes,
11245 disable_pipes);
Daniel Vetterf30da182013-04-11 20:22:50 +020011246}
11247
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011248void intel_crtc_restore_mode(struct drm_crtc *crtc)
11249{
Matt Roperf4510a22014-04-01 15:22:40 -070011250 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011251}
11252
Daniel Vetter25c5b262012-07-08 22:08:04 +020011253#undef for_each_intel_crtc_masked
11254
Daniel Vetterd9e55602012-07-04 22:16:09 +020011255static void intel_set_config_free(struct intel_set_config *config)
11256{
11257 if (!config)
11258 return;
11259
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011260 kfree(config->save_connector_encoders);
11261 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020011262 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020011263 kfree(config);
11264}
11265
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011266static int intel_set_config_save_state(struct drm_device *dev,
11267 struct intel_set_config *config)
11268{
Ville Syrjälä76688512014-01-10 11:28:06 +020011269 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011270 struct drm_encoder *encoder;
11271 struct drm_connector *connector;
11272 int count;
11273
Ville Syrjälä76688512014-01-10 11:28:06 +020011274 config->save_crtc_enabled =
11275 kcalloc(dev->mode_config.num_crtc,
11276 sizeof(bool), GFP_KERNEL);
11277 if (!config->save_crtc_enabled)
11278 return -ENOMEM;
11279
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011280 config->save_encoder_crtcs =
11281 kcalloc(dev->mode_config.num_encoder,
11282 sizeof(struct drm_crtc *), GFP_KERNEL);
11283 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011284 return -ENOMEM;
11285
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011286 config->save_connector_encoders =
11287 kcalloc(dev->mode_config.num_connector,
11288 sizeof(struct drm_encoder *), GFP_KERNEL);
11289 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011290 return -ENOMEM;
11291
11292 /* Copy data. Note that driver private data is not affected.
11293 * Should anything bad happen only the expected state is
11294 * restored, not the drivers personal bookkeeping.
11295 */
11296 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010011297 for_each_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011298 config->save_crtc_enabled[count++] = crtc->enabled;
11299 }
11300
11301 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011302 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011303 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011304 }
11305
11306 count = 0;
11307 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011308 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011309 }
11310
11311 return 0;
11312}
11313
11314static void intel_set_config_restore_state(struct drm_device *dev,
11315 struct intel_set_config *config)
11316{
Ville Syrjälä76688512014-01-10 11:28:06 +020011317 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011318 struct intel_encoder *encoder;
11319 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011320 int count;
11321
11322 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011323 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011324 crtc->new_enabled = config->save_crtc_enabled[count++];
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011325
11326 if (crtc->new_enabled)
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011327 crtc->new_config = crtc->config;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011328 else
11329 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020011330 }
11331
11332 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010011333 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011334 encoder->new_crtc =
11335 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011336 }
11337
11338 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020011339 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11340 connector->new_encoder =
11341 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011342 }
11343}
11344
Imre Deake3de42b2013-05-03 19:44:07 +020011345static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010011346is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020011347{
11348 int i;
11349
Chris Wilson2e57f472013-07-17 12:14:40 +010011350 if (set->num_connectors == 0)
11351 return false;
11352
11353 if (WARN_ON(set->connectors == NULL))
11354 return false;
11355
11356 for (i = 0; i < set->num_connectors; i++)
11357 if (set->connectors[i]->encoder &&
11358 set->connectors[i]->encoder->crtc == set->crtc &&
11359 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020011360 return true;
11361
11362 return false;
11363}
11364
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011365static void
11366intel_set_config_compute_mode_changes(struct drm_mode_set *set,
11367 struct intel_set_config *config)
11368{
11369
11370 /* We should be able to check here if the fb has the same properties
11371 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010011372 if (is_crtc_connector_off(set)) {
11373 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070011374 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070011375 /*
11376 * If we have no fb, we can only flip as long as the crtc is
11377 * active, otherwise we need a full mode set. The crtc may
11378 * be active if we've only disabled the primary plane, or
11379 * in fastboot situations.
11380 */
Matt Roperf4510a22014-04-01 15:22:40 -070011381 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030011382 struct intel_crtc *intel_crtc =
11383 to_intel_crtc(set->crtc);
11384
Matt Roper3b150f02014-05-29 08:06:53 -070011385 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030011386 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
11387 config->fb_changed = true;
11388 } else {
11389 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
11390 config->mode_changed = true;
11391 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011392 } else if (set->fb == NULL) {
11393 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010011394 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070011395 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011396 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020011397 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011398 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020011399 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011400 }
11401
Daniel Vetter835c5872012-07-10 18:11:08 +020011402 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011403 config->fb_changed = true;
11404
11405 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
11406 DRM_DEBUG_KMS("modes are different, full mode set\n");
11407 drm_mode_debug_printmodeline(&set->crtc->mode);
11408 drm_mode_debug_printmodeline(set->mode);
11409 config->mode_changed = true;
11410 }
Chris Wilsona1d95702013-08-13 18:48:47 +010011411
11412 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
11413 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011414}
11415
Daniel Vetter2e431052012-07-04 22:42:15 +020011416static int
Daniel Vetter9a935852012-07-05 22:34:27 +020011417intel_modeset_stage_output_state(struct drm_device *dev,
11418 struct drm_mode_set *set,
11419 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +020011420{
Daniel Vetter9a935852012-07-05 22:34:27 +020011421 struct intel_connector *connector;
11422 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020011423 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030011424 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020011425
Damien Lespiau9abdda72013-02-13 13:29:23 +000011426 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020011427 * of connectors. For paranoia, double-check this. */
11428 WARN_ON(!set->fb && (set->num_connectors != 0));
11429 WARN_ON(set->fb && (set->num_connectors == 0));
11430
Daniel Vetter9a935852012-07-05 22:34:27 +020011431 list_for_each_entry(connector, &dev->mode_config.connector_list,
11432 base.head) {
11433 /* Otherwise traverse passed in connector list and get encoders
11434 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020011435 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011436 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100011437 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020011438 break;
11439 }
11440 }
11441
Daniel Vetter9a935852012-07-05 22:34:27 +020011442 /* If we disable the crtc, disable all its connectors. Also, if
11443 * the connector is on the changing crtc but not on the new
11444 * connector list, disable it. */
11445 if ((!set->fb || ro == set->num_connectors) &&
11446 connector->base.encoder &&
11447 connector->base.encoder->crtc == set->crtc) {
11448 connector->new_encoder = NULL;
11449
11450 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11451 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011452 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020011453 }
11454
11455
11456 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +020011457 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011458 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020011459 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011460 }
11461 /* connector->new_encoder is now updated for all connectors. */
11462
11463 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +020011464 list_for_each_entry(connector, &dev->mode_config.connector_list,
11465 base.head) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011466 struct drm_crtc *new_crtc;
11467
Daniel Vetter9a935852012-07-05 22:34:27 +020011468 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020011469 continue;
11470
Daniel Vetter9a935852012-07-05 22:34:27 +020011471 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020011472
11473 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011474 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020011475 new_crtc = set->crtc;
11476 }
11477
11478 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010011479 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11480 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011481 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020011482 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011483 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020011484
11485 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11486 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011487 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020011488 new_crtc->base.id);
11489 }
11490
11491 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010011492 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011493 int num_connectors = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020011494 list_for_each_entry(connector,
11495 &dev->mode_config.connector_list,
11496 base.head) {
11497 if (connector->new_encoder == encoder) {
11498 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011499 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020011500 }
11501 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011502
11503 if (num_connectors == 0)
11504 encoder->new_crtc = NULL;
11505 else if (num_connectors > 1)
11506 return -EINVAL;
11507
Daniel Vetter9a935852012-07-05 22:34:27 +020011508 /* Only now check for crtc changes so we don't miss encoders
11509 * that will be disabled. */
11510 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +020011511 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011512 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020011513 }
11514 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011515 /* Now we've also updated encoder->new_crtc for all encoders. */
Dave Airlie0e32b392014-05-02 14:02:48 +100011516 list_for_each_entry(connector, &dev->mode_config.connector_list,
11517 base.head) {
11518 if (connector->new_encoder)
11519 if (connector->new_encoder != connector->encoder)
11520 connector->encoder = connector->new_encoder;
11521 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011522 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011523 crtc->new_enabled = false;
11524
Damien Lespiaub2784e12014-08-05 11:29:37 +010011525 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011526 if (encoder->new_crtc == crtc) {
11527 crtc->new_enabled = true;
11528 break;
11529 }
11530 }
11531
11532 if (crtc->new_enabled != crtc->base.enabled) {
11533 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
11534 crtc->new_enabled ? "en" : "dis");
11535 config->mode_changed = true;
11536 }
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011537
11538 if (crtc->new_enabled)
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011539 crtc->new_config = crtc->config;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011540 else
11541 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020011542 }
11543
Daniel Vetter2e431052012-07-04 22:42:15 +020011544 return 0;
11545}
11546
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011547static void disable_crtc_nofb(struct intel_crtc *crtc)
11548{
11549 struct drm_device *dev = crtc->base.dev;
11550 struct intel_encoder *encoder;
11551 struct intel_connector *connector;
11552
11553 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
11554 pipe_name(crtc->pipe));
11555
11556 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11557 if (connector->new_encoder &&
11558 connector->new_encoder->new_crtc == crtc)
11559 connector->new_encoder = NULL;
11560 }
11561
Damien Lespiaub2784e12014-08-05 11:29:37 +010011562 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011563 if (encoder->new_crtc == crtc)
11564 encoder->new_crtc = NULL;
11565 }
11566
11567 crtc->new_enabled = false;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011568 crtc->new_config = NULL;
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011569}
11570
Daniel Vetter2e431052012-07-04 22:42:15 +020011571static int intel_crtc_set_config(struct drm_mode_set *set)
11572{
11573 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020011574 struct drm_mode_set save_set;
11575 struct intel_set_config *config;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011576 struct intel_crtc_state *pipe_config;
Jesse Barnes50f52752014-11-07 13:11:00 -080011577 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Daniel Vetter2e431052012-07-04 22:42:15 +020011578 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020011579
Daniel Vetter8d3e3752012-07-05 16:09:09 +020011580 BUG_ON(!set);
11581 BUG_ON(!set->crtc);
11582 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020011583
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010011584 /* Enforce sane interface api - has been abused by the fb helper. */
11585 BUG_ON(!set->mode && set->fb);
11586 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020011587
Daniel Vetter2e431052012-07-04 22:42:15 +020011588 if (set->fb) {
11589 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
11590 set->crtc->base.id, set->fb->base.id,
11591 (int)set->num_connectors, set->x, set->y);
11592 } else {
11593 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020011594 }
11595
11596 dev = set->crtc->dev;
11597
11598 ret = -ENOMEM;
11599 config = kzalloc(sizeof(*config), GFP_KERNEL);
11600 if (!config)
11601 goto out_config;
11602
11603 ret = intel_set_config_save_state(dev, config);
11604 if (ret)
11605 goto out_config;
11606
11607 save_set.crtc = set->crtc;
11608 save_set.mode = &set->crtc->mode;
11609 save_set.x = set->crtc->x;
11610 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070011611 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020011612
11613 /* Compute whether we need a full modeset, only an fb base update or no
11614 * change at all. In the future we might also check whether only the
11615 * mode changed, e.g. for LVDS where we only change the panel fitter in
11616 * such cases. */
11617 intel_set_config_compute_mode_changes(set, config);
11618
Daniel Vetter9a935852012-07-05 22:34:27 +020011619 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +020011620 if (ret)
11621 goto fail;
11622
Jesse Barnes50f52752014-11-07 13:11:00 -080011623 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
11624 set->fb,
11625 &modeset_pipes,
11626 &prepare_pipes,
11627 &disable_pipes);
Jesse Barnes20664592014-11-05 14:26:09 -080011628 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080011629 ret = PTR_ERR(pipe_config);
Jesse Barnes50f52752014-11-07 13:11:00 -080011630 goto fail;
Jesse Barnes20664592014-11-05 14:26:09 -080011631 } else if (pipe_config) {
Ville Syrjäläb9950a12014-11-21 21:00:36 +020011632 if (pipe_config->has_audio !=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011633 to_intel_crtc(set->crtc)->config->has_audio)
Jesse Barnes20664592014-11-05 14:26:09 -080011634 config->mode_changed = true;
11635
Jesse Barnesaf15d2c2014-12-01 09:54:28 -080011636 /*
11637 * Note we have an issue here with infoframes: current code
11638 * only updates them on the full mode set path per hw
11639 * requirements. So here we should be checking for any
11640 * required changes and forcing a mode set.
11641 */
Jesse Barnes20664592014-11-05 14:26:09 -080011642 }
Jesse Barnes50f52752014-11-07 13:11:00 -080011643
11644 /* set_mode will free it in the mode_changed case */
11645 if (!config->mode_changed)
11646 kfree(pipe_config);
11647
Jesse Barnes1f9954d2014-11-05 14:26:10 -080011648 intel_update_pipe_size(to_intel_crtc(set->crtc));
11649
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011650 if (config->mode_changed) {
Jesse Barnes50f52752014-11-07 13:11:00 -080011651 ret = intel_set_mode_pipes(set->crtc, set->mode,
11652 set->x, set->y, set->fb, pipe_config,
11653 modeset_pipes, prepare_pipes,
11654 disable_pipes);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011655 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070011656 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011657 struct drm_plane *primary = set->crtc->primary;
11658 int vdisplay, hdisplay;
Matt Roper3b150f02014-05-29 08:06:53 -070011659
Gustavo Padovan455a6802014-12-01 15:40:11 -080011660 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
11661 ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
11662 0, 0, hdisplay, vdisplay,
11663 set->x << 16, set->y << 16,
11664 hdisplay << 16, vdisplay << 16);
Matt Roper3b150f02014-05-29 08:06:53 -070011665
11666 /*
11667 * We need to make sure the primary plane is re-enabled if it
11668 * has previously been turned off.
11669 */
11670 if (!intel_crtc->primary_enabled && ret == 0) {
11671 WARN_ON(!intel_crtc->active);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +030011672 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070011673 }
11674
Jesse Barnes7ca51a32014-01-07 13:50:49 -080011675 /*
11676 * In the fastboot case this may be our only check of the
11677 * state after boot. It would be better to only do it on
11678 * the first update, but we don't have a nice way of doing that
11679 * (and really, set_config isn't used much for high freq page
11680 * flipping, so increasing its cost here shouldn't be a big
11681 * deal).
11682 */
Jani Nikulad330a952014-01-21 11:24:25 +020011683 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080011684 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020011685 }
11686
Chris Wilson2d05eae2013-05-03 17:36:25 +010011687 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020011688 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11689 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020011690fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010011691 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020011692
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011693 /*
11694 * HACK: if the pipe was on, but we didn't have a framebuffer,
11695 * force the pipe off to avoid oopsing in the modeset code
11696 * due to fb==NULL. This should only happen during boot since
11697 * we don't yet reconstruct the FB from the hardware state.
11698 */
11699 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11700 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11701
Chris Wilson2d05eae2013-05-03 17:36:25 +010011702 /* Try to restore the config */
11703 if (config->mode_changed &&
11704 intel_set_mode(save_set.crtc, save_set.mode,
11705 save_set.x, save_set.y, save_set.fb))
11706 DRM_ERROR("failed to restore config after modeset failure\n");
11707 }
Daniel Vetter50f56112012-07-02 09:35:43 +020011708
Daniel Vetterd9e55602012-07-04 22:16:09 +020011709out_config:
11710 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020011711 return ret;
11712}
11713
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011714static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011715 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020011716 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011717 .destroy = intel_crtc_destroy,
11718 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080011719 .atomic_duplicate_state = intel_crtc_duplicate_state,
11720 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011721};
11722
Daniel Vetter53589012013-06-05 13:34:16 +020011723static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11724 struct intel_shared_dpll *pll,
11725 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011726{
Daniel Vetter53589012013-06-05 13:34:16 +020011727 uint32_t val;
11728
Daniel Vetterf458ebb2014-09-30 10:56:39 +020011729 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030011730 return false;
11731
Daniel Vetter53589012013-06-05 13:34:16 +020011732 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020011733 hw_state->dpll = val;
11734 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11735 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020011736
11737 return val & DPLL_VCO_ENABLE;
11738}
11739
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011740static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11741 struct intel_shared_dpll *pll)
11742{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011743 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
11744 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011745}
11746
Daniel Vettere7b903d2013-06-05 13:34:14 +020011747static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11748 struct intel_shared_dpll *pll)
11749{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011750 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020011751 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020011752
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011753 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011754
11755 /* Wait for the clocks to stabilize. */
11756 POSTING_READ(PCH_DPLL(pll->id));
11757 udelay(150);
11758
11759 /* The pixel multiplier can only be updated once the
11760 * DPLL is enabled and the clocks are stable.
11761 *
11762 * So write it again.
11763 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011764 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011765 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020011766 udelay(200);
11767}
11768
11769static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11770 struct intel_shared_dpll *pll)
11771{
11772 struct drm_device *dev = dev_priv->dev;
11773 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011774
11775 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011776 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020011777 if (intel_crtc_to_shared_dpll(crtc) == pll)
11778 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
11779 }
11780
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011781 I915_WRITE(PCH_DPLL(pll->id), 0);
11782 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020011783 udelay(200);
11784}
11785
Daniel Vetter46edb022013-06-05 13:34:12 +020011786static char *ibx_pch_dpll_names[] = {
11787 "PCH DPLL A",
11788 "PCH DPLL B",
11789};
11790
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011791static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011792{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011793 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011794 int i;
11795
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011796 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011797
Daniel Vettere72f9fb2013-06-05 13:34:06 +020011798 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020011799 dev_priv->shared_dplls[i].id = i;
11800 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011801 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011802 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11803 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020011804 dev_priv->shared_dplls[i].get_hw_state =
11805 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011806 }
11807}
11808
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011809static void intel_shared_dpll_init(struct drm_device *dev)
11810{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011811 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011812
Daniel Vetter9cd86932014-06-25 22:01:57 +030011813 if (HAS_DDI(dev))
11814 intel_ddi_pll_init(dev);
11815 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011816 ibx_pch_dpll_init(dev);
11817 else
11818 dev_priv->num_shared_dpll = 0;
11819
11820 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011821}
11822
Matt Roper6beb8c232014-12-01 15:40:14 -080011823/**
11824 * intel_prepare_plane_fb - Prepare fb for usage on plane
11825 * @plane: drm plane to prepare for
11826 * @fb: framebuffer to prepare for presentation
11827 *
11828 * Prepares a framebuffer for usage on a display plane. Generally this
11829 * involves pinning the underlying object and updating the frontbuffer tracking
11830 * bits. Some older platforms need special physical address handling for
11831 * cursor planes.
11832 *
11833 * Returns 0 on success, negative error code on failure.
11834 */
11835int
11836intel_prepare_plane_fb(struct drm_plane *plane,
11837 struct drm_framebuffer *fb)
Matt Roper465c1202014-05-29 08:06:54 -070011838{
11839 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080011840 struct intel_plane *intel_plane = to_intel_plane(plane);
11841 enum pipe pipe = intel_plane->pipe;
11842 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11843 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
11844 unsigned frontbuffer_bits = 0;
11845 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070011846
Matt Roperea2c67b2014-12-23 10:41:52 -080011847 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070011848 return 0;
11849
Matt Roper6beb8c232014-12-01 15:40:14 -080011850 switch (plane->type) {
11851 case DRM_PLANE_TYPE_PRIMARY:
11852 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
11853 break;
11854 case DRM_PLANE_TYPE_CURSOR:
11855 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
11856 break;
11857 case DRM_PLANE_TYPE_OVERLAY:
11858 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
11859 break;
11860 }
Matt Roper465c1202014-05-29 08:06:54 -070011861
Matt Roper4c345742014-07-09 16:22:10 -070011862 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070011863
Matt Roper6beb8c232014-12-01 15:40:14 -080011864 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
11865 INTEL_INFO(dev)->cursor_needs_physical) {
11866 int align = IS_I830(dev) ? 16 * 1024 : 256;
11867 ret = i915_gem_object_attach_phys(obj, align);
11868 if (ret)
11869 DRM_DEBUG_KMS("failed to attach phys object\n");
11870 } else {
11871 ret = intel_pin_and_fence_fb_obj(plane, fb, NULL);
11872 }
11873
11874 if (ret == 0)
11875 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
11876
11877 mutex_unlock(&dev->struct_mutex);
11878
11879 return ret;
11880}
11881
Matt Roper38f3ce32014-12-02 07:45:25 -080011882/**
11883 * intel_cleanup_plane_fb - Cleans up an fb after plane use
11884 * @plane: drm plane to clean up for
11885 * @fb: old framebuffer that was on plane
11886 *
11887 * Cleans up a framebuffer that has just been removed from a plane.
11888 */
11889void
11890intel_cleanup_plane_fb(struct drm_plane *plane,
11891 struct drm_framebuffer *fb)
11892{
11893 struct drm_device *dev = plane->dev;
11894 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11895
11896 if (WARN_ON(!obj))
11897 return;
11898
11899 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
11900 !INTEL_INFO(dev)->cursor_needs_physical) {
11901 mutex_lock(&dev->struct_mutex);
11902 intel_unpin_fb_obj(obj);
11903 mutex_unlock(&dev->struct_mutex);
11904 }
Matt Roper465c1202014-05-29 08:06:54 -070011905}
11906
11907static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011908intel_check_primary_plane(struct drm_plane *plane,
11909 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070011910{
Matt Roper32b7eee2014-12-24 07:59:06 -080011911 struct drm_device *dev = plane->dev;
11912 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080011913 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080011914 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080011915 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011916 struct drm_rect *dest = &state->dst;
11917 struct drm_rect *src = &state->src;
11918 const struct drm_rect *clip = &state->clip;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011919 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011920
Matt Roperea2c67b2014-12-23 10:41:52 -080011921 crtc = crtc ? crtc : plane->crtc;
11922 intel_crtc = to_intel_crtc(crtc);
11923
Matt Roperc59cb172014-12-01 15:40:16 -080011924 ret = drm_plane_helper_check_update(plane, crtc, fb,
11925 src, dest, clip,
11926 DRM_PLANE_HELPER_NO_SCALING,
11927 DRM_PLANE_HELPER_NO_SCALING,
11928 false, true, &state->visible);
11929 if (ret)
11930 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011931
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011932 if (intel_crtc->active) {
Matt Roper32b7eee2014-12-24 07:59:06 -080011933 intel_crtc->atomic.wait_for_flips = true;
11934
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011935 /*
11936 * FBC does not work on some platforms for rotated
11937 * planes, so disable it when rotation is not 0 and
11938 * update it when rotation is set back to 0.
11939 *
11940 * FIXME: This is redundant with the fbc update done in
11941 * the primary plane enable function except that that
11942 * one is done too late. We eventually need to unify
11943 * this.
11944 */
11945 if (intel_crtc->primary_enabled &&
11946 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11947 dev_priv->fbc.plane == intel_crtc->plane &&
Matt Roper8e7d6882015-01-21 16:35:41 -080011948 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080011949 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011950 }
11951
11952 if (state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080011953 /*
11954 * BDW signals flip done immediately if the plane
11955 * is disabled, even if the plane enable is already
11956 * armed to occur at the next vblank :(
11957 */
11958 if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
11959 intel_crtc->atomic.wait_vblank = true;
11960 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011961
Matt Roper32b7eee2014-12-24 07:59:06 -080011962 intel_crtc->atomic.fb_bits |=
11963 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
11964
11965 intel_crtc->atomic.update_fbc = true;
Matt Roperc59cb172014-12-01 15:40:16 -080011966 }
11967
11968 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070011969}
11970
Sonika Jindal48404c12014-08-22 14:06:04 +053011971static void
11972intel_commit_primary_plane(struct drm_plane *plane,
11973 struct intel_plane_state *state)
11974{
Matt Roper2b875c22014-12-01 15:40:13 -080011975 struct drm_crtc *crtc = state->base.crtc;
11976 struct drm_framebuffer *fb = state->base.fb;
11977 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053011978 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080011979 struct intel_crtc *intel_crtc;
Sonika Jindal48404c12014-08-22 14:06:04 +053011980 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Sonika Jindalce54d852014-08-21 11:44:39 +053011981 struct intel_plane *intel_plane = to_intel_plane(plane);
11982 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080011983
Matt Roperea2c67b2014-12-23 10:41:52 -080011984 crtc = crtc ? crtc : plane->crtc;
11985 intel_crtc = to_intel_crtc(crtc);
11986
Matt Ropercf4c7c12014-12-04 10:27:42 -080011987 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053011988 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070011989 crtc->y = src->y1 >> 16;
11990
Sonika Jindalce54d852014-08-21 11:44:39 +053011991 intel_plane->obj = obj;
Matt Roper465c1202014-05-29 08:06:54 -070011992
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011993 if (intel_crtc->active) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011994 if (state->visible) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011995 /* FIXME: kill this fastboot hack */
11996 intel_update_pipe_size(intel_crtc);
11997
11998 intel_crtc->primary_enabled = true;
11999
12000 dev_priv->display.update_primary_plane(crtc, plane->fb,
12001 crtc->x, crtc->y);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012002 } else {
12003 /*
12004 * If clipping results in a non-visible primary plane,
12005 * we'll disable the primary plane. Note that this is
12006 * a bit different than what happens if userspace
12007 * explicitly disables the plane by passing fb=0
12008 * because plane->fb still gets set and pinned.
12009 */
12010 intel_disable_primary_hw_plane(plane, crtc);
12011 }
Matt Roper32b7eee2014-12-24 07:59:06 -080012012 }
12013}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012014
Matt Roper32b7eee2014-12-24 07:59:06 -080012015static void intel_begin_crtc_commit(struct drm_crtc *crtc)
12016{
12017 struct drm_device *dev = crtc->dev;
12018 struct drm_i915_private *dev_priv = dev->dev_private;
12019 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080012020 struct intel_plane *intel_plane;
12021 struct drm_plane *p;
12022 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012023
Matt Roperea2c67b2014-12-23 10:41:52 -080012024 /* Track fb's for any planes being disabled */
12025 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
12026 intel_plane = to_intel_plane(p);
12027
12028 if (intel_crtc->atomic.disabled_planes &
12029 (1 << drm_plane_index(p))) {
12030 switch (p->type) {
12031 case DRM_PLANE_TYPE_PRIMARY:
12032 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
12033 break;
12034 case DRM_PLANE_TYPE_CURSOR:
12035 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
12036 break;
12037 case DRM_PLANE_TYPE_OVERLAY:
12038 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
12039 break;
12040 }
12041
12042 mutex_lock(&dev->struct_mutex);
12043 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
12044 mutex_unlock(&dev->struct_mutex);
12045 }
12046 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012047
Matt Roper32b7eee2014-12-24 07:59:06 -080012048 if (intel_crtc->atomic.wait_for_flips)
12049 intel_crtc_wait_for_pending_flips(crtc);
12050
12051 if (intel_crtc->atomic.disable_fbc)
12052 intel_fbc_disable(dev);
12053
12054 if (intel_crtc->atomic.pre_disable_primary)
12055 intel_pre_disable_primary(crtc);
12056
12057 if (intel_crtc->atomic.update_wm)
12058 intel_update_watermarks(crtc);
12059
12060 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080012061
12062 /* Perform vblank evasion around commit operation */
12063 if (intel_crtc->active)
12064 intel_crtc->atomic.evade =
12065 intel_pipe_update_start(intel_crtc,
12066 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080012067}
12068
12069static void intel_finish_crtc_commit(struct drm_crtc *crtc)
12070{
12071 struct drm_device *dev = crtc->dev;
12072 struct drm_i915_private *dev_priv = dev->dev_private;
12073 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12074 struct drm_plane *p;
12075
Matt Roperc34c9ee2014-12-23 10:41:50 -080012076 if (intel_crtc->atomic.evade)
12077 intel_pipe_update_end(intel_crtc,
12078 intel_crtc->atomic.start_vbl_count);
12079
Matt Roper32b7eee2014-12-24 07:59:06 -080012080 intel_runtime_pm_put(dev_priv);
12081
12082 if (intel_crtc->atomic.wait_vblank)
12083 intel_wait_for_vblank(dev, intel_crtc->pipe);
12084
12085 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
12086
12087 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012088 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020012089 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012090 mutex_unlock(&dev->struct_mutex);
12091 }
Matt Roper465c1202014-05-29 08:06:54 -070012092
Matt Roper32b7eee2014-12-24 07:59:06 -080012093 if (intel_crtc->atomic.post_enable_primary)
12094 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012095
Matt Roper32b7eee2014-12-24 07:59:06 -080012096 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
12097 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
12098 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
12099 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012100
Matt Roper32b7eee2014-12-24 07:59:06 -080012101 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012102}
12103
Matt Ropercf4c7c12014-12-04 10:27:42 -080012104/**
Matt Roper4a3b8762014-12-23 10:41:51 -080012105 * intel_plane_destroy - destroy a plane
12106 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080012107 *
Matt Roper4a3b8762014-12-23 10:41:51 -080012108 * Common destruction function for all types of planes (primary, cursor,
12109 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080012110 */
Matt Roper4a3b8762014-12-23 10:41:51 -080012111void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070012112{
12113 struct intel_plane *intel_plane = to_intel_plane(plane);
12114 drm_plane_cleanup(plane);
12115 kfree(intel_plane);
12116}
12117
Matt Roper65a3fea2015-01-21 16:35:42 -080012118const struct drm_plane_funcs intel_plane_funcs = {
Matt Roperea2c67b2014-12-23 10:41:52 -080012119 .update_plane = drm_plane_helper_update,
12120 .disable_plane = drm_plane_helper_disable,
Matt Roper3d7d6512014-06-10 08:28:13 -070012121 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080012122 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080012123 .atomic_get_property = intel_plane_atomic_get_property,
12124 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080012125 .atomic_duplicate_state = intel_plane_duplicate_state,
12126 .atomic_destroy_state = intel_plane_destroy_state,
12127
Matt Roper465c1202014-05-29 08:06:54 -070012128};
12129
12130static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
12131 int pipe)
12132{
12133 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080012134 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070012135 const uint32_t *intel_primary_formats;
12136 int num_formats;
12137
12138 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
12139 if (primary == NULL)
12140 return NULL;
12141
Matt Roper8e7d6882015-01-21 16:35:41 -080012142 state = intel_create_plane_state(&primary->base);
12143 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080012144 kfree(primary);
12145 return NULL;
12146 }
Matt Roper8e7d6882015-01-21 16:35:41 -080012147 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080012148
Matt Roper465c1202014-05-29 08:06:54 -070012149 primary->can_scale = false;
12150 primary->max_downscale = 1;
12151 primary->pipe = pipe;
12152 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080012153 primary->check_plane = intel_check_primary_plane;
12154 primary->commit_plane = intel_commit_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070012155 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
12156 primary->plane = !pipe;
12157
12158 if (INTEL_INFO(dev)->gen <= 3) {
12159 intel_primary_formats = intel_primary_formats_gen2;
12160 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
12161 } else {
12162 intel_primary_formats = intel_primary_formats_gen4;
12163 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
12164 }
12165
12166 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080012167 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070012168 intel_primary_formats, num_formats,
12169 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053012170
12171 if (INTEL_INFO(dev)->gen >= 4) {
12172 if (!dev->mode_config.rotation_property)
12173 dev->mode_config.rotation_property =
12174 drm_mode_create_rotation_property(dev,
12175 BIT(DRM_ROTATE_0) |
12176 BIT(DRM_ROTATE_180));
12177 if (dev->mode_config.rotation_property)
12178 drm_object_attach_property(&primary->base.base,
12179 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080012180 state->base.rotation);
Sonika Jindal48404c12014-08-22 14:06:04 +053012181 }
12182
Matt Roperea2c67b2014-12-23 10:41:52 -080012183 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
12184
Matt Roper465c1202014-05-29 08:06:54 -070012185 return &primary->base;
12186}
12187
Matt Roper3d7d6512014-06-10 08:28:13 -070012188static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030012189intel_check_cursor_plane(struct drm_plane *plane,
12190 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070012191{
Matt Roper2b875c22014-12-01 15:40:13 -080012192 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080012193 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080012194 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012195 struct drm_rect *dest = &state->dst;
12196 struct drm_rect *src = &state->src;
12197 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012198 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080012199 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012200 unsigned stride;
12201 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012202
Matt Roperea2c67b2014-12-23 10:41:52 -080012203 crtc = crtc ? crtc : plane->crtc;
12204 intel_crtc = to_intel_crtc(crtc);
12205
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012206 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030012207 src, dest, clip,
12208 DRM_PLANE_HELPER_NO_SCALING,
12209 DRM_PLANE_HELPER_NO_SCALING,
12210 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012211 if (ret)
12212 return ret;
12213
12214
12215 /* if we want to turn off the cursor ignore width and height */
12216 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080012217 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012218
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012219 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080012220 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
12221 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
12222 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012223 return -EINVAL;
12224 }
12225
Matt Roperea2c67b2014-12-23 10:41:52 -080012226 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
12227 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012228 DRM_DEBUG_KMS("buffer is too small\n");
12229 return -ENOMEM;
12230 }
12231
12232 /* we only need to pin inside GTT if cursor is non-phy */
12233 mutex_lock(&dev->struct_mutex);
12234 if (!INTEL_INFO(dev)->cursor_needs_physical && obj->tiling_mode) {
12235 DRM_DEBUG_KMS("cursor cannot be tiled\n");
12236 ret = -EINVAL;
12237 }
12238 mutex_unlock(&dev->struct_mutex);
12239
Matt Roper32b7eee2014-12-24 07:59:06 -080012240finish:
12241 if (intel_crtc->active) {
Matt Roperea2c67b2014-12-23 10:41:52 -080012242 if (intel_crtc->cursor_width != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080012243 intel_crtc->atomic.update_wm = true;
12244
12245 intel_crtc->atomic.fb_bits |=
12246 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
12247 }
12248
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012249 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012250}
12251
Matt Roperf4a2cf22014-12-01 15:40:12 -080012252static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030012253intel_commit_cursor_plane(struct drm_plane *plane,
12254 struct intel_plane_state *state)
12255{
Matt Roper2b875c22014-12-01 15:40:13 -080012256 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080012257 struct drm_device *dev = plane->dev;
12258 struct intel_crtc *intel_crtc;
Sonika Jindala919db92014-10-23 07:41:33 -070012259 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper2b875c22014-12-01 15:40:13 -080012260 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080012261 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070012262
Matt Roperea2c67b2014-12-23 10:41:52 -080012263 crtc = crtc ? crtc : plane->crtc;
12264 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070012265
Matt Roperea2c67b2014-12-23 10:41:52 -080012266 plane->fb = state->base.fb;
12267 crtc->cursor_x = state->base.crtc_x;
12268 crtc->cursor_y = state->base.crtc_y;
12269
Sonika Jindala919db92014-10-23 07:41:33 -070012270 intel_plane->obj = obj;
12271
Gustavo Padovana912f122014-12-01 15:40:10 -080012272 if (intel_crtc->cursor_bo == obj)
12273 goto update;
12274
Matt Roperf4a2cf22014-12-01 15:40:12 -080012275 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080012276 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080012277 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080012278 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080012279 else
Gustavo Padovana912f122014-12-01 15:40:10 -080012280 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080012281
Gustavo Padovana912f122014-12-01 15:40:10 -080012282 intel_crtc->cursor_addr = addr;
12283 intel_crtc->cursor_bo = obj;
12284update:
Matt Roperea2c67b2014-12-23 10:41:52 -080012285 intel_crtc->cursor_width = state->base.crtc_w;
12286 intel_crtc->cursor_height = state->base.crtc_h;
Gustavo Padovana912f122014-12-01 15:40:10 -080012287
Matt Roper32b7eee2014-12-24 07:59:06 -080012288 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030012289 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070012290}
Gustavo Padovan852e7872014-09-05 17:22:31 -030012291
Matt Roper3d7d6512014-06-10 08:28:13 -070012292static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
12293 int pipe)
12294{
12295 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080012296 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070012297
12298 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
12299 if (cursor == NULL)
12300 return NULL;
12301
Matt Roper8e7d6882015-01-21 16:35:41 -080012302 state = intel_create_plane_state(&cursor->base);
12303 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080012304 kfree(cursor);
12305 return NULL;
12306 }
Matt Roper8e7d6882015-01-21 16:35:41 -080012307 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080012308
Matt Roper3d7d6512014-06-10 08:28:13 -070012309 cursor->can_scale = false;
12310 cursor->max_downscale = 1;
12311 cursor->pipe = pipe;
12312 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080012313 cursor->check_plane = intel_check_cursor_plane;
12314 cursor->commit_plane = intel_commit_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070012315
12316 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080012317 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070012318 intel_cursor_formats,
12319 ARRAY_SIZE(intel_cursor_formats),
12320 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070012321
12322 if (INTEL_INFO(dev)->gen >= 4) {
12323 if (!dev->mode_config.rotation_property)
12324 dev->mode_config.rotation_property =
12325 drm_mode_create_rotation_property(dev,
12326 BIT(DRM_ROTATE_0) |
12327 BIT(DRM_ROTATE_180));
12328 if (dev->mode_config.rotation_property)
12329 drm_object_attach_property(&cursor->base.base,
12330 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080012331 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070012332 }
12333
Matt Roperea2c67b2014-12-23 10:41:52 -080012334 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
12335
Matt Roper3d7d6512014-06-10 08:28:13 -070012336 return &cursor->base;
12337}
12338
Hannes Ederb358d0a2008-12-18 21:18:47 +010012339static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080012340{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012341 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080012342 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012343 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070012344 struct drm_plane *primary = NULL;
12345 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070012346 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080012347
Daniel Vetter955382f2013-09-19 14:05:45 +020012348 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080012349 if (intel_crtc == NULL)
12350 return;
12351
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012352 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
12353 if (!crtc_state)
12354 goto fail;
12355 intel_crtc_set_state(intel_crtc, crtc_state);
12356
Matt Roper465c1202014-05-29 08:06:54 -070012357 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070012358 if (!primary)
12359 goto fail;
12360
12361 cursor = intel_cursor_plane_create(dev, pipe);
12362 if (!cursor)
12363 goto fail;
12364
Matt Roper465c1202014-05-29 08:06:54 -070012365 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070012366 cursor, &intel_crtc_funcs);
12367 if (ret)
12368 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080012369
12370 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080012371 for (i = 0; i < 256; i++) {
12372 intel_crtc->lut_r[i] = i;
12373 intel_crtc->lut_g[i] = i;
12374 intel_crtc->lut_b[i] = i;
12375 }
12376
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020012377 /*
12378 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020012379 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020012380 */
Jesse Barnes80824002009-09-10 15:28:06 -070012381 intel_crtc->pipe = pipe;
12382 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010012383 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080012384 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010012385 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070012386 }
12387
Chris Wilson4b0e3332014-05-30 16:35:26 +030012388 intel_crtc->cursor_base = ~0;
12389 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030012390 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030012391
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080012392 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
12393 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
12394 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
12395 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
12396
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020012397 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
12398
Jesse Barnes79e53942008-11-07 14:24:08 -080012399 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020012400
12401 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070012402 return;
12403
12404fail:
12405 if (primary)
12406 drm_plane_cleanup(primary);
12407 if (cursor)
12408 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012409 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070012410 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080012411}
12412
Jesse Barnes752aa882013-10-31 18:55:49 +020012413enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
12414{
12415 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012416 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020012417
Rob Clark51fd3712013-11-19 12:10:12 -050012418 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020012419
Ville Syrjäläd3babd32014-11-07 11:16:01 +020012420 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020012421 return INVALID_PIPE;
12422
12423 return to_intel_crtc(encoder->crtc)->pipe;
12424}
12425
Carl Worth08d7b3d2009-04-29 14:43:54 -070012426int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000012427 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070012428{
Carl Worth08d7b3d2009-04-29 14:43:54 -070012429 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040012430 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020012431 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012432
Daniel Vetter1cff8f62012-04-24 09:55:08 +020012433 if (!drm_core_check_feature(dev, DRIVER_MODESET))
12434 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012435
Rob Clark7707e652014-07-17 23:30:04 -040012436 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070012437
Rob Clark7707e652014-07-17 23:30:04 -040012438 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070012439 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030012440 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012441 }
12442
Rob Clark7707e652014-07-17 23:30:04 -040012443 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020012444 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012445
Daniel Vetterc05422d2009-08-11 16:05:30 +020012446 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012447}
12448
Daniel Vetter66a92782012-07-12 20:08:18 +020012449static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080012450{
Daniel Vetter66a92782012-07-12 20:08:18 +020012451 struct drm_device *dev = encoder->base.dev;
12452 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080012453 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080012454 int entry = 0;
12455
Damien Lespiaub2784e12014-08-05 11:29:37 +010012456 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012457 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020012458 index_mask |= (1 << entry);
12459
Jesse Barnes79e53942008-11-07 14:24:08 -080012460 entry++;
12461 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010012462
Jesse Barnes79e53942008-11-07 14:24:08 -080012463 return index_mask;
12464}
12465
Chris Wilson4d302442010-12-14 19:21:29 +000012466static bool has_edp_a(struct drm_device *dev)
12467{
12468 struct drm_i915_private *dev_priv = dev->dev_private;
12469
12470 if (!IS_MOBILE(dev))
12471 return false;
12472
12473 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
12474 return false;
12475
Damien Lespiaue3589902014-02-07 19:12:50 +000012476 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000012477 return false;
12478
12479 return true;
12480}
12481
Jesse Barnes84b4e042014-06-25 08:24:29 -070012482static bool intel_crt_present(struct drm_device *dev)
12483{
12484 struct drm_i915_private *dev_priv = dev->dev_private;
12485
Damien Lespiau884497e2013-12-03 13:56:23 +000012486 if (INTEL_INFO(dev)->gen >= 9)
12487 return false;
12488
Damien Lespiaucf404ce2014-10-01 20:04:15 +010012489 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070012490 return false;
12491
12492 if (IS_CHERRYVIEW(dev))
12493 return false;
12494
12495 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
12496 return false;
12497
12498 return true;
12499}
12500
Jesse Barnes79e53942008-11-07 14:24:08 -080012501static void intel_setup_outputs(struct drm_device *dev)
12502{
Eric Anholt725e30a2009-01-22 13:01:02 -080012503 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010012504 struct intel_encoder *encoder;
Matt Roperc6f95f22015-01-22 16:50:32 -080012505 struct drm_connector *connector;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012506 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080012507
Daniel Vetterc9093352013-06-06 22:22:47 +020012508 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012509
Jesse Barnes84b4e042014-06-25 08:24:29 -070012510 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020012511 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012512
Paulo Zanoniaffa9352012-11-23 15:30:39 -020012513 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030012514 int found;
12515
12516 /* Haswell uses DDI functions to detect digital outputs */
12517 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
12518 /* DDI A only supports eDP */
12519 if (found)
12520 intel_ddi_init(dev, PORT_A);
12521
12522 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
12523 * register */
12524 found = I915_READ(SFUSE_STRAP);
12525
12526 if (found & SFUSE_STRAP_DDIB_DETECTED)
12527 intel_ddi_init(dev, PORT_B);
12528 if (found & SFUSE_STRAP_DDIC_DETECTED)
12529 intel_ddi_init(dev, PORT_C);
12530 if (found & SFUSE_STRAP_DDID_DETECTED)
12531 intel_ddi_init(dev, PORT_D);
12532 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012533 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020012534 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020012535
12536 if (has_edp_a(dev))
12537 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012538
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012539 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080012540 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010012541 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012542 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012543 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012544 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012545 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012546 }
12547
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012548 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012549 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012550
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012551 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012552 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012553
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012554 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012555 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012556
Daniel Vetter270b3042012-10-27 15:52:05 +020012557 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012558 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070012559 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012560 /*
12561 * The DP_DETECTED bit is the latched state of the DDC
12562 * SDA pin at boot. However since eDP doesn't require DDC
12563 * (no way to plug in a DP->HDMI dongle) the DDC pins for
12564 * eDP ports may have been muxed to an alternate function.
12565 * Thus we can't rely on the DP_DETECTED bit alone to detect
12566 * eDP ports. Consult the VBT as well as DP_DETECTED to
12567 * detect eDP ports.
12568 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020012569 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
12570 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030012571 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
12572 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012573 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
12574 intel_dp_is_edp(dev, PORT_B))
12575 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030012576
Ville Syrjäläd2182a62015-01-09 14:21:14 +020012577 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
12578 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070012579 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
12580 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012581 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
12582 intel_dp_is_edp(dev, PORT_C))
12583 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053012584
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030012585 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012586 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030012587 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
12588 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012589 /* eDP not supported on port D, so don't check VBT */
12590 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
12591 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030012592 }
12593
Jani Nikula3cfca972013-08-27 15:12:26 +030012594 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080012595 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080012596 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080012597
Paulo Zanonie2debe92013-02-18 19:00:27 -030012598 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012599 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012600 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012601 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
12602 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012603 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012604 }
Ma Ling27185ae2009-08-24 13:50:23 +080012605
Imre Deake7281ea2013-05-08 13:14:08 +030012606 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012607 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080012608 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040012609
12610 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040012611
Paulo Zanonie2debe92013-02-18 19:00:27 -030012612 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012613 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012614 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012615 }
Ma Ling27185ae2009-08-24 13:50:23 +080012616
Paulo Zanonie2debe92013-02-18 19:00:27 -030012617 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080012618
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012619 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
12620 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012621 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012622 }
Imre Deake7281ea2013-05-08 13:14:08 +030012623 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012624 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080012625 }
Ma Ling27185ae2009-08-24 13:50:23 +080012626
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012627 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030012628 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012629 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070012630 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080012631 intel_dvo_init(dev);
12632
Zhenyu Wang103a1962009-11-27 11:44:36 +080012633 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080012634 intel_tv_init(dev);
12635
Matt Roperc6f95f22015-01-22 16:50:32 -080012636 /*
12637 * FIXME: We don't have full atomic support yet, but we want to be
12638 * able to enable/test plane updates via the atomic interface in the
12639 * meantime. However as soon as we flip DRIVER_ATOMIC on, the DRM core
12640 * will take some atomic codepaths to lookup properties during
12641 * drmModeGetConnector() that unconditionally dereference
12642 * connector->state.
12643 *
12644 * We create a dummy connector state here for each connector to ensure
12645 * the DRM core doesn't try to dereference a NULL connector->state.
12646 * The actual connector properties will never be updated or contain
12647 * useful information, but since we're doing this specifically for
12648 * testing/debug of the plane operations (and only when a specific
12649 * kernel module option is given), that shouldn't really matter.
12650 *
12651 * Once atomic support for crtc's + connectors lands, this loop should
12652 * be removed since we'll be setting up real connector state, which
12653 * will contain Intel-specific properties.
12654 */
12655 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
12656 list_for_each_entry(connector,
12657 &dev->mode_config.connector_list,
12658 head) {
12659 if (!WARN_ON(connector->state)) {
12660 connector->state =
12661 kzalloc(sizeof(*connector->state),
12662 GFP_KERNEL);
12663 }
12664 }
12665 }
12666
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080012667 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070012668
Damien Lespiaub2784e12014-08-05 11:29:37 +010012669 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010012670 encoder->base.possible_crtcs = encoder->crtc_mask;
12671 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020012672 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080012673 }
Chris Wilson47356eb2011-01-11 17:06:04 +000012674
Paulo Zanonidde86e22012-12-01 12:04:25 -020012675 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020012676
12677 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012678}
12679
12680static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
12681{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012682 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080012683 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080012684
Daniel Vetteref2d6332014-02-10 18:00:38 +010012685 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012686 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010012687 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012688 drm_gem_object_unreference(&intel_fb->obj->base);
12689 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080012690 kfree(intel_fb);
12691}
12692
12693static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000012694 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080012695 unsigned int *handle)
12696{
12697 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000012698 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080012699
Chris Wilson05394f32010-11-08 19:18:58 +000012700 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080012701}
12702
12703static const struct drm_framebuffer_funcs intel_fb_funcs = {
12704 .destroy = intel_user_framebuffer_destroy,
12705 .create_handle = intel_user_framebuffer_create_handle,
12706};
12707
Daniel Vetterb5ea6422014-03-02 21:18:00 +010012708static int intel_framebuffer_init(struct drm_device *dev,
12709 struct intel_framebuffer *intel_fb,
12710 struct drm_mode_fb_cmd2 *mode_cmd,
12711 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080012712{
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080012713 int aligned_height;
Chris Wilsona35cdaa2013-06-25 17:26:45 +010012714 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -080012715 int ret;
12716
Daniel Vetterdd4916c2013-10-09 21:23:51 +020012717 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
12718
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012719 if (obj->tiling_mode == I915_TILING_Y) {
12720 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +010012721 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012722 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012723
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012724 if (mode_cmd->pitches[0] & 63) {
12725 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
12726 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010012727 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012728 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012729
Chris Wilsona35cdaa2013-06-25 17:26:45 +010012730 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
12731 pitch_limit = 32*1024;
12732 } else if (INTEL_INFO(dev)->gen >= 4) {
12733 if (obj->tiling_mode)
12734 pitch_limit = 16*1024;
12735 else
12736 pitch_limit = 32*1024;
12737 } else if (INTEL_INFO(dev)->gen >= 3) {
12738 if (obj->tiling_mode)
12739 pitch_limit = 8*1024;
12740 else
12741 pitch_limit = 16*1024;
12742 } else
12743 /* XXX DSPC is limited to 4k tiled */
12744 pitch_limit = 8*1024;
12745
12746 if (mode_cmd->pitches[0] > pitch_limit) {
12747 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
12748 obj->tiling_mode ? "tiled" : "linear",
12749 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012750 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012751 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012752
12753 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012754 mode_cmd->pitches[0] != obj->stride) {
12755 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
12756 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012757 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012758 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012759
Ville Syrjälä57779d02012-10-31 17:50:14 +020012760 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012761 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020012762 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020012763 case DRM_FORMAT_RGB565:
12764 case DRM_FORMAT_XRGB8888:
12765 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020012766 break;
12767 case DRM_FORMAT_XRGB1555:
12768 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012769 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012770 DRM_DEBUG("unsupported pixel format: %s\n",
12771 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012772 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012773 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020012774 break;
12775 case DRM_FORMAT_XBGR8888:
12776 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020012777 case DRM_FORMAT_XRGB2101010:
12778 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020012779 case DRM_FORMAT_XBGR2101010:
12780 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012781 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012782 DRM_DEBUG("unsupported pixel format: %s\n",
12783 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012784 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012785 }
Jesse Barnesb5626742011-06-24 12:19:27 -070012786 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020012787 case DRM_FORMAT_YUYV:
12788 case DRM_FORMAT_UYVY:
12789 case DRM_FORMAT_YVYU:
12790 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012791 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012792 DRM_DEBUG("unsupported pixel format: %s\n",
12793 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012794 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012795 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012796 break;
12797 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012798 DRM_DEBUG("unsupported pixel format: %s\n",
12799 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010012800 return -EINVAL;
12801 }
12802
Ville Syrjälä90f9a332012-10-31 17:50:19 +020012803 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
12804 if (mode_cmd->offsets[0] != 0)
12805 return -EINVAL;
12806
Damien Lespiauec2c9812015-01-20 12:51:45 +000012807 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
12808 obj->tiling_mode);
Daniel Vetter53155c02013-10-09 21:55:33 +020012809 /* FIXME drm helper for size checks (especially planar formats)? */
12810 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
12811 return -EINVAL;
12812
Daniel Vetterc7d73f62012-12-13 23:38:38 +010012813 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
12814 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020012815 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010012816
Jesse Barnes79e53942008-11-07 14:24:08 -080012817 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
12818 if (ret) {
12819 DRM_ERROR("framebuffer init failed %d\n", ret);
12820 return ret;
12821 }
12822
Jesse Barnes79e53942008-11-07 14:24:08 -080012823 return 0;
12824}
12825
Jesse Barnes79e53942008-11-07 14:24:08 -080012826static struct drm_framebuffer *
12827intel_user_framebuffer_create(struct drm_device *dev,
12828 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012829 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080012830{
Chris Wilson05394f32010-11-08 19:18:58 +000012831 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080012832
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012833 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
12834 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000012835 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010012836 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080012837
Chris Wilsond2dff872011-04-19 08:36:26 +010012838 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080012839}
12840
Daniel Vetter4520f532013-10-09 09:18:51 +020012841#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020012842static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020012843{
12844}
12845#endif
12846
Jesse Barnes79e53942008-11-07 14:24:08 -080012847static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080012848 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020012849 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080012850 .atomic_check = intel_atomic_check,
12851 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080012852};
12853
Jesse Barnese70236a2009-09-21 10:42:27 -070012854/* Set up chip specific display functions */
12855static void intel_init_display(struct drm_device *dev)
12856{
12857 struct drm_i915_private *dev_priv = dev->dev_private;
12858
Daniel Vetteree9300b2013-06-03 22:40:22 +020012859 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
12860 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030012861 else if (IS_CHERRYVIEW(dev))
12862 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020012863 else if (IS_VALLEYVIEW(dev))
12864 dev_priv->display.find_dpll = vlv_find_best_dpll;
12865 else if (IS_PINEVIEW(dev))
12866 dev_priv->display.find_dpll = pnv_find_best_dpll;
12867 else
12868 dev_priv->display.find_dpll = i9xx_find_best_dpll;
12869
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000012870 if (INTEL_INFO(dev)->gen >= 9) {
12871 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000012872 dev_priv->display.get_initial_plane_config =
12873 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000012874 dev_priv->display.crtc_compute_clock =
12875 haswell_crtc_compute_clock;
12876 dev_priv->display.crtc_enable = haswell_crtc_enable;
12877 dev_priv->display.crtc_disable = haswell_crtc_disable;
12878 dev_priv->display.off = ironlake_crtc_off;
12879 dev_priv->display.update_primary_plane =
12880 skylake_update_primary_plane;
12881 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012882 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000012883 dev_priv->display.get_initial_plane_config =
12884 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020012885 dev_priv->display.crtc_compute_clock =
12886 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020012887 dev_priv->display.crtc_enable = haswell_crtc_enable;
12888 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030012889 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000012890 dev_priv->display.update_primary_plane =
12891 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030012892 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012893 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000012894 dev_priv->display.get_initial_plane_config =
12895 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020012896 dev_priv->display.crtc_compute_clock =
12897 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020012898 dev_priv->display.crtc_enable = ironlake_crtc_enable;
12899 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012900 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012901 dev_priv->display.update_primary_plane =
12902 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070012903 } else if (IS_VALLEYVIEW(dev)) {
12904 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000012905 dev_priv->display.get_initial_plane_config =
12906 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020012907 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070012908 dev_priv->display.crtc_enable = valleyview_crtc_enable;
12909 dev_priv->display.crtc_disable = i9xx_crtc_disable;
12910 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012911 dev_priv->display.update_primary_plane =
12912 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070012913 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012914 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000012915 dev_priv->display.get_initial_plane_config =
12916 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020012917 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020012918 dev_priv->display.crtc_enable = i9xx_crtc_enable;
12919 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012920 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012921 dev_priv->display.update_primary_plane =
12922 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070012923 }
Jesse Barnese70236a2009-09-21 10:42:27 -070012924
Jesse Barnese70236a2009-09-21 10:42:27 -070012925 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070012926 if (IS_VALLEYVIEW(dev))
12927 dev_priv->display.get_display_clock_speed =
12928 valleyview_get_display_clock_speed;
12929 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070012930 dev_priv->display.get_display_clock_speed =
12931 i945_get_display_clock_speed;
12932 else if (IS_I915G(dev))
12933 dev_priv->display.get_display_clock_speed =
12934 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020012935 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070012936 dev_priv->display.get_display_clock_speed =
12937 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020012938 else if (IS_PINEVIEW(dev))
12939 dev_priv->display.get_display_clock_speed =
12940 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070012941 else if (IS_I915GM(dev))
12942 dev_priv->display.get_display_clock_speed =
12943 i915gm_get_display_clock_speed;
12944 else if (IS_I865G(dev))
12945 dev_priv->display.get_display_clock_speed =
12946 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020012947 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070012948 dev_priv->display.get_display_clock_speed =
12949 i855_get_display_clock_speed;
12950 else /* 852, 830 */
12951 dev_priv->display.get_display_clock_speed =
12952 i830_get_display_clock_speed;
12953
Jani Nikula7c10a2b2014-10-27 16:26:43 +020012954 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012955 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012956 } else if (IS_GEN6(dev)) {
12957 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012958 } else if (IS_IVYBRIDGE(dev)) {
12959 /* FIXME: detect B0+ stepping and use auto training */
12960 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012961 dev_priv->display.modeset_global_resources =
12962 ivb_modeset_global_resources;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030012963 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012964 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080012965 } else if (IS_VALLEYVIEW(dev)) {
12966 dev_priv->display.modeset_global_resources =
12967 valleyview_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070012968 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070012969
12970 /* Default just returns -ENODEV to indicate unsupported */
12971 dev_priv->display.queue_flip = intel_default_queue_flip;
12972
12973 switch (INTEL_INFO(dev)->gen) {
12974 case 2:
12975 dev_priv->display.queue_flip = intel_gen2_queue_flip;
12976 break;
12977
12978 case 3:
12979 dev_priv->display.queue_flip = intel_gen3_queue_flip;
12980 break;
12981
12982 case 4:
12983 case 5:
12984 dev_priv->display.queue_flip = intel_gen4_queue_flip;
12985 break;
12986
12987 case 6:
12988 dev_priv->display.queue_flip = intel_gen6_queue_flip;
12989 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070012990 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070012991 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070012992 dev_priv->display.queue_flip = intel_gen7_queue_flip;
12993 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000012994 case 9:
12995 dev_priv->display.queue_flip = intel_gen9_queue_flip;
12996 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070012997 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020012998
12999 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030013000
13001 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070013002}
13003
Jesse Barnesb690e962010-07-19 13:53:12 -070013004/*
13005 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
13006 * resume, or other times. This quirk makes sure that's the case for
13007 * affected systems.
13008 */
Akshay Joshi0206e352011-08-16 15:34:10 -040013009static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070013010{
13011 struct drm_i915_private *dev_priv = dev->dev_private;
13012
13013 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013014 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070013015}
13016
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030013017static void quirk_pipeb_force(struct drm_device *dev)
13018{
13019 struct drm_i915_private *dev_priv = dev->dev_private;
13020
13021 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
13022 DRM_INFO("applying pipe b force quirk\n");
13023}
13024
Keith Packard435793d2011-07-12 14:56:22 -070013025/*
13026 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
13027 */
13028static void quirk_ssc_force_disable(struct drm_device *dev)
13029{
13030 struct drm_i915_private *dev_priv = dev->dev_private;
13031 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013032 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070013033}
13034
Carsten Emde4dca20e2012-03-15 15:56:26 +010013035/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010013036 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
13037 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010013038 */
13039static void quirk_invert_brightness(struct drm_device *dev)
13040{
13041 struct drm_i915_private *dev_priv = dev->dev_private;
13042 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013043 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070013044}
13045
Scot Doyle9c72cc62014-07-03 23:27:50 +000013046/* Some VBT's incorrectly indicate no backlight is present */
13047static void quirk_backlight_present(struct drm_device *dev)
13048{
13049 struct drm_i915_private *dev_priv = dev->dev_private;
13050 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
13051 DRM_INFO("applying backlight present quirk\n");
13052}
13053
Jesse Barnesb690e962010-07-19 13:53:12 -070013054struct intel_quirk {
13055 int device;
13056 int subsystem_vendor;
13057 int subsystem_device;
13058 void (*hook)(struct drm_device *dev);
13059};
13060
Egbert Eich5f85f1762012-10-14 15:46:38 +020013061/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
13062struct intel_dmi_quirk {
13063 void (*hook)(struct drm_device *dev);
13064 const struct dmi_system_id (*dmi_id_list)[];
13065};
13066
13067static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
13068{
13069 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
13070 return 1;
13071}
13072
13073static const struct intel_dmi_quirk intel_dmi_quirks[] = {
13074 {
13075 .dmi_id_list = &(const struct dmi_system_id[]) {
13076 {
13077 .callback = intel_dmi_reverse_brightness,
13078 .ident = "NCR Corporation",
13079 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
13080 DMI_MATCH(DMI_PRODUCT_NAME, ""),
13081 },
13082 },
13083 { } /* terminating entry */
13084 },
13085 .hook = quirk_invert_brightness,
13086 },
13087};
13088
Ben Widawskyc43b5632012-04-16 14:07:40 -070013089static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070013090 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040013091 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070013092
Jesse Barnesb690e962010-07-19 13:53:12 -070013093 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
13094 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
13095
Jesse Barnesb690e962010-07-19 13:53:12 -070013096 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
13097 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
13098
Ville Syrjälä5f080c02014-08-15 01:22:06 +030013099 /* 830 needs to leave pipe A & dpll A up */
13100 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
13101
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030013102 /* 830 needs to leave pipe B & dpll B up */
13103 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
13104
Keith Packard435793d2011-07-12 14:56:22 -070013105 /* Lenovo U160 cannot use SSC on LVDS */
13106 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020013107
13108 /* Sony Vaio Y cannot use SSC on LVDS */
13109 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010013110
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010013111 /* Acer Aspire 5734Z must invert backlight brightness */
13112 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
13113
13114 /* Acer/eMachines G725 */
13115 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
13116
13117 /* Acer/eMachines e725 */
13118 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
13119
13120 /* Acer/Packard Bell NCL20 */
13121 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
13122
13123 /* Acer Aspire 4736Z */
13124 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020013125
13126 /* Acer Aspire 5336 */
13127 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000013128
13129 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
13130 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000013131
Scot Doyledfb3d47b2014-08-21 16:08:02 +000013132 /* Acer C720 Chromebook (Core i3 4005U) */
13133 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
13134
jens steinb2a96012014-10-28 20:25:53 +010013135 /* Apple Macbook 2,1 (Core 2 T7400) */
13136 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
13137
Scot Doyled4967d82014-07-03 23:27:52 +000013138 /* Toshiba CB35 Chromebook (Celeron 2955U) */
13139 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000013140
13141 /* HP Chromebook 14 (Celeron 2955U) */
13142 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020013143
13144 /* Dell Chromebook 11 */
13145 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070013146};
13147
13148static void intel_init_quirks(struct drm_device *dev)
13149{
13150 struct pci_dev *d = dev->pdev;
13151 int i;
13152
13153 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
13154 struct intel_quirk *q = &intel_quirks[i];
13155
13156 if (d->device == q->device &&
13157 (d->subsystem_vendor == q->subsystem_vendor ||
13158 q->subsystem_vendor == PCI_ANY_ID) &&
13159 (d->subsystem_device == q->subsystem_device ||
13160 q->subsystem_device == PCI_ANY_ID))
13161 q->hook(dev);
13162 }
Egbert Eich5f85f1762012-10-14 15:46:38 +020013163 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
13164 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
13165 intel_dmi_quirks[i].hook(dev);
13166 }
Jesse Barnesb690e962010-07-19 13:53:12 -070013167}
13168
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013169/* Disable the VGA plane that we never use */
13170static void i915_disable_vga(struct drm_device *dev)
13171{
13172 struct drm_i915_private *dev_priv = dev->dev_private;
13173 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020013174 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013175
Ville Syrjälä2b37c612014-01-22 21:32:38 +020013176 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013177 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070013178 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013179 sr1 = inb(VGA_SR_DATA);
13180 outb(sr1 | 1<<5, VGA_SR_DATA);
13181 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
13182 udelay(300);
13183
Ville Syrjälä01f5a622014-12-16 18:38:37 +020013184 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013185 POSTING_READ(vga_reg);
13186}
13187
Daniel Vetterf8175862012-04-10 15:50:11 +020013188void intel_modeset_init_hw(struct drm_device *dev)
13189{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030013190 intel_prepare_ddi(dev);
13191
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030013192 if (IS_VALLEYVIEW(dev))
13193 vlv_update_cdclk(dev);
13194
Daniel Vetterf8175862012-04-10 15:50:11 +020013195 intel_init_clock_gating(dev);
13196
Daniel Vetter8090c6b2012-06-24 16:42:32 +020013197 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020013198}
13199
Jesse Barnes79e53942008-11-07 14:24:08 -080013200void intel_modeset_init(struct drm_device *dev)
13201{
Jesse Barnes652c3932009-08-17 13:31:43 -070013202 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000013203 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000013204 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080013205 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080013206
13207 drm_mode_config_init(dev);
13208
13209 dev->mode_config.min_width = 0;
13210 dev->mode_config.min_height = 0;
13211
Dave Airlie019d96c2011-09-29 16:20:42 +010013212 dev->mode_config.preferred_depth = 24;
13213 dev->mode_config.prefer_shadow = 1;
13214
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020013215 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080013216
Jesse Barnesb690e962010-07-19 13:53:12 -070013217 intel_init_quirks(dev);
13218
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030013219 intel_init_pm(dev);
13220
Ben Widawskye3c74752013-04-05 13:12:39 -070013221 if (INTEL_INFO(dev)->num_pipes == 0)
13222 return;
13223
Jesse Barnese70236a2009-09-21 10:42:27 -070013224 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020013225 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070013226
Chris Wilsona6c45cf2010-09-17 00:32:17 +010013227 if (IS_GEN2(dev)) {
13228 dev->mode_config.max_width = 2048;
13229 dev->mode_config.max_height = 2048;
13230 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070013231 dev->mode_config.max_width = 4096;
13232 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080013233 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010013234 dev->mode_config.max_width = 8192;
13235 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080013236 }
Damien Lespiau068be562014-03-28 14:17:49 +000013237
Ville Syrjälädc41c152014-08-13 11:57:05 +030013238 if (IS_845G(dev) || IS_I865G(dev)) {
13239 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
13240 dev->mode_config.cursor_height = 1023;
13241 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000013242 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
13243 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
13244 } else {
13245 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
13246 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
13247 }
13248
Ben Widawsky5d4545a2013-01-17 12:45:15 -080013249 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080013250
Zhao Yakui28c97732009-10-09 11:39:41 +080013251 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070013252 INTEL_INFO(dev)->num_pipes,
13253 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080013254
Damien Lespiau055e3932014-08-18 13:49:10 +010013255 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000013256 intel_crtc_init(dev, pipe);
Damien Lespiau1fe47782014-03-03 17:31:47 +000013257 for_each_sprite(pipe, sprite) {
13258 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070013259 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030013260 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000013261 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070013262 }
Jesse Barnes79e53942008-11-07 14:24:08 -080013263 }
13264
Jesse Barnesf42bb702013-12-16 16:34:23 -080013265 intel_init_dpio(dev);
13266
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013267 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013268
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013269 /* Just disable it once at startup */
13270 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013271 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000013272
13273 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013274 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080013275
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013276 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080013277 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013278 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080013279
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013280 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080013281 if (!crtc->active)
13282 continue;
13283
Jesse Barnes46f297f2014-03-07 08:57:48 -080013284 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080013285 * Note that reserving the BIOS fb up front prevents us
13286 * from stuffing other stolen allocations like the ring
13287 * on top. This prevents some ugliness at boot time, and
13288 * can even allow for smooth boot transitions if the BIOS
13289 * fb is large enough for the active pipe configuration.
13290 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013291 if (dev_priv->display.get_initial_plane_config) {
13292 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080013293 &crtc->plane_config);
13294 /*
13295 * If the fb is shared between multiple heads, we'll
13296 * just get the first one.
13297 */
Jesse Barnes484b41d2014-03-07 08:57:55 -080013298 intel_find_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080013299 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080013300 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010013301}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080013302
Daniel Vetter7fad7982012-07-04 17:51:47 +020013303static void intel_enable_pipe_a(struct drm_device *dev)
13304{
13305 struct intel_connector *connector;
13306 struct drm_connector *crt = NULL;
13307 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030013308 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020013309
13310 /* We can't just switch on the pipe A, we need to set things up with a
13311 * proper mode and output configuration. As a gross hack, enable pipe A
13312 * by enabling the load detect pipe once. */
13313 list_for_each_entry(connector,
13314 &dev->mode_config.connector_list,
13315 base.head) {
13316 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
13317 crt = &connector->base;
13318 break;
13319 }
13320 }
13321
13322 if (!crt)
13323 return;
13324
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030013325 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
13326 intel_release_load_detect_pipe(crt, &load_detect_temp);
Daniel Vetter7fad7982012-07-04 17:51:47 +020013327}
13328
Daniel Vetterfa555832012-10-10 23:14:00 +020013329static bool
13330intel_check_plane_mapping(struct intel_crtc *crtc)
13331{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070013332 struct drm_device *dev = crtc->base.dev;
13333 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020013334 u32 reg, val;
13335
Ben Widawsky7eb552a2013-03-13 14:05:41 -070013336 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020013337 return true;
13338
13339 reg = DSPCNTR(!crtc->plane);
13340 val = I915_READ(reg);
13341
13342 if ((val & DISPLAY_PLANE_ENABLE) &&
13343 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
13344 return false;
13345
13346 return true;
13347}
13348
Daniel Vetter24929352012-07-02 20:28:59 +020013349static void intel_sanitize_crtc(struct intel_crtc *crtc)
13350{
13351 struct drm_device *dev = crtc->base.dev;
13352 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020013353 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020013354
Daniel Vetter24929352012-07-02 20:28:59 +020013355 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013356 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020013357 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
13358
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030013359 /* restore vblank interrupts to correct state */
Ville Syrjäläd297e102014-08-06 14:50:01 +030013360 if (crtc->active) {
13361 update_scanline_offset(crtc);
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030013362 drm_vblank_on(dev, crtc->pipe);
Ville Syrjäläd297e102014-08-06 14:50:01 +030013363 } else
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030013364 drm_vblank_off(dev, crtc->pipe);
13365
Daniel Vetter24929352012-07-02 20:28:59 +020013366 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020013367 * disable the crtc (and hence change the state) if it is wrong. Note
13368 * that gen4+ has a fixed plane -> pipe mapping. */
13369 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020013370 struct intel_connector *connector;
13371 bool plane;
13372
Daniel Vetter24929352012-07-02 20:28:59 +020013373 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
13374 crtc->base.base.id);
13375
13376 /* Pipe has the wrong plane attached and the plane is active.
13377 * Temporarily change the plane mapping and disable everything
13378 * ... */
13379 plane = crtc->plane;
13380 crtc->plane = !plane;
Daniel Vetter9c8958b2014-07-14 19:35:31 +020013381 crtc->primary_enabled = true;
Daniel Vetter24929352012-07-02 20:28:59 +020013382 dev_priv->display.crtc_disable(&crtc->base);
13383 crtc->plane = plane;
13384
13385 /* ... and break all links. */
13386 list_for_each_entry(connector, &dev->mode_config.connector_list,
13387 base.head) {
13388 if (connector->encoder->base.crtc != &crtc->base)
13389 continue;
13390
Egbert Eich7f1950f2014-04-25 10:56:22 +020013391 connector->base.dpms = DRM_MODE_DPMS_OFF;
13392 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020013393 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020013394 /* multiple connectors may have the same encoder:
13395 * handle them and break crtc link separately */
13396 list_for_each_entry(connector, &dev->mode_config.connector_list,
13397 base.head)
13398 if (connector->encoder->base.crtc == &crtc->base) {
13399 connector->encoder->base.crtc = NULL;
13400 connector->encoder->connectors_active = false;
13401 }
Daniel Vetter24929352012-07-02 20:28:59 +020013402
13403 WARN_ON(crtc->active);
13404 crtc->base.enabled = false;
13405 }
Daniel Vetter24929352012-07-02 20:28:59 +020013406
Daniel Vetter7fad7982012-07-04 17:51:47 +020013407 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
13408 crtc->pipe == PIPE_A && !crtc->active) {
13409 /* BIOS forgot to enable pipe A, this mostly happens after
13410 * resume. Force-enable the pipe to fix this, the update_dpms
13411 * call below we restore the pipe to the right state, but leave
13412 * the required bits on. */
13413 intel_enable_pipe_a(dev);
13414 }
13415
Daniel Vetter24929352012-07-02 20:28:59 +020013416 /* Adjust the state of the output pipe according to whether we
13417 * have active connectors/encoders. */
13418 intel_crtc_update_dpms(&crtc->base);
13419
13420 if (crtc->active != crtc->base.enabled) {
13421 struct intel_encoder *encoder;
13422
13423 /* This can happen either due to bugs in the get_hw_state
13424 * functions or because the pipe is force-enabled due to the
13425 * pipe A quirk. */
13426 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
13427 crtc->base.base.id,
13428 crtc->base.enabled ? "enabled" : "disabled",
13429 crtc->active ? "enabled" : "disabled");
13430
13431 crtc->base.enabled = crtc->active;
13432
13433 /* Because we only establish the connector -> encoder ->
13434 * crtc links if something is active, this means the
13435 * crtc is now deactivated. Break the links. connector
13436 * -> encoder links are only establish when things are
13437 * actually up, hence no need to break them. */
13438 WARN_ON(crtc->active);
13439
13440 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
13441 WARN_ON(encoder->connectors_active);
13442 encoder->base.crtc = NULL;
13443 }
13444 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020013445
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030013446 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010013447 /*
13448 * We start out with underrun reporting disabled to avoid races.
13449 * For correct bookkeeping mark this on active crtcs.
13450 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020013451 * Also on gmch platforms we dont have any hardware bits to
13452 * disable the underrun reporting. Which means we need to start
13453 * out with underrun reporting disabled also on inactive pipes,
13454 * since otherwise we'll complain about the garbage we read when
13455 * e.g. coming up after runtime pm.
13456 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010013457 * No protection against concurrent access is required - at
13458 * worst a fifo underrun happens which also sets this to false.
13459 */
13460 crtc->cpu_fifo_underrun_disabled = true;
13461 crtc->pch_fifo_underrun_disabled = true;
13462 }
Daniel Vetter24929352012-07-02 20:28:59 +020013463}
13464
13465static void intel_sanitize_encoder(struct intel_encoder *encoder)
13466{
13467 struct intel_connector *connector;
13468 struct drm_device *dev = encoder->base.dev;
13469
13470 /* We need to check both for a crtc link (meaning that the
13471 * encoder is active and trying to read from a pipe) and the
13472 * pipe itself being active. */
13473 bool has_active_crtc = encoder->base.crtc &&
13474 to_intel_crtc(encoder->base.crtc)->active;
13475
13476 if (encoder->connectors_active && !has_active_crtc) {
13477 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
13478 encoder->base.base.id,
Jani Nikula8e329a02014-06-03 14:56:21 +030013479 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020013480
13481 /* Connector is active, but has no active pipe. This is
13482 * fallout from our resume register restoring. Disable
13483 * the encoder manually again. */
13484 if (encoder->base.crtc) {
13485 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
13486 encoder->base.base.id,
Jani Nikula8e329a02014-06-03 14:56:21 +030013487 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020013488 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030013489 if (encoder->post_disable)
13490 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020013491 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020013492 encoder->base.crtc = NULL;
13493 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020013494
13495 /* Inconsistent output/port/pipe state happens presumably due to
13496 * a bug in one of the get_hw_state functions. Or someplace else
13497 * in our code, like the register restore mess on resume. Clamp
13498 * things to off as a safer default. */
13499 list_for_each_entry(connector,
13500 &dev->mode_config.connector_list,
13501 base.head) {
13502 if (connector->encoder != encoder)
13503 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020013504 connector->base.dpms = DRM_MODE_DPMS_OFF;
13505 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020013506 }
13507 }
13508 /* Enabled encoders without active connectors will be fixed in
13509 * the crtc fixup. */
13510}
13511
Imre Deak04098752014-02-18 00:02:16 +020013512void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010013513{
13514 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020013515 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010013516
Imre Deak04098752014-02-18 00:02:16 +020013517 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
13518 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
13519 i915_disable_vga(dev);
13520 }
13521}
13522
13523void i915_redisable_vga(struct drm_device *dev)
13524{
13525 struct drm_i915_private *dev_priv = dev->dev_private;
13526
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030013527 /* This function can be called both from intel_modeset_setup_hw_state or
13528 * at a very early point in our resume sequence, where the power well
13529 * structures are not yet restored. Since this function is at a very
13530 * paranoid "someone might have enabled VGA while we were not looking"
13531 * level, just check if the power well is enabled instead of trying to
13532 * follow the "don't touch the power well if we don't need it" policy
13533 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013534 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030013535 return;
13536
Imre Deak04098752014-02-18 00:02:16 +020013537 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010013538}
13539
Ville Syrjälä98ec7732014-04-30 17:43:01 +030013540static bool primary_get_hw_state(struct intel_crtc *crtc)
13541{
13542 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
13543
13544 if (!crtc->active)
13545 return false;
13546
13547 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
13548}
13549
Daniel Vetter30e984d2013-06-05 13:34:17 +020013550static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020013551{
13552 struct drm_i915_private *dev_priv = dev->dev_private;
13553 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020013554 struct intel_crtc *crtc;
13555 struct intel_encoder *encoder;
13556 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020013557 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020013558
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013559 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013560 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020013561
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013562 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020013563
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013564 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013565 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020013566
13567 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030013568 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020013569
13570 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
13571 crtc->base.base.id,
13572 crtc->active ? "enabled" : "disabled");
13573 }
13574
Daniel Vetter53589012013-06-05 13:34:16 +020013575 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13576 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13577
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013578 pll->on = pll->get_hw_state(dev_priv, pll,
13579 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020013580 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013581 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013582 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020013583 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020013584 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013585 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020013586 }
Daniel Vetter53589012013-06-05 13:34:16 +020013587 }
Daniel Vetter53589012013-06-05 13:34:16 +020013588
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020013589 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013590 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013591
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013592 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013593 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020013594 }
13595
Damien Lespiaub2784e12014-08-05 11:29:37 +010013596 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020013597 pipe = 0;
13598
13599 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070013600 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13601 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013602 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020013603 } else {
13604 encoder->base.crtc = NULL;
13605 }
13606
13607 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010013608 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020013609 encoder->base.base.id,
Jani Nikula8e329a02014-06-03 14:56:21 +030013610 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020013611 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010013612 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020013613 }
13614
13615 list_for_each_entry(connector, &dev->mode_config.connector_list,
13616 base.head) {
13617 if (connector->get_hw_state(connector)) {
13618 connector->base.dpms = DRM_MODE_DPMS_ON;
13619 connector->encoder->connectors_active = true;
13620 connector->base.encoder = &connector->encoder->base;
13621 } else {
13622 connector->base.dpms = DRM_MODE_DPMS_OFF;
13623 connector->base.encoder = NULL;
13624 }
13625 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
13626 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013627 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020013628 connector->base.encoder ? "enabled" : "disabled");
13629 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020013630}
13631
13632/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
13633 * and i915 state tracking structures. */
13634void intel_modeset_setup_hw_state(struct drm_device *dev,
13635 bool force_restore)
13636{
13637 struct drm_i915_private *dev_priv = dev->dev_private;
13638 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020013639 struct intel_crtc *crtc;
13640 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020013641 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020013642
13643 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020013644
Jesse Barnesbabea612013-06-26 18:57:38 +030013645 /*
13646 * Now that we have the config, copy it to each CRTC struct
13647 * Note that this could go away if we move to using crtc_config
13648 * checking everywhere.
13649 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013650 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020013651 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013652 intel_mode_from_pipe_config(&crtc->base.mode,
13653 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030013654 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
13655 crtc->base.base.id);
13656 drm_mode_debug_printmodeline(&crtc->base.mode);
13657 }
13658 }
13659
Daniel Vetter24929352012-07-02 20:28:59 +020013660 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010013661 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020013662 intel_sanitize_encoder(encoder);
13663 }
13664
Damien Lespiau055e3932014-08-18 13:49:10 +010013665 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020013666 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13667 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013668 intel_dump_pipe_config(crtc, crtc->config,
13669 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020013670 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013671
Daniel Vetter35c95372013-07-17 06:55:04 +020013672 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13673 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13674
13675 if (!pll->on || pll->active)
13676 continue;
13677
13678 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
13679
13680 pll->disable(dev_priv, pll);
13681 pll->on = false;
13682 }
13683
Pradeep Bhat30789992014-11-04 17:06:45 +000013684 if (IS_GEN9(dev))
13685 skl_wm_get_hw_state(dev);
13686 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030013687 ilk_wm_get_hw_state(dev);
13688
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010013689 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030013690 i915_redisable_vga(dev);
13691
Daniel Vetterf30da182013-04-11 20:22:50 +020013692 /*
13693 * We need to use raw interfaces for restoring state to avoid
13694 * checking (bogus) intermediate states.
13695 */
Damien Lespiau055e3932014-08-18 13:49:10 +010013696 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070013697 struct drm_crtc *crtc =
13698 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020013699
Jesse Barnes7f271262014-11-05 14:26:06 -080013700 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
13701 crtc->primary->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010013702 }
13703 } else {
13704 intel_modeset_update_staged_output_state(dev);
13705 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013706
13707 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010013708}
13709
13710void intel_modeset_gem_init(struct drm_device *dev)
13711{
Jesse Barnes92122782014-10-09 12:57:42 -070013712 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080013713 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070013714 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -080013715
Imre Deakae484342014-03-31 15:10:44 +030013716 mutex_lock(&dev->struct_mutex);
13717 intel_init_gt_powersave(dev);
13718 mutex_unlock(&dev->struct_mutex);
13719
Jesse Barnes92122782014-10-09 12:57:42 -070013720 /*
13721 * There may be no VBT; and if the BIOS enabled SSC we can
13722 * just keep using it to avoid unnecessary flicker. Whereas if the
13723 * BIOS isn't using it, don't assume it will work even if the VBT
13724 * indicates as much.
13725 */
13726 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
13727 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
13728 DREF_SSC1_ENABLE);
13729
Chris Wilson1833b132012-05-09 11:56:28 +010013730 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020013731
13732 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080013733
13734 /*
13735 * Make sure any fbs we allocated at startup are properly
13736 * pinned & fenced. When we do the allocation it's too early
13737 * for this.
13738 */
13739 mutex_lock(&dev->struct_mutex);
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010013740 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070013741 obj = intel_fb_obj(c->primary->fb);
13742 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080013743 continue;
13744
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +000013745 if (intel_pin_and_fence_fb_obj(c->primary,
13746 c->primary->fb,
13747 NULL)) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080013748 DRM_ERROR("failed to pin boot fb on pipe %d\n",
13749 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100013750 drm_framebuffer_unreference(c->primary->fb);
13751 c->primary->fb = NULL;
Xi Ruoyao319c1d42015-03-12 20:16:32 +080013752 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080013753 }
13754 }
13755 mutex_unlock(&dev->struct_mutex);
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020013756
13757 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013758}
13759
Imre Deak4932e2c2014-02-11 17:12:48 +020013760void intel_connector_unregister(struct intel_connector *intel_connector)
13761{
13762 struct drm_connector *connector = &intel_connector->base;
13763
13764 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010013765 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020013766}
13767
Jesse Barnes79e53942008-11-07 14:24:08 -080013768void intel_modeset_cleanup(struct drm_device *dev)
13769{
Jesse Barnes652c3932009-08-17 13:31:43 -070013770 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030013771 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070013772
Imre Deak2eb52522014-11-19 15:30:05 +020013773 intel_disable_gt_powersave(dev);
13774
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020013775 intel_backlight_unregister(dev);
13776
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013777 /*
13778 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020013779 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013780 * experience fancy races otherwise.
13781 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020013782 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070013783
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013784 /*
13785 * Due to the hpd irq storm handling the hotplug work can re-arm the
13786 * poll handlers. Hence disable polling after hpd handling is shut down.
13787 */
Keith Packardf87ea762010-10-03 19:36:26 -070013788 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013789
Jesse Barnes652c3932009-08-17 13:31:43 -070013790 mutex_lock(&dev->struct_mutex);
13791
Jesse Barnes723bfd72010-10-07 16:01:13 -070013792 intel_unregister_dsm_handler();
13793
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013794 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070013795
Daniel Vetter930ebb42012-06-29 23:32:16 +020013796 ironlake_teardown_rc6(dev);
13797
Kristian Høgsberg69341a52009-11-11 12:19:17 -050013798 mutex_unlock(&dev->struct_mutex);
13799
Chris Wilson1630fe72011-07-08 12:22:42 +010013800 /* flush any delayed tasks or pending work */
13801 flush_scheduled_work();
13802
Jani Nikuladb31af12013-11-08 16:48:53 +020013803 /* destroy the backlight and sysfs files before encoders/connectors */
13804 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020013805 struct intel_connector *intel_connector;
13806
13807 intel_connector = to_intel_connector(connector);
13808 intel_connector->unregister(intel_connector);
Jani Nikuladb31af12013-11-08 16:48:53 +020013809 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030013810
Jesse Barnes79e53942008-11-07 14:24:08 -080013811 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010013812
13813 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030013814
13815 mutex_lock(&dev->struct_mutex);
13816 intel_cleanup_gt_powersave(dev);
13817 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013818}
13819
Dave Airlie28d52042009-09-21 14:33:58 +100013820/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080013821 * Return which encoder is currently attached for connector.
13822 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010013823struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080013824{
Chris Wilsondf0e9242010-09-09 16:20:55 +010013825 return &intel_attached_encoder(connector)->base;
13826}
Jesse Barnes79e53942008-11-07 14:24:08 -080013827
Chris Wilsondf0e9242010-09-09 16:20:55 +010013828void intel_connector_attach_encoder(struct intel_connector *connector,
13829 struct intel_encoder *encoder)
13830{
13831 connector->encoder = encoder;
13832 drm_mode_connector_attach_encoder(&connector->base,
13833 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080013834}
Dave Airlie28d52042009-09-21 14:33:58 +100013835
13836/*
13837 * set vga decode state - true == enable VGA decode
13838 */
13839int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
13840{
13841 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000013842 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100013843 u16 gmch_ctrl;
13844
Chris Wilson75fa0412014-02-07 18:37:02 -020013845 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
13846 DRM_ERROR("failed to read control word\n");
13847 return -EIO;
13848 }
13849
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020013850 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
13851 return 0;
13852
Dave Airlie28d52042009-09-21 14:33:58 +100013853 if (state)
13854 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
13855 else
13856 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020013857
13858 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
13859 DRM_ERROR("failed to write control word\n");
13860 return -EIO;
13861 }
13862
Dave Airlie28d52042009-09-21 14:33:58 +100013863 return 0;
13864}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013865
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013866struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030013867
13868 u32 power_well_driver;
13869
Chris Wilson63b66e52013-08-08 15:12:06 +020013870 int num_transcoders;
13871
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013872 struct intel_cursor_error_state {
13873 u32 control;
13874 u32 position;
13875 u32 base;
13876 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010013877 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013878
13879 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020013880 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013881 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030013882 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010013883 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013884
13885 struct intel_plane_error_state {
13886 u32 control;
13887 u32 stride;
13888 u32 size;
13889 u32 pos;
13890 u32 addr;
13891 u32 surface;
13892 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010013893 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020013894
13895 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020013896 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020013897 enum transcoder cpu_transcoder;
13898
13899 u32 conf;
13900
13901 u32 htotal;
13902 u32 hblank;
13903 u32 hsync;
13904 u32 vtotal;
13905 u32 vblank;
13906 u32 vsync;
13907 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013908};
13909
13910struct intel_display_error_state *
13911intel_display_capture_error_state(struct drm_device *dev)
13912{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013913 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013914 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020013915 int transcoders[] = {
13916 TRANSCODER_A,
13917 TRANSCODER_B,
13918 TRANSCODER_C,
13919 TRANSCODER_EDP,
13920 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013921 int i;
13922
Chris Wilson63b66e52013-08-08 15:12:06 +020013923 if (INTEL_INFO(dev)->num_pipes == 0)
13924 return NULL;
13925
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020013926 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013927 if (error == NULL)
13928 return NULL;
13929
Imre Deak190be112013-11-25 17:15:31 +020013930 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030013931 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
13932
Damien Lespiau055e3932014-08-18 13:49:10 +010013933 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020013934 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013935 __intel_display_power_is_enabled(dev_priv,
13936 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020013937 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020013938 continue;
13939
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030013940 error->cursor[i].control = I915_READ(CURCNTR(i));
13941 error->cursor[i].position = I915_READ(CURPOS(i));
13942 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013943
13944 error->plane[i].control = I915_READ(DSPCNTR(i));
13945 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013946 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030013947 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013948 error->plane[i].pos = I915_READ(DSPPOS(i));
13949 }
Paulo Zanonica291362013-03-06 20:03:14 -030013950 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
13951 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013952 if (INTEL_INFO(dev)->gen >= 4) {
13953 error->plane[i].surface = I915_READ(DSPSURF(i));
13954 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
13955 }
13956
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013957 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030013958
Sonika Jindal3abfce72014-07-21 15:23:43 +053013959 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030013960 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020013961 }
13962
13963 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
13964 if (HAS_DDI(dev_priv->dev))
13965 error->num_transcoders++; /* Account for eDP. */
13966
13967 for (i = 0; i < error->num_transcoders; i++) {
13968 enum transcoder cpu_transcoder = transcoders[i];
13969
Imre Deakddf9c532013-11-27 22:02:02 +020013970 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013971 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020013972 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020013973 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020013974 continue;
13975
Chris Wilson63b66e52013-08-08 15:12:06 +020013976 error->transcoder[i].cpu_transcoder = cpu_transcoder;
13977
13978 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
13979 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
13980 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
13981 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
13982 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
13983 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
13984 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013985 }
13986
13987 return error;
13988}
13989
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013990#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13991
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013992void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013993intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013994 struct drm_device *dev,
13995 struct intel_display_error_state *error)
13996{
Damien Lespiau055e3932014-08-18 13:49:10 +010013997 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013998 int i;
13999
Chris Wilson63b66e52013-08-08 15:12:06 +020014000 if (!error)
14001 return;
14002
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014003 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020014004 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014005 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030014006 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010014007 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014008 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020014009 err_printf(m, " Power: %s\n",
14010 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014011 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030014012 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014013
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014014 err_printf(m, "Plane [%d]:\n", i);
14015 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
14016 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014017 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014018 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
14019 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014020 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030014021 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014022 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014023 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014024 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
14025 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014026 }
14027
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014028 err_printf(m, "Cursor [%d]:\n", i);
14029 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
14030 err_printf(m, " POS: %08x\n", error->cursor[i].position);
14031 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014032 }
Chris Wilson63b66e52013-08-08 15:12:06 +020014033
14034 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010014035 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020014036 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020014037 err_printf(m, " Power: %s\n",
14038 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020014039 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
14040 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
14041 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
14042 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
14043 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
14044 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
14045 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
14046 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014047}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014048
14049void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
14050{
14051 struct intel_crtc *crtc;
14052
14053 for_each_intel_crtc(dev, crtc) {
14054 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014055
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020014056 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014057
14058 work = crtc->unpin_work;
14059
14060 if (work && work->event &&
14061 work->event->base.file_priv == file) {
14062 kfree(work->event);
14063 work->event = NULL;
14064 }
14065
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020014066 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014067 }
14068}