blob: d547d9c8dda2226909fcdbc5193dde4e2013246a [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080040#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080041#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010042#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070044#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080046#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080047
Matt Roper465c1202014-05-29 08:06:54 -070048/* Primary plane formats supported by all gen */
49#define COMMON_PRIMARY_FORMATS \
50 DRM_FORMAT_C8, \
51 DRM_FORMAT_RGB565, \
52 DRM_FORMAT_XRGB8888, \
53 DRM_FORMAT_ARGB8888
54
55/* Primary plane formats for gen <= 3 */
56static const uint32_t intel_primary_formats_gen2[] = {
57 COMMON_PRIMARY_FORMATS,
58 DRM_FORMAT_XRGB1555,
59 DRM_FORMAT_ARGB1555,
60};
61
62/* Primary plane formats for gen >= 4 */
63static const uint32_t intel_primary_formats_gen4[] = {
64 COMMON_PRIMARY_FORMATS, \
65 DRM_FORMAT_XBGR8888,
66 DRM_FORMAT_ABGR8888,
67 DRM_FORMAT_XRGB2101010,
68 DRM_FORMAT_ARGB2101010,
69 DRM_FORMAT_XBGR2101010,
70 DRM_FORMAT_ABGR2101010,
71};
72
Matt Roper3d7d6512014-06-10 08:28:13 -070073/* Cursor formats */
74static const uint32_t intel_cursor_formats[] = {
75 DRM_FORMAT_ARGB8888,
76};
77
Chris Wilson6b383a72010-09-13 13:54:26 +010078static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080079
Jesse Barnesf1f644d2013-06-27 00:39:25 +030080static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020081 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030082static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020083 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030084
Damien Lespiaue7457a92013-08-08 22:28:59 +010085static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020086 int x, int y, struct drm_framebuffer *old_fb,
87 struct drm_atomic_state *state);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080088static int intel_framebuffer_init(struct drm_device *dev,
89 struct intel_framebuffer *ifb,
90 struct drm_mode_fb_cmd2 *mode_cmd,
91 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020092static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
93static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020094static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070095 struct intel_link_m_n *m_n,
96 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +020097static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +020098static void haswell_set_pipeconf(struct drm_crtc *crtc);
99static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200100static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200101 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200102static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200103 const struct intel_crtc_state *pipe_config);
Matt Roperea2c67b2014-12-23 10:41:52 -0800104static void intel_begin_crtc_commit(struct drm_crtc *crtc);
105static void intel_finish_crtc_commit(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100106
Dave Airlie0e32b392014-05-02 14:02:48 +1000107static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
108{
109 if (!connector->mst_port)
110 return connector->encoder;
111 else
112 return &connector->mst_port->mst_encoders[pipe]->base;
113}
114
Jesse Barnes79e53942008-11-07 14:24:08 -0800115typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400116 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800117} intel_range_t;
118
119typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400120 int dot_limit;
121 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800122} intel_p2_t;
123
Ma Lingd4906092009-03-18 20:13:27 +0800124typedef struct intel_limit intel_limit_t;
125struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400126 intel_range_t dot, vco, n, m, m1, m2, p, p1;
127 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800128};
Jesse Barnes79e53942008-11-07 14:24:08 -0800129
Daniel Vetterd2acd212012-10-20 20:57:43 +0200130int
131intel_pch_rawclk(struct drm_device *dev)
132{
133 struct drm_i915_private *dev_priv = dev->dev_private;
134
135 WARN_ON(!HAS_PCH_SPLIT(dev));
136
137 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
138}
139
Chris Wilson021357a2010-09-07 20:54:59 +0100140static inline u32 /* units of 100MHz */
141intel_fdi_link_freq(struct drm_device *dev)
142{
Chris Wilson8b99e682010-10-13 09:59:17 +0100143 if (IS_GEN5(dev)) {
144 struct drm_i915_private *dev_priv = dev->dev_private;
145 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
146 } else
147 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100148}
149
Daniel Vetter5d536e22013-07-06 12:52:06 +0200150static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400151 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200152 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200153 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400154 .m = { .min = 96, .max = 140 },
155 .m1 = { .min = 18, .max = 26 },
156 .m2 = { .min = 6, .max = 16 },
157 .p = { .min = 4, .max = 128 },
158 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700159 .p2 = { .dot_limit = 165000,
160 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700161};
162
Daniel Vetter5d536e22013-07-06 12:52:06 +0200163static const intel_limit_t intel_limits_i8xx_dvo = {
164 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200165 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200166 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200167 .m = { .min = 96, .max = 140 },
168 .m1 = { .min = 18, .max = 26 },
169 .m2 = { .min = 6, .max = 16 },
170 .p = { .min = 4, .max = 128 },
171 .p1 = { .min = 2, .max = 33 },
172 .p2 = { .dot_limit = 165000,
173 .p2_slow = 4, .p2_fast = 4 },
174};
175
Keith Packarde4b36692009-06-05 19:22:17 -0700176static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400177 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200178 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200179 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400180 .m = { .min = 96, .max = 140 },
181 .m1 = { .min = 18, .max = 26 },
182 .m2 = { .min = 6, .max = 16 },
183 .p = { .min = 4, .max = 128 },
184 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700185 .p2 = { .dot_limit = 165000,
186 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700187};
Eric Anholt273e27c2011-03-30 13:01:10 -0700188
Keith Packarde4b36692009-06-05 19:22:17 -0700189static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400190 .dot = { .min = 20000, .max = 400000 },
191 .vco = { .min = 1400000, .max = 2800000 },
192 .n = { .min = 1, .max = 6 },
193 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100194 .m1 = { .min = 8, .max = 18 },
195 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400196 .p = { .min = 5, .max = 80 },
197 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700198 .p2 = { .dot_limit = 200000,
199 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700200};
201
202static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400203 .dot = { .min = 20000, .max = 400000 },
204 .vco = { .min = 1400000, .max = 2800000 },
205 .n = { .min = 1, .max = 6 },
206 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100207 .m1 = { .min = 8, .max = 18 },
208 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400209 .p = { .min = 7, .max = 98 },
210 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700211 .p2 = { .dot_limit = 112000,
212 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700213};
214
Eric Anholt273e27c2011-03-30 13:01:10 -0700215
Keith Packarde4b36692009-06-05 19:22:17 -0700216static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700217 .dot = { .min = 25000, .max = 270000 },
218 .vco = { .min = 1750000, .max = 3500000},
219 .n = { .min = 1, .max = 4 },
220 .m = { .min = 104, .max = 138 },
221 .m1 = { .min = 17, .max = 23 },
222 .m2 = { .min = 5, .max = 11 },
223 .p = { .min = 10, .max = 30 },
224 .p1 = { .min = 1, .max = 3},
225 .p2 = { .dot_limit = 270000,
226 .p2_slow = 10,
227 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800228 },
Keith Packarde4b36692009-06-05 19:22:17 -0700229};
230
231static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700232 .dot = { .min = 22000, .max = 400000 },
233 .vco = { .min = 1750000, .max = 3500000},
234 .n = { .min = 1, .max = 4 },
235 .m = { .min = 104, .max = 138 },
236 .m1 = { .min = 16, .max = 23 },
237 .m2 = { .min = 5, .max = 11 },
238 .p = { .min = 5, .max = 80 },
239 .p1 = { .min = 1, .max = 8},
240 .p2 = { .dot_limit = 165000,
241 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700242};
243
244static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700245 .dot = { .min = 20000, .max = 115000 },
246 .vco = { .min = 1750000, .max = 3500000 },
247 .n = { .min = 1, .max = 3 },
248 .m = { .min = 104, .max = 138 },
249 .m1 = { .min = 17, .max = 23 },
250 .m2 = { .min = 5, .max = 11 },
251 .p = { .min = 28, .max = 112 },
252 .p1 = { .min = 2, .max = 8 },
253 .p2 = { .dot_limit = 0,
254 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800255 },
Keith Packarde4b36692009-06-05 19:22:17 -0700256};
257
258static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700259 .dot = { .min = 80000, .max = 224000 },
260 .vco = { .min = 1750000, .max = 3500000 },
261 .n = { .min = 1, .max = 3 },
262 .m = { .min = 104, .max = 138 },
263 .m1 = { .min = 17, .max = 23 },
264 .m2 = { .min = 5, .max = 11 },
265 .p = { .min = 14, .max = 42 },
266 .p1 = { .min = 2, .max = 6 },
267 .p2 = { .dot_limit = 0,
268 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800269 },
Keith Packarde4b36692009-06-05 19:22:17 -0700270};
271
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500272static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400273 .dot = { .min = 20000, .max = 400000},
274 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700275 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400276 .n = { .min = 3, .max = 6 },
277 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700278 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400279 .m1 = { .min = 0, .max = 0 },
280 .m2 = { .min = 0, .max = 254 },
281 .p = { .min = 5, .max = 80 },
282 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700283 .p2 = { .dot_limit = 200000,
284 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700285};
286
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500287static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400288 .dot = { .min = 20000, .max = 400000 },
289 .vco = { .min = 1700000, .max = 3500000 },
290 .n = { .min = 3, .max = 6 },
291 .m = { .min = 2, .max = 256 },
292 .m1 = { .min = 0, .max = 0 },
293 .m2 = { .min = 0, .max = 254 },
294 .p = { .min = 7, .max = 112 },
295 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700296 .p2 = { .dot_limit = 112000,
297 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700298};
299
Eric Anholt273e27c2011-03-30 13:01:10 -0700300/* Ironlake / Sandybridge
301 *
302 * We calculate clock using (register_value + 2) for N/M1/M2, so here
303 * the range value for them is (actual_value - 2).
304 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800305static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700306 .dot = { .min = 25000, .max = 350000 },
307 .vco = { .min = 1760000, .max = 3510000 },
308 .n = { .min = 1, .max = 5 },
309 .m = { .min = 79, .max = 127 },
310 .m1 = { .min = 12, .max = 22 },
311 .m2 = { .min = 5, .max = 9 },
312 .p = { .min = 5, .max = 80 },
313 .p1 = { .min = 1, .max = 8 },
314 .p2 = { .dot_limit = 225000,
315 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700316};
317
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800318static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700319 .dot = { .min = 25000, .max = 350000 },
320 .vco = { .min = 1760000, .max = 3510000 },
321 .n = { .min = 1, .max = 3 },
322 .m = { .min = 79, .max = 118 },
323 .m1 = { .min = 12, .max = 22 },
324 .m2 = { .min = 5, .max = 9 },
325 .p = { .min = 28, .max = 112 },
326 .p1 = { .min = 2, .max = 8 },
327 .p2 = { .dot_limit = 225000,
328 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800329};
330
331static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700332 .dot = { .min = 25000, .max = 350000 },
333 .vco = { .min = 1760000, .max = 3510000 },
334 .n = { .min = 1, .max = 3 },
335 .m = { .min = 79, .max = 127 },
336 .m1 = { .min = 12, .max = 22 },
337 .m2 = { .min = 5, .max = 9 },
338 .p = { .min = 14, .max = 56 },
339 .p1 = { .min = 2, .max = 8 },
340 .p2 = { .dot_limit = 225000,
341 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800342};
343
Eric Anholt273e27c2011-03-30 13:01:10 -0700344/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800345static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700346 .dot = { .min = 25000, .max = 350000 },
347 .vco = { .min = 1760000, .max = 3510000 },
348 .n = { .min = 1, .max = 2 },
349 .m = { .min = 79, .max = 126 },
350 .m1 = { .min = 12, .max = 22 },
351 .m2 = { .min = 5, .max = 9 },
352 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400353 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700354 .p2 = { .dot_limit = 225000,
355 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800356};
357
358static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700359 .dot = { .min = 25000, .max = 350000 },
360 .vco = { .min = 1760000, .max = 3510000 },
361 .n = { .min = 1, .max = 3 },
362 .m = { .min = 79, .max = 126 },
363 .m1 = { .min = 12, .max = 22 },
364 .m2 = { .min = 5, .max = 9 },
365 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400366 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700367 .p2 = { .dot_limit = 225000,
368 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800369};
370
Ville Syrjälädc730512013-09-24 21:26:30 +0300371static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300372 /*
373 * These are the data rate limits (measured in fast clocks)
374 * since those are the strictest limits we have. The fast
375 * clock and actual rate limits are more relaxed, so checking
376 * them would make no difference.
377 */
378 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200379 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700380 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700381 .m1 = { .min = 2, .max = 3 },
382 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300383 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300384 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700385};
386
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300387static const intel_limit_t intel_limits_chv = {
388 /*
389 * These are the data rate limits (measured in fast clocks)
390 * since those are the strictest limits we have. The fast
391 * clock and actual rate limits are more relaxed, so checking
392 * them would make no difference.
393 */
394 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200395 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300396 .n = { .min = 1, .max = 1 },
397 .m1 = { .min = 2, .max = 2 },
398 .m2 = { .min = 24 << 22, .max = 175 << 22 },
399 .p1 = { .min = 2, .max = 4 },
400 .p2 = { .p2_slow = 1, .p2_fast = 14 },
401};
402
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300403static void vlv_clock(int refclk, intel_clock_t *clock)
404{
405 clock->m = clock->m1 * clock->m2;
406 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200407 if (WARN_ON(clock->n == 0 || clock->p == 0))
408 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300409 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
410 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300411}
412
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300413/**
414 * Returns whether any output on the specified pipe is of the specified type
415 */
Damien Lespiau40935612014-10-29 11:16:59 +0000416bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300417{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300418 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300419 struct intel_encoder *encoder;
420
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300421 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300422 if (encoder->type == type)
423 return true;
424
425 return false;
426}
427
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200428/**
429 * Returns whether any output on the specified pipe will have the specified
430 * type after a staged modeset is complete, i.e., the same as
431 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
432 * encoder->crtc.
433 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200434static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
435 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200436{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200437 struct drm_atomic_state *state = crtc_state->base.state;
438 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200439 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200440 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200441
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200442 for (i = 0; i < state->num_connector; i++) {
443 if (!state->connectors[i])
444 continue;
445
446 connector_state = state->connector_states[i];
447 if (connector_state->crtc != crtc_state->base.crtc)
448 continue;
449
450 num_connectors++;
451
452 encoder = to_intel_encoder(connector_state->best_encoder);
453 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200454 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200455 }
456
457 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200458
459 return false;
460}
461
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200462static const intel_limit_t *
463intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800464{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200465 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800466 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800467
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200468 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100469 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000470 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800471 limit = &intel_limits_ironlake_dual_lvds_100m;
472 else
473 limit = &intel_limits_ironlake_dual_lvds;
474 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000475 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800476 limit = &intel_limits_ironlake_single_lvds_100m;
477 else
478 limit = &intel_limits_ironlake_single_lvds;
479 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200480 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800481 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800482
483 return limit;
484}
485
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200486static const intel_limit_t *
487intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800488{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200489 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800490 const intel_limit_t *limit;
491
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200492 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100493 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700494 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800495 else
Keith Packarde4b36692009-06-05 19:22:17 -0700496 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200497 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
498 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700499 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200500 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700501 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800502 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700503 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800504
505 return limit;
506}
507
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200508static const intel_limit_t *
509intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800510{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200511 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800512 const intel_limit_t *limit;
513
Eric Anholtbad720f2009-10-22 16:11:14 -0700514 if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200515 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800516 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200517 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500518 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200519 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500520 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800521 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500522 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300523 } else if (IS_CHERRYVIEW(dev)) {
524 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700525 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300526 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100527 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200528 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100529 limit = &intel_limits_i9xx_lvds;
530 else
531 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800532 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200533 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700534 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200535 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700536 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200537 else
538 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800539 }
540 return limit;
541}
542
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500543/* m1 is reserved as 0 in Pineview, n is a ring counter */
544static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800545{
Shaohua Li21778322009-02-23 15:19:16 +0800546 clock->m = clock->m2 + 2;
547 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200548 if (WARN_ON(clock->n == 0 || clock->p == 0))
549 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300550 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
551 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800552}
553
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200554static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
555{
556 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
557}
558
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200559static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800560{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200561 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800562 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200563 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
564 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300565 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
566 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800567}
568
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300569static void chv_clock(int refclk, intel_clock_t *clock)
570{
571 clock->m = clock->m1 * clock->m2;
572 clock->p = clock->p1 * clock->p2;
573 if (WARN_ON(clock->n == 0 || clock->p == 0))
574 return;
575 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
576 clock->n << 22);
577 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
578}
579
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800580#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800581/**
582 * Returns whether the given set of divisors are valid for a given refclk with
583 * the given connectors.
584 */
585
Chris Wilson1b894b52010-12-14 20:04:54 +0000586static bool intel_PLL_is_valid(struct drm_device *dev,
587 const intel_limit_t *limit,
588 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800589{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300590 if (clock->n < limit->n.min || limit->n.max < clock->n)
591 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800592 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400593 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800594 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400595 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800596 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400597 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300598
599 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
600 if (clock->m1 <= clock->m2)
601 INTELPllInvalid("m1 <= m2\n");
602
603 if (!IS_VALLEYVIEW(dev)) {
604 if (clock->p < limit->p.min || limit->p.max < clock->p)
605 INTELPllInvalid("p out of range\n");
606 if (clock->m < limit->m.min || limit->m.max < clock->m)
607 INTELPllInvalid("m out of range\n");
608 }
609
Jesse Barnes79e53942008-11-07 14:24:08 -0800610 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400611 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
613 * connector, etc., rather than just a single range.
614 */
615 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400616 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800617
618 return true;
619}
620
Ma Lingd4906092009-03-18 20:13:27 +0800621static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200622i9xx_find_best_dpll(const intel_limit_t *limit,
623 struct intel_crtc_state *crtc_state,
Sean Paulcec2f352012-01-10 15:09:36 -0800624 int target, int refclk, intel_clock_t *match_clock,
625 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800626{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200627 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300628 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800629 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800630 int err = target;
631
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200632 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800633 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100634 * For LVDS just rely on its current settings for dual-channel.
635 * We haven't figured out how to reliably set up different
636 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800637 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100638 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800639 clock.p2 = limit->p2.p2_fast;
640 else
641 clock.p2 = limit->p2.p2_slow;
642 } else {
643 if (target < limit->p2.dot_limit)
644 clock.p2 = limit->p2.p2_slow;
645 else
646 clock.p2 = limit->p2.p2_fast;
647 }
648
Akshay Joshi0206e352011-08-16 15:34:10 -0400649 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800650
Zhao Yakui42158662009-11-20 11:24:18 +0800651 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
652 clock.m1++) {
653 for (clock.m2 = limit->m2.min;
654 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200655 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800656 break;
657 for (clock.n = limit->n.min;
658 clock.n <= limit->n.max; clock.n++) {
659 for (clock.p1 = limit->p1.min;
660 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800661 int this_err;
662
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200663 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000664 if (!intel_PLL_is_valid(dev, limit,
665 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800666 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800667 if (match_clock &&
668 clock.p != match_clock->p)
669 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800670
671 this_err = abs(clock.dot - target);
672 if (this_err < err) {
673 *best_clock = clock;
674 err = this_err;
675 }
676 }
677 }
678 }
679 }
680
681 return (err != target);
682}
683
Ma Lingd4906092009-03-18 20:13:27 +0800684static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200685pnv_find_best_dpll(const intel_limit_t *limit,
686 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200687 int target, int refclk, intel_clock_t *match_clock,
688 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200689{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200690 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300691 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200692 intel_clock_t clock;
693 int err = target;
694
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200695 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200696 /*
697 * For LVDS just rely on its current settings for dual-channel.
698 * We haven't figured out how to reliably set up different
699 * single/dual channel state, if we even can.
700 */
701 if (intel_is_dual_link_lvds(dev))
702 clock.p2 = limit->p2.p2_fast;
703 else
704 clock.p2 = limit->p2.p2_slow;
705 } else {
706 if (target < limit->p2.dot_limit)
707 clock.p2 = limit->p2.p2_slow;
708 else
709 clock.p2 = limit->p2.p2_fast;
710 }
711
712 memset(best_clock, 0, sizeof(*best_clock));
713
714 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
715 clock.m1++) {
716 for (clock.m2 = limit->m2.min;
717 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200718 for (clock.n = limit->n.min;
719 clock.n <= limit->n.max; clock.n++) {
720 for (clock.p1 = limit->p1.min;
721 clock.p1 <= limit->p1.max; clock.p1++) {
722 int this_err;
723
724 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800725 if (!intel_PLL_is_valid(dev, limit,
726 &clock))
727 continue;
728 if (match_clock &&
729 clock.p != match_clock->p)
730 continue;
731
732 this_err = abs(clock.dot - target);
733 if (this_err < err) {
734 *best_clock = clock;
735 err = this_err;
736 }
737 }
738 }
739 }
740 }
741
742 return (err != target);
743}
744
Ma Lingd4906092009-03-18 20:13:27 +0800745static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200746g4x_find_best_dpll(const intel_limit_t *limit,
747 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200748 int target, int refclk, intel_clock_t *match_clock,
749 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800750{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200751 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300752 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800753 intel_clock_t clock;
754 int max_n;
755 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400756 /* approximately equals target * 0.00585 */
757 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800758 found = false;
759
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200760 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100761 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800762 clock.p2 = limit->p2.p2_fast;
763 else
764 clock.p2 = limit->p2.p2_slow;
765 } else {
766 if (target < limit->p2.dot_limit)
767 clock.p2 = limit->p2.p2_slow;
768 else
769 clock.p2 = limit->p2.p2_fast;
770 }
771
772 memset(best_clock, 0, sizeof(*best_clock));
773 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200774 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800775 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200776 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800777 for (clock.m1 = limit->m1.max;
778 clock.m1 >= limit->m1.min; clock.m1--) {
779 for (clock.m2 = limit->m2.max;
780 clock.m2 >= limit->m2.min; clock.m2--) {
781 for (clock.p1 = limit->p1.max;
782 clock.p1 >= limit->p1.min; clock.p1--) {
783 int this_err;
784
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200785 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000786 if (!intel_PLL_is_valid(dev, limit,
787 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800788 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000789
790 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800791 if (this_err < err_most) {
792 *best_clock = clock;
793 err_most = this_err;
794 max_n = clock.n;
795 found = true;
796 }
797 }
798 }
799 }
800 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800801 return found;
802}
Ma Lingd4906092009-03-18 20:13:27 +0800803
Imre Deakd5dd62b2015-03-17 11:40:03 +0200804/*
805 * Check if the calculated PLL configuration is more optimal compared to the
806 * best configuration and error found so far. Return the calculated error.
807 */
808static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
809 const intel_clock_t *calculated_clock,
810 const intel_clock_t *best_clock,
811 unsigned int best_error_ppm,
812 unsigned int *error_ppm)
813{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200814 /*
815 * For CHV ignore the error and consider only the P value.
816 * Prefer a bigger P value based on HW requirements.
817 */
818 if (IS_CHERRYVIEW(dev)) {
819 *error_ppm = 0;
820
821 return calculated_clock->p > best_clock->p;
822 }
823
Imre Deak24be4e42015-03-17 11:40:04 +0200824 if (WARN_ON_ONCE(!target_freq))
825 return false;
826
Imre Deakd5dd62b2015-03-17 11:40:03 +0200827 *error_ppm = div_u64(1000000ULL *
828 abs(target_freq - calculated_clock->dot),
829 target_freq);
830 /*
831 * Prefer a better P value over a better (smaller) error if the error
832 * is small. Ensure this preference for future configurations too by
833 * setting the error to 0.
834 */
835 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
836 *error_ppm = 0;
837
838 return true;
839 }
840
841 return *error_ppm + 10 < best_error_ppm;
842}
843
Zhenyu Wang2c072452009-06-05 15:38:42 +0800844static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200845vlv_find_best_dpll(const intel_limit_t *limit,
846 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200847 int target, int refclk, intel_clock_t *match_clock,
848 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700849{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200850 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300851 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300852 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300853 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300854 /* min update 19.2 MHz */
855 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300856 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700857
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300858 target *= 5; /* fast clock */
859
860 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700861
862 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300863 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300864 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300865 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300866 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300867 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700868 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300869 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200870 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300871
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300872 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
873 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300874
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300875 vlv_clock(refclk, &clock);
876
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300877 if (!intel_PLL_is_valid(dev, limit,
878 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300879 continue;
880
Imre Deakd5dd62b2015-03-17 11:40:03 +0200881 if (!vlv_PLL_is_optimal(dev, target,
882 &clock,
883 best_clock,
884 bestppm, &ppm))
885 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300886
Imre Deakd5dd62b2015-03-17 11:40:03 +0200887 *best_clock = clock;
888 bestppm = ppm;
889 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700890 }
891 }
892 }
893 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700894
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300895 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700896}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700897
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300898static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200899chv_find_best_dpll(const intel_limit_t *limit,
900 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300901 int target, int refclk, intel_clock_t *match_clock,
902 intel_clock_t *best_clock)
903{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200904 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300905 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200906 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300907 intel_clock_t clock;
908 uint64_t m2;
909 int found = false;
910
911 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200912 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300913
914 /*
915 * Based on hardware doc, the n always set to 1, and m1 always
916 * set to 2. If requires to support 200Mhz refclk, we need to
917 * revisit this because n may not 1 anymore.
918 */
919 clock.n = 1, clock.m1 = 2;
920 target *= 5; /* fast clock */
921
922 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
923 for (clock.p2 = limit->p2.p2_fast;
924 clock.p2 >= limit->p2.p2_slow;
925 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200926 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300927
928 clock.p = clock.p1 * clock.p2;
929
930 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
931 clock.n) << 22, refclk * clock.m1);
932
933 if (m2 > INT_MAX/clock.m1)
934 continue;
935
936 clock.m2 = m2;
937
938 chv_clock(refclk, &clock);
939
940 if (!intel_PLL_is_valid(dev, limit, &clock))
941 continue;
942
Imre Deak9ca3ba02015-03-17 11:40:05 +0200943 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
944 best_error_ppm, &error_ppm))
945 continue;
946
947 *best_clock = clock;
948 best_error_ppm = error_ppm;
949 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300950 }
951 }
952
953 return found;
954}
955
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300956bool intel_crtc_active(struct drm_crtc *crtc)
957{
958 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
959
960 /* Be paranoid as we can arrive here with only partial
961 * state retrieved from the hardware during setup.
962 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100963 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300964 * as Haswell has gained clock readout/fastboot support.
965 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000966 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300967 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700968 *
969 * FIXME: The intel_crtc->active here should be switched to
970 * crtc->state->active once we have proper CRTC states wired up
971 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300972 */
Matt Roperc3d1f432015-03-09 10:19:23 -0700973 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200974 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300975}
976
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200977enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
978 enum pipe pipe)
979{
980 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
981 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
982
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200983 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200984}
985
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300986static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
987{
988 struct drm_i915_private *dev_priv = dev->dev_private;
989 u32 reg = PIPEDSL(pipe);
990 u32 line1, line2;
991 u32 line_mask;
992
993 if (IS_GEN2(dev))
994 line_mask = DSL_LINEMASK_GEN2;
995 else
996 line_mask = DSL_LINEMASK_GEN3;
997
998 line1 = I915_READ(reg) & line_mask;
999 mdelay(5);
1000 line2 = I915_READ(reg) & line_mask;
1001
1002 return line1 == line2;
1003}
1004
Keith Packardab7ad7f2010-10-03 00:33:06 -07001005/*
1006 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001007 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001008 *
1009 * After disabling a pipe, we can't wait for vblank in the usual way,
1010 * spinning on the vblank interrupt status bit, since we won't actually
1011 * see an interrupt when the pipe is disabled.
1012 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001013 * On Gen4 and above:
1014 * wait for the pipe register state bit to turn off
1015 *
1016 * Otherwise:
1017 * wait for the display line value to settle (it usually
1018 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001019 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001020 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001021static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001022{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001023 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001024 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001025 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001026 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001027
Keith Packardab7ad7f2010-10-03 00:33:06 -07001028 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001029 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001030
Keith Packardab7ad7f2010-10-03 00:33:06 -07001031 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001032 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1033 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001034 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001035 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001036 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001037 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001038 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001039 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001040}
1041
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001042/*
1043 * ibx_digital_port_connected - is the specified port connected?
1044 * @dev_priv: i915 private structure
1045 * @port: the port to test
1046 *
1047 * Returns true if @port is connected, false otherwise.
1048 */
1049bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1050 struct intel_digital_port *port)
1051{
1052 u32 bit;
1053
Damien Lespiauc36346e2012-12-13 16:09:03 +00001054 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001055 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001056 case PORT_B:
1057 bit = SDE_PORTB_HOTPLUG;
1058 break;
1059 case PORT_C:
1060 bit = SDE_PORTC_HOTPLUG;
1061 break;
1062 case PORT_D:
1063 bit = SDE_PORTD_HOTPLUG;
1064 break;
1065 default:
1066 return true;
1067 }
1068 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001069 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001070 case PORT_B:
1071 bit = SDE_PORTB_HOTPLUG_CPT;
1072 break;
1073 case PORT_C:
1074 bit = SDE_PORTC_HOTPLUG_CPT;
1075 break;
1076 case PORT_D:
1077 bit = SDE_PORTD_HOTPLUG_CPT;
1078 break;
1079 default:
1080 return true;
1081 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001082 }
1083
1084 return I915_READ(SDEISR) & bit;
1085}
1086
Jesse Barnesb24e7172011-01-04 15:09:30 -08001087static const char *state_string(bool enabled)
1088{
1089 return enabled ? "on" : "off";
1090}
1091
1092/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001093void assert_pll(struct drm_i915_private *dev_priv,
1094 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001095{
1096 int reg;
1097 u32 val;
1098 bool cur_state;
1099
1100 reg = DPLL(pipe);
1101 val = I915_READ(reg);
1102 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001103 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001104 "PLL state assertion failure (expected %s, current %s)\n",
1105 state_string(state), state_string(cur_state));
1106}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001107
Jani Nikula23538ef2013-08-27 15:12:22 +03001108/* XXX: the dsi pll is shared between MIPI DSI ports */
1109static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1110{
1111 u32 val;
1112 bool cur_state;
1113
1114 mutex_lock(&dev_priv->dpio_lock);
1115 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1116 mutex_unlock(&dev_priv->dpio_lock);
1117
1118 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001119 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001120 "DSI PLL state assertion failure (expected %s, current %s)\n",
1121 state_string(state), state_string(cur_state));
1122}
1123#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1124#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1125
Daniel Vetter55607e82013-06-16 21:42:39 +02001126struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001127intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001128{
Daniel Vettere2b78262013-06-07 23:10:03 +02001129 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1130
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001131 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001132 return NULL;
1133
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001134 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001135}
1136
Jesse Barnesb24e7172011-01-04 15:09:30 -08001137/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001138void assert_shared_dpll(struct drm_i915_private *dev_priv,
1139 struct intel_shared_dpll *pll,
1140 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001141{
Jesse Barnes040484a2011-01-03 12:14:26 -08001142 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001143 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001144
Chris Wilson92b27b02012-05-20 18:10:50 +01001145 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001146 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001147 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001148
Daniel Vetter53589012013-06-05 13:34:16 +02001149 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001150 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001151 "%s assertion failure (expected %s, current %s)\n",
1152 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001153}
Jesse Barnes040484a2011-01-03 12:14:26 -08001154
1155static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1156 enum pipe pipe, bool state)
1157{
1158 int reg;
1159 u32 val;
1160 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001161 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1162 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001163
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001164 if (HAS_DDI(dev_priv->dev)) {
1165 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001166 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001167 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001168 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001169 } else {
1170 reg = FDI_TX_CTL(pipe);
1171 val = I915_READ(reg);
1172 cur_state = !!(val & FDI_TX_ENABLE);
1173 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001174 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001175 "FDI TX state assertion failure (expected %s, current %s)\n",
1176 state_string(state), state_string(cur_state));
1177}
1178#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1179#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1180
1181static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1182 enum pipe pipe, bool state)
1183{
1184 int reg;
1185 u32 val;
1186 bool cur_state;
1187
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001188 reg = FDI_RX_CTL(pipe);
1189 val = I915_READ(reg);
1190 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001191 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001192 "FDI RX state assertion failure (expected %s, current %s)\n",
1193 state_string(state), state_string(cur_state));
1194}
1195#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1196#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1197
1198static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1199 enum pipe pipe)
1200{
1201 int reg;
1202 u32 val;
1203
1204 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001205 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001206 return;
1207
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001208 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001209 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001210 return;
1211
Jesse Barnes040484a2011-01-03 12:14:26 -08001212 reg = FDI_TX_CTL(pipe);
1213 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001214 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 -08001215}
1216
Daniel Vetter55607e82013-06-16 21:42:39 +02001217void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1218 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001219{
1220 int reg;
1221 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001222 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001223
1224 reg = FDI_RX_CTL(pipe);
1225 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001226 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001227 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001228 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1229 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001230}
1231
Daniel Vetterb680c372014-09-19 18:27:27 +02001232void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1233 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001234{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001235 struct drm_device *dev = dev_priv->dev;
1236 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001237 u32 val;
1238 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001239 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001240
Jani Nikulabedd4db2014-08-22 15:04:13 +03001241 if (WARN_ON(HAS_DDI(dev)))
1242 return;
1243
1244 if (HAS_PCH_SPLIT(dev)) {
1245 u32 port_sel;
1246
Jesse Barnesea0760c2011-01-04 15:09:32 -08001247 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001248 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1249
1250 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1251 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1252 panel_pipe = PIPE_B;
1253 /* XXX: else fix for eDP */
1254 } else if (IS_VALLEYVIEW(dev)) {
1255 /* presumably write lock depends on pipe, not port select */
1256 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1257 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001258 } else {
1259 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001260 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1261 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001262 }
1263
1264 val = I915_READ(pp_reg);
1265 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001266 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001267 locked = false;
1268
Rob Clarke2c719b2014-12-15 13:56:32 -05001269 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001270 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001271 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001272}
1273
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001274static void assert_cursor(struct drm_i915_private *dev_priv,
1275 enum pipe pipe, bool state)
1276{
1277 struct drm_device *dev = dev_priv->dev;
1278 bool cur_state;
1279
Paulo Zanonid9d82082014-02-27 16:30:56 -03001280 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001281 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001282 else
Ville Syrjälä5efb3e282014-04-09 13:28:53 +03001283 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001284
Rob Clarke2c719b2014-12-15 13:56:32 -05001285 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001286 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1287 pipe_name(pipe), state_string(state), state_string(cur_state));
1288}
1289#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1290#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1291
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001292void assert_pipe(struct drm_i915_private *dev_priv,
1293 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001294{
1295 int reg;
1296 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001297 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001298 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1299 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001300
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001301 /* if we need the pipe quirk it must be always on */
1302 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1303 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001304 state = true;
1305
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001306 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001307 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001308 cur_state = false;
1309 } else {
1310 reg = PIPECONF(cpu_transcoder);
1311 val = I915_READ(reg);
1312 cur_state = !!(val & PIPECONF_ENABLE);
1313 }
1314
Rob Clarke2c719b2014-12-15 13:56:32 -05001315 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001316 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001317 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001318}
1319
Chris Wilson931872f2012-01-16 23:01:13 +00001320static void assert_plane(struct drm_i915_private *dev_priv,
1321 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001322{
1323 int reg;
1324 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001325 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001326
1327 reg = DSPCNTR(plane);
1328 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001329 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001330 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001331 "plane %c assertion failure (expected %s, current %s)\n",
1332 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001333}
1334
Chris Wilson931872f2012-01-16 23:01:13 +00001335#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1336#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1337
Jesse Barnesb24e7172011-01-04 15:09:30 -08001338static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1339 enum pipe pipe)
1340{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001341 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001342 int reg, i;
1343 u32 val;
1344 int cur_pipe;
1345
Ville Syrjälä653e1022013-06-04 13:49:05 +03001346 /* Primary planes are fixed to pipes on gen4+ */
1347 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001348 reg = DSPCNTR(pipe);
1349 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001350 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001351 "plane %c assertion failure, should be disabled but not\n",
1352 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001353 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001354 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001355
Jesse Barnesb24e7172011-01-04 15:09:30 -08001356 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001357 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001358 reg = DSPCNTR(i);
1359 val = I915_READ(reg);
1360 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1361 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001362 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001363 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1364 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001365 }
1366}
1367
Jesse Barnes19332d72013-03-28 09:55:38 -07001368static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1369 enum pipe pipe)
1370{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001371 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001372 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001373 u32 val;
1374
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001375 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001376 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001377 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001378 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001379 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1380 sprite, pipe_name(pipe));
1381 }
1382 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001383 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001384 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001385 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001386 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001387 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001388 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001389 }
1390 } else if (INTEL_INFO(dev)->gen >= 7) {
1391 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001392 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001393 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001394 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001395 plane_name(pipe), pipe_name(pipe));
1396 } else if (INTEL_INFO(dev)->gen >= 5) {
1397 reg = DVSCNTR(pipe);
1398 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001399 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001400 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1401 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001402 }
1403}
1404
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001405static void assert_vblank_disabled(struct drm_crtc *crtc)
1406{
Rob Clarke2c719b2014-12-15 13:56:32 -05001407 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001408 drm_crtc_vblank_put(crtc);
1409}
1410
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001411static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001412{
1413 u32 val;
1414 bool enabled;
1415
Rob Clarke2c719b2014-12-15 13:56:32 -05001416 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001417
Jesse Barnes92f25842011-01-04 15:09:34 -08001418 val = I915_READ(PCH_DREF_CONTROL);
1419 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1420 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001421 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001422}
1423
Daniel Vetterab9412b2013-05-03 11:49:46 +02001424static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1425 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001426{
1427 int reg;
1428 u32 val;
1429 bool enabled;
1430
Daniel Vetterab9412b2013-05-03 11:49:46 +02001431 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001432 val = I915_READ(reg);
1433 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001434 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001435 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1436 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001437}
1438
Keith Packard4e634382011-08-06 10:39:45 -07001439static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1440 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001441{
1442 if ((val & DP_PORT_EN) == 0)
1443 return false;
1444
1445 if (HAS_PCH_CPT(dev_priv->dev)) {
1446 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1447 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1448 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1449 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001450 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1451 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1452 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001453 } else {
1454 if ((val & DP_PIPE_MASK) != (pipe << 30))
1455 return false;
1456 }
1457 return true;
1458}
1459
Keith Packard1519b992011-08-06 10:35:34 -07001460static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1461 enum pipe pipe, u32 val)
1462{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001463 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001464 return false;
1465
1466 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001467 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001468 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001469 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1470 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1471 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001472 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001473 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001474 return false;
1475 }
1476 return true;
1477}
1478
1479static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1480 enum pipe pipe, u32 val)
1481{
1482 if ((val & LVDS_PORT_EN) == 0)
1483 return false;
1484
1485 if (HAS_PCH_CPT(dev_priv->dev)) {
1486 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1487 return false;
1488 } else {
1489 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1490 return false;
1491 }
1492 return true;
1493}
1494
1495static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1496 enum pipe pipe, u32 val)
1497{
1498 if ((val & ADPA_DAC_ENABLE) == 0)
1499 return false;
1500 if (HAS_PCH_CPT(dev_priv->dev)) {
1501 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1502 return false;
1503 } else {
1504 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1505 return false;
1506 }
1507 return true;
1508}
1509
Jesse Barnes291906f2011-02-02 12:28:03 -08001510static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001511 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001512{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001513 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001514 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001515 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001516 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001517
Rob Clarke2c719b2014-12-15 13:56:32 -05001518 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001519 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001520 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001521}
1522
1523static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1524 enum pipe pipe, int reg)
1525{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001526 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001527 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001528 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001529 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001530
Rob Clarke2c719b2014-12-15 13:56:32 -05001531 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001532 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001533 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001534}
1535
1536static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1537 enum pipe pipe)
1538{
1539 int reg;
1540 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001541
Keith Packardf0575e92011-07-25 22:12:43 -07001542 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1543 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1544 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001545
1546 reg = PCH_ADPA;
1547 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001548 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001549 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001550 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001551
1552 reg = PCH_LVDS;
1553 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001554 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001555 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001556 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001557
Paulo Zanonie2debe92013-02-18 19:00:27 -03001558 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1559 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1560 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001561}
1562
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001563static void intel_init_dpio(struct drm_device *dev)
1564{
1565 struct drm_i915_private *dev_priv = dev->dev_private;
1566
1567 if (!IS_VALLEYVIEW(dev))
1568 return;
1569
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001570 /*
1571 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1572 * CHV x1 PHY (DP/HDMI D)
1573 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1574 */
1575 if (IS_CHERRYVIEW(dev)) {
1576 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1577 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1578 } else {
1579 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1580 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001581}
1582
Ville Syrjäläd288f652014-10-28 13:20:22 +02001583static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001584 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001585{
Daniel Vetter426115c2013-07-11 22:13:42 +02001586 struct drm_device *dev = crtc->base.dev;
1587 struct drm_i915_private *dev_priv = dev->dev_private;
1588 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001589 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001590
Daniel Vetter426115c2013-07-11 22:13:42 +02001591 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001592
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001593 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001594 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1595
1596 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001597 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001598 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001599
Daniel Vetter426115c2013-07-11 22:13:42 +02001600 I915_WRITE(reg, dpll);
1601 POSTING_READ(reg);
1602 udelay(150);
1603
1604 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1605 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1606
Ville Syrjäläd288f652014-10-28 13:20:22 +02001607 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001608 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001609
1610 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001611 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001612 POSTING_READ(reg);
1613 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001614 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001615 POSTING_READ(reg);
1616 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001617 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001618 POSTING_READ(reg);
1619 udelay(150); /* wait for warmup */
1620}
1621
Ville Syrjäläd288f652014-10-28 13:20:22 +02001622static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001623 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001624{
1625 struct drm_device *dev = crtc->base.dev;
1626 struct drm_i915_private *dev_priv = dev->dev_private;
1627 int pipe = crtc->pipe;
1628 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001629 u32 tmp;
1630
1631 assert_pipe_disabled(dev_priv, crtc->pipe);
1632
1633 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1634
1635 mutex_lock(&dev_priv->dpio_lock);
1636
1637 /* Enable back the 10bit clock to display controller */
1638 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1639 tmp |= DPIO_DCLKP_EN;
1640 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1641
1642 /*
1643 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1644 */
1645 udelay(1);
1646
1647 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001648 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001649
1650 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001651 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001652 DRM_ERROR("PLL %d failed to lock\n", pipe);
1653
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001654 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001655 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001656 POSTING_READ(DPLL_MD(pipe));
1657
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001658 mutex_unlock(&dev_priv->dpio_lock);
1659}
1660
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001661static int intel_num_dvo_pipes(struct drm_device *dev)
1662{
1663 struct intel_crtc *crtc;
1664 int count = 0;
1665
1666 for_each_intel_crtc(dev, crtc)
1667 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001668 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001669
1670 return count;
1671}
1672
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001673static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001674{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001675 struct drm_device *dev = crtc->base.dev;
1676 struct drm_i915_private *dev_priv = dev->dev_private;
1677 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001678 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001679
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001680 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001681
1682 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001683 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001684
1685 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001686 if (IS_MOBILE(dev) && !IS_I830(dev))
1687 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001688
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001689 /* Enable DVO 2x clock on both PLLs if necessary */
1690 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1691 /*
1692 * It appears to be important that we don't enable this
1693 * for the current pipe before otherwise configuring the
1694 * PLL. No idea how this should be handled if multiple
1695 * DVO outputs are enabled simultaneosly.
1696 */
1697 dpll |= DPLL_DVO_2X_MODE;
1698 I915_WRITE(DPLL(!crtc->pipe),
1699 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1700 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001701
1702 /* Wait for the clocks to stabilize. */
1703 POSTING_READ(reg);
1704 udelay(150);
1705
1706 if (INTEL_INFO(dev)->gen >= 4) {
1707 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001708 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001709 } else {
1710 /* The pixel multiplier can only be updated once the
1711 * DPLL is enabled and the clocks are stable.
1712 *
1713 * So write it again.
1714 */
1715 I915_WRITE(reg, dpll);
1716 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001717
1718 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001719 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001720 POSTING_READ(reg);
1721 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001722 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001723 POSTING_READ(reg);
1724 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001725 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001726 POSTING_READ(reg);
1727 udelay(150); /* wait for warmup */
1728}
1729
1730/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001731 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001732 * @dev_priv: i915 private structure
1733 * @pipe: pipe PLL to disable
1734 *
1735 * Disable the PLL for @pipe, making sure the pipe is off first.
1736 *
1737 * Note! This is for pre-ILK only.
1738 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001739static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001740{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001741 struct drm_device *dev = crtc->base.dev;
1742 struct drm_i915_private *dev_priv = dev->dev_private;
1743 enum pipe pipe = crtc->pipe;
1744
1745 /* Disable DVO 2x clock on both PLLs if necessary */
1746 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001747 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001748 intel_num_dvo_pipes(dev) == 1) {
1749 I915_WRITE(DPLL(PIPE_B),
1750 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1751 I915_WRITE(DPLL(PIPE_A),
1752 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1753 }
1754
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001755 /* Don't disable pipe or pipe PLLs if needed */
1756 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1757 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001758 return;
1759
1760 /* Make sure the pipe isn't still relying on us */
1761 assert_pipe_disabled(dev_priv, pipe);
1762
Daniel Vetter50b44a42013-06-05 13:34:33 +02001763 I915_WRITE(DPLL(pipe), 0);
1764 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001765}
1766
Jesse Barnesf6071162013-10-01 10:41:38 -07001767static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1768{
1769 u32 val = 0;
1770
1771 /* Make sure the pipe isn't still relying on us */
1772 assert_pipe_disabled(dev_priv, pipe);
1773
Imre Deake5cbfbf2014-01-09 17:08:16 +02001774 /*
1775 * Leave integrated clock source and reference clock enabled for pipe B.
1776 * The latter is needed for VGA hotplug / manual detection.
1777 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001778 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001779 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001780 I915_WRITE(DPLL(pipe), val);
1781 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001782
1783}
1784
1785static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1786{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001787 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001788 u32 val;
1789
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001790 /* Make sure the pipe isn't still relying on us */
1791 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001792
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001793 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001794 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001795 if (pipe != PIPE_A)
1796 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1797 I915_WRITE(DPLL(pipe), val);
1798 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001799
1800 mutex_lock(&dev_priv->dpio_lock);
1801
1802 /* Disable 10bit clock to display controller */
1803 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1804 val &= ~DPIO_DCLKP_EN;
1805 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1806
Ville Syrjälä61407f62014-05-27 16:32:55 +03001807 /* disable left/right clock distribution */
1808 if (pipe != PIPE_B) {
1809 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1810 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1811 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1812 } else {
1813 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1814 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1815 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1816 }
1817
Ville Syrjäläd7520482014-04-09 13:28:59 +03001818 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001819}
1820
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001821void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1822 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001823{
1824 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001825 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001826
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001827 switch (dport->port) {
1828 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001829 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001830 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001831 break;
1832 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001833 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001834 dpll_reg = DPLL(0);
1835 break;
1836 case PORT_D:
1837 port_mask = DPLL_PORTD_READY_MASK;
1838 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001839 break;
1840 default:
1841 BUG();
1842 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001843
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001844 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
Jesse Barnes89b667f2013-04-18 14:51:36 -07001845 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001846 port_name(dport->port), I915_READ(dpll_reg));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001847}
1848
Daniel Vetterb14b1052014-04-24 23:55:13 +02001849static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1850{
1851 struct drm_device *dev = crtc->base.dev;
1852 struct drm_i915_private *dev_priv = dev->dev_private;
1853 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1854
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001855 if (WARN_ON(pll == NULL))
1856 return;
1857
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001858 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001859 if (pll->active == 0) {
1860 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1861 WARN_ON(pll->on);
1862 assert_shared_dpll_disabled(dev_priv, pll);
1863
1864 pll->mode_set(dev_priv, pll);
1865 }
1866}
1867
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001868/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001869 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001870 * @dev_priv: i915 private structure
1871 * @pipe: pipe PLL to enable
1872 *
1873 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1874 * drives the transcoder clock.
1875 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001876static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001877{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001878 struct drm_device *dev = crtc->base.dev;
1879 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001880 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001881
Daniel Vetter87a875b2013-06-05 13:34:19 +02001882 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001883 return;
1884
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001885 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001886 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001887
Damien Lespiau74dd6922014-07-29 18:06:17 +01001888 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001889 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001890 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001891
Daniel Vettercdbd2312013-06-05 13:34:03 +02001892 if (pll->active++) {
1893 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001894 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001895 return;
1896 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001897 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001898
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001899 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1900
Daniel Vetter46edb022013-06-05 13:34:12 +02001901 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001902 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001903 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001904}
1905
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001906static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001907{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001908 struct drm_device *dev = crtc->base.dev;
1909 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001910 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001911
Jesse Barnes92f25842011-01-04 15:09:34 -08001912 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001913 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001914 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001915 return;
1916
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001917 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001918 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001919
Daniel Vetter46edb022013-06-05 13:34:12 +02001920 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1921 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001922 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001923
Chris Wilson48da64a2012-05-13 20:16:12 +01001924 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001925 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001926 return;
1927 }
1928
Daniel Vettere9d69442013-06-05 13:34:15 +02001929 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001930 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001931 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001932 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001933
Daniel Vetter46edb022013-06-05 13:34:12 +02001934 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001935 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001936 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001937
1938 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001939}
1940
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001941static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1942 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001943{
Daniel Vetter23670b322012-11-01 09:15:30 +01001944 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001945 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001946 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001947 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001948
1949 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001950 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001951
1952 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001953 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001954 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001955
1956 /* FDI must be feeding us bits for PCH ports */
1957 assert_fdi_tx_enabled(dev_priv, pipe);
1958 assert_fdi_rx_enabled(dev_priv, pipe);
1959
Daniel Vetter23670b322012-11-01 09:15:30 +01001960 if (HAS_PCH_CPT(dev)) {
1961 /* Workaround: Set the timing override bit before enabling the
1962 * pch transcoder. */
1963 reg = TRANS_CHICKEN2(pipe);
1964 val = I915_READ(reg);
1965 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1966 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001967 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001968
Daniel Vetterab9412b2013-05-03 11:49:46 +02001969 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001970 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001971 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001972
1973 if (HAS_PCH_IBX(dev_priv->dev)) {
1974 /*
1975 * make the BPC in transcoder be consistent with
1976 * that in pipeconf reg.
1977 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001978 val &= ~PIPECONF_BPC_MASK;
1979 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001980 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001981
1982 val &= ~TRANS_INTERLACE_MASK;
1983 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001984 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001985 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001986 val |= TRANS_LEGACY_INTERLACED_ILK;
1987 else
1988 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001989 else
1990 val |= TRANS_PROGRESSIVE;
1991
Jesse Barnes040484a2011-01-03 12:14:26 -08001992 I915_WRITE(reg, val | TRANS_ENABLE);
1993 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001994 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001995}
1996
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001997static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001998 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001999{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002000 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002001
2002 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002003 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002004
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002005 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002006 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002007 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002008
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002009 /* Workaround: set timing override bit. */
2010 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002011 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002012 I915_WRITE(_TRANSA_CHICKEN2, val);
2013
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002014 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002015 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002016
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002017 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2018 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002019 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002020 else
2021 val |= TRANS_PROGRESSIVE;
2022
Daniel Vetterab9412b2013-05-03 11:49:46 +02002023 I915_WRITE(LPT_TRANSCONF, val);
2024 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002025 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002026}
2027
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002028static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2029 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002030{
Daniel Vetter23670b322012-11-01 09:15:30 +01002031 struct drm_device *dev = dev_priv->dev;
2032 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002033
2034 /* FDI relies on the transcoder */
2035 assert_fdi_tx_disabled(dev_priv, pipe);
2036 assert_fdi_rx_disabled(dev_priv, pipe);
2037
Jesse Barnes291906f2011-02-02 12:28:03 -08002038 /* Ports must be off as well */
2039 assert_pch_ports_disabled(dev_priv, pipe);
2040
Daniel Vetterab9412b2013-05-03 11:49:46 +02002041 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002042 val = I915_READ(reg);
2043 val &= ~TRANS_ENABLE;
2044 I915_WRITE(reg, val);
2045 /* wait for PCH transcoder off, transcoder state */
2046 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002047 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002048
2049 if (!HAS_PCH_IBX(dev)) {
2050 /* Workaround: Clear the timing override chicken bit again. */
2051 reg = TRANS_CHICKEN2(pipe);
2052 val = I915_READ(reg);
2053 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2054 I915_WRITE(reg, val);
2055 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002056}
2057
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002058static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002059{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002060 u32 val;
2061
Daniel Vetterab9412b2013-05-03 11:49:46 +02002062 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002063 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002064 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002065 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002066 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002067 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002068
2069 /* Workaround: clear timing override bit. */
2070 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002071 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002072 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002073}
2074
2075/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002076 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002077 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002078 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002079 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002080 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002081 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002082static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002083{
Paulo Zanoni03722642014-01-17 13:51:09 -02002084 struct drm_device *dev = crtc->base.dev;
2085 struct drm_i915_private *dev_priv = dev->dev_private;
2086 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002087 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2088 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002089 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002090 int reg;
2091 u32 val;
2092
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002093 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002094 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002095 assert_sprites_disabled(dev_priv, pipe);
2096
Paulo Zanoni681e5812012-12-06 11:12:38 -02002097 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002098 pch_transcoder = TRANSCODER_A;
2099 else
2100 pch_transcoder = pipe;
2101
Jesse Barnesb24e7172011-01-04 15:09:30 -08002102 /*
2103 * A pipe without a PLL won't actually be able to drive bits from
2104 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2105 * need the check.
2106 */
2107 if (!HAS_PCH_SPLIT(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002108 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002109 assert_dsi_pll_enabled(dev_priv);
2110 else
2111 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002112 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002113 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002114 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002115 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002116 assert_fdi_tx_pll_enabled(dev_priv,
2117 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002118 }
2119 /* FIXME: assert CPU port conditions for SNB+ */
2120 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002121
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002122 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002123 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002124 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002125 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2126 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002127 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002128 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002129
2130 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002131 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002132}
2133
2134/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002135 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002136 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002137 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002138 * Disable the pipe of @crtc, making sure that various hardware
2139 * specific requirements are met, if applicable, e.g. plane
2140 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002141 *
2142 * Will wait until the pipe has shut down before returning.
2143 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002144static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002145{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002146 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002147 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002148 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002149 int reg;
2150 u32 val;
2151
2152 /*
2153 * Make sure planes won't keep trying to pump pixels to us,
2154 * or we might hang the display.
2155 */
2156 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002157 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002158 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002159
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002160 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002161 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002162 if ((val & PIPECONF_ENABLE) == 0)
2163 return;
2164
Ville Syrjälä67adc642014-08-15 01:21:57 +03002165 /*
2166 * Double wide has implications for planes
2167 * so best keep it disabled when not needed.
2168 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002169 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002170 val &= ~PIPECONF_DOUBLE_WIDE;
2171
2172 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002173 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2174 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002175 val &= ~PIPECONF_ENABLE;
2176
2177 I915_WRITE(reg, val);
2178 if ((val & PIPECONF_ENABLE) == 0)
2179 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002180}
2181
Keith Packardd74362c2011-07-28 14:47:14 -07002182/*
2183 * Plane regs are double buffered, going from enabled->disabled needs a
2184 * trigger in order to latch. The display address reg provides this.
2185 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002186void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2187 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002188{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002189 struct drm_device *dev = dev_priv->dev;
2190 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002191
2192 I915_WRITE(reg, I915_READ(reg));
2193 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002194}
2195
Jesse Barnesb24e7172011-01-04 15:09:30 -08002196/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002197 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002198 * @plane: plane to be enabled
2199 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002200 *
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002201 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002202 */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002203static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2204 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002205{
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002206 struct drm_device *dev = plane->dev;
2207 struct drm_i915_private *dev_priv = dev->dev_private;
2208 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002209
2210 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002211 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002212
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002213 if (intel_crtc->primary_enabled)
2214 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002215
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002216 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002217
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002218 dev_priv->display.update_primary_plane(crtc, plane->fb,
2219 crtc->x, crtc->y);
Ville Syrjälä33c3b0d2014-06-24 13:59:28 +03002220
2221 /*
2222 * BDW signals flip done immediately if the plane
2223 * is disabled, even if the plane enable is already
2224 * armed to occur at the next vblank :(
2225 */
2226 if (IS_BROADWELL(dev))
2227 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002228}
2229
Jesse Barnesb24e7172011-01-04 15:09:30 -08002230/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002231 * intel_disable_primary_hw_plane - disable the primary hardware plane
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002232 * @plane: plane to be disabled
2233 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002234 *
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002235 * Disable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002236 */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002237static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2238 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002239{
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002240 struct drm_device *dev = plane->dev;
2241 struct drm_i915_private *dev_priv = dev->dev_private;
2242 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2243
Matt Roper32b7eee2014-12-24 07:59:06 -08002244 if (WARN_ON(!intel_crtc->active))
2245 return;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002246
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002247 if (!intel_crtc->primary_enabled)
2248 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002249
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002250 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002251
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002252 dev_priv->display.update_primary_plane(crtc, plane->fb,
2253 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002254}
2255
Chris Wilson693db182013-03-05 14:52:39 +00002256static bool need_vtd_wa(struct drm_device *dev)
2257{
2258#ifdef CONFIG_INTEL_IOMMU
2259 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2260 return true;
2261#endif
2262 return false;
2263}
2264
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002265unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002266intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2267 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002268{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002269 unsigned int tile_height;
2270 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002271
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002272 switch (fb_format_modifier) {
2273 case DRM_FORMAT_MOD_NONE:
2274 tile_height = 1;
2275 break;
2276 case I915_FORMAT_MOD_X_TILED:
2277 tile_height = IS_GEN2(dev) ? 16 : 8;
2278 break;
2279 case I915_FORMAT_MOD_Y_TILED:
2280 tile_height = 32;
2281 break;
2282 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002283 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2284 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002285 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002286 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002287 tile_height = 64;
2288 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002289 case 2:
2290 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002291 tile_height = 32;
2292 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002293 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002294 tile_height = 16;
2295 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002296 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002297 WARN_ONCE(1,
2298 "128-bit pixels are not supported for display!");
2299 tile_height = 16;
2300 break;
2301 }
2302 break;
2303 default:
2304 MISSING_CASE(fb_format_modifier);
2305 tile_height = 1;
2306 break;
2307 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002308
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002309 return tile_height;
2310}
2311
2312unsigned int
2313intel_fb_align_height(struct drm_device *dev, unsigned int height,
2314 uint32_t pixel_format, uint64_t fb_format_modifier)
2315{
2316 return ALIGN(height, intel_tile_height(dev, pixel_format,
2317 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002318}
2319
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002320static int
2321intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2322 const struct drm_plane_state *plane_state)
2323{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002324 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002325
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002326 *view = i915_ggtt_view_normal;
2327
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002328 if (!plane_state)
2329 return 0;
2330
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002331 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002332 return 0;
2333
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002334 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002335
2336 info->height = fb->height;
2337 info->pixel_format = fb->pixel_format;
2338 info->pitch = fb->pitches[0];
2339 info->fb_modifier = fb->modifier[0];
2340
2341 if (!(info->fb_modifier == I915_FORMAT_MOD_Y_TILED ||
2342 info->fb_modifier == I915_FORMAT_MOD_Yf_TILED)) {
2343 DRM_DEBUG_KMS(
2344 "Y or Yf tiling is needed for 90/270 rotation!\n");
2345 return -EINVAL;
2346 }
2347
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002348 return 0;
2349}
2350
Chris Wilson127bd2a2010-07-23 23:32:05 +01002351int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002352intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2353 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002354 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002355 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002356{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002357 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002358 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002359 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002360 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002361 u32 alignment;
2362 int ret;
2363
Matt Roperebcdd392014-07-09 16:22:11 -07002364 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2365
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002366 switch (fb->modifier[0]) {
2367 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002368 if (INTEL_INFO(dev)->gen >= 9)
2369 alignment = 256 * 1024;
2370 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002371 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002372 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002373 alignment = 4 * 1024;
2374 else
2375 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002376 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002377 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002378 if (INTEL_INFO(dev)->gen >= 9)
2379 alignment = 256 * 1024;
2380 else {
2381 /* pin() will align the object as required by fence */
2382 alignment = 0;
2383 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002384 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002385 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002386 case I915_FORMAT_MOD_Yf_TILED:
2387 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2388 "Y tiling bo slipped through, driver bug!\n"))
2389 return -EINVAL;
2390 alignment = 1 * 1024 * 1024;
2391 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002392 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002393 MISSING_CASE(fb->modifier[0]);
2394 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002395 }
2396
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002397 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2398 if (ret)
2399 return ret;
2400
Chris Wilson693db182013-03-05 14:52:39 +00002401 /* Note that the w/a also requires 64 PTE of padding following the
2402 * bo. We currently fill all unused PTE with the shadow page and so
2403 * we should always have valid PTE following the scanout preventing
2404 * the VT-d warning.
2405 */
2406 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2407 alignment = 256 * 1024;
2408
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002409 /*
2410 * Global gtt pte registers are special registers which actually forward
2411 * writes to a chunk of system memory. Which means that there is no risk
2412 * that the register values disappear as soon as we call
2413 * intel_runtime_pm_put(), so it is correct to wrap only the
2414 * pin/unpin/fence and not more.
2415 */
2416 intel_runtime_pm_get(dev_priv);
2417
Chris Wilsonce453d82011-02-21 14:43:56 +00002418 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002419 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002420 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002421 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002422 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002423
2424 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2425 * fence, whereas 965+ only requires a fence if using
2426 * framebuffer compression. For simplicity, we always install
2427 * a fence as the cost is not that onerous.
2428 */
Chris Wilson06d98132012-04-17 15:31:24 +01002429 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002430 if (ret)
2431 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002432
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002433 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002434
Chris Wilsonce453d82011-02-21 14:43:56 +00002435 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002436 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002437 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002438
2439err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002440 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002441err_interruptible:
2442 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002443 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002444 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002445}
2446
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002447static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2448 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002449{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002450 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002451 struct i915_ggtt_view view;
2452 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002453
Matt Roperebcdd392014-07-09 16:22:11 -07002454 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2455
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002456 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2457 WARN_ONCE(ret, "Couldn't get view from plane state!");
2458
Chris Wilson1690e1e2011-12-14 13:57:08 +01002459 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002460 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002461}
2462
Daniel Vetterc2c75132012-07-05 12:17:30 +02002463/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2464 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002465unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2466 unsigned int tiling_mode,
2467 unsigned int cpp,
2468 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002469{
Chris Wilsonbc752862013-02-21 20:04:31 +00002470 if (tiling_mode != I915_TILING_NONE) {
2471 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002472
Chris Wilsonbc752862013-02-21 20:04:31 +00002473 tile_rows = *y / 8;
2474 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002475
Chris Wilsonbc752862013-02-21 20:04:31 +00002476 tiles = *x / (512/cpp);
2477 *x %= 512/cpp;
2478
2479 return tile_rows * pitch * 8 + tiles * 4096;
2480 } else {
2481 unsigned int offset;
2482
2483 offset = *y * pitch + *x * cpp;
2484 *y = 0;
2485 *x = (offset & 4095) / cpp;
2486 return offset & -4096;
2487 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002488}
2489
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002490static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002491{
2492 switch (format) {
2493 case DISPPLANE_8BPP:
2494 return DRM_FORMAT_C8;
2495 case DISPPLANE_BGRX555:
2496 return DRM_FORMAT_XRGB1555;
2497 case DISPPLANE_BGRX565:
2498 return DRM_FORMAT_RGB565;
2499 default:
2500 case DISPPLANE_BGRX888:
2501 return DRM_FORMAT_XRGB8888;
2502 case DISPPLANE_RGBX888:
2503 return DRM_FORMAT_XBGR8888;
2504 case DISPPLANE_BGRX101010:
2505 return DRM_FORMAT_XRGB2101010;
2506 case DISPPLANE_RGBX101010:
2507 return DRM_FORMAT_XBGR2101010;
2508 }
2509}
2510
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002511static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2512{
2513 switch (format) {
2514 case PLANE_CTL_FORMAT_RGB_565:
2515 return DRM_FORMAT_RGB565;
2516 default:
2517 case PLANE_CTL_FORMAT_XRGB_8888:
2518 if (rgb_order) {
2519 if (alpha)
2520 return DRM_FORMAT_ABGR8888;
2521 else
2522 return DRM_FORMAT_XBGR8888;
2523 } else {
2524 if (alpha)
2525 return DRM_FORMAT_ARGB8888;
2526 else
2527 return DRM_FORMAT_XRGB8888;
2528 }
2529 case PLANE_CTL_FORMAT_XRGB_2101010:
2530 if (rgb_order)
2531 return DRM_FORMAT_XBGR2101010;
2532 else
2533 return DRM_FORMAT_XRGB2101010;
2534 }
2535}
2536
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002537static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002538intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2539 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002540{
2541 struct drm_device *dev = crtc->base.dev;
2542 struct drm_i915_gem_object *obj = NULL;
2543 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002544 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002545 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2546 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2547 PAGE_SIZE);
2548
2549 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002550
Chris Wilsonff2652e2014-03-10 08:07:02 +00002551 if (plane_config->size == 0)
2552 return false;
2553
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002554 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2555 base_aligned,
2556 base_aligned,
2557 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002558 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002559 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002560
Damien Lespiau49af4492015-01-20 12:51:44 +00002561 obj->tiling_mode = plane_config->tiling;
2562 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002563 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002564
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002565 mode_cmd.pixel_format = fb->pixel_format;
2566 mode_cmd.width = fb->width;
2567 mode_cmd.height = fb->height;
2568 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002569 mode_cmd.modifier[0] = fb->modifier[0];
2570 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002571
2572 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002573 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002574 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002575 DRM_DEBUG_KMS("intel fb init failed\n");
2576 goto out_unref_obj;
2577 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002578 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002579
Daniel Vetterf6936e22015-03-26 12:17:05 +01002580 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002581 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002582
2583out_unref_obj:
2584 drm_gem_object_unreference(&obj->base);
2585 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002586 return false;
2587}
2588
Matt Roperafd65eb2015-02-03 13:10:04 -08002589/* Update plane->state->fb to match plane->fb after driver-internal updates */
2590static void
2591update_state_fb(struct drm_plane *plane)
2592{
2593 if (plane->fb == plane->state->fb)
2594 return;
2595
2596 if (plane->state->fb)
2597 drm_framebuffer_unreference(plane->state->fb);
2598 plane->state->fb = plane->fb;
2599 if (plane->state->fb)
2600 drm_framebuffer_reference(plane->state->fb);
2601}
2602
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002603static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002604intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2605 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002606{
2607 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002608 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002609 struct drm_crtc *c;
2610 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002611 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002612 struct drm_plane *primary = intel_crtc->base.primary;
2613 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002614
Damien Lespiau2d140302015-02-05 17:22:18 +00002615 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002616 return;
2617
Daniel Vetterf6936e22015-03-26 12:17:05 +01002618 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002619 fb = &plane_config->fb->base;
2620 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002621 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002622
Damien Lespiau2d140302015-02-05 17:22:18 +00002623 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002624
2625 /*
2626 * Failed to alloc the obj, check to see if we should share
2627 * an fb with another CRTC instead
2628 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002629 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002630 i = to_intel_crtc(c);
2631
2632 if (c == &intel_crtc->base)
2633 continue;
2634
Matt Roper2ff8fde2014-07-08 07:50:07 -07002635 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002636 continue;
2637
Daniel Vetter88595ac2015-03-26 12:42:24 +01002638 fb = c->primary->fb;
2639 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002640 continue;
2641
Daniel Vetter88595ac2015-03-26 12:42:24 +01002642 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002643 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002644 drm_framebuffer_reference(fb);
2645 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002646 }
2647 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002648
2649 return;
2650
2651valid_fb:
2652 obj = intel_fb_obj(fb);
2653 if (obj->tiling_mode != I915_TILING_NONE)
2654 dev_priv->preserve_bios_swizzle = true;
2655
2656 primary->fb = fb;
2657 primary->state->crtc = &intel_crtc->base;
2658 primary->crtc = &intel_crtc->base;
2659 update_state_fb(primary);
2660 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002661}
2662
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002663static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2664 struct drm_framebuffer *fb,
2665 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002666{
2667 struct drm_device *dev = crtc->dev;
2668 struct drm_i915_private *dev_priv = dev->dev_private;
2669 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002670 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002671 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002672 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002673 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002674 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302675 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002676
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002677 if (!intel_crtc->primary_enabled) {
2678 I915_WRITE(reg, 0);
2679 if (INTEL_INFO(dev)->gen >= 4)
2680 I915_WRITE(DSPSURF(plane), 0);
2681 else
2682 I915_WRITE(DSPADDR(plane), 0);
2683 POSTING_READ(reg);
2684 return;
2685 }
2686
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002687 obj = intel_fb_obj(fb);
2688 if (WARN_ON(obj == NULL))
2689 return;
2690
2691 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2692
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002693 dspcntr = DISPPLANE_GAMMA_ENABLE;
2694
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002695 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002696
2697 if (INTEL_INFO(dev)->gen < 4) {
2698 if (intel_crtc->pipe == PIPE_B)
2699 dspcntr |= DISPPLANE_SEL_PIPE_B;
2700
2701 /* pipesrc and dspsize control the size that is scaled from,
2702 * which should always be the user's requested size.
2703 */
2704 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002705 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2706 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002707 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002708 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2709 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002710 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2711 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002712 I915_WRITE(PRIMPOS(plane), 0);
2713 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002714 }
2715
Ville Syrjälä57779d02012-10-31 17:50:14 +02002716 switch (fb->pixel_format) {
2717 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002718 dspcntr |= DISPPLANE_8BPP;
2719 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002720 case DRM_FORMAT_XRGB1555:
2721 case DRM_FORMAT_ARGB1555:
2722 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002723 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002724 case DRM_FORMAT_RGB565:
2725 dspcntr |= DISPPLANE_BGRX565;
2726 break;
2727 case DRM_FORMAT_XRGB8888:
2728 case DRM_FORMAT_ARGB8888:
2729 dspcntr |= DISPPLANE_BGRX888;
2730 break;
2731 case DRM_FORMAT_XBGR8888:
2732 case DRM_FORMAT_ABGR8888:
2733 dspcntr |= DISPPLANE_RGBX888;
2734 break;
2735 case DRM_FORMAT_XRGB2101010:
2736 case DRM_FORMAT_ARGB2101010:
2737 dspcntr |= DISPPLANE_BGRX101010;
2738 break;
2739 case DRM_FORMAT_XBGR2101010:
2740 case DRM_FORMAT_ABGR2101010:
2741 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002742 break;
2743 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002744 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002745 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002746
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002747 if (INTEL_INFO(dev)->gen >= 4 &&
2748 obj->tiling_mode != I915_TILING_NONE)
2749 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002750
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002751 if (IS_G4X(dev))
2752 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2753
Ville Syrjäläb98971272014-08-27 16:51:22 +03002754 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002755
Daniel Vetterc2c75132012-07-05 12:17:30 +02002756 if (INTEL_INFO(dev)->gen >= 4) {
2757 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002758 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002759 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002760 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002761 linear_offset -= intel_crtc->dspaddr_offset;
2762 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002763 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002764 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002765
Matt Roper8e7d6882015-01-21 16:35:41 -08002766 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302767 dspcntr |= DISPPLANE_ROTATE_180;
2768
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002769 x += (intel_crtc->config->pipe_src_w - 1);
2770 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302771
2772 /* Finding the last pixel of the last line of the display
2773 data and adding to linear_offset*/
2774 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002775 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2776 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302777 }
2778
2779 I915_WRITE(reg, dspcntr);
2780
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002781 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002782 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002783 I915_WRITE(DSPSURF(plane),
2784 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002785 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002786 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002787 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002788 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002789 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002790}
2791
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002792static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2793 struct drm_framebuffer *fb,
2794 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002795{
2796 struct drm_device *dev = crtc->dev;
2797 struct drm_i915_private *dev_priv = dev->dev_private;
2798 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002799 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002800 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002801 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002802 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002803 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302804 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002805
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002806 if (!intel_crtc->primary_enabled) {
2807 I915_WRITE(reg, 0);
2808 I915_WRITE(DSPSURF(plane), 0);
2809 POSTING_READ(reg);
2810 return;
2811 }
2812
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002813 obj = intel_fb_obj(fb);
2814 if (WARN_ON(obj == NULL))
2815 return;
2816
2817 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2818
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002819 dspcntr = DISPPLANE_GAMMA_ENABLE;
2820
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002821 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002822
2823 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2824 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2825
Ville Syrjälä57779d02012-10-31 17:50:14 +02002826 switch (fb->pixel_format) {
2827 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002828 dspcntr |= DISPPLANE_8BPP;
2829 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002830 case DRM_FORMAT_RGB565:
2831 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002832 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002833 case DRM_FORMAT_XRGB8888:
2834 case DRM_FORMAT_ARGB8888:
2835 dspcntr |= DISPPLANE_BGRX888;
2836 break;
2837 case DRM_FORMAT_XBGR8888:
2838 case DRM_FORMAT_ABGR8888:
2839 dspcntr |= DISPPLANE_RGBX888;
2840 break;
2841 case DRM_FORMAT_XRGB2101010:
2842 case DRM_FORMAT_ARGB2101010:
2843 dspcntr |= DISPPLANE_BGRX101010;
2844 break;
2845 case DRM_FORMAT_XBGR2101010:
2846 case DRM_FORMAT_ABGR2101010:
2847 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002848 break;
2849 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002850 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002851 }
2852
2853 if (obj->tiling_mode != I915_TILING_NONE)
2854 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002855
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002856 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002857 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002858
Ville Syrjäläb98971272014-08-27 16:51:22 +03002859 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002860 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002861 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002862 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002863 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002864 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002865 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302866 dspcntr |= DISPPLANE_ROTATE_180;
2867
2868 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002869 x += (intel_crtc->config->pipe_src_w - 1);
2870 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302871
2872 /* Finding the last pixel of the last line of the display
2873 data and adding to linear_offset*/
2874 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002875 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2876 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302877 }
2878 }
2879
2880 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002881
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002882 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002883 I915_WRITE(DSPSURF(plane),
2884 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002885 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002886 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2887 } else {
2888 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2889 I915_WRITE(DSPLINOFF(plane), linear_offset);
2890 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002891 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002892}
2893
Damien Lespiaub3218032015-02-27 11:15:18 +00002894u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2895 uint32_t pixel_format)
2896{
2897 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2898
2899 /*
2900 * The stride is either expressed as a multiple of 64 bytes
2901 * chunks for linear buffers or in number of tiles for tiled
2902 * buffers.
2903 */
2904 switch (fb_modifier) {
2905 case DRM_FORMAT_MOD_NONE:
2906 return 64;
2907 case I915_FORMAT_MOD_X_TILED:
2908 if (INTEL_INFO(dev)->gen == 2)
2909 return 128;
2910 return 512;
2911 case I915_FORMAT_MOD_Y_TILED:
2912 /* No need to check for old gens and Y tiling since this is
2913 * about the display engine and those will be blocked before
2914 * we get here.
2915 */
2916 return 128;
2917 case I915_FORMAT_MOD_Yf_TILED:
2918 if (bits_per_pixel == 8)
2919 return 64;
2920 else
2921 return 128;
2922 default:
2923 MISSING_CASE(fb_modifier);
2924 return 64;
2925 }
2926}
2927
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002928unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2929 struct drm_i915_gem_object *obj)
2930{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002931 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002932
2933 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002934 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002935
2936 return i915_gem_obj_ggtt_offset_view(obj, view);
2937}
2938
Damien Lespiau70d21f02013-07-03 21:06:04 +01002939static void skylake_update_primary_plane(struct drm_crtc *crtc,
2940 struct drm_framebuffer *fb,
2941 int x, int y)
2942{
2943 struct drm_device *dev = crtc->dev;
2944 struct drm_i915_private *dev_priv = dev->dev_private;
2945 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002946 struct drm_i915_gem_object *obj;
2947 int pipe = intel_crtc->pipe;
Damien Lespiaub3218032015-02-27 11:15:18 +00002948 u32 plane_ctl, stride_div;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002949 unsigned long surf_addr;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002950
2951 if (!intel_crtc->primary_enabled) {
2952 I915_WRITE(PLANE_CTL(pipe, 0), 0);
2953 I915_WRITE(PLANE_SURF(pipe, 0), 0);
2954 POSTING_READ(PLANE_CTL(pipe, 0));
2955 return;
2956 }
2957
2958 plane_ctl = PLANE_CTL_ENABLE |
2959 PLANE_CTL_PIPE_GAMMA_ENABLE |
2960 PLANE_CTL_PIPE_CSC_ENABLE;
2961
2962 switch (fb->pixel_format) {
2963 case DRM_FORMAT_RGB565:
2964 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
2965 break;
2966 case DRM_FORMAT_XRGB8888:
2967 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2968 break;
Jani Nikulaf75fb422015-02-10 13:15:49 +02002969 case DRM_FORMAT_ARGB8888:
2970 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2971 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2972 break;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002973 case DRM_FORMAT_XBGR8888:
2974 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2975 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2976 break;
Jani Nikulaf75fb422015-02-10 13:15:49 +02002977 case DRM_FORMAT_ABGR8888:
2978 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2979 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2980 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2981 break;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002982 case DRM_FORMAT_XRGB2101010:
2983 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2984 break;
2985 case DRM_FORMAT_XBGR2101010:
2986 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2987 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2988 break;
2989 default:
2990 BUG();
2991 }
2992
Daniel Vetter30af77c2015-02-10 17:16:11 +00002993 switch (fb->modifier[0]) {
2994 case DRM_FORMAT_MOD_NONE:
Damien Lespiau70d21f02013-07-03 21:06:04 +01002995 break;
Daniel Vetter30af77c2015-02-10 17:16:11 +00002996 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau70d21f02013-07-03 21:06:04 +01002997 plane_ctl |= PLANE_CTL_TILED_X;
Damien Lespiaub3218032015-02-27 11:15:18 +00002998 break;
2999 case I915_FORMAT_MOD_Y_TILED:
3000 plane_ctl |= PLANE_CTL_TILED_Y;
3001 break;
3002 case I915_FORMAT_MOD_Yf_TILED:
3003 plane_ctl |= PLANE_CTL_TILED_YF;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003004 break;
3005 default:
Damien Lespiaub3218032015-02-27 11:15:18 +00003006 MISSING_CASE(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003007 }
3008
3009 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Matt Roper8e7d6882015-01-21 16:35:41 -08003010 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180))
Sonika Jindal1447dde2014-10-04 10:53:31 +01003011 plane_ctl |= PLANE_CTL_ROTATE_180;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003012
Damien Lespiaub3218032015-02-27 11:15:18 +00003013 obj = intel_fb_obj(fb);
3014 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3015 fb->pixel_format);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003016 surf_addr = intel_plane_obj_offset(to_intel_plane(crtc->primary), obj);
Damien Lespiaub3218032015-02-27 11:15:18 +00003017
Damien Lespiau70d21f02013-07-03 21:06:04 +01003018 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003019 I915_WRITE(PLANE_POS(pipe, 0), 0);
3020 I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
3021 I915_WRITE(PLANE_SIZE(pipe, 0),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003022 (intel_crtc->config->pipe_src_h - 1) << 16 |
3023 (intel_crtc->config->pipe_src_w - 1));
Damien Lespiaub3218032015-02-27 11:15:18 +00003024 I915_WRITE(PLANE_STRIDE(pipe, 0), fb->pitches[0] / stride_div);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003025 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003026
3027 POSTING_READ(PLANE_SURF(pipe, 0));
3028}
3029
Jesse Barnes17638cd2011-06-24 12:19:23 -07003030/* Assume fb object is pinned & idle & fenced and just update base pointers */
3031static int
3032intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3033 int x, int y, enum mode_set_atomic state)
3034{
3035 struct drm_device *dev = crtc->dev;
3036 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003037
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003038 if (dev_priv->display.disable_fbc)
3039 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003040
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003041 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3042
3043 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003044}
3045
Ville Syrjälä75147472014-11-24 18:28:11 +02003046static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003047{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003048 struct drm_crtc *crtc;
3049
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003050 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003051 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3052 enum plane plane = intel_crtc->plane;
3053
3054 intel_prepare_page_flip(dev, plane);
3055 intel_finish_page_flip_plane(dev, plane);
3056 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003057}
3058
3059static void intel_update_primary_planes(struct drm_device *dev)
3060{
3061 struct drm_i915_private *dev_priv = dev->dev_private;
3062 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003063
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003064 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003065 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3066
Rob Clark51fd3712013-11-19 12:10:12 -05003067 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003068 /*
3069 * FIXME: Once we have proper support for primary planes (and
3070 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003071 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003072 */
Matt Roperf4510a22014-04-01 15:22:40 -07003073 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003074 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003075 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003076 crtc->x,
3077 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003078 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003079 }
3080}
3081
Ville Syrjälä75147472014-11-24 18:28:11 +02003082void intel_prepare_reset(struct drm_device *dev)
3083{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003084 struct drm_i915_private *dev_priv = to_i915(dev);
3085 struct intel_crtc *crtc;
3086
Ville Syrjälä75147472014-11-24 18:28:11 +02003087 /* no reset support for gen2 */
3088 if (IS_GEN2(dev))
3089 return;
3090
3091 /* reset doesn't touch the display */
3092 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3093 return;
3094
3095 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003096
3097 /*
3098 * Disabling the crtcs gracefully seems nicer. Also the
3099 * g33 docs say we should at least disable all the planes.
3100 */
3101 for_each_intel_crtc(dev, crtc) {
3102 if (crtc->active)
3103 dev_priv->display.crtc_disable(&crtc->base);
3104 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003105}
3106
3107void intel_finish_reset(struct drm_device *dev)
3108{
3109 struct drm_i915_private *dev_priv = to_i915(dev);
3110
3111 /*
3112 * Flips in the rings will be nuked by the reset,
3113 * so complete all pending flips so that user space
3114 * will get its events and not get stuck.
3115 */
3116 intel_complete_page_flips(dev);
3117
3118 /* no reset support for gen2 */
3119 if (IS_GEN2(dev))
3120 return;
3121
3122 /* reset doesn't touch the display */
3123 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3124 /*
3125 * Flips in the rings have been nuked by the reset,
3126 * so update the base address of all primary
3127 * planes to the the last fb to make sure we're
3128 * showing the correct fb after a reset.
3129 */
3130 intel_update_primary_planes(dev);
3131 return;
3132 }
3133
3134 /*
3135 * The display has been reset as well,
3136 * so need a full re-initialization.
3137 */
3138 intel_runtime_pm_disable_interrupts(dev_priv);
3139 intel_runtime_pm_enable_interrupts(dev_priv);
3140
3141 intel_modeset_init_hw(dev);
3142
3143 spin_lock_irq(&dev_priv->irq_lock);
3144 if (dev_priv->display.hpd_irq_setup)
3145 dev_priv->display.hpd_irq_setup(dev);
3146 spin_unlock_irq(&dev_priv->irq_lock);
3147
3148 intel_modeset_setup_hw_state(dev, true);
3149
3150 intel_hpd_init(dev_priv);
3151
3152 drm_modeset_unlock_all(dev);
3153}
3154
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003155static int
Chris Wilson14667a42012-04-03 17:58:35 +01003156intel_finish_fb(struct drm_framebuffer *old_fb)
3157{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003158 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01003159 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3160 bool was_interruptible = dev_priv->mm.interruptible;
3161 int ret;
3162
Chris Wilson14667a42012-04-03 17:58:35 +01003163 /* Big Hammer, we also need to ensure that any pending
3164 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3165 * current scanout is retired before unpinning the old
3166 * framebuffer.
3167 *
3168 * This should only fail upon a hung GPU, in which case we
3169 * can safely continue.
3170 */
3171 dev_priv->mm.interruptible = false;
3172 ret = i915_gem_object_finish_gpu(obj);
3173 dev_priv->mm.interruptible = was_interruptible;
3174
3175 return ret;
3176}
3177
Chris Wilson7d5e3792014-03-04 13:15:08 +00003178static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3179{
3180 struct drm_device *dev = crtc->dev;
3181 struct drm_i915_private *dev_priv = dev->dev_private;
3182 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003183 bool pending;
3184
3185 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3186 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3187 return false;
3188
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003189 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003190 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003191 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003192
3193 return pending;
3194}
3195
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003196static void intel_update_pipe_size(struct intel_crtc *crtc)
3197{
3198 struct drm_device *dev = crtc->base.dev;
3199 struct drm_i915_private *dev_priv = dev->dev_private;
3200 const struct drm_display_mode *adjusted_mode;
3201
3202 if (!i915.fastboot)
3203 return;
3204
3205 /*
3206 * Update pipe size and adjust fitter if needed: the reason for this is
3207 * that in compute_mode_changes we check the native mode (not the pfit
3208 * mode) to see if we can flip rather than do a full mode set. In the
3209 * fastboot case, we'll flip, but if we don't update the pipesrc and
3210 * pfit state, we'll end up with a big fb scanned out into the wrong
3211 * sized surface.
3212 *
3213 * To fix this properly, we need to hoist the checks up into
3214 * compute_mode_changes (or above), check the actual pfit state and
3215 * whether the platform allows pfit disable with pipe active, and only
3216 * then update the pipesrc and pfit state, even on the flip path.
3217 */
3218
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003219 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003220
3221 I915_WRITE(PIPESRC(crtc->pipe),
3222 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3223 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003224 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003225 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3226 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003227 I915_WRITE(PF_CTL(crtc->pipe), 0);
3228 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3229 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3230 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003231 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3232 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003233}
3234
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003235static void intel_fdi_normal_train(struct drm_crtc *crtc)
3236{
3237 struct drm_device *dev = crtc->dev;
3238 struct drm_i915_private *dev_priv = dev->dev_private;
3239 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3240 int pipe = intel_crtc->pipe;
3241 u32 reg, temp;
3242
3243 /* enable normal train */
3244 reg = FDI_TX_CTL(pipe);
3245 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003246 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003247 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3248 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003249 } else {
3250 temp &= ~FDI_LINK_TRAIN_NONE;
3251 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003252 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003253 I915_WRITE(reg, temp);
3254
3255 reg = FDI_RX_CTL(pipe);
3256 temp = I915_READ(reg);
3257 if (HAS_PCH_CPT(dev)) {
3258 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3259 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3260 } else {
3261 temp &= ~FDI_LINK_TRAIN_NONE;
3262 temp |= FDI_LINK_TRAIN_NONE;
3263 }
3264 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3265
3266 /* wait one idle pattern time */
3267 POSTING_READ(reg);
3268 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003269
3270 /* IVB wants error correction enabled */
3271 if (IS_IVYBRIDGE(dev))
3272 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3273 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003274}
3275
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003276/* The FDI link training functions for ILK/Ibexpeak. */
3277static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3278{
3279 struct drm_device *dev = crtc->dev;
3280 struct drm_i915_private *dev_priv = dev->dev_private;
3281 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3282 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003283 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003284
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003285 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003286 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003287
Adam Jacksone1a44742010-06-25 15:32:14 -04003288 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3289 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003290 reg = FDI_RX_IMR(pipe);
3291 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003292 temp &= ~FDI_RX_SYMBOL_LOCK;
3293 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003294 I915_WRITE(reg, temp);
3295 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003296 udelay(150);
3297
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003298 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003299 reg = FDI_TX_CTL(pipe);
3300 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003301 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003302 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003303 temp &= ~FDI_LINK_TRAIN_NONE;
3304 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003305 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003306
Chris Wilson5eddb702010-09-11 13:48:45 +01003307 reg = FDI_RX_CTL(pipe);
3308 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003309 temp &= ~FDI_LINK_TRAIN_NONE;
3310 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003311 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3312
3313 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003314 udelay(150);
3315
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003316 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003317 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3318 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3319 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003320
Chris Wilson5eddb702010-09-11 13:48:45 +01003321 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003322 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003323 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003324 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3325
3326 if ((temp & FDI_RX_BIT_LOCK)) {
3327 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003328 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003329 break;
3330 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003331 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003332 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003333 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003334
3335 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003336 reg = FDI_TX_CTL(pipe);
3337 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003338 temp &= ~FDI_LINK_TRAIN_NONE;
3339 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003340 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003341
Chris Wilson5eddb702010-09-11 13:48:45 +01003342 reg = FDI_RX_CTL(pipe);
3343 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003344 temp &= ~FDI_LINK_TRAIN_NONE;
3345 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003346 I915_WRITE(reg, temp);
3347
3348 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003349 udelay(150);
3350
Chris Wilson5eddb702010-09-11 13:48:45 +01003351 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003352 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003353 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003354 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3355
3356 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003357 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003358 DRM_DEBUG_KMS("FDI train 2 done.\n");
3359 break;
3360 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003361 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003362 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003363 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003364
3365 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003366
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003367}
3368
Akshay Joshi0206e352011-08-16 15:34:10 -04003369static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003370 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3371 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3372 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3373 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3374};
3375
3376/* The FDI link training functions for SNB/Cougarpoint. */
3377static void gen6_fdi_link_train(struct drm_crtc *crtc)
3378{
3379 struct drm_device *dev = crtc->dev;
3380 struct drm_i915_private *dev_priv = dev->dev_private;
3381 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3382 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003383 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003384
Adam Jacksone1a44742010-06-25 15:32:14 -04003385 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3386 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003387 reg = FDI_RX_IMR(pipe);
3388 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003389 temp &= ~FDI_RX_SYMBOL_LOCK;
3390 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003391 I915_WRITE(reg, temp);
3392
3393 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003394 udelay(150);
3395
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003396 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003397 reg = FDI_TX_CTL(pipe);
3398 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003399 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003400 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003401 temp &= ~FDI_LINK_TRAIN_NONE;
3402 temp |= FDI_LINK_TRAIN_PATTERN_1;
3403 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3404 /* SNB-B */
3405 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003406 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003407
Daniel Vetterd74cf322012-10-26 10:58:13 +02003408 I915_WRITE(FDI_RX_MISC(pipe),
3409 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3410
Chris Wilson5eddb702010-09-11 13:48:45 +01003411 reg = FDI_RX_CTL(pipe);
3412 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003413 if (HAS_PCH_CPT(dev)) {
3414 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3415 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3416 } else {
3417 temp &= ~FDI_LINK_TRAIN_NONE;
3418 temp |= FDI_LINK_TRAIN_PATTERN_1;
3419 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003420 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3421
3422 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003423 udelay(150);
3424
Akshay Joshi0206e352011-08-16 15:34:10 -04003425 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003426 reg = FDI_TX_CTL(pipe);
3427 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003428 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3429 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003430 I915_WRITE(reg, temp);
3431
3432 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003433 udelay(500);
3434
Sean Paulfa37d392012-03-02 12:53:39 -05003435 for (retry = 0; retry < 5; retry++) {
3436 reg = FDI_RX_IIR(pipe);
3437 temp = I915_READ(reg);
3438 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3439 if (temp & FDI_RX_BIT_LOCK) {
3440 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3441 DRM_DEBUG_KMS("FDI train 1 done.\n");
3442 break;
3443 }
3444 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003445 }
Sean Paulfa37d392012-03-02 12:53:39 -05003446 if (retry < 5)
3447 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003448 }
3449 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003450 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003451
3452 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003453 reg = FDI_TX_CTL(pipe);
3454 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003455 temp &= ~FDI_LINK_TRAIN_NONE;
3456 temp |= FDI_LINK_TRAIN_PATTERN_2;
3457 if (IS_GEN6(dev)) {
3458 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3459 /* SNB-B */
3460 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3461 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003462 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003463
Chris Wilson5eddb702010-09-11 13:48:45 +01003464 reg = FDI_RX_CTL(pipe);
3465 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003466 if (HAS_PCH_CPT(dev)) {
3467 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3468 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3469 } else {
3470 temp &= ~FDI_LINK_TRAIN_NONE;
3471 temp |= FDI_LINK_TRAIN_PATTERN_2;
3472 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003473 I915_WRITE(reg, temp);
3474
3475 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003476 udelay(150);
3477
Akshay Joshi0206e352011-08-16 15:34:10 -04003478 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003479 reg = FDI_TX_CTL(pipe);
3480 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003481 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3482 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003483 I915_WRITE(reg, temp);
3484
3485 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003486 udelay(500);
3487
Sean Paulfa37d392012-03-02 12:53:39 -05003488 for (retry = 0; retry < 5; retry++) {
3489 reg = FDI_RX_IIR(pipe);
3490 temp = I915_READ(reg);
3491 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3492 if (temp & FDI_RX_SYMBOL_LOCK) {
3493 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3494 DRM_DEBUG_KMS("FDI train 2 done.\n");
3495 break;
3496 }
3497 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003498 }
Sean Paulfa37d392012-03-02 12:53:39 -05003499 if (retry < 5)
3500 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003501 }
3502 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003503 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003504
3505 DRM_DEBUG_KMS("FDI train done.\n");
3506}
3507
Jesse Barnes357555c2011-04-28 15:09:55 -07003508/* Manual link training for Ivy Bridge A0 parts */
3509static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3510{
3511 struct drm_device *dev = crtc->dev;
3512 struct drm_i915_private *dev_priv = dev->dev_private;
3513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3514 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003515 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003516
3517 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3518 for train result */
3519 reg = FDI_RX_IMR(pipe);
3520 temp = I915_READ(reg);
3521 temp &= ~FDI_RX_SYMBOL_LOCK;
3522 temp &= ~FDI_RX_BIT_LOCK;
3523 I915_WRITE(reg, temp);
3524
3525 POSTING_READ(reg);
3526 udelay(150);
3527
Daniel Vetter01a415f2012-10-27 15:58:40 +02003528 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3529 I915_READ(FDI_RX_IIR(pipe)));
3530
Jesse Barnes139ccd32013-08-19 11:04:55 -07003531 /* Try each vswing and preemphasis setting twice before moving on */
3532 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3533 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003534 reg = FDI_TX_CTL(pipe);
3535 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003536 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3537 temp &= ~FDI_TX_ENABLE;
3538 I915_WRITE(reg, temp);
3539
3540 reg = FDI_RX_CTL(pipe);
3541 temp = I915_READ(reg);
3542 temp &= ~FDI_LINK_TRAIN_AUTO;
3543 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3544 temp &= ~FDI_RX_ENABLE;
3545 I915_WRITE(reg, temp);
3546
3547 /* enable CPU FDI TX and PCH FDI RX */
3548 reg = FDI_TX_CTL(pipe);
3549 temp = I915_READ(reg);
3550 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003551 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003552 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003553 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003554 temp |= snb_b_fdi_train_param[j/2];
3555 temp |= FDI_COMPOSITE_SYNC;
3556 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3557
3558 I915_WRITE(FDI_RX_MISC(pipe),
3559 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3560
3561 reg = FDI_RX_CTL(pipe);
3562 temp = I915_READ(reg);
3563 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3564 temp |= FDI_COMPOSITE_SYNC;
3565 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3566
3567 POSTING_READ(reg);
3568 udelay(1); /* should be 0.5us */
3569
3570 for (i = 0; i < 4; i++) {
3571 reg = FDI_RX_IIR(pipe);
3572 temp = I915_READ(reg);
3573 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3574
3575 if (temp & FDI_RX_BIT_LOCK ||
3576 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3577 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3578 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3579 i);
3580 break;
3581 }
3582 udelay(1); /* should be 0.5us */
3583 }
3584 if (i == 4) {
3585 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3586 continue;
3587 }
3588
3589 /* Train 2 */
3590 reg = FDI_TX_CTL(pipe);
3591 temp = I915_READ(reg);
3592 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3593 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3594 I915_WRITE(reg, temp);
3595
3596 reg = FDI_RX_CTL(pipe);
3597 temp = I915_READ(reg);
3598 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3599 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003600 I915_WRITE(reg, temp);
3601
3602 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003603 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003604
Jesse Barnes139ccd32013-08-19 11:04:55 -07003605 for (i = 0; i < 4; i++) {
3606 reg = FDI_RX_IIR(pipe);
3607 temp = I915_READ(reg);
3608 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003609
Jesse Barnes139ccd32013-08-19 11:04:55 -07003610 if (temp & FDI_RX_SYMBOL_LOCK ||
3611 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3612 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3613 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3614 i);
3615 goto train_done;
3616 }
3617 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003618 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003619 if (i == 4)
3620 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003621 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003622
Jesse Barnes139ccd32013-08-19 11:04:55 -07003623train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003624 DRM_DEBUG_KMS("FDI train done.\n");
3625}
3626
Daniel Vetter88cefb6c2012-08-12 19:27:14 +02003627static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003628{
Daniel Vetter88cefb6c2012-08-12 19:27:14 +02003629 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003630 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003631 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003632 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003633
Jesse Barnesc64e3112010-09-10 11:27:03 -07003634
Jesse Barnes0e23b992010-09-10 11:10:00 -07003635 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003636 reg = FDI_RX_CTL(pipe);
3637 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003638 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003639 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003640 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003641 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3642
3643 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003644 udelay(200);
3645
3646 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003647 temp = I915_READ(reg);
3648 I915_WRITE(reg, temp | FDI_PCDCLK);
3649
3650 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003651 udelay(200);
3652
Paulo Zanoni20749732012-11-23 15:30:38 -02003653 /* Enable CPU FDI TX PLL, always on for Ironlake */
3654 reg = FDI_TX_CTL(pipe);
3655 temp = I915_READ(reg);
3656 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3657 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003658
Paulo Zanoni20749732012-11-23 15:30:38 -02003659 POSTING_READ(reg);
3660 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003661 }
3662}
3663
Daniel Vetter88cefb6c2012-08-12 19:27:14 +02003664static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3665{
3666 struct drm_device *dev = intel_crtc->base.dev;
3667 struct drm_i915_private *dev_priv = dev->dev_private;
3668 int pipe = intel_crtc->pipe;
3669 u32 reg, temp;
3670
3671 /* Switch from PCDclk to Rawclk */
3672 reg = FDI_RX_CTL(pipe);
3673 temp = I915_READ(reg);
3674 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3675
3676 /* Disable CPU FDI TX PLL */
3677 reg = FDI_TX_CTL(pipe);
3678 temp = I915_READ(reg);
3679 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3680
3681 POSTING_READ(reg);
3682 udelay(100);
3683
3684 reg = FDI_RX_CTL(pipe);
3685 temp = I915_READ(reg);
3686 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3687
3688 /* Wait for the clocks to turn off. */
3689 POSTING_READ(reg);
3690 udelay(100);
3691}
3692
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003693static void ironlake_fdi_disable(struct drm_crtc *crtc)
3694{
3695 struct drm_device *dev = crtc->dev;
3696 struct drm_i915_private *dev_priv = dev->dev_private;
3697 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3698 int pipe = intel_crtc->pipe;
3699 u32 reg, temp;
3700
3701 /* disable CPU FDI tx and PCH FDI rx */
3702 reg = FDI_TX_CTL(pipe);
3703 temp = I915_READ(reg);
3704 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3705 POSTING_READ(reg);
3706
3707 reg = FDI_RX_CTL(pipe);
3708 temp = I915_READ(reg);
3709 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003710 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003711 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3712
3713 POSTING_READ(reg);
3714 udelay(100);
3715
3716 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003717 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003718 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003719
3720 /* still set train pattern 1 */
3721 reg = FDI_TX_CTL(pipe);
3722 temp = I915_READ(reg);
3723 temp &= ~FDI_LINK_TRAIN_NONE;
3724 temp |= FDI_LINK_TRAIN_PATTERN_1;
3725 I915_WRITE(reg, temp);
3726
3727 reg = FDI_RX_CTL(pipe);
3728 temp = I915_READ(reg);
3729 if (HAS_PCH_CPT(dev)) {
3730 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3731 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3732 } else {
3733 temp &= ~FDI_LINK_TRAIN_NONE;
3734 temp |= FDI_LINK_TRAIN_PATTERN_1;
3735 }
3736 /* BPC in FDI rx is consistent with that in PIPECONF */
3737 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003738 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003739 I915_WRITE(reg, temp);
3740
3741 POSTING_READ(reg);
3742 udelay(100);
3743}
3744
Chris Wilson5dce5b932014-01-20 10:17:36 +00003745bool intel_has_pending_fb_unpin(struct drm_device *dev)
3746{
3747 struct intel_crtc *crtc;
3748
3749 /* Note that we don't need to be called with mode_config.lock here
3750 * as our list of CRTC objects is static for the lifetime of the
3751 * device and so cannot disappear as we iterate. Similarly, we can
3752 * happily treat the predicates as racy, atomic checks as userspace
3753 * cannot claim and pin a new fb without at least acquring the
3754 * struct_mutex and so serialising with us.
3755 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003756 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003757 if (atomic_read(&crtc->unpin_work_count) == 0)
3758 continue;
3759
3760 if (crtc->unpin_work)
3761 intel_wait_for_vblank(dev, crtc->pipe);
3762
3763 return true;
3764 }
3765
3766 return false;
3767}
3768
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003769static void page_flip_completed(struct intel_crtc *intel_crtc)
3770{
3771 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3772 struct intel_unpin_work *work = intel_crtc->unpin_work;
3773
3774 /* ensure that the unpin work is consistent wrt ->pending. */
3775 smp_rmb();
3776 intel_crtc->unpin_work = NULL;
3777
3778 if (work->event)
3779 drm_send_vblank_event(intel_crtc->base.dev,
3780 intel_crtc->pipe,
3781 work->event);
3782
3783 drm_crtc_vblank_put(&intel_crtc->base);
3784
3785 wake_up_all(&dev_priv->pending_flip_queue);
3786 queue_work(dev_priv->wq, &work->work);
3787
3788 trace_i915_flip_complete(intel_crtc->plane,
3789 work->pending_flip_obj);
3790}
3791
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003792void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003793{
Chris Wilson0f911282012-04-17 10:05:38 +01003794 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003795 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003796
Daniel Vetter2c10d572012-12-20 21:24:07 +01003797 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003798 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3799 !intel_crtc_has_pending_flip(crtc),
3800 60*HZ) == 0)) {
3801 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003802
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003803 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003804 if (intel_crtc->unpin_work) {
3805 WARN_ONCE(1, "Removing stuck page flip\n");
3806 page_flip_completed(intel_crtc);
3807 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003808 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003809 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003810
Chris Wilson975d5682014-08-20 13:13:34 +01003811 if (crtc->primary->fb) {
3812 mutex_lock(&dev->struct_mutex);
3813 intel_finish_fb(crtc->primary->fb);
3814 mutex_unlock(&dev->struct_mutex);
3815 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003816}
3817
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003818/* Program iCLKIP clock to the desired frequency */
3819static void lpt_program_iclkip(struct drm_crtc *crtc)
3820{
3821 struct drm_device *dev = crtc->dev;
3822 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003823 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003824 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3825 u32 temp;
3826
Daniel Vetter09153002012-12-12 14:06:44 +01003827 mutex_lock(&dev_priv->dpio_lock);
3828
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003829 /* It is necessary to ungate the pixclk gate prior to programming
3830 * the divisors, and gate it back when it is done.
3831 */
3832 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3833
3834 /* Disable SSCCTL */
3835 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003836 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3837 SBI_SSCCTL_DISABLE,
3838 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003839
3840 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003841 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003842 auxdiv = 1;
3843 divsel = 0x41;
3844 phaseinc = 0x20;
3845 } else {
3846 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003847 * but the adjusted_mode->crtc_clock in in KHz. To get the
3848 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003849 * convert the virtual clock precision to KHz here for higher
3850 * precision.
3851 */
3852 u32 iclk_virtual_root_freq = 172800 * 1000;
3853 u32 iclk_pi_range = 64;
3854 u32 desired_divisor, msb_divisor_value, pi_value;
3855
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003856 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003857 msb_divisor_value = desired_divisor / iclk_pi_range;
3858 pi_value = desired_divisor % iclk_pi_range;
3859
3860 auxdiv = 0;
3861 divsel = msb_divisor_value - 2;
3862 phaseinc = pi_value;
3863 }
3864
3865 /* This should not happen with any sane values */
3866 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3867 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3868 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3869 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3870
3871 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 +03003872 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003873 auxdiv,
3874 divsel,
3875 phasedir,
3876 phaseinc);
3877
3878 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003879 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003880 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3881 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3882 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3883 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3884 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3885 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003886 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003887
3888 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003889 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003890 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3891 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003892 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003893
3894 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003895 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003896 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003897 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003898
3899 /* Wait for initialization time */
3900 udelay(24);
3901
3902 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003903
3904 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003905}
3906
Daniel Vetter275f01b22013-05-03 11:49:47 +02003907static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3908 enum pipe pch_transcoder)
3909{
3910 struct drm_device *dev = crtc->base.dev;
3911 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003912 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003913
3914 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3915 I915_READ(HTOTAL(cpu_transcoder)));
3916 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3917 I915_READ(HBLANK(cpu_transcoder)));
3918 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3919 I915_READ(HSYNC(cpu_transcoder)));
3920
3921 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3922 I915_READ(VTOTAL(cpu_transcoder)));
3923 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3924 I915_READ(VBLANK(cpu_transcoder)));
3925 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3926 I915_READ(VSYNC(cpu_transcoder)));
3927 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3928 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3929}
3930
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003931static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003932{
3933 struct drm_i915_private *dev_priv = dev->dev_private;
3934 uint32_t temp;
3935
3936 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003937 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003938 return;
3939
3940 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3941 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3942
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003943 temp &= ~FDI_BC_BIFURCATION_SELECT;
3944 if (enable)
3945 temp |= FDI_BC_BIFURCATION_SELECT;
3946
3947 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003948 I915_WRITE(SOUTH_CHICKEN1, temp);
3949 POSTING_READ(SOUTH_CHICKEN1);
3950}
3951
3952static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3953{
3954 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003955
3956 switch (intel_crtc->pipe) {
3957 case PIPE_A:
3958 break;
3959 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003960 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003961 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003962 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003963 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003964
3965 break;
3966 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003967 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003968
3969 break;
3970 default:
3971 BUG();
3972 }
3973}
3974
Jesse Barnesf67a5592011-01-05 10:31:48 -08003975/*
3976 * Enable PCH resources required for PCH ports:
3977 * - PCH PLLs
3978 * - FDI training & RX/TX
3979 * - update transcoder timings
3980 * - DP transcoding bits
3981 * - transcoder
3982 */
3983static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003984{
3985 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003986 struct drm_i915_private *dev_priv = dev->dev_private;
3987 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3988 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003989 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003990
Daniel Vetterab9412b2013-05-03 11:49:46 +02003991 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003992
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003993 if (IS_IVYBRIDGE(dev))
3994 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3995
Daniel Vettercd986ab2012-10-26 10:58:12 +02003996 /* Write the TU size bits before fdi link training, so that error
3997 * detection works. */
3998 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3999 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4000
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004001 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004002 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004003
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004004 /* We need to program the right clock selection before writing the pixel
4005 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004006 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004007 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004008
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004009 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004010 temp |= TRANS_DPLL_ENABLE(pipe);
4011 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004012 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004013 temp |= sel;
4014 else
4015 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004016 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004017 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004018
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004019 /* XXX: pch pll's can be enabled any time before we enable the PCH
4020 * transcoder, and we actually should do this to not upset any PCH
4021 * transcoder that already use the clock when we share it.
4022 *
4023 * Note that enable_shared_dpll tries to do the right thing, but
4024 * get_shared_dpll unconditionally resets the pll - we need that to have
4025 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004026 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004027
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004028 /* set transcoder timing, panel must allow it */
4029 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004030 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004031
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004032 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004033
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004034 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004035 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004036 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004037 reg = TRANS_DP_CTL(pipe);
4038 temp = I915_READ(reg);
4039 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004040 TRANS_DP_SYNC_MASK |
4041 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01004042 temp |= (TRANS_DP_OUTPUT_ENABLE |
4043 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004044 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004045
4046 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004047 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004048 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004049 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004050
4051 switch (intel_trans_dp_port_sel(crtc)) {
4052 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004053 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004054 break;
4055 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004056 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004057 break;
4058 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004059 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004060 break;
4061 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004062 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004063 }
4064
Chris Wilson5eddb702010-09-11 13:48:45 +01004065 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004066 }
4067
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004068 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004069}
4070
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004071static void lpt_pch_enable(struct drm_crtc *crtc)
4072{
4073 struct drm_device *dev = crtc->dev;
4074 struct drm_i915_private *dev_priv = dev->dev_private;
4075 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004076 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004077
Daniel Vetterab9412b2013-05-03 11:49:46 +02004078 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004079
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004080 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004081
Paulo Zanoni0540e482012-10-31 18:12:40 -02004082 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004083 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004084
Paulo Zanoni937bb612012-10-31 18:12:47 -02004085 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004086}
4087
Daniel Vetter716c2e52014-06-25 22:02:02 +03004088void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004089{
Daniel Vettere2b78262013-06-07 23:10:03 +02004090 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004091
4092 if (pll == NULL)
4093 return;
4094
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004095 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004096 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004097 return;
4098 }
4099
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004100 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4101 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004102 WARN_ON(pll->on);
4103 WARN_ON(pll->active);
4104 }
4105
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004106 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004107}
4108
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004109struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4110 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004111{
Daniel Vettere2b78262013-06-07 23:10:03 +02004112 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004113 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004114 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004115
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004116 if (HAS_PCH_IBX(dev_priv->dev)) {
4117 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004118 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004119 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004120
Daniel Vetter46edb022013-06-05 13:34:12 +02004121 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4122 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004123
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004124 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f442014-05-20 15:19:19 +02004125
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004126 goto found;
4127 }
4128
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004129 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4130 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004131
4132 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004133 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004134 continue;
4135
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004136 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004137 &pll->new_config->hw_state,
4138 sizeof(pll->new_config->hw_state)) == 0) {
4139 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004140 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004141 pll->new_config->crtc_mask,
4142 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004143 goto found;
4144 }
4145 }
4146
4147 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004148 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4149 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004150 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004151 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4152 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004153 goto found;
4154 }
4155 }
4156
4157 return NULL;
4158
4159found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004160 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004161 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f442014-05-20 15:19:19 +02004162
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004163 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004164 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4165 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004166
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004167 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004168
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004169 return pll;
4170}
4171
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004172/**
4173 * intel_shared_dpll_start_config - start a new PLL staged config
4174 * @dev_priv: DRM device
4175 * @clear_pipes: mask of pipes that will have their PLLs freed
4176 *
4177 * Starts a new PLL staged config, copying the current config but
4178 * releasing the references of pipes specified in clear_pipes.
4179 */
4180static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4181 unsigned clear_pipes)
4182{
4183 struct intel_shared_dpll *pll;
4184 enum intel_dpll_id i;
4185
4186 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4187 pll = &dev_priv->shared_dplls[i];
4188
4189 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4190 GFP_KERNEL);
4191 if (!pll->new_config)
4192 goto cleanup;
4193
4194 pll->new_config->crtc_mask &= ~clear_pipes;
4195 }
4196
4197 return 0;
4198
4199cleanup:
4200 while (--i >= 0) {
4201 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004202 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004203 pll->new_config = NULL;
4204 }
4205
4206 return -ENOMEM;
4207}
4208
4209static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4210{
4211 struct intel_shared_dpll *pll;
4212 enum intel_dpll_id i;
4213
4214 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4215 pll = &dev_priv->shared_dplls[i];
4216
4217 WARN_ON(pll->new_config == &pll->config);
4218
4219 pll->config = *pll->new_config;
4220 kfree(pll->new_config);
4221 pll->new_config = NULL;
4222 }
4223}
4224
4225static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4226{
4227 struct intel_shared_dpll *pll;
4228 enum intel_dpll_id i;
4229
4230 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4231 pll = &dev_priv->shared_dplls[i];
4232
4233 WARN_ON(pll->new_config == &pll->config);
4234
4235 kfree(pll->new_config);
4236 pll->new_config = NULL;
4237 }
4238}
4239
Daniel Vettera1520312013-05-03 11:49:50 +02004240static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004241{
4242 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004243 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004244 u32 temp;
4245
4246 temp = I915_READ(dslreg);
4247 udelay(500);
4248 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004249 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004250 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004251 }
4252}
4253
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004254static void skylake_pfit_enable(struct intel_crtc *crtc)
4255{
4256 struct drm_device *dev = crtc->base.dev;
4257 struct drm_i915_private *dev_priv = dev->dev_private;
4258 int pipe = crtc->pipe;
4259
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004260 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004261 I915_WRITE(PS_CTL(pipe), PS_ENABLE);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004262 I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4263 I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004264 }
4265}
4266
Jesse Barnesb074cec2013-04-25 12:55:02 -07004267static void ironlake_pfit_enable(struct intel_crtc *crtc)
4268{
4269 struct drm_device *dev = crtc->base.dev;
4270 struct drm_i915_private *dev_priv = dev->dev_private;
4271 int pipe = crtc->pipe;
4272
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004273 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004274 /* Force use of hard-coded filter coefficients
4275 * as some pre-programmed values are broken,
4276 * e.g. x201.
4277 */
4278 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4279 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4280 PF_PIPE_SEL_IVB(pipe));
4281 else
4282 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004283 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4284 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004285 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004286}
4287
Matt Roper4a3b8762014-12-23 10:41:51 -08004288static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004289{
4290 struct drm_device *dev = crtc->dev;
4291 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004292 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004293 struct intel_plane *intel_plane;
4294
Matt Roperaf2b6532014-04-01 15:22:32 -07004295 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4296 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004297 if (intel_plane->pipe == pipe)
4298 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004299 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004300}
4301
Matt Roper0d703d42015-03-04 10:49:04 -08004302/*
4303 * Disable a plane internally without actually modifying the plane's state.
4304 * This will allow us to easily restore the plane later by just reprogramming
4305 * its state.
4306 */
4307static void disable_plane_internal(struct drm_plane *plane)
4308{
4309 struct intel_plane *intel_plane = to_intel_plane(plane);
4310 struct drm_plane_state *state =
4311 plane->funcs->atomic_duplicate_state(plane);
4312 struct intel_plane_state *intel_state = to_intel_plane_state(state);
4313
4314 intel_state->visible = false;
4315 intel_plane->commit_plane(plane, intel_state);
4316
4317 intel_plane_destroy_state(plane, state);
4318}
4319
Matt Roper4a3b8762014-12-23 10:41:51 -08004320static void intel_disable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004321{
4322 struct drm_device *dev = crtc->dev;
4323 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004324 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004325 struct intel_plane *intel_plane;
4326
Matt Roperaf2b6532014-04-01 15:22:32 -07004327 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4328 intel_plane = to_intel_plane(plane);
Matt Roper0d703d42015-03-04 10:49:04 -08004329 if (plane->fb && intel_plane->pipe == pipe)
4330 disable_plane_internal(plane);
Matt Roperaf2b6532014-04-01 15:22:32 -07004331 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004332}
4333
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004334void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004335{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004336 struct drm_device *dev = crtc->base.dev;
4337 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004338
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004339 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004340 return;
4341
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004342 /* We can only enable IPS after we enable a plane and wait for a vblank */
4343 intel_wait_for_vblank(dev, crtc->pipe);
4344
Paulo Zanonid77e4532013-09-24 13:52:55 -03004345 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004346 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004347 mutex_lock(&dev_priv->rps.hw_lock);
4348 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4349 mutex_unlock(&dev_priv->rps.hw_lock);
4350 /* Quoting Art Runyan: "its not safe to expect any particular
4351 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004352 * mailbox." Moreover, the mailbox may return a bogus state,
4353 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004354 */
4355 } else {
4356 I915_WRITE(IPS_CTL, IPS_ENABLE);
4357 /* The bit only becomes 1 in the next vblank, so this wait here
4358 * is essentially intel_wait_for_vblank. If we don't have this
4359 * and don't wait for vblanks until the end of crtc_enable, then
4360 * the HW state readout code will complain that the expected
4361 * IPS_CTL value is not the one we read. */
4362 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4363 DRM_ERROR("Timed out waiting for IPS enable\n");
4364 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004365}
4366
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004367void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004368{
4369 struct drm_device *dev = crtc->base.dev;
4370 struct drm_i915_private *dev_priv = dev->dev_private;
4371
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004372 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004373 return;
4374
4375 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004376 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004377 mutex_lock(&dev_priv->rps.hw_lock);
4378 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4379 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004380 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4381 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4382 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004383 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004384 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004385 POSTING_READ(IPS_CTL);
4386 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004387
4388 /* We need to wait for a vblank before we can disable the plane. */
4389 intel_wait_for_vblank(dev, crtc->pipe);
4390}
4391
4392/** Loads the palette/gamma unit for the CRTC with the prepared values */
4393static void intel_crtc_load_lut(struct drm_crtc *crtc)
4394{
4395 struct drm_device *dev = crtc->dev;
4396 struct drm_i915_private *dev_priv = dev->dev_private;
4397 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4398 enum pipe pipe = intel_crtc->pipe;
4399 int palreg = PALETTE(pipe);
4400 int i;
4401 bool reenable_ips = false;
4402
4403 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004404 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004405 return;
4406
4407 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004408 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004409 assert_dsi_pll_enabled(dev_priv);
4410 else
4411 assert_pll_enabled(dev_priv, pipe);
4412 }
4413
4414 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304415 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004416 palreg = LGC_PALETTE(pipe);
4417
4418 /* Workaround : Do not read or write the pipe palette/gamma data while
4419 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4420 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004421 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004422 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4423 GAMMA_MODE_MODE_SPLIT)) {
4424 hsw_disable_ips(intel_crtc);
4425 reenable_ips = true;
4426 }
4427
4428 for (i = 0; i < 256; i++) {
4429 I915_WRITE(palreg + 4 * i,
4430 (intel_crtc->lut_r[i] << 16) |
4431 (intel_crtc->lut_g[i] << 8) |
4432 intel_crtc->lut_b[i]);
4433 }
4434
4435 if (reenable_ips)
4436 hsw_enable_ips(intel_crtc);
4437}
4438
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004439static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4440{
4441 if (!enable && intel_crtc->overlay) {
4442 struct drm_device *dev = intel_crtc->base.dev;
4443 struct drm_i915_private *dev_priv = dev->dev_private;
4444
4445 mutex_lock(&dev->struct_mutex);
4446 dev_priv->mm.interruptible = false;
4447 (void) intel_overlay_switch_off(intel_crtc->overlay);
4448 dev_priv->mm.interruptible = true;
4449 mutex_unlock(&dev->struct_mutex);
4450 }
4451
4452 /* Let userspace switch the overlay on again. In most cases userspace
4453 * has to recompute where to put it anyway.
4454 */
4455}
4456
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004457static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004458{
4459 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004460 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4461 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004462
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03004463 intel_enable_primary_hw_plane(crtc->primary, crtc);
Matt Roper4a3b8762014-12-23 10:41:51 -08004464 intel_enable_sprite_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004465 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004466 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004467
4468 hsw_enable_ips(intel_crtc);
4469
4470 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004471 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004472 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004473
4474 /*
4475 * FIXME: Once we grow proper nuclear flip support out of this we need
4476 * to compute the mask of flip planes precisely. For the time being
4477 * consider this a flip from a NULL plane.
4478 */
4479 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004480}
4481
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004482static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004483{
4484 struct drm_device *dev = crtc->dev;
4485 struct drm_i915_private *dev_priv = dev->dev_private;
4486 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4487 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004488
4489 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004490
Paulo Zanonie35fef22015-02-09 14:46:29 -02004491 if (dev_priv->fbc.crtc == intel_crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004492 intel_fbc_disable(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004493
4494 hsw_disable_ips(intel_crtc);
4495
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004496 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004497 intel_crtc_update_cursor(crtc, false);
Matt Roper4a3b8762014-12-23 10:41:51 -08004498 intel_disable_sprite_planes(crtc);
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03004499 intel_disable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004500
Daniel Vetterf99d7062014-06-19 16:01:59 +02004501 /*
4502 * FIXME: Once we grow proper nuclear flip support out of this we need
4503 * to compute the mask of flip planes precisely. For the time being
4504 * consider this a flip to a NULL plane.
4505 */
4506 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004507}
4508
Jesse Barnesf67a5592011-01-05 10:31:48 -08004509static void ironlake_crtc_enable(struct drm_crtc *crtc)
4510{
4511 struct drm_device *dev = crtc->dev;
4512 struct drm_i915_private *dev_priv = dev->dev_private;
4513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004514 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004515 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004516
Matt Roper83d65732015-02-25 13:12:16 -08004517 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004518
Jesse Barnesf67a5592011-01-05 10:31:48 -08004519 if (intel_crtc->active)
4520 return;
4521
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004522 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004523 intel_prepare_shared_dpll(intel_crtc);
4524
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004525 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304526 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004527
4528 intel_set_pipe_timings(intel_crtc);
4529
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004530 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004531 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004532 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004533 }
4534
4535 ironlake_set_pipeconf(crtc);
4536
Jesse Barnesf67a5592011-01-05 10:31:48 -08004537 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004538
Daniel Vettera72e4c92014-09-30 10:56:47 +02004539 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4540 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004541
Daniel Vetterf6736a12013-06-05 13:34:30 +02004542 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004543 if (encoder->pre_enable)
4544 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004545
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004546 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004547 /* Note: FDI PLL enabling _must_ be done before we enable the
4548 * cpu pipes, hence this is separate from all the other fdi/pch
4549 * enabling. */
Daniel Vetter88cefb6c2012-08-12 19:27:14 +02004550 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004551 } else {
4552 assert_fdi_tx_disabled(dev_priv, pipe);
4553 assert_fdi_rx_disabled(dev_priv, pipe);
4554 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004555
Jesse Barnesb074cec2013-04-25 12:55:02 -07004556 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004557
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004558 /*
4559 * On ILK+ LUT must be loaded before the pipe is running but with
4560 * clocks enabled
4561 */
4562 intel_crtc_load_lut(crtc);
4563
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004564 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004565 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004566
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004567 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004568 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004569
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004570 assert_vblank_disabled(crtc);
4571 drm_crtc_vblank_on(crtc);
4572
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004573 for_each_encoder_on_crtc(dev, crtc, encoder)
4574 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004575
4576 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004577 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004578
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004579 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004580}
4581
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004582/* IPS only exists on ULT machines and is tied to pipe A. */
4583static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4584{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004585 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004586}
4587
Paulo Zanonie4916942013-09-20 16:21:19 -03004588/*
4589 * This implements the workaround described in the "notes" section of the mode
4590 * set sequence documentation. When going from no pipes or single pipe to
4591 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4592 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4593 */
4594static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4595{
4596 struct drm_device *dev = crtc->base.dev;
4597 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4598
4599 /* We want to get the other_active_crtc only if there's only 1 other
4600 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004601 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004602 if (!crtc_it->active || crtc_it == crtc)
4603 continue;
4604
4605 if (other_active_crtc)
4606 return;
4607
4608 other_active_crtc = crtc_it;
4609 }
4610 if (!other_active_crtc)
4611 return;
4612
4613 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4614 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4615}
4616
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004617static void haswell_crtc_enable(struct drm_crtc *crtc)
4618{
4619 struct drm_device *dev = crtc->dev;
4620 struct drm_i915_private *dev_priv = dev->dev_private;
4621 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4622 struct intel_encoder *encoder;
4623 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004624
Matt Roper83d65732015-02-25 13:12:16 -08004625 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004626
4627 if (intel_crtc->active)
4628 return;
4629
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004630 if (intel_crtc_to_shared_dpll(intel_crtc))
4631 intel_enable_shared_dpll(intel_crtc);
4632
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004633 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304634 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004635
4636 intel_set_pipe_timings(intel_crtc);
4637
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004638 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4639 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4640 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004641 }
4642
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004643 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004644 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004645 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004646 }
4647
4648 haswell_set_pipeconf(crtc);
4649
4650 intel_set_pipe_csc(crtc);
4651
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004652 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004653
Daniel Vettera72e4c92014-09-30 10:56:47 +02004654 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004655 for_each_encoder_on_crtc(dev, crtc, encoder)
4656 if (encoder->pre_enable)
4657 encoder->pre_enable(encoder);
4658
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004659 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004660 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4661 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004662 dev_priv->display.fdi_link_train(crtc);
4663 }
4664
Paulo Zanoni1f544382012-10-24 11:32:00 -02004665 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004666
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004667 if (IS_SKYLAKE(dev))
4668 skylake_pfit_enable(intel_crtc);
4669 else
4670 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004671
4672 /*
4673 * On ILK+ LUT must be loaded before the pipe is running but with
4674 * clocks enabled
4675 */
4676 intel_crtc_load_lut(crtc);
4677
Paulo Zanoni1f544382012-10-24 11:32:00 -02004678 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004679 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004680
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004681 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004682 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004683
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004684 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004685 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004686
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004687 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004688 intel_ddi_set_vc_payload_alloc(crtc, true);
4689
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004690 assert_vblank_disabled(crtc);
4691 drm_crtc_vblank_on(crtc);
4692
Jani Nikula8807e552013-08-30 19:40:32 +03004693 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004694 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004695 intel_opregion_notify_encoder(encoder, true);
4696 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004697
Paulo Zanonie4916942013-09-20 16:21:19 -03004698 /* If we change the relative order between pipe/planes enabling, we need
4699 * to change the workaround. */
4700 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004701 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004702}
4703
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004704static void skylake_pfit_disable(struct intel_crtc *crtc)
4705{
4706 struct drm_device *dev = crtc->base.dev;
4707 struct drm_i915_private *dev_priv = dev->dev_private;
4708 int pipe = crtc->pipe;
4709
4710 /* To avoid upsetting the power well on haswell only disable the pfit if
4711 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004712 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004713 I915_WRITE(PS_CTL(pipe), 0);
4714 I915_WRITE(PS_WIN_POS(pipe), 0);
4715 I915_WRITE(PS_WIN_SZ(pipe), 0);
4716 }
4717}
4718
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004719static void ironlake_pfit_disable(struct intel_crtc *crtc)
4720{
4721 struct drm_device *dev = crtc->base.dev;
4722 struct drm_i915_private *dev_priv = dev->dev_private;
4723 int pipe = crtc->pipe;
4724
4725 /* To avoid upsetting the power well on haswell only disable the pfit if
4726 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004727 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004728 I915_WRITE(PF_CTL(pipe), 0);
4729 I915_WRITE(PF_WIN_POS(pipe), 0);
4730 I915_WRITE(PF_WIN_SZ(pipe), 0);
4731 }
4732}
4733
Jesse Barnes6be4a602010-09-10 10:26:01 -07004734static void ironlake_crtc_disable(struct drm_crtc *crtc)
4735{
4736 struct drm_device *dev = crtc->dev;
4737 struct drm_i915_private *dev_priv = dev->dev_private;
4738 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004739 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004740 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004741 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004742
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004743 if (!intel_crtc->active)
4744 return;
4745
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004746 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004747
Daniel Vetterea9d7582012-07-10 10:42:52 +02004748 for_each_encoder_on_crtc(dev, crtc, encoder)
4749 encoder->disable(encoder);
4750
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004751 drm_crtc_vblank_off(crtc);
4752 assert_vblank_disabled(crtc);
4753
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004754 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004755 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02004756
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004757 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004758
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004759 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004760
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004761 for_each_encoder_on_crtc(dev, crtc, encoder)
4762 if (encoder->post_disable)
4763 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004764
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004765 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004766 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004767
Daniel Vetterd925c592013-06-05 13:34:04 +02004768 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004769
Daniel Vetterd925c592013-06-05 13:34:04 +02004770 if (HAS_PCH_CPT(dev)) {
4771 /* disable TRANS_DP_CTL */
4772 reg = TRANS_DP_CTL(pipe);
4773 temp = I915_READ(reg);
4774 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4775 TRANS_DP_PORT_SEL_MASK);
4776 temp |= TRANS_DP_PORT_SEL_NONE;
4777 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004778
Daniel Vetterd925c592013-06-05 13:34:04 +02004779 /* disable DPLL_SEL */
4780 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004781 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004782 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004783 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004784
4785 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004786 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02004787
4788 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004789 }
4790
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004791 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004792 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004793
4794 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004795 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004796 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004797}
4798
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004799static void haswell_crtc_disable(struct drm_crtc *crtc)
4800{
4801 struct drm_device *dev = crtc->dev;
4802 struct drm_i915_private *dev_priv = dev->dev_private;
4803 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4804 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004805 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004806
4807 if (!intel_crtc->active)
4808 return;
4809
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004810 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03004811
Jani Nikula8807e552013-08-30 19:40:32 +03004812 for_each_encoder_on_crtc(dev, crtc, encoder) {
4813 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004814 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004815 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004816
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004817 drm_crtc_vblank_off(crtc);
4818 assert_vblank_disabled(crtc);
4819
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004820 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004821 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4822 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004823 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004824
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004825 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03004826 intel_ddi_set_vc_payload_alloc(crtc, false);
4827
Paulo Zanoniad80a812012-10-24 16:06:19 -02004828 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004829
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004830 if (IS_SKYLAKE(dev))
4831 skylake_pfit_disable(intel_crtc);
4832 else
4833 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004834
Paulo Zanoni1f544382012-10-24 11:32:00 -02004835 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004836
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004837 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02004838 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02004839 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02004840 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004841
Imre Deak97b040a2014-06-25 22:01:50 +03004842 for_each_encoder_on_crtc(dev, crtc, encoder)
4843 if (encoder->post_disable)
4844 encoder->post_disable(encoder);
4845
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004846 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004847 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004848
4849 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004850 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004851 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004852
4853 if (intel_crtc_to_shared_dpll(intel_crtc))
4854 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004855}
4856
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004857static void ironlake_crtc_off(struct drm_crtc *crtc)
4858{
4859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004860 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004861}
4862
Paulo Zanoni6441ab52012-10-05 12:05:58 -03004863
Jesse Barnes2dd24552013-04-25 12:55:01 -07004864static void i9xx_pfit_enable(struct intel_crtc *crtc)
4865{
4866 struct drm_device *dev = crtc->base.dev;
4867 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004868 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07004869
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02004870 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07004871 return;
4872
Daniel Vetterc0b03412013-05-28 12:05:54 +02004873 /*
4874 * The panel fitter should only be adjusted whilst the pipe is disabled,
4875 * according to register description and PRM.
4876 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07004877 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4878 assert_pipe_disabled(dev_priv, crtc->pipe);
4879
Jesse Barnesb074cec2013-04-25 12:55:02 -07004880 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4881 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02004882
4883 /* Border color in case we don't scale up to the full screen. Black by
4884 * default, change to something else for debugging. */
4885 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07004886}
4887
Dave Airlied05410f2014-06-05 13:22:59 +10004888static enum intel_display_power_domain port_to_power_domain(enum port port)
4889{
4890 switch (port) {
4891 case PORT_A:
4892 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4893 case PORT_B:
4894 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4895 case PORT_C:
4896 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4897 case PORT_D:
4898 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4899 default:
4900 WARN_ON_ONCE(1);
4901 return POWER_DOMAIN_PORT_OTHER;
4902 }
4903}
4904
Imre Deak77d22dc2014-03-05 16:20:52 +02004905#define for_each_power_domain(domain, mask) \
4906 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4907 if ((1 << (domain)) & (mask))
4908
Imre Deak319be8a2014-03-04 19:22:57 +02004909enum intel_display_power_domain
4910intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02004911{
Imre Deak319be8a2014-03-04 19:22:57 +02004912 struct drm_device *dev = intel_encoder->base.dev;
4913 struct intel_digital_port *intel_dig_port;
4914
4915 switch (intel_encoder->type) {
4916 case INTEL_OUTPUT_UNKNOWN:
4917 /* Only DDI platforms should ever use this output type */
4918 WARN_ON_ONCE(!HAS_DDI(dev));
4919 case INTEL_OUTPUT_DISPLAYPORT:
4920 case INTEL_OUTPUT_HDMI:
4921 case INTEL_OUTPUT_EDP:
4922 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10004923 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10004924 case INTEL_OUTPUT_DP_MST:
4925 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4926 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02004927 case INTEL_OUTPUT_ANALOG:
4928 return POWER_DOMAIN_PORT_CRT;
4929 case INTEL_OUTPUT_DSI:
4930 return POWER_DOMAIN_PORT_DSI;
4931 default:
4932 return POWER_DOMAIN_PORT_OTHER;
4933 }
4934}
4935
4936static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4937{
4938 struct drm_device *dev = crtc->dev;
4939 struct intel_encoder *intel_encoder;
4940 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4941 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02004942 unsigned long mask;
4943 enum transcoder transcoder;
4944
4945 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4946
4947 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4948 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004949 if (intel_crtc->config->pch_pfit.enabled ||
4950 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02004951 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4952
Imre Deak319be8a2014-03-04 19:22:57 +02004953 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4954 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4955
Imre Deak77d22dc2014-03-05 16:20:52 +02004956 return mask;
4957}
4958
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02004959static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02004960{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02004961 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02004962 struct drm_i915_private *dev_priv = dev->dev_private;
4963 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4964 struct intel_crtc *crtc;
4965
4966 /*
4967 * First get all needed power domains, then put all unneeded, to avoid
4968 * any unnecessary toggling of the power wells.
4969 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004970 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004971 enum intel_display_power_domain domain;
4972
Matt Roper83d65732015-02-25 13:12:16 -08004973 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02004974 continue;
4975
Imre Deak319be8a2014-03-04 19:22:57 +02004976 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02004977
4978 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4979 intel_display_power_get(dev_priv, domain);
4980 }
4981
Ville Syrjälä50f6e502014-11-06 14:49:12 +02004982 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02004983 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02004984
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004985 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004986 enum intel_display_power_domain domain;
4987
4988 for_each_power_domain(domain, crtc->enabled_power_domains)
4989 intel_display_power_put(dev_priv, domain);
4990
4991 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4992 }
4993
4994 intel_display_set_init_power(dev_priv, false);
4995}
4996
Ville Syrjälädfcab172014-06-13 13:37:47 +03004997/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004998static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004999{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005000 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005001
Jesse Barnes586f49d2013-11-04 16:06:59 -08005002 /* Obtain SKU information */
5003 mutex_lock(&dev_priv->dpio_lock);
5004 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5005 CCK_FUSE_HPLL_FREQ_MASK;
5006 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005007
Ville Syrjälädfcab172014-06-13 13:37:47 +03005008 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005009}
5010
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005011static void vlv_update_cdclk(struct drm_device *dev)
5012{
5013 struct drm_i915_private *dev_priv = dev->dev_private;
5014
5015 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03005016 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005017 dev_priv->vlv_cdclk_freq);
5018
5019 /*
5020 * Program the gmbus_freq based on the cdclk frequency.
5021 * BSpec erroneously claims we should aim for 4MHz, but
5022 * in fact 1MHz is the correct frequency.
5023 */
Ville Syrjälä6be1e3d2014-10-16 20:52:31 +03005024 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005025}
5026
Jesse Barnes30a970c2013-11-04 13:48:12 -08005027/* Adjust CDclk dividers to allow high res or save power if possible */
5028static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5029{
5030 struct drm_i915_private *dev_priv = dev->dev_private;
5031 u32 val, cmd;
5032
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005033 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005034
Ville Syrjälädfcab172014-06-13 13:37:47 +03005035 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005036 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005037 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005038 cmd = 1;
5039 else
5040 cmd = 0;
5041
5042 mutex_lock(&dev_priv->rps.hw_lock);
5043 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5044 val &= ~DSPFREQGUAR_MASK;
5045 val |= (cmd << DSPFREQGUAR_SHIFT);
5046 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5047 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5048 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5049 50)) {
5050 DRM_ERROR("timed out waiting for CDclk change\n");
5051 }
5052 mutex_unlock(&dev_priv->rps.hw_lock);
5053
Ville Syrjälädfcab172014-06-13 13:37:47 +03005054 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005055 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005056
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005057 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005058
5059 mutex_lock(&dev_priv->dpio_lock);
5060 /* adjust cdclk divider */
5061 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005062 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005063 val |= divider;
5064 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005065
5066 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5067 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5068 50))
5069 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005070 mutex_unlock(&dev_priv->dpio_lock);
5071 }
5072
5073 mutex_lock(&dev_priv->dpio_lock);
5074 /* adjust self-refresh exit latency value */
5075 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5076 val &= ~0x7f;
5077
5078 /*
5079 * For high bandwidth configs, we set a higher latency in the bunit
5080 * so that the core display fetch happens in time to avoid underruns.
5081 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005082 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005083 val |= 4500 / 250; /* 4.5 usec */
5084 else
5085 val |= 3000 / 250; /* 3.0 usec */
5086 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5087 mutex_unlock(&dev_priv->dpio_lock);
5088
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005089 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005090}
5091
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005092static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5093{
5094 struct drm_i915_private *dev_priv = dev->dev_private;
5095 u32 val, cmd;
5096
5097 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
5098
5099 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005100 case 333333:
5101 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005102 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005103 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005104 break;
5105 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005106 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005107 return;
5108 }
5109
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005110 /*
5111 * Specs are full of misinformation, but testing on actual
5112 * hardware has shown that we just need to write the desired
5113 * CCK divider into the Punit register.
5114 */
5115 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5116
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005117 mutex_lock(&dev_priv->rps.hw_lock);
5118 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5119 val &= ~DSPFREQGUAR_MASK_CHV;
5120 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5121 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5122 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5123 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5124 50)) {
5125 DRM_ERROR("timed out waiting for CDclk change\n");
5126 }
5127 mutex_unlock(&dev_priv->rps.hw_lock);
5128
5129 vlv_update_cdclk(dev);
5130}
5131
Jesse Barnes30a970c2013-11-04 13:48:12 -08005132static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5133 int max_pixclk)
5134{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005135 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005136 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005137
Jesse Barnes30a970c2013-11-04 13:48:12 -08005138 /*
5139 * Really only a few cases to deal with, as only 4 CDclks are supported:
5140 * 200MHz
5141 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005142 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005143 * 400MHz (VLV only)
5144 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5145 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005146 *
5147 * We seem to get an unstable or solid color picture at 200MHz.
5148 * Not sure what's wrong. For now use 200MHz only when all pipes
5149 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005150 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005151 if (!IS_CHERRYVIEW(dev_priv) &&
5152 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005153 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005154 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005155 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005156 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005157 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005158 else
5159 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005160}
5161
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005162/* compute the max pixel clock for new configuration */
5163static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005164{
5165 struct drm_device *dev = dev_priv->dev;
5166 struct intel_crtc *intel_crtc;
5167 int max_pixclk = 0;
5168
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005169 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005170 if (intel_crtc->new_enabled)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005171 max_pixclk = max(max_pixclk,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02005172 intel_crtc->new_config->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005173 }
5174
5175 return max_pixclk;
5176}
5177
5178static void valleyview_modeset_global_pipes(struct drm_device *dev,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005179 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005180{
5181 struct drm_i915_private *dev_priv = dev->dev_private;
5182 struct intel_crtc *intel_crtc;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005183 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005184
Imre Deakd60c4472014-03-27 17:45:10 +02005185 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
5186 dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005187 return;
5188
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005189 /* disable/enable all currently active pipes while we change cdclk */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005190 for_each_intel_crtc(dev, intel_crtc)
Matt Roper83d65732015-02-25 13:12:16 -08005191 if (intel_crtc->base.state->enable)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005192 *prepare_pipes |= (1 << intel_crtc->pipe);
5193}
5194
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005195static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5196{
5197 unsigned int credits, default_credits;
5198
5199 if (IS_CHERRYVIEW(dev_priv))
5200 default_credits = PFI_CREDIT(12);
5201 else
5202 default_credits = PFI_CREDIT(8);
5203
5204 if (DIV_ROUND_CLOSEST(dev_priv->vlv_cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
5205 /* CHV suggested value is 31 or 63 */
5206 if (IS_CHERRYVIEW(dev_priv))
5207 credits = PFI_CREDIT_31;
5208 else
5209 credits = PFI_CREDIT(15);
5210 } else {
5211 credits = default_credits;
5212 }
5213
5214 /*
5215 * WA - write default credits before re-programming
5216 * FIXME: should we also set the resend bit here?
5217 */
5218 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5219 default_credits);
5220
5221 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5222 credits | PFI_CREDIT_RESEND);
5223
5224 /*
5225 * FIXME is this guaranteed to clear
5226 * immediately or should we poll for it?
5227 */
5228 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5229}
5230
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005231static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005232{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005233 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005234 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005235 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005236 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5237
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005238 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005239 /*
5240 * FIXME: We can end up here with all power domains off, yet
5241 * with a CDCLK frequency other than the minimum. To account
5242 * for this take the PIPE-A power domain, which covers the HW
5243 * blocks needed for the following programming. This can be
5244 * removed once it's guaranteed that we get here either with
5245 * the minimum CDCLK set, or the required power domains
5246 * enabled.
5247 */
5248 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5249
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005250 if (IS_CHERRYVIEW(dev))
5251 cherryview_set_cdclk(dev, req_cdclk);
5252 else
5253 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005254
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005255 vlv_program_pfi_credits(dev_priv);
5256
Imre Deak738c05c2014-11-19 16:25:37 +02005257 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005258 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005259}
5260
Jesse Barnes89b667f2013-04-18 14:51:36 -07005261static void valleyview_crtc_enable(struct drm_crtc *crtc)
5262{
5263 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005264 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005265 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5266 struct intel_encoder *encoder;
5267 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005268 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005269
Matt Roper83d65732015-02-25 13:12:16 -08005270 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005271
5272 if (intel_crtc->active)
5273 return;
5274
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005275 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305276
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005277 if (!is_dsi) {
5278 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005279 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005280 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005281 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005282 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005283
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005284 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305285 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005286
5287 intel_set_pipe_timings(intel_crtc);
5288
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005289 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5290 struct drm_i915_private *dev_priv = dev->dev_private;
5291
5292 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5293 I915_WRITE(CHV_CANVAS(pipe), 0);
5294 }
5295
Daniel Vetter5b18e572014-04-24 23:55:06 +02005296 i9xx_set_pipeconf(intel_crtc);
5297
Jesse Barnes89b667f2013-04-18 14:51:36 -07005298 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005299
Daniel Vettera72e4c92014-09-30 10:56:47 +02005300 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005301
Jesse Barnes89b667f2013-04-18 14:51:36 -07005302 for_each_encoder_on_crtc(dev, crtc, encoder)
5303 if (encoder->pre_pll_enable)
5304 encoder->pre_pll_enable(encoder);
5305
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005306 if (!is_dsi) {
5307 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005308 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005309 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005310 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005311 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005312
5313 for_each_encoder_on_crtc(dev, crtc, encoder)
5314 if (encoder->pre_enable)
5315 encoder->pre_enable(encoder);
5316
Jesse Barnes2dd24552013-04-25 12:55:01 -07005317 i9xx_pfit_enable(intel_crtc);
5318
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005319 intel_crtc_load_lut(crtc);
5320
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005321 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005322 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005323
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005324 assert_vblank_disabled(crtc);
5325 drm_crtc_vblank_on(crtc);
5326
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005327 for_each_encoder_on_crtc(dev, crtc, encoder)
5328 encoder->enable(encoder);
5329
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005330 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005331
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005332 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005333 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005334}
5335
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005336static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5337{
5338 struct drm_device *dev = crtc->base.dev;
5339 struct drm_i915_private *dev_priv = dev->dev_private;
5340
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005341 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5342 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005343}
5344
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005345static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005346{
5347 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005348 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005349 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005350 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005351 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005352
Matt Roper83d65732015-02-25 13:12:16 -08005353 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02005354
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005355 if (intel_crtc->active)
5356 return;
5357
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005358 i9xx_set_pll_dividers(intel_crtc);
5359
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005360 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305361 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005362
5363 intel_set_pipe_timings(intel_crtc);
5364
Daniel Vetter5b18e572014-04-24 23:55:06 +02005365 i9xx_set_pipeconf(intel_crtc);
5366
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005367 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005368
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005369 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005370 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005371
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005372 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005373 if (encoder->pre_enable)
5374 encoder->pre_enable(encoder);
5375
Daniel Vetterf6736a12013-06-05 13:34:30 +02005376 i9xx_enable_pll(intel_crtc);
5377
Jesse Barnes2dd24552013-04-25 12:55:01 -07005378 i9xx_pfit_enable(intel_crtc);
5379
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005380 intel_crtc_load_lut(crtc);
5381
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005382 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005383 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005384
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005385 assert_vblank_disabled(crtc);
5386 drm_crtc_vblank_on(crtc);
5387
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005388 for_each_encoder_on_crtc(dev, crtc, encoder)
5389 encoder->enable(encoder);
5390
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005391 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005392
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005393 /*
5394 * Gen2 reports pipe underruns whenever all planes are disabled.
5395 * So don't enable underrun reporting before at least some planes
5396 * are enabled.
5397 * FIXME: Need to fix the logic to work when we turn off all planes
5398 * but leave the pipe running.
5399 */
5400 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005401 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005402
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005403 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005404 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005405}
5406
Daniel Vetter87476d62013-04-11 16:29:06 +02005407static void i9xx_pfit_disable(struct intel_crtc *crtc)
5408{
5409 struct drm_device *dev = crtc->base.dev;
5410 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02005411
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005412 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005413 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02005414
5415 assert_pipe_disabled(dev_priv, crtc->pipe);
5416
Daniel Vetter328d8e82013-05-08 10:36:31 +02005417 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5418 I915_READ(PFIT_CONTROL));
5419 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02005420}
5421
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005422static void i9xx_crtc_disable(struct drm_crtc *crtc)
5423{
5424 struct drm_device *dev = crtc->dev;
5425 struct drm_i915_private *dev_priv = dev->dev_private;
5426 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005427 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005428 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005429
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005430 if (!intel_crtc->active)
5431 return;
5432
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005433 /*
5434 * Gen2 reports pipe underruns whenever all planes are disabled.
5435 * So diasble underrun reporting before all the planes get disabled.
5436 * FIXME: Need to fix the logic to work when we turn off all planes
5437 * but leave the pipe running.
5438 */
5439 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005440 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005441
Imre Deak564ed192014-06-13 14:54:21 +03005442 /*
5443 * Vblank time updates from the shadow to live plane control register
5444 * are blocked if the memory self-refresh mode is active at that
5445 * moment. So to make sure the plane gets truly disabled, disable
5446 * first the self-refresh mode. The self-refresh enable bit in turn
5447 * will be checked/applied by the HW only at the next frame start
5448 * event which is after the vblank start event, so we need to have a
5449 * wait-for-vblank between disabling the plane and the pipe.
5450 */
5451 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005452 intel_crtc_disable_planes(crtc);
5453
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005454 /*
5455 * On gen2 planes are double buffered but the pipe isn't, so we must
5456 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03005457 * We also need to wait on all gmch platforms because of the
5458 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005459 */
Imre Deak564ed192014-06-13 14:54:21 +03005460 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005461
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005462 for_each_encoder_on_crtc(dev, crtc, encoder)
5463 encoder->disable(encoder);
5464
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005465 drm_crtc_vblank_off(crtc);
5466 assert_vblank_disabled(crtc);
5467
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005468 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005469
Daniel Vetter87476d62013-04-11 16:29:06 +02005470 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005471
Jesse Barnes89b667f2013-04-18 14:51:36 -07005472 for_each_encoder_on_crtc(dev, crtc, encoder)
5473 if (encoder->post_disable)
5474 encoder->post_disable(encoder);
5475
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005476 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005477 if (IS_CHERRYVIEW(dev))
5478 chv_disable_pll(dev_priv, pipe);
5479 else if (IS_VALLEYVIEW(dev))
5480 vlv_disable_pll(dev_priv, pipe);
5481 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03005482 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005483 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005484
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005485 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005486 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005487
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005488 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005489 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005490
Daniel Vetterefa96242014-04-24 23:55:02 +02005491 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005492 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02005493 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005494}
5495
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005496static void i9xx_crtc_off(struct drm_crtc *crtc)
5497{
5498}
5499
Borun Fub04c5bd2014-07-12 10:02:27 +05305500/* Master function to enable/disable CRTC and corresponding power wells */
5501void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01005502{
Chris Wilsoncdd59982010-09-08 16:30:16 +01005503 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005504 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe72014-04-24 23:55:42 +02005505 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe72014-04-24 23:55:42 +02005506 enum intel_display_power_domain domain;
5507 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005508
Daniel Vetter0e572fe72014-04-24 23:55:42 +02005509 if (enable) {
5510 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005511 domains = get_crtc_power_domains(crtc);
5512 for_each_power_domain(domain, domains)
5513 intel_display_power_get(dev_priv, domain);
5514 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe72014-04-24 23:55:42 +02005515
5516 dev_priv->display.crtc_enable(crtc);
5517 }
5518 } else {
5519 if (intel_crtc->active) {
5520 dev_priv->display.crtc_disable(crtc);
5521
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005522 domains = intel_crtc->enabled_power_domains;
5523 for_each_power_domain(domain, domains)
5524 intel_display_power_put(dev_priv, domain);
5525 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe72014-04-24 23:55:42 +02005526 }
5527 }
Borun Fub04c5bd2014-07-12 10:02:27 +05305528}
5529
5530/**
5531 * Sets the power management mode of the pipe and plane.
5532 */
5533void intel_crtc_update_dpms(struct drm_crtc *crtc)
5534{
5535 struct drm_device *dev = crtc->dev;
5536 struct intel_encoder *intel_encoder;
5537 bool enable = false;
5538
5539 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5540 enable |= intel_encoder->connectors_active;
5541
5542 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005543}
5544
Daniel Vetter976f8a22012-07-08 22:34:21 +02005545static void intel_crtc_disable(struct drm_crtc *crtc)
5546{
5547 struct drm_device *dev = crtc->dev;
5548 struct drm_connector *connector;
5549 struct drm_i915_private *dev_priv = dev->dev_private;
5550
5551 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08005552 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005553
5554 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005555 dev_priv->display.off(crtc);
5556
Gustavo Padovan455a6802014-12-01 15:40:11 -08005557 crtc->primary->funcs->disable_plane(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005558
5559 /* Update computed state. */
5560 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5561 if (!connector->encoder || !connector->encoder->crtc)
5562 continue;
5563
5564 if (connector->encoder->crtc != crtc)
5565 continue;
5566
5567 connector->dpms = DRM_MODE_DPMS_OFF;
5568 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01005569 }
5570}
5571
Chris Wilsonea5b2132010-08-04 13:50:23 +01005572void intel_encoder_destroy(struct drm_encoder *encoder)
5573{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005574 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01005575
Chris Wilsonea5b2132010-08-04 13:50:23 +01005576 drm_encoder_cleanup(encoder);
5577 kfree(intel_encoder);
5578}
5579
Damien Lespiau92373292013-08-08 22:28:57 +01005580/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005581 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5582 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01005583static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005584{
5585 if (mode == DRM_MODE_DPMS_ON) {
5586 encoder->connectors_active = true;
5587
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005588 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005589 } else {
5590 encoder->connectors_active = false;
5591
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005592 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005593 }
5594}
5595
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005596/* Cross check the actual hw state with our own modeset state tracking (and it's
5597 * internal consistency). */
Daniel Vetterb980514c2012-08-31 17:37:33 +02005598static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005599{
5600 if (connector->get_hw_state(connector)) {
5601 struct intel_encoder *encoder = connector->encoder;
5602 struct drm_crtc *crtc;
5603 bool encoder_enabled;
5604 enum pipe pipe;
5605
5606 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5607 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03005608 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005609
Dave Airlie0e32b392014-05-02 14:02:48 +10005610 /* there is no real hw state for MST connectors */
5611 if (connector->mst_port)
5612 return;
5613
Rob Clarke2c719b2014-12-15 13:56:32 -05005614 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005615 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05005616 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005617 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005618
Dave Airlie36cd7442014-05-02 13:44:18 +10005619 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05005620 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10005621 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005622
Dave Airlie36cd7442014-05-02 13:44:18 +10005623 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05005624 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
5625 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10005626 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005627
Dave Airlie36cd7442014-05-02 13:44:18 +10005628 crtc = encoder->base.crtc;
5629
Matt Roper83d65732015-02-25 13:12:16 -08005630 I915_STATE_WARN(!crtc->state->enable,
5631 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05005632 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5633 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10005634 "encoder active on the wrong pipe\n");
5635 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005636 }
5637}
5638
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03005639int intel_connector_init(struct intel_connector *connector)
5640{
5641 struct drm_connector_state *connector_state;
5642
5643 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
5644 if (!connector_state)
5645 return -ENOMEM;
5646
5647 connector->base.state = connector_state;
5648 return 0;
5649}
5650
5651struct intel_connector *intel_connector_alloc(void)
5652{
5653 struct intel_connector *connector;
5654
5655 connector = kzalloc(sizeof *connector, GFP_KERNEL);
5656 if (!connector)
5657 return NULL;
5658
5659 if (intel_connector_init(connector) < 0) {
5660 kfree(connector);
5661 return NULL;
5662 }
5663
5664 return connector;
5665}
5666
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005667/* Even simpler default implementation, if there's really no special case to
5668 * consider. */
5669void intel_connector_dpms(struct drm_connector *connector, int mode)
5670{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005671 /* All the simple cases only support two dpms states. */
5672 if (mode != DRM_MODE_DPMS_ON)
5673 mode = DRM_MODE_DPMS_OFF;
5674
5675 if (mode == connector->dpms)
5676 return;
5677
5678 connector->dpms = mode;
5679
5680 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01005681 if (connector->encoder)
5682 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005683
Daniel Vetterb980514c2012-08-31 17:37:33 +02005684 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005685}
5686
Daniel Vetterf0947c32012-07-02 13:10:34 +02005687/* Simple connector->get_hw_state implementation for encoders that support only
5688 * one connector and no cloning and hence the encoder state determines the state
5689 * of the connector. */
5690bool intel_connector_get_hw_state(struct intel_connector *connector)
5691{
Daniel Vetter24929352012-07-02 20:28:59 +02005692 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02005693 struct intel_encoder *encoder = connector->encoder;
5694
5695 return encoder->get_hw_state(encoder, &pipe);
5696}
5697
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02005698static int pipe_required_fdi_lanes(struct drm_device *dev, enum pipe pipe)
5699{
5700 struct intel_crtc *crtc =
5701 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5702
5703 if (crtc->base.state->enable &&
5704 crtc->config->has_pch_encoder)
5705 return crtc->config->fdi_lanes;
5706
5707 return 0;
5708}
5709
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005710static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005711 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005712{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005713 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5714 pipe_name(pipe), pipe_config->fdi_lanes);
5715 if (pipe_config->fdi_lanes > 4) {
5716 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5717 pipe_name(pipe), pipe_config->fdi_lanes);
5718 return false;
5719 }
5720
Paulo Zanonibafb6552013-11-02 21:07:44 -07005721 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005722 if (pipe_config->fdi_lanes > 2) {
5723 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5724 pipe_config->fdi_lanes);
5725 return false;
5726 } else {
5727 return true;
5728 }
5729 }
5730
5731 if (INTEL_INFO(dev)->num_pipes == 2)
5732 return true;
5733
5734 /* Ivybridge 3 pipe is really complicated */
5735 switch (pipe) {
5736 case PIPE_A:
5737 return true;
5738 case PIPE_B:
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02005739 if (pipe_config->fdi_lanes > 2 &&
5740 pipe_required_fdi_lanes(dev, PIPE_C) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005741 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5742 pipe_name(pipe), pipe_config->fdi_lanes);
5743 return false;
5744 }
5745 return true;
5746 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02005747 if (pipe_config->fdi_lanes > 2) {
5748 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
5749 pipe_name(pipe), pipe_config->fdi_lanes);
5750 return false;
5751 }
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02005752 if (pipe_required_fdi_lanes(dev, PIPE_B) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005753 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5754 return false;
5755 }
5756 return true;
5757 default:
5758 BUG();
5759 }
5760}
5761
Daniel Vettere29c22c2013-02-21 00:00:16 +01005762#define RETRY 1
5763static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005764 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02005765{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005766 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02005767 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02005768 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01005769 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005770
Daniel Vettere29c22c2013-02-21 00:00:16 +01005771retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02005772 /* FDI is a binary signal running at ~2.7GHz, encoding
5773 * each output octet as 10 bits. The actual frequency
5774 * is stored as a divider into a 100MHz clock, and the
5775 * mode pixel clock is stored in units of 1KHz.
5776 * Hence the bw of each lane in terms of the mode signal
5777 * is:
5778 */
5779 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5780
Damien Lespiau241bfc32013-09-25 16:45:37 +01005781 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005782
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005783 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005784 pipe_config->pipe_bpp);
5785
5786 pipe_config->fdi_lanes = lane;
5787
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005788 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005789 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005790
Daniel Vettere29c22c2013-02-21 00:00:16 +01005791 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5792 intel_crtc->pipe, pipe_config);
5793 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5794 pipe_config->pipe_bpp -= 2*3;
5795 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5796 pipe_config->pipe_bpp);
5797 needs_recompute = true;
5798 pipe_config->bw_constrained = true;
5799
5800 goto retry;
5801 }
5802
5803 if (needs_recompute)
5804 return RETRY;
5805
5806 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005807}
5808
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005809static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005810 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005811{
Jani Nikulad330a952014-01-21 11:24:25 +02005812 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03005813 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07005814 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005815}
5816
Daniel Vettera43f6e02013-06-07 23:10:32 +02005817static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005818 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08005819{
Daniel Vettera43f6e02013-06-07 23:10:32 +02005820 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02005821 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02005822 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01005823
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005824 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005825 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005826 int clock_limit =
5827 dev_priv->display.get_display_clock_speed(dev);
5828
5829 /*
5830 * Enable pixel doubling when the dot clock
5831 * is > 90% of the (display) core speed.
5832 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03005833 * GDG double wide on either pipe,
5834 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005835 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03005836 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01005837 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005838 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005839 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005840 }
5841
Damien Lespiau241bfc32013-09-25 16:45:37 +01005842 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005843 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005844 }
Chris Wilson89749352010-09-12 18:25:19 +01005845
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005846 /*
5847 * Pipe horizontal size must be even in:
5848 * - DVO ganged mode
5849 * - LVDS dual channel mode
5850 * - Double wide pipe
5851 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02005852 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005853 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5854 pipe_config->pipe_src_w &= ~1;
5855
Damien Lespiau8693a822013-05-03 18:48:11 +01005856 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5857 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03005858 */
5859 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5860 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005861 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03005862
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005863 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005864 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005865 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005866 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5867 * for lvds. */
5868 pipe_config->pipe_bpp = 8*3;
5869 }
5870
Damien Lespiauf5adf942013-06-24 18:29:34 +01005871 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005872 hsw_compute_ips_config(crtc, pipe_config);
5873
Daniel Vetter877d48d2013-04-19 11:24:43 +02005874 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02005875 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02005876
Daniel Vettere29c22c2013-02-21 00:00:16 +01005877 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005878}
5879
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005880static int valleyview_get_display_clock_speed(struct drm_device *dev)
5881{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005882 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005883 u32 val;
5884 int divider;
5885
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005886 if (dev_priv->hpll_freq == 0)
5887 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
5888
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005889 mutex_lock(&dev_priv->dpio_lock);
5890 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5891 mutex_unlock(&dev_priv->dpio_lock);
5892
5893 divider = val & DISPLAY_FREQUENCY_VALUES;
5894
Ville Syrjälä7d007f42014-06-13 13:37:53 +03005895 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5896 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5897 "cdclk change in progress\n");
5898
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005899 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005900}
5901
Jesse Barnese70236a2009-09-21 10:42:27 -07005902static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08005903{
Jesse Barnese70236a2009-09-21 10:42:27 -07005904 return 400000;
5905}
Jesse Barnes79e53942008-11-07 14:24:08 -08005906
Jesse Barnese70236a2009-09-21 10:42:27 -07005907static int i915_get_display_clock_speed(struct drm_device *dev)
5908{
5909 return 333000;
5910}
Jesse Barnes79e53942008-11-07 14:24:08 -08005911
Jesse Barnese70236a2009-09-21 10:42:27 -07005912static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5913{
5914 return 200000;
5915}
Jesse Barnes79e53942008-11-07 14:24:08 -08005916
Daniel Vetter257a7ff2013-07-26 08:35:42 +02005917static int pnv_get_display_clock_speed(struct drm_device *dev)
5918{
5919 u16 gcfgc = 0;
5920
5921 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5922
5923 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5924 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5925 return 267000;
5926 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5927 return 333000;
5928 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5929 return 444000;
5930 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5931 return 200000;
5932 default:
5933 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5934 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5935 return 133000;
5936 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5937 return 167000;
5938 }
5939}
5940
Jesse Barnese70236a2009-09-21 10:42:27 -07005941static int i915gm_get_display_clock_speed(struct drm_device *dev)
5942{
5943 u16 gcfgc = 0;
5944
5945 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5946
5947 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08005948 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07005949 else {
5950 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5951 case GC_DISPLAY_CLOCK_333_MHZ:
5952 return 333000;
5953 default:
5954 case GC_DISPLAY_CLOCK_190_200_MHZ:
5955 return 190000;
5956 }
5957 }
5958}
Jesse Barnes79e53942008-11-07 14:24:08 -08005959
Jesse Barnese70236a2009-09-21 10:42:27 -07005960static int i865_get_display_clock_speed(struct drm_device *dev)
5961{
5962 return 266000;
5963}
5964
5965static int i855_get_display_clock_speed(struct drm_device *dev)
5966{
5967 u16 hpllcc = 0;
5968 /* Assume that the hardware is in the high speed state. This
5969 * should be the default.
5970 */
5971 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5972 case GC_CLOCK_133_200:
5973 case GC_CLOCK_100_200:
5974 return 200000;
5975 case GC_CLOCK_166_250:
5976 return 250000;
5977 case GC_CLOCK_100_133:
5978 return 133000;
5979 }
5980
5981 /* Shouldn't happen */
5982 return 0;
5983}
5984
5985static int i830_get_display_clock_speed(struct drm_device *dev)
5986{
5987 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08005988}
5989
Zhenyu Wang2c072452009-06-05 15:38:42 +08005990static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005991intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005992{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005993 while (*num > DATA_LINK_M_N_MASK ||
5994 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08005995 *num >>= 1;
5996 *den >>= 1;
5997 }
5998}
5999
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006000static void compute_m_n(unsigned int m, unsigned int n,
6001 uint32_t *ret_m, uint32_t *ret_n)
6002{
6003 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6004 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6005 intel_reduce_m_n_ratio(ret_m, ret_n);
6006}
6007
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006008void
6009intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6010 int pixel_clock, int link_clock,
6011 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006012{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006013 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006014
6015 compute_m_n(bits_per_pixel * pixel_clock,
6016 link_clock * nlanes * 8,
6017 &m_n->gmch_m, &m_n->gmch_n);
6018
6019 compute_m_n(pixel_clock, link_clock,
6020 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006021}
6022
Chris Wilsona7615032011-01-12 17:04:08 +00006023static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6024{
Jani Nikulad330a952014-01-21 11:24:25 +02006025 if (i915.panel_use_ssc >= 0)
6026 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006027 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006028 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006029}
6030
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006031static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6032 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006033{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006034 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006035 struct drm_i915_private *dev_priv = dev->dev_private;
6036 int refclk;
6037
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006038 WARN_ON(!crtc_state->base.state);
6039
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006040 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006041 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006042 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006043 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006044 refclk = dev_priv->vbt.lvds_ssc_freq;
6045 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006046 } else if (!IS_GEN2(dev)) {
6047 refclk = 96000;
6048 } else {
6049 refclk = 48000;
6050 }
6051
6052 return refclk;
6053}
6054
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006055static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006056{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006057 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006058}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006059
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006060static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6061{
6062 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006063}
6064
Daniel Vetterf47709a2013-03-28 10:42:02 +01006065static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006066 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08006067 intel_clock_t *reduced_clock)
6068{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006069 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006070 u32 fp, fp2 = 0;
6071
6072 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006073 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006074 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006075 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006076 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006077 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006078 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006079 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006080 }
6081
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006082 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006083
Daniel Vetterf47709a2013-03-28 10:42:02 +01006084 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006085 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006086 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006087 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006088 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006089 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006090 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006091 }
6092}
6093
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006094static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6095 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006096{
6097 u32 reg_val;
6098
6099 /*
6100 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6101 * and set it to a reasonable value instead.
6102 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006103 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006104 reg_val &= 0xffffff00;
6105 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006106 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006107
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006108 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006109 reg_val &= 0x8cffffff;
6110 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006111 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006112
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006113 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006114 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006115 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006116
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006117 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006118 reg_val &= 0x00ffffff;
6119 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006120 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006121}
6122
Daniel Vetterb5518422013-05-03 11:49:48 +02006123static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6124 struct intel_link_m_n *m_n)
6125{
6126 struct drm_device *dev = crtc->base.dev;
6127 struct drm_i915_private *dev_priv = dev->dev_private;
6128 int pipe = crtc->pipe;
6129
Daniel Vettere3b95f12013-05-03 11:49:49 +02006130 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6131 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6132 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6133 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006134}
6135
6136static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006137 struct intel_link_m_n *m_n,
6138 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006139{
6140 struct drm_device *dev = crtc->base.dev;
6141 struct drm_i915_private *dev_priv = dev->dev_private;
6142 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006143 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006144
6145 if (INTEL_INFO(dev)->gen >= 5) {
6146 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6147 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6148 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6149 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006150 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6151 * for gen < 8) and if DRRS is supported (to make sure the
6152 * registers are not unnecessarily accessed).
6153 */
Durgadoss R44395bf2015-02-13 15:33:02 +05306154 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006155 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006156 I915_WRITE(PIPE_DATA_M2(transcoder),
6157 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6158 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6159 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6160 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6161 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006162 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006163 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6164 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6165 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6166 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006167 }
6168}
6169
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306170void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006171{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306172 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6173
6174 if (m_n == M1_N1) {
6175 dp_m_n = &crtc->config->dp_m_n;
6176 dp_m2_n2 = &crtc->config->dp_m2_n2;
6177 } else if (m_n == M2_N2) {
6178
6179 /*
6180 * M2_N2 registers are not supported. Hence m2_n2 divider value
6181 * needs to be programmed into M1_N1.
6182 */
6183 dp_m_n = &crtc->config->dp_m2_n2;
6184 } else {
6185 DRM_ERROR("Unsupported divider value\n");
6186 return;
6187 }
6188
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006189 if (crtc->config->has_pch_encoder)
6190 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006191 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306192 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006193}
6194
Ville Syrjäläd288f652014-10-28 13:20:22 +02006195static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006196 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006197{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006198 u32 dpll, dpll_md;
6199
6200 /*
6201 * Enable DPIO clock input. We should never disable the reference
6202 * clock for pipe B, since VGA hotplug / manual detection depends
6203 * on it.
6204 */
6205 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6206 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6207 /* We should never disable this, set it here for state tracking */
6208 if (crtc->pipe == PIPE_B)
6209 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6210 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006211 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006212
Ville Syrjäläd288f652014-10-28 13:20:22 +02006213 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006214 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006215 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006216}
6217
Ville Syrjäläd288f652014-10-28 13:20:22 +02006218static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006219 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006220{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006221 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006222 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006223 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006224 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006225 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006226 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006227
Daniel Vetter09153002012-12-12 14:06:44 +01006228 mutex_lock(&dev_priv->dpio_lock);
6229
Ville Syrjäläd288f652014-10-28 13:20:22 +02006230 bestn = pipe_config->dpll.n;
6231 bestm1 = pipe_config->dpll.m1;
6232 bestm2 = pipe_config->dpll.m2;
6233 bestp1 = pipe_config->dpll.p1;
6234 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006235
Jesse Barnes89b667f2013-04-18 14:51:36 -07006236 /* See eDP HDMI DPIO driver vbios notes doc */
6237
6238 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006239 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006240 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006241
6242 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006243 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006244
6245 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006246 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006247 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006248 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006249
6250 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006251 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006252
6253 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006254 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6255 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6256 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006257 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006258
6259 /*
6260 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6261 * but we don't support that).
6262 * Note: don't use the DAC post divider as it seems unstable.
6263 */
6264 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006265 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006266
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006267 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006268 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006269
Jesse Barnes89b667f2013-04-18 14:51:36 -07006270 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006271 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006272 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6273 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006274 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03006275 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006276 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006277 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006278 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006279
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02006280 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006281 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006282 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006283 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006284 0x0df40000);
6285 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006286 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006287 0x0df70000);
6288 } else { /* HDMI or VGA */
6289 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006290 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006291 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006292 0x0df70000);
6293 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006294 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006295 0x0df40000);
6296 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006297
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006298 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006299 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006300 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6301 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006302 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006303 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006304
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006305 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01006306 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006307}
6308
Ville Syrjäläd288f652014-10-28 13:20:22 +02006309static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006310 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006311{
Ville Syrjäläd288f652014-10-28 13:20:22 +02006312 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006313 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6314 DPLL_VCO_ENABLE;
6315 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006316 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006317
Ville Syrjäläd288f652014-10-28 13:20:22 +02006318 pipe_config->dpll_hw_state.dpll_md =
6319 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006320}
6321
Ville Syrjäläd288f652014-10-28 13:20:22 +02006322static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006323 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006324{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006325 struct drm_device *dev = crtc->base.dev;
6326 struct drm_i915_private *dev_priv = dev->dev_private;
6327 int pipe = crtc->pipe;
6328 int dpll_reg = DPLL(crtc->pipe);
6329 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306330 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006331 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306332 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306333 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006334
Ville Syrjäläd288f652014-10-28 13:20:22 +02006335 bestn = pipe_config->dpll.n;
6336 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6337 bestm1 = pipe_config->dpll.m1;
6338 bestm2 = pipe_config->dpll.m2 >> 22;
6339 bestp1 = pipe_config->dpll.p1;
6340 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306341 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306342 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306343 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006344
6345 /*
6346 * Enable Refclk and SSC
6347 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006348 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02006349 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006350
6351 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006352
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006353 /* p1 and p2 divider */
6354 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6355 5 << DPIO_CHV_S1_DIV_SHIFT |
6356 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6357 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6358 1 << DPIO_CHV_K_DIV_SHIFT);
6359
6360 /* Feedback post-divider - m2 */
6361 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6362
6363 /* Feedback refclk divider - n and m1 */
6364 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6365 DPIO_CHV_M1_DIV_BY_2 |
6366 1 << DPIO_CHV_N_DIV_SHIFT);
6367
6368 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306369 if (bestm2_frac)
6370 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006371
6372 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306373 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6374 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6375 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6376 if (bestm2_frac)
6377 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6378 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006379
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05306380 /* Program digital lock detect threshold */
6381 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6382 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6383 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6384 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6385 if (!bestm2_frac)
6386 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6387 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6388
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006389 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306390 if (vco == 5400000) {
6391 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6392 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6393 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6394 tribuf_calcntr = 0x9;
6395 } else if (vco <= 6200000) {
6396 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6397 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6398 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6399 tribuf_calcntr = 0x9;
6400 } else if (vco <= 6480000) {
6401 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6402 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6403 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6404 tribuf_calcntr = 0x8;
6405 } else {
6406 /* Not supported. Apply the same limits as in the max case */
6407 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6408 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6409 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6410 tribuf_calcntr = 0;
6411 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006412 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6413
Ville Syrjälä968040b2015-03-11 22:52:08 +02006414 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306415 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6416 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6417 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6418
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006419 /* AFC Recal */
6420 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6421 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6422 DPIO_AFC_RECAL);
6423
6424 mutex_unlock(&dev_priv->dpio_lock);
6425}
6426
Ville Syrjäläd288f652014-10-28 13:20:22 +02006427/**
6428 * vlv_force_pll_on - forcibly enable just the PLL
6429 * @dev_priv: i915 private structure
6430 * @pipe: pipe PLL to enable
6431 * @dpll: PLL configuration
6432 *
6433 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6434 * in cases where we need the PLL enabled even when @pipe is not going to
6435 * be enabled.
6436 */
6437void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
6438 const struct dpll *dpll)
6439{
6440 struct intel_crtc *crtc =
6441 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006442 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006443 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02006444 .pixel_multiplier = 1,
6445 .dpll = *dpll,
6446 };
6447
6448 if (IS_CHERRYVIEW(dev)) {
6449 chv_update_pll(crtc, &pipe_config);
6450 chv_prepare_pll(crtc, &pipe_config);
6451 chv_enable_pll(crtc, &pipe_config);
6452 } else {
6453 vlv_update_pll(crtc, &pipe_config);
6454 vlv_prepare_pll(crtc, &pipe_config);
6455 vlv_enable_pll(crtc, &pipe_config);
6456 }
6457}
6458
6459/**
6460 * vlv_force_pll_off - forcibly disable just the PLL
6461 * @dev_priv: i915 private structure
6462 * @pipe: pipe PLL to disable
6463 *
6464 * Disable the PLL for @pipe. To be used in cases where we need
6465 * the PLL enabled even when @pipe is not going to be enabled.
6466 */
6467void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
6468{
6469 if (IS_CHERRYVIEW(dev))
6470 chv_disable_pll(to_i915(dev), pipe);
6471 else
6472 vlv_disable_pll(to_i915(dev), pipe);
6473}
6474
Daniel Vetterf47709a2013-03-28 10:42:02 +01006475static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006476 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01006477 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006478 int num_connectors)
6479{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006480 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006481 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006482 u32 dpll;
6483 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006484 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006485
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006486 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306487
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006488 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
6489 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006490
6491 dpll = DPLL_VGA_MODE_DIS;
6492
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006493 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006494 dpll |= DPLLB_MODE_LVDS;
6495 else
6496 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006497
Daniel Vetteref1b4602013-06-01 17:17:04 +02006498 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006499 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02006500 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006501 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02006502
6503 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006504 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006505
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006506 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006507 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006508
6509 /* compute bitmask from p1 value */
6510 if (IS_PINEVIEW(dev))
6511 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6512 else {
6513 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6514 if (IS_G4X(dev) && reduced_clock)
6515 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6516 }
6517 switch (clock->p2) {
6518 case 5:
6519 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6520 break;
6521 case 7:
6522 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6523 break;
6524 case 10:
6525 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6526 break;
6527 case 14:
6528 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6529 break;
6530 }
6531 if (INTEL_INFO(dev)->gen >= 4)
6532 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6533
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006534 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006535 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006536 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006537 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6538 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6539 else
6540 dpll |= PLL_REF_INPUT_DREFCLK;
6541
6542 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006543 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006544
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006545 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006546 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02006547 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006548 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006549 }
6550}
6551
Daniel Vetterf47709a2013-03-28 10:42:02 +01006552static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006553 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01006554 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006555 int num_connectors)
6556{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006557 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006558 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006559 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006560 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006561
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006562 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306563
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006564 dpll = DPLL_VGA_MODE_DIS;
6565
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006566 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006567 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6568 } else {
6569 if (clock->p1 == 2)
6570 dpll |= PLL_P1_DIVIDE_BY_TWO;
6571 else
6572 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6573 if (clock->p2 == 4)
6574 dpll |= PLL_P2_DIVIDE_BY_4;
6575 }
6576
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006577 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02006578 dpll |= DPLL_DVO_2X_MODE;
6579
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006580 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006581 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6582 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6583 else
6584 dpll |= PLL_REF_INPUT_DREFCLK;
6585
6586 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006587 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006588}
6589
Daniel Vetter8a654f32013-06-01 17:16:22 +02006590static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006591{
6592 struct drm_device *dev = intel_crtc->base.dev;
6593 struct drm_i915_private *dev_priv = dev->dev_private;
6594 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006595 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02006596 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006597 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006598 uint32_t crtc_vtotal, crtc_vblank_end;
6599 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006600
6601 /* We need to be careful not to changed the adjusted mode, for otherwise
6602 * the hw state checker will get angry at the mismatch. */
6603 crtc_vtotal = adjusted_mode->crtc_vtotal;
6604 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006605
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006606 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006607 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006608 crtc_vtotal -= 1;
6609 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006610
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006611 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006612 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6613 else
6614 vsyncshift = adjusted_mode->crtc_hsync_start -
6615 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006616 if (vsyncshift < 0)
6617 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006618 }
6619
6620 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006621 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006622
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006623 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006624 (adjusted_mode->crtc_hdisplay - 1) |
6625 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006626 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006627 (adjusted_mode->crtc_hblank_start - 1) |
6628 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006629 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006630 (adjusted_mode->crtc_hsync_start - 1) |
6631 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6632
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006633 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006634 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006635 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006636 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006637 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006638 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006639 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006640 (adjusted_mode->crtc_vsync_start - 1) |
6641 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6642
Paulo Zanonib5e508d2012-10-24 11:34:43 -02006643 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6644 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6645 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6646 * bits. */
6647 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6648 (pipe == PIPE_B || pipe == PIPE_C))
6649 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6650
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006651 /* pipesrc controls the size that is scaled from, which should
6652 * always be the user's requested size.
6653 */
6654 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006655 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6656 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006657}
6658
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006659static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006660 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006661{
6662 struct drm_device *dev = crtc->base.dev;
6663 struct drm_i915_private *dev_priv = dev->dev_private;
6664 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6665 uint32_t tmp;
6666
6667 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006668 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6669 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006670 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006671 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6672 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006673 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006674 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6675 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006676
6677 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006678 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6679 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006680 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006681 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6682 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006683 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006684 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6685 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006686
6687 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006688 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6689 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
6690 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006691 }
6692
6693 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03006694 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6695 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6696
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006697 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
6698 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006699}
6700
Daniel Vetterf6a83282014-02-11 15:28:57 -08006701void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006702 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03006703{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006704 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
6705 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
6706 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
6707 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006708
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006709 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
6710 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
6711 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
6712 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006713
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006714 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006715
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006716 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
6717 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006718}
6719
Daniel Vetter84b046f2013-02-19 18:48:54 +01006720static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6721{
6722 struct drm_device *dev = intel_crtc->base.dev;
6723 struct drm_i915_private *dev_priv = dev->dev_private;
6724 uint32_t pipeconf;
6725
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006726 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006727
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03006728 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6729 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6730 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02006731
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006732 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006733 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006734
Daniel Vetterff9ce462013-04-24 14:57:17 +02006735 /* only g4x and later have fancy bpc/dither controls */
6736 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02006737 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006738 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02006739 pipeconf |= PIPECONF_DITHER_EN |
6740 PIPECONF_DITHER_TYPE_SP;
6741
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006742 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02006743 case 18:
6744 pipeconf |= PIPECONF_6BPC;
6745 break;
6746 case 24:
6747 pipeconf |= PIPECONF_8BPC;
6748 break;
6749 case 30:
6750 pipeconf |= PIPECONF_10BPC;
6751 break;
6752 default:
6753 /* Case prevented by intel_choose_pipe_bpp_dither. */
6754 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01006755 }
6756 }
6757
6758 if (HAS_PIPE_CXSR(dev)) {
6759 if (intel_crtc->lowfreq_avail) {
6760 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6761 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6762 } else {
6763 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01006764 }
6765 }
6766
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006767 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006768 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006769 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006770 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6771 else
6772 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6773 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01006774 pipeconf |= PIPECONF_PROGRESSIVE;
6775
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006776 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006777 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03006778
Daniel Vetter84b046f2013-02-19 18:48:54 +01006779 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6780 POSTING_READ(PIPECONF(intel_crtc->pipe));
6781}
6782
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006783static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
6784 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08006785{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006786 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08006787 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07006788 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07006789 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02006790 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006791 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01006792 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08006793 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02006794 struct drm_atomic_state *state = crtc_state->base.state;
6795 struct drm_connector_state *connector_state;
6796 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006797
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02006798 for (i = 0; i < state->num_connector; i++) {
6799 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006800 continue;
6801
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02006802 connector_state = state->connector_states[i];
6803 if (connector_state->crtc != &crtc->base)
6804 continue;
6805
6806 encoder = to_intel_encoder(connector_state->best_encoder);
6807
Chris Wilson5eddb702010-09-11 13:48:45 +01006808 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006809 case INTEL_OUTPUT_LVDS:
6810 is_lvds = true;
6811 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006812 case INTEL_OUTPUT_DSI:
6813 is_dsi = true;
6814 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02006815 default:
6816 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006817 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006818
Eric Anholtc751ce42010-03-25 11:48:48 -07006819 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08006820 }
6821
Jani Nikulaf2335332013-09-13 11:03:09 +03006822 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02006823 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006824
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006825 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006826 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03006827
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006828 /*
6829 * Returns a set of divisors for the desired target clock with
6830 * the given refclk, or FALSE. The returned values represent
6831 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6832 * 2) / p1 / p2.
6833 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006834 limit = intel_limit(crtc_state, refclk);
6835 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006836 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006837 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03006838 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006839 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6840 return -EINVAL;
6841 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006842
Jani Nikulaf2335332013-09-13 11:03:09 +03006843 if (is_lvds && dev_priv->lvds_downclock_avail) {
6844 /*
6845 * Ensure we match the reduced clock's P to the target
6846 * clock. If the clocks don't match, we can't switch
6847 * the display clock by using the FP0/FP1. In such case
6848 * we will disable the LVDS downclock feature.
6849 */
6850 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006851 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03006852 dev_priv->lvds_downclock,
6853 refclk, &clock,
6854 &reduced_clock);
6855 }
6856 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006857 crtc_state->dpll.n = clock.n;
6858 crtc_state->dpll.m1 = clock.m1;
6859 crtc_state->dpll.m2 = clock.m2;
6860 crtc_state->dpll.p1 = clock.p1;
6861 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006862 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006863
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006864 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006865 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306866 has_reduced_clock ? &reduced_clock : NULL,
6867 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006868 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006869 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006870 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006871 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006872 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006873 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006874 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02006875 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006876 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006877
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02006878 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07006879}
6880
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006881static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006882 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006883{
6884 struct drm_device *dev = crtc->base.dev;
6885 struct drm_i915_private *dev_priv = dev->dev_private;
6886 uint32_t tmp;
6887
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02006888 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6889 return;
6890
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006891 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02006892 if (!(tmp & PFIT_ENABLE))
6893 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006894
Daniel Vetter06922822013-07-11 13:35:40 +02006895 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006896 if (INTEL_INFO(dev)->gen < 4) {
6897 if (crtc->pipe != PIPE_B)
6898 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006899 } else {
6900 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6901 return;
6902 }
6903
Daniel Vetter06922822013-07-11 13:35:40 +02006904 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006905 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6906 if (INTEL_INFO(dev)->gen < 5)
6907 pipe_config->gmch_pfit.lvds_border_bits =
6908 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6909}
6910
Jesse Barnesacbec812013-09-20 11:29:32 -07006911static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006912 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07006913{
6914 struct drm_device *dev = crtc->base.dev;
6915 struct drm_i915_private *dev_priv = dev->dev_private;
6916 int pipe = pipe_config->cpu_transcoder;
6917 intel_clock_t clock;
6918 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07006919 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07006920
Shobhit Kumarf573de52014-07-30 20:32:37 +05306921 /* In case of MIPI DPLL will not even be used */
6922 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6923 return;
6924
Jesse Barnesacbec812013-09-20 11:29:32 -07006925 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006926 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07006927 mutex_unlock(&dev_priv->dpio_lock);
6928
6929 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6930 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6931 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6932 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6933 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6934
Ville Syrjäläf646628b2013-10-14 14:50:31 +03006935 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07006936
Ville Syrjäläf646628b2013-10-14 14:50:31 +03006937 /* clock.dot is the fast clock */
6938 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07006939}
6940
Damien Lespiau5724dbd2015-01-20 12:51:52 +00006941static void
6942i9xx_get_initial_plane_config(struct intel_crtc *crtc,
6943 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006944{
6945 struct drm_device *dev = crtc->base.dev;
6946 struct drm_i915_private *dev_priv = dev->dev_private;
6947 u32 val, base, offset;
6948 int pipe = crtc->pipe, plane = crtc->plane;
6949 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00006950 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006951 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00006952 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006953
Damien Lespiau42a7b082015-02-05 19:35:13 +00006954 val = I915_READ(DSPCNTR(plane));
6955 if (!(val & DISPLAY_PLANE_ENABLE))
6956 return;
6957
Damien Lespiaud9806c92015-01-21 14:07:19 +00006958 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00006959 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006960 DRM_DEBUG_KMS("failed to alloc fb\n");
6961 return;
6962 }
6963
Damien Lespiau1b842c82015-01-21 13:50:54 +00006964 fb = &intel_fb->base;
6965
Daniel Vetter18c52472015-02-10 17:16:09 +00006966 if (INTEL_INFO(dev)->gen >= 4) {
6967 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00006968 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00006969 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
6970 }
6971 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006972
6973 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00006974 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006975 fb->pixel_format = fourcc;
6976 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006977
6978 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00006979 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006980 offset = I915_READ(DSPTILEOFF(plane));
6981 else
6982 offset = I915_READ(DSPLINOFF(plane));
6983 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6984 } else {
6985 base = I915_READ(DSPADDR(plane));
6986 }
6987 plane_config->base = base;
6988
6989 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006990 fb->width = ((val >> 16) & 0xfff) + 1;
6991 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006992
6993 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006994 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006995
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006996 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00006997 fb->pixel_format,
6998 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006999
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007000 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007001
Damien Lespiau2844a922015-01-20 12:51:48 +00007002 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7003 pipe_name(pipe), plane, fb->width, fb->height,
7004 fb->bits_per_pixel, base, fb->pitches[0],
7005 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007006
Damien Lespiau2d140302015-02-05 17:22:18 +00007007 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007008}
7009
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007010static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007011 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007012{
7013 struct drm_device *dev = crtc->base.dev;
7014 struct drm_i915_private *dev_priv = dev->dev_private;
7015 int pipe = pipe_config->cpu_transcoder;
7016 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7017 intel_clock_t clock;
7018 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7019 int refclk = 100000;
7020
7021 mutex_lock(&dev_priv->dpio_lock);
7022 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7023 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7024 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7025 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7026 mutex_unlock(&dev_priv->dpio_lock);
7027
7028 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7029 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7030 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7031 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7032 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7033
7034 chv_clock(refclk, &clock);
7035
7036 /* clock.dot is the fast clock */
7037 pipe_config->port_clock = clock.dot / 5;
7038}
7039
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007040static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007041 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007042{
7043 struct drm_device *dev = crtc->base.dev;
7044 struct drm_i915_private *dev_priv = dev->dev_private;
7045 uint32_t tmp;
7046
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007047 if (!intel_display_power_is_enabled(dev_priv,
7048 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007049 return false;
7050
Daniel Vettere143a212013-07-04 12:01:15 +02007051 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007052 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007053
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007054 tmp = I915_READ(PIPECONF(crtc->pipe));
7055 if (!(tmp & PIPECONF_ENABLE))
7056 return false;
7057
Ville Syrjälä42571aef2013-09-06 23:29:00 +03007058 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7059 switch (tmp & PIPECONF_BPC_MASK) {
7060 case PIPECONF_6BPC:
7061 pipe_config->pipe_bpp = 18;
7062 break;
7063 case PIPECONF_8BPC:
7064 pipe_config->pipe_bpp = 24;
7065 break;
7066 case PIPECONF_10BPC:
7067 pipe_config->pipe_bpp = 30;
7068 break;
7069 default:
7070 break;
7071 }
7072 }
7073
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007074 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7075 pipe_config->limited_color_range = true;
7076
Ville Syrjälä282740f2013-09-04 18:30:03 +03007077 if (INTEL_INFO(dev)->gen < 4)
7078 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7079
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007080 intel_get_pipe_timings(crtc, pipe_config);
7081
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007082 i9xx_get_pfit_config(crtc, pipe_config);
7083
Daniel Vetter6c49f242013-06-06 12:45:25 +02007084 if (INTEL_INFO(dev)->gen >= 4) {
7085 tmp = I915_READ(DPLL_MD(crtc->pipe));
7086 pipe_config->pixel_multiplier =
7087 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7088 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007089 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02007090 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7091 tmp = I915_READ(DPLL(crtc->pipe));
7092 pipe_config->pixel_multiplier =
7093 ((tmp & SDVO_MULTIPLIER_MASK)
7094 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7095 } else {
7096 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7097 * port and will be fixed up in the encoder->get_config
7098 * function. */
7099 pipe_config->pixel_multiplier = 1;
7100 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007101 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7102 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007103 /*
7104 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7105 * on 830. Filter it out here so that we don't
7106 * report errors due to that.
7107 */
7108 if (IS_I830(dev))
7109 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7110
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007111 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7112 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007113 } else {
7114 /* Mask out read-only status bits. */
7115 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7116 DPLL_PORTC_READY_MASK |
7117 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007118 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007119
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007120 if (IS_CHERRYVIEW(dev))
7121 chv_crtc_clock_get(crtc, pipe_config);
7122 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07007123 vlv_crtc_clock_get(crtc, pipe_config);
7124 else
7125 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007126
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007127 return true;
7128}
7129
Paulo Zanonidde86e22012-12-01 12:04:25 -02007130static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007131{
7132 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007133 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007134 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007135 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007136 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007137 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007138 bool has_ck505 = false;
7139 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007140
7141 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01007142 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007143 switch (encoder->type) {
7144 case INTEL_OUTPUT_LVDS:
7145 has_panel = true;
7146 has_lvds = true;
7147 break;
7148 case INTEL_OUTPUT_EDP:
7149 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03007150 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007151 has_cpu_edp = true;
7152 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007153 default:
7154 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007155 }
7156 }
7157
Keith Packard99eb6a02011-09-26 14:29:12 -07007158 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007159 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007160 can_ssc = has_ck505;
7161 } else {
7162 has_ck505 = false;
7163 can_ssc = true;
7164 }
7165
Imre Deak2de69052013-05-08 13:14:04 +03007166 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7167 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007168
7169 /* Ironlake: try to setup display ref clock before DPLL
7170 * enabling. This is only under driver's control after
7171 * PCH B stepping, previous chipset stepping should be
7172 * ignoring this setting.
7173 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007174 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007175
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007176 /* As we must carefully and slowly disable/enable each source in turn,
7177 * compute the final state we want first and check if we need to
7178 * make any changes at all.
7179 */
7180 final = val;
7181 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007182 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007183 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007184 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007185 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7186
7187 final &= ~DREF_SSC_SOURCE_MASK;
7188 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7189 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007190
Keith Packard199e5d72011-09-22 12:01:57 -07007191 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007192 final |= DREF_SSC_SOURCE_ENABLE;
7193
7194 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7195 final |= DREF_SSC1_ENABLE;
7196
7197 if (has_cpu_edp) {
7198 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7199 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7200 else
7201 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7202 } else
7203 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7204 } else {
7205 final |= DREF_SSC_SOURCE_DISABLE;
7206 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7207 }
7208
7209 if (final == val)
7210 return;
7211
7212 /* Always enable nonspread source */
7213 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7214
7215 if (has_ck505)
7216 val |= DREF_NONSPREAD_CK505_ENABLE;
7217 else
7218 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7219
7220 if (has_panel) {
7221 val &= ~DREF_SSC_SOURCE_MASK;
7222 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007223
Keith Packard199e5d72011-09-22 12:01:57 -07007224 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007225 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007226 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007227 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007228 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007229 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007230
7231 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007232 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007233 POSTING_READ(PCH_DREF_CONTROL);
7234 udelay(200);
7235
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007236 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007237
7238 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007239 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007240 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007241 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007242 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007243 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007244 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007245 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007246 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007247
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007248 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007249 POSTING_READ(PCH_DREF_CONTROL);
7250 udelay(200);
7251 } else {
7252 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7253
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007254 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007255
7256 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007257 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007258
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007259 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007260 POSTING_READ(PCH_DREF_CONTROL);
7261 udelay(200);
7262
7263 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007264 val &= ~DREF_SSC_SOURCE_MASK;
7265 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007266
7267 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007268 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007269
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007270 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007271 POSTING_READ(PCH_DREF_CONTROL);
7272 udelay(200);
7273 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007274
7275 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007276}
7277
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007278static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007279{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007280 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007281
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007282 tmp = I915_READ(SOUTH_CHICKEN2);
7283 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7284 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007285
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007286 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7287 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7288 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007289
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007290 tmp = I915_READ(SOUTH_CHICKEN2);
7291 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7292 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007293
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007294 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7295 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7296 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007297}
7298
7299/* WaMPhyProgramming:hsw */
7300static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7301{
7302 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007303
7304 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7305 tmp &= ~(0xFF << 24);
7306 tmp |= (0x12 << 24);
7307 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7308
Paulo Zanonidde86e22012-12-01 12:04:25 -02007309 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7310 tmp |= (1 << 11);
7311 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7312
7313 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7314 tmp |= (1 << 11);
7315 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7316
Paulo Zanonidde86e22012-12-01 12:04:25 -02007317 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7318 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7319 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7320
7321 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7322 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7323 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7324
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007325 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7326 tmp &= ~(7 << 13);
7327 tmp |= (5 << 13);
7328 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007329
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007330 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7331 tmp &= ~(7 << 13);
7332 tmp |= (5 << 13);
7333 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007334
7335 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7336 tmp &= ~0xFF;
7337 tmp |= 0x1C;
7338 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7339
7340 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7341 tmp &= ~0xFF;
7342 tmp |= 0x1C;
7343 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7344
7345 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7346 tmp &= ~(0xFF << 16);
7347 tmp |= (0x1C << 16);
7348 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7349
7350 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7351 tmp &= ~(0xFF << 16);
7352 tmp |= (0x1C << 16);
7353 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7354
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007355 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7356 tmp |= (1 << 27);
7357 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007358
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007359 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7360 tmp |= (1 << 27);
7361 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007362
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007363 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7364 tmp &= ~(0xF << 28);
7365 tmp |= (4 << 28);
7366 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007367
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007368 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7369 tmp &= ~(0xF << 28);
7370 tmp |= (4 << 28);
7371 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007372}
7373
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007374/* Implements 3 different sequences from BSpec chapter "Display iCLK
7375 * Programming" based on the parameters passed:
7376 * - Sequence to enable CLKOUT_DP
7377 * - Sequence to enable CLKOUT_DP without spread
7378 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7379 */
7380static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
7381 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007382{
7383 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007384 uint32_t reg, tmp;
7385
7386 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7387 with_spread = true;
7388 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
7389 with_fdi, "LP PCH doesn't have FDI\n"))
7390 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007391
7392 mutex_lock(&dev_priv->dpio_lock);
7393
7394 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7395 tmp &= ~SBI_SSCCTL_DISABLE;
7396 tmp |= SBI_SSCCTL_PATHALT;
7397 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7398
7399 udelay(24);
7400
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007401 if (with_spread) {
7402 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7403 tmp &= ~SBI_SSCCTL_PATHALT;
7404 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007405
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007406 if (with_fdi) {
7407 lpt_reset_fdi_mphy(dev_priv);
7408 lpt_program_fdi_mphy(dev_priv);
7409 }
7410 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02007411
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007412 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7413 SBI_GEN0 : SBI_DBUFF0;
7414 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7415 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7416 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01007417
7418 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007419}
7420
Paulo Zanoni47701c32013-07-23 11:19:25 -03007421/* Sequence to disable CLKOUT_DP */
7422static void lpt_disable_clkout_dp(struct drm_device *dev)
7423{
7424 struct drm_i915_private *dev_priv = dev->dev_private;
7425 uint32_t reg, tmp;
7426
7427 mutex_lock(&dev_priv->dpio_lock);
7428
7429 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7430 SBI_GEN0 : SBI_DBUFF0;
7431 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7432 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7433 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7434
7435 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7436 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7437 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7438 tmp |= SBI_SSCCTL_PATHALT;
7439 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7440 udelay(32);
7441 }
7442 tmp |= SBI_SSCCTL_DISABLE;
7443 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7444 }
7445
7446 mutex_unlock(&dev_priv->dpio_lock);
7447}
7448
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007449static void lpt_init_pch_refclk(struct drm_device *dev)
7450{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007451 struct intel_encoder *encoder;
7452 bool has_vga = false;
7453
Damien Lespiaub2784e12014-08-05 11:29:37 +01007454 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007455 switch (encoder->type) {
7456 case INTEL_OUTPUT_ANALOG:
7457 has_vga = true;
7458 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007459 default:
7460 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007461 }
7462 }
7463
Paulo Zanoni47701c32013-07-23 11:19:25 -03007464 if (has_vga)
7465 lpt_enable_clkout_dp(dev, true, true);
7466 else
7467 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007468}
7469
Paulo Zanonidde86e22012-12-01 12:04:25 -02007470/*
7471 * Initialize reference clocks when the driver loads
7472 */
7473void intel_init_pch_refclk(struct drm_device *dev)
7474{
7475 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7476 ironlake_init_pch_refclk(dev);
7477 else if (HAS_PCH_LPT(dev))
7478 lpt_init_pch_refclk(dev);
7479}
7480
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007481static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007482{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007483 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007484 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007485 struct drm_atomic_state *state = crtc_state->base.state;
7486 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007487 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007488 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007489 bool is_lvds = false;
7490
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007491 for (i = 0; i < state->num_connector; i++) {
7492 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007493 continue;
7494
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007495 connector_state = state->connector_states[i];
7496 if (connector_state->crtc != crtc_state->base.crtc)
7497 continue;
7498
7499 encoder = to_intel_encoder(connector_state->best_encoder);
7500
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007501 switch (encoder->type) {
7502 case INTEL_OUTPUT_LVDS:
7503 is_lvds = true;
7504 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007505 default:
7506 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007507 }
7508 num_connectors++;
7509 }
7510
7511 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007512 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007513 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007514 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007515 }
7516
7517 return 120000;
7518}
7519
Daniel Vetter6ff93602013-04-19 11:24:36 +02007520static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03007521{
7522 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
7523 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7524 int pipe = intel_crtc->pipe;
7525 uint32_t val;
7526
Daniel Vetter78114072013-06-13 00:54:57 +02007527 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03007528
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007529 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03007530 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007531 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007532 break;
7533 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007534 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007535 break;
7536 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007537 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007538 break;
7539 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007540 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007541 break;
7542 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03007543 /* Case prevented by intel_choose_pipe_bpp_dither. */
7544 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03007545 }
7546
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007547 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03007548 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7549
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007550 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03007551 val |= PIPECONF_INTERLACED_ILK;
7552 else
7553 val |= PIPECONF_PROGRESSIVE;
7554
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007555 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007556 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007557
Paulo Zanonic8203562012-09-12 10:06:29 -03007558 I915_WRITE(PIPECONF(pipe), val);
7559 POSTING_READ(PIPECONF(pipe));
7560}
7561
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007562/*
7563 * Set up the pipe CSC unit.
7564 *
7565 * Currently only full range RGB to limited range RGB conversion
7566 * is supported, but eventually this should handle various
7567 * RGB<->YCbCr scenarios as well.
7568 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01007569static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007570{
7571 struct drm_device *dev = crtc->dev;
7572 struct drm_i915_private *dev_priv = dev->dev_private;
7573 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7574 int pipe = intel_crtc->pipe;
7575 uint16_t coeff = 0x7800; /* 1.0 */
7576
7577 /*
7578 * TODO: Check what kind of values actually come out of the pipe
7579 * with these coeff/postoff values and adjust to get the best
7580 * accuracy. Perhaps we even need to take the bpc value into
7581 * consideration.
7582 */
7583
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007584 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007585 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
7586
7587 /*
7588 * GY/GU and RY/RU should be the other way around according
7589 * to BSpec, but reality doesn't agree. Just set them up in
7590 * a way that results in the correct picture.
7591 */
7592 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
7593 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
7594
7595 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
7596 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
7597
7598 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
7599 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
7600
7601 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
7602 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
7603 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
7604
7605 if (INTEL_INFO(dev)->gen > 6) {
7606 uint16_t postoff = 0;
7607
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007608 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02007609 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007610
7611 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
7612 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
7613 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
7614
7615 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
7616 } else {
7617 uint32_t mode = CSC_MODE_YUV_TO_RGB;
7618
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007619 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007620 mode |= CSC_BLACK_SCREEN_OFFSET;
7621
7622 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
7623 }
7624}
7625
Daniel Vetter6ff93602013-04-19 11:24:36 +02007626static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007627{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007628 struct drm_device *dev = crtc->dev;
7629 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007630 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007631 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007632 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007633 uint32_t val;
7634
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007635 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007636
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007637 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007638 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7639
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007640 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007641 val |= PIPECONF_INTERLACED_ILK;
7642 else
7643 val |= PIPECONF_PROGRESSIVE;
7644
Paulo Zanoni702e7a52012-10-23 18:29:59 -02007645 I915_WRITE(PIPECONF(cpu_transcoder), val);
7646 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007647
7648 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
7649 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007650
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05307651 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007652 val = 0;
7653
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007654 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007655 case 18:
7656 val |= PIPEMISC_DITHER_6_BPC;
7657 break;
7658 case 24:
7659 val |= PIPEMISC_DITHER_8_BPC;
7660 break;
7661 case 30:
7662 val |= PIPEMISC_DITHER_10_BPC;
7663 break;
7664 case 36:
7665 val |= PIPEMISC_DITHER_12_BPC;
7666 break;
7667 default:
7668 /* Case prevented by pipe_config_set_bpp. */
7669 BUG();
7670 }
7671
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007672 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007673 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7674
7675 I915_WRITE(PIPEMISC(pipe), val);
7676 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007677}
7678
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007679static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007680 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007681 intel_clock_t *clock,
7682 bool *has_reduced_clock,
7683 intel_clock_t *reduced_clock)
7684{
7685 struct drm_device *dev = crtc->dev;
7686 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007687 int refclk;
7688 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02007689 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007690
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007691 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007692
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007693 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007694
7695 /*
7696 * Returns a set of divisors for the desired target clock with the given
7697 * refclk, or FALSE. The returned values represent the clock equation:
7698 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7699 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007700 limit = intel_limit(crtc_state, refclk);
7701 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007702 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007703 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007704 if (!ret)
7705 return false;
7706
7707 if (is_lvds && dev_priv->lvds_downclock_avail) {
7708 /*
7709 * Ensure we match the reduced clock's P to the target clock.
7710 * If the clocks don't match, we can't switch the display clock
7711 * by using the FP0/FP1. In such case we will disable the LVDS
7712 * downclock feature.
7713 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02007714 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007715 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007716 dev_priv->lvds_downclock,
7717 refclk, clock,
7718 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007719 }
7720
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007721 return true;
7722}
7723
Paulo Zanonid4b19312012-11-29 11:29:32 -02007724int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7725{
7726 /*
7727 * Account for spread spectrum to avoid
7728 * oversubscribing the link. Max center spread
7729 * is 2.5%; use 5% for safety's sake.
7730 */
7731 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02007732 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02007733}
7734
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007735static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02007736{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007737 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03007738}
7739
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007740static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007741 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007742 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007743 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007744{
7745 struct drm_crtc *crtc = &intel_crtc->base;
7746 struct drm_device *dev = crtc->dev;
7747 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007748 struct drm_atomic_state *state = crtc_state->base.state;
7749 struct drm_connector_state *connector_state;
7750 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007751 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007752 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02007753 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007754
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007755 for (i = 0; i < state->num_connector; i++) {
7756 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007757 continue;
7758
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007759 connector_state = state->connector_states[i];
7760 if (connector_state->crtc != crtc_state->base.crtc)
7761 continue;
7762
7763 encoder = to_intel_encoder(connector_state->best_encoder);
7764
7765 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007766 case INTEL_OUTPUT_LVDS:
7767 is_lvds = true;
7768 break;
7769 case INTEL_OUTPUT_SDVO:
7770 case INTEL_OUTPUT_HDMI:
7771 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007772 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007773 default:
7774 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007775 }
7776
7777 num_connectors++;
7778 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007779
Chris Wilsonc1858122010-12-03 21:35:48 +00007780 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07007781 factor = 21;
7782 if (is_lvds) {
7783 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007784 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02007785 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07007786 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007787 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07007788 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00007789
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007790 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02007791 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00007792
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007793 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7794 *fp2 |= FP_CB_TUNE;
7795
Chris Wilson5eddb702010-09-11 13:48:45 +01007796 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08007797
Eric Anholta07d6782011-03-30 13:01:08 -07007798 if (is_lvds)
7799 dpll |= DPLLB_MODE_LVDS;
7800 else
7801 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007802
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007803 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007804 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007805
7806 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007807 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007808 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007809 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08007810
Eric Anholta07d6782011-03-30 13:01:08 -07007811 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007812 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007813 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007814 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007815
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007816 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07007817 case 5:
7818 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7819 break;
7820 case 7:
7821 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7822 break;
7823 case 10:
7824 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7825 break;
7826 case 14:
7827 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7828 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007829 }
7830
Daniel Vetterb4c09f32013-04-30 14:01:42 +02007831 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007832 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08007833 else
7834 dpll |= PLL_REF_INPUT_DREFCLK;
7835
Daniel Vetter959e16d2013-06-05 13:34:21 +02007836 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007837}
7838
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007839static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
7840 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007841{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007842 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007843 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5bd2013-04-19 11:14:31 +02007844 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03007845 bool ok, has_reduced_clock = false;
Daniel Vetter8b47047bd2013-03-28 10:41:59 +01007846 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007847 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007848
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007849 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08007850
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007851 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7852 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
7853
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007854 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007855 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007856 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007857 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7858 return -EINVAL;
7859 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01007860 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007861 if (!crtc_state->clock_set) {
7862 crtc_state->dpll.n = clock.n;
7863 crtc_state->dpll.m1 = clock.m1;
7864 crtc_state->dpll.m2 = clock.m2;
7865 crtc_state->dpll.p1 = clock.p1;
7866 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007867 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007868
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007869 /* 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 +02007870 if (crtc_state->has_pch_encoder) {
7871 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5bd2013-04-19 11:14:31 +02007872 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007873 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5bd2013-04-19 11:14:31 +02007874
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007875 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5bd2013-04-19 11:14:31 +02007876 &fp, &reduced_clock,
7877 has_reduced_clock ? &fp2 : NULL);
7878
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007879 crtc_state->dpll_hw_state.dpll = dpll;
7880 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007881 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007882 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007883 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007884 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007885
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007886 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007887 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03007888 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007889 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07007890 return -EINVAL;
7891 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02007892 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007893
Rodrigo Viviab585de2015-03-24 12:40:09 -07007894 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007895 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02007896 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007897 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007898
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007899 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007900}
7901
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007902static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7903 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02007904{
7905 struct drm_device *dev = crtc->base.dev;
7906 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007907 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02007908
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007909 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7910 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7911 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7912 & ~TU_SIZE_MASK;
7913 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7914 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7915 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7916}
7917
7918static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7919 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007920 struct intel_link_m_n *m_n,
7921 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007922{
7923 struct drm_device *dev = crtc->base.dev;
7924 struct drm_i915_private *dev_priv = dev->dev_private;
7925 enum pipe pipe = crtc->pipe;
7926
7927 if (INTEL_INFO(dev)->gen >= 5) {
7928 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7929 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7930 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7931 & ~TU_SIZE_MASK;
7932 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7933 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7934 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007935 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7936 * gen < 8) and if DRRS is supported (to make sure the
7937 * registers are not unnecessarily read).
7938 */
7939 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007940 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007941 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7942 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7943 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7944 & ~TU_SIZE_MASK;
7945 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7946 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7947 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7948 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007949 } else {
7950 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7951 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7952 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7953 & ~TU_SIZE_MASK;
7954 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7955 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7956 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7957 }
7958}
7959
7960void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007961 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007962{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007963 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007964 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7965 else
7966 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007967 &pipe_config->dp_m_n,
7968 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007969}
7970
Daniel Vetter72419202013-04-04 13:28:53 +02007971static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007972 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02007973{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007974 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007975 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02007976}
7977
Jesse Barnesbd2e2442014-11-13 17:51:47 +00007978static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007979 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00007980{
7981 struct drm_device *dev = crtc->base.dev;
7982 struct drm_i915_private *dev_priv = dev->dev_private;
7983 uint32_t tmp;
7984
7985 tmp = I915_READ(PS_CTL(crtc->pipe));
7986
7987 if (tmp & PS_ENABLE) {
7988 pipe_config->pch_pfit.enabled = true;
7989 pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
7990 pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
7991 }
7992}
7993
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007994static void
7995skylake_get_initial_plane_config(struct intel_crtc *crtc,
7996 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007997{
7998 struct drm_device *dev = crtc->base.dev;
7999 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f462832015-02-27 11:15:21 +00008000 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008001 int pipe = crtc->pipe;
8002 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008003 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008004 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008005 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008006
Damien Lespiaud9806c92015-01-21 14:07:19 +00008007 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008008 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008009 DRM_DEBUG_KMS("failed to alloc fb\n");
8010 return;
8011 }
8012
Damien Lespiau1b842c82015-01-21 13:50:54 +00008013 fb = &intel_fb->base;
8014
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008015 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008016 if (!(val & PLANE_CTL_ENABLE))
8017 goto error;
8018
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008019 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8020 fourcc = skl_format_to_fourcc(pixel_format,
8021 val & PLANE_CTL_ORDER_RGBX,
8022 val & PLANE_CTL_ALPHA_MASK);
8023 fb->pixel_format = fourcc;
8024 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8025
Damien Lespiau40f462832015-02-27 11:15:21 +00008026 tiling = val & PLANE_CTL_TILED_MASK;
8027 switch (tiling) {
8028 case PLANE_CTL_TILED_LINEAR:
8029 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8030 break;
8031 case PLANE_CTL_TILED_X:
8032 plane_config->tiling = I915_TILING_X;
8033 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8034 break;
8035 case PLANE_CTL_TILED_Y:
8036 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8037 break;
8038 case PLANE_CTL_TILED_YF:
8039 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8040 break;
8041 default:
8042 MISSING_CASE(tiling);
8043 goto error;
8044 }
8045
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008046 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8047 plane_config->base = base;
8048
8049 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8050
8051 val = I915_READ(PLANE_SIZE(pipe, 0));
8052 fb->height = ((val >> 16) & 0xfff) + 1;
8053 fb->width = ((val >> 0) & 0x1fff) + 1;
8054
8055 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f462832015-02-27 11:15:21 +00008056 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8057 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008058 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8059
8060 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008061 fb->pixel_format,
8062 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008063
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008064 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008065
8066 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8067 pipe_name(pipe), fb->width, fb->height,
8068 fb->bits_per_pixel, base, fb->pitches[0],
8069 plane_config->size);
8070
Damien Lespiau2d140302015-02-05 17:22:18 +00008071 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008072 return;
8073
8074error:
8075 kfree(fb);
8076}
8077
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008078static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008079 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008080{
8081 struct drm_device *dev = crtc->base.dev;
8082 struct drm_i915_private *dev_priv = dev->dev_private;
8083 uint32_t tmp;
8084
8085 tmp = I915_READ(PF_CTL(crtc->pipe));
8086
8087 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008088 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008089 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8090 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008091
8092 /* We currently do not free assignements of panel fitters on
8093 * ivb/hsw (since we don't use the higher upscaling modes which
8094 * differentiates them) so just WARN about this case for now. */
8095 if (IS_GEN7(dev)) {
8096 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8097 PF_PIPE_SEL_IVB(crtc->pipe));
8098 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008099 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008100}
8101
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008102static void
8103ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8104 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008105{
8106 struct drm_device *dev = crtc->base.dev;
8107 struct drm_i915_private *dev_priv = dev->dev_private;
8108 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008109 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008110 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008111 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008112 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008113 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008114
Damien Lespiau42a7b082015-02-05 19:35:13 +00008115 val = I915_READ(DSPCNTR(pipe));
8116 if (!(val & DISPLAY_PLANE_ENABLE))
8117 return;
8118
Damien Lespiaud9806c92015-01-21 14:07:19 +00008119 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008120 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008121 DRM_DEBUG_KMS("failed to alloc fb\n");
8122 return;
8123 }
8124
Damien Lespiau1b842c82015-01-21 13:50:54 +00008125 fb = &intel_fb->base;
8126
Daniel Vetter18c52472015-02-10 17:16:09 +00008127 if (INTEL_INFO(dev)->gen >= 4) {
8128 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008129 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008130 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8131 }
8132 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008133
8134 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008135 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008136 fb->pixel_format = fourcc;
8137 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008138
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008139 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008140 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008141 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008142 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00008143 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008144 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008145 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008146 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008147 }
8148 plane_config->base = base;
8149
8150 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008151 fb->width = ((val >> 16) & 0xfff) + 1;
8152 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008153
8154 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008155 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008156
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008157 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008158 fb->pixel_format,
8159 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008160
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008161 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008162
Damien Lespiau2844a922015-01-20 12:51:48 +00008163 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8164 pipe_name(pipe), fb->width, fb->height,
8165 fb->bits_per_pixel, base, fb->pitches[0],
8166 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008167
Damien Lespiau2d140302015-02-05 17:22:18 +00008168 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008169}
8170
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008171static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008172 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008173{
8174 struct drm_device *dev = crtc->base.dev;
8175 struct drm_i915_private *dev_priv = dev->dev_private;
8176 uint32_t tmp;
8177
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008178 if (!intel_display_power_is_enabled(dev_priv,
8179 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008180 return false;
8181
Daniel Vettere143a212013-07-04 12:01:15 +02008182 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008183 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008184
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008185 tmp = I915_READ(PIPECONF(crtc->pipe));
8186 if (!(tmp & PIPECONF_ENABLE))
8187 return false;
8188
Ville Syrjälä42571aef2013-09-06 23:29:00 +03008189 switch (tmp & PIPECONF_BPC_MASK) {
8190 case PIPECONF_6BPC:
8191 pipe_config->pipe_bpp = 18;
8192 break;
8193 case PIPECONF_8BPC:
8194 pipe_config->pipe_bpp = 24;
8195 break;
8196 case PIPECONF_10BPC:
8197 pipe_config->pipe_bpp = 30;
8198 break;
8199 case PIPECONF_12BPC:
8200 pipe_config->pipe_bpp = 36;
8201 break;
8202 default:
8203 break;
8204 }
8205
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008206 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8207 pipe_config->limited_color_range = true;
8208
Daniel Vetterab9412b2013-05-03 11:49:46 +02008209 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008210 struct intel_shared_dpll *pll;
8211
Daniel Vetter88adfff2013-03-28 10:42:01 +01008212 pipe_config->has_pch_encoder = true;
8213
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008214 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8215 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8216 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008217
8218 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008219
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008220 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02008221 pipe_config->shared_dpll =
8222 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008223 } else {
8224 tmp = I915_READ(PCH_DPLL_SEL);
8225 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8226 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8227 else
8228 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8229 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008230
8231 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8232
8233 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8234 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008235
8236 tmp = pipe_config->dpll_hw_state.dpll;
8237 pipe_config->pixel_multiplier =
8238 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8239 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008240
8241 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008242 } else {
8243 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008244 }
8245
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008246 intel_get_pipe_timings(crtc, pipe_config);
8247
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008248 ironlake_get_pfit_config(crtc, pipe_config);
8249
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008250 return true;
8251}
8252
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008253static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8254{
8255 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008256 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008257
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008258 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008259 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008260 pipe_name(crtc->pipe));
8261
Rob Clarke2c719b2014-12-15 13:56:32 -05008262 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8263 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8264 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8265 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8266 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8267 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008268 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03008269 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05008270 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008271 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008272 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008273 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008274 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008275 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008276 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008277
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008278 /*
8279 * In theory we can still leave IRQs enabled, as long as only the HPD
8280 * interrupts remain enabled. We used to check for that, but since it's
8281 * gen-specific and since we only disable LCPLL after we fully disable
8282 * the interrupts, the check below should be enough.
8283 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008284 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008285}
8286
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008287static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8288{
8289 struct drm_device *dev = dev_priv->dev;
8290
8291 if (IS_HASWELL(dev))
8292 return I915_READ(D_COMP_HSW);
8293 else
8294 return I915_READ(D_COMP_BDW);
8295}
8296
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008297static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8298{
8299 struct drm_device *dev = dev_priv->dev;
8300
8301 if (IS_HASWELL(dev)) {
8302 mutex_lock(&dev_priv->rps.hw_lock);
8303 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8304 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03008305 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008306 mutex_unlock(&dev_priv->rps.hw_lock);
8307 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008308 I915_WRITE(D_COMP_BDW, val);
8309 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008310 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008311}
8312
8313/*
8314 * This function implements pieces of two sequences from BSpec:
8315 * - Sequence for display software to disable LCPLL
8316 * - Sequence for display software to allow package C8+
8317 * The steps implemented here are just the steps that actually touch the LCPLL
8318 * register. Callers should take care of disabling all the display engine
8319 * functions, doing the mode unset, fixing interrupts, etc.
8320 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008321static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8322 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008323{
8324 uint32_t val;
8325
8326 assert_can_disable_lcpll(dev_priv);
8327
8328 val = I915_READ(LCPLL_CTL);
8329
8330 if (switch_to_fclk) {
8331 val |= LCPLL_CD_SOURCE_FCLK;
8332 I915_WRITE(LCPLL_CTL, val);
8333
8334 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
8335 LCPLL_CD_SOURCE_FCLK_DONE, 1))
8336 DRM_ERROR("Switching to FCLK failed\n");
8337
8338 val = I915_READ(LCPLL_CTL);
8339 }
8340
8341 val |= LCPLL_PLL_DISABLE;
8342 I915_WRITE(LCPLL_CTL, val);
8343 POSTING_READ(LCPLL_CTL);
8344
8345 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
8346 DRM_ERROR("LCPLL still locked\n");
8347
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008348 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008349 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008350 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008351 ndelay(100);
8352
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008353 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8354 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008355 DRM_ERROR("D_COMP RCOMP still in progress\n");
8356
8357 if (allow_power_down) {
8358 val = I915_READ(LCPLL_CTL);
8359 val |= LCPLL_POWER_DOWN_ALLOW;
8360 I915_WRITE(LCPLL_CTL, val);
8361 POSTING_READ(LCPLL_CTL);
8362 }
8363}
8364
8365/*
8366 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8367 * source.
8368 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008369static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008370{
8371 uint32_t val;
8372
8373 val = I915_READ(LCPLL_CTL);
8374
8375 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8376 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8377 return;
8378
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008379 /*
8380 * Make sure we're not on PC8 state before disabling PC8, otherwise
8381 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008382 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02008383 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03008384
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008385 if (val & LCPLL_POWER_DOWN_ALLOW) {
8386 val &= ~LCPLL_POWER_DOWN_ALLOW;
8387 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02008388 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008389 }
8390
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008391 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008392 val |= D_COMP_COMP_FORCE;
8393 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008394 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008395
8396 val = I915_READ(LCPLL_CTL);
8397 val &= ~LCPLL_PLL_DISABLE;
8398 I915_WRITE(LCPLL_CTL, val);
8399
8400 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
8401 DRM_ERROR("LCPLL not locked yet\n");
8402
8403 if (val & LCPLL_CD_SOURCE_FCLK) {
8404 val = I915_READ(LCPLL_CTL);
8405 val &= ~LCPLL_CD_SOURCE_FCLK;
8406 I915_WRITE(LCPLL_CTL, val);
8407
8408 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
8409 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
8410 DRM_ERROR("Switching back to LCPLL failed\n");
8411 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03008412
Mika Kuoppala59bad942015-01-16 11:34:40 +02008413 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008414}
8415
Paulo Zanoni765dab672014-03-07 20:08:18 -03008416/*
8417 * Package states C8 and deeper are really deep PC states that can only be
8418 * reached when all the devices on the system allow it, so even if the graphics
8419 * device allows PC8+, it doesn't mean the system will actually get to these
8420 * states. Our driver only allows PC8+ when going into runtime PM.
8421 *
8422 * The requirements for PC8+ are that all the outputs are disabled, the power
8423 * well is disabled and most interrupts are disabled, and these are also
8424 * requirements for runtime PM. When these conditions are met, we manually do
8425 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8426 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8427 * hang the machine.
8428 *
8429 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8430 * the state of some registers, so when we come back from PC8+ we need to
8431 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8432 * need to take care of the registers kept by RC6. Notice that this happens even
8433 * if we don't put the device in PCI D3 state (which is what currently happens
8434 * because of the runtime PM support).
8435 *
8436 * For more, read "Display Sequences for Package C8" on the hardware
8437 * documentation.
8438 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008439void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008440{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008441 struct drm_device *dev = dev_priv->dev;
8442 uint32_t val;
8443
Paulo Zanonic67a4702013-08-19 13:18:09 -03008444 DRM_DEBUG_KMS("Enabling package C8+\n");
8445
Paulo Zanonic67a4702013-08-19 13:18:09 -03008446 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8447 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8448 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8449 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8450 }
8451
8452 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008453 hsw_disable_lcpll(dev_priv, true, true);
8454}
8455
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008456void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008457{
8458 struct drm_device *dev = dev_priv->dev;
8459 uint32_t val;
8460
Paulo Zanonic67a4702013-08-19 13:18:09 -03008461 DRM_DEBUG_KMS("Disabling package C8+\n");
8462
8463 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008464 lpt_init_pch_refclk(dev);
8465
8466 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8467 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8468 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8469 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8470 }
8471
8472 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008473}
8474
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008475static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8476 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008477{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008478 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008479 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03008480
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008481 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02008482
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008483 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008484}
8485
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008486static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8487 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008488 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008489{
Damien Lespiau3148ade2014-11-21 16:14:56 +00008490 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008491
8492 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
8493 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
8494
8495 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00008496 case SKL_DPLL0:
8497 /*
8498 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
8499 * of the shared DPLL framework and thus needs to be read out
8500 * separately
8501 */
8502 dpll_ctl1 = I915_READ(DPLL_CTRL1);
8503 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
8504 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008505 case SKL_DPLL1:
8506 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
8507 break;
8508 case SKL_DPLL2:
8509 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
8510 break;
8511 case SKL_DPLL3:
8512 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
8513 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008514 }
8515}
8516
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008517static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8518 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008519 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008520{
8521 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8522
8523 switch (pipe_config->ddi_pll_sel) {
8524 case PORT_CLK_SEL_WRPLL1:
8525 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
8526 break;
8527 case PORT_CLK_SEL_WRPLL2:
8528 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
8529 break;
8530 }
8531}
8532
Daniel Vetter26804af2014-06-25 22:01:55 +03008533static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008534 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03008535{
8536 struct drm_device *dev = crtc->base.dev;
8537 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03008538 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03008539 enum port port;
8540 uint32_t tmp;
8541
8542 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
8543
8544 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
8545
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008546 if (IS_SKYLAKE(dev))
8547 skylake_get_ddi_pll(dev_priv, port, pipe_config);
8548 else
8549 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03008550
Daniel Vetterd452c5b2014-07-04 11:27:39 -03008551 if (pipe_config->shared_dpll >= 0) {
8552 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8553
8554 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8555 &pipe_config->dpll_hw_state));
8556 }
8557
Daniel Vetter26804af2014-06-25 22:01:55 +03008558 /*
8559 * Haswell has only FDI/PCH transcoder A. It is which is connected to
8560 * DDI E. So just check whether this pipe is wired to DDI E and whether
8561 * the PCH transcoder is on.
8562 */
Damien Lespiauca370452013-12-03 13:56:24 +00008563 if (INTEL_INFO(dev)->gen < 9 &&
8564 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03008565 pipe_config->has_pch_encoder = true;
8566
8567 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
8568 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8569 FDI_DP_PORT_WIDTH_SHIFT) + 1;
8570
8571 ironlake_get_fdi_m_n_config(crtc, pipe_config);
8572 }
8573}
8574
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008575static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008576 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008577{
8578 struct drm_device *dev = crtc->base.dev;
8579 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008580 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008581 uint32_t tmp;
8582
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008583 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02008584 POWER_DOMAIN_PIPE(crtc->pipe)))
8585 return false;
8586
Daniel Vettere143a212013-07-04 12:01:15 +02008587 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008588 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
8589
Daniel Vettereccb1402013-05-22 00:50:22 +02008590 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8591 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8592 enum pipe trans_edp_pipe;
8593 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8594 default:
8595 WARN(1, "unknown pipe linked to edp transcoder\n");
8596 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8597 case TRANS_DDI_EDP_INPUT_A_ON:
8598 trans_edp_pipe = PIPE_A;
8599 break;
8600 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8601 trans_edp_pipe = PIPE_B;
8602 break;
8603 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8604 trans_edp_pipe = PIPE_C;
8605 break;
8606 }
8607
8608 if (trans_edp_pipe == crtc->pipe)
8609 pipe_config->cpu_transcoder = TRANSCODER_EDP;
8610 }
8611
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008612 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02008613 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03008614 return false;
8615
Daniel Vettereccb1402013-05-22 00:50:22 +02008616 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008617 if (!(tmp & PIPECONF_ENABLE))
8618 return false;
8619
Daniel Vetter26804af2014-06-25 22:01:55 +03008620 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008621
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008622 intel_get_pipe_timings(crtc, pipe_config);
8623
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008624 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008625 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
8626 if (IS_SKYLAKE(dev))
8627 skylake_get_pfit_config(crtc, pipe_config);
8628 else
8629 ironlake_get_pfit_config(crtc, pipe_config);
8630 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01008631
Jesse Barnese59150d2014-01-07 13:30:45 -08008632 if (IS_HASWELL(dev))
8633 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
8634 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008635
Clint Taylorebb69c92014-09-30 10:30:22 -07008636 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
8637 pipe_config->pixel_multiplier =
8638 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
8639 } else {
8640 pipe_config->pixel_multiplier = 1;
8641 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008642
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008643 return true;
8644}
8645
Chris Wilson560b85b2010-08-07 11:01:38 +01008646static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8647{
8648 struct drm_device *dev = crtc->dev;
8649 struct drm_i915_private *dev_priv = dev->dev_private;
8650 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03008651 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01008652
Ville Syrjälädc41c152014-08-13 11:57:05 +03008653 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08008654 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
8655 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03008656 unsigned int stride = roundup_pow_of_two(width) * 4;
8657
8658 switch (stride) {
8659 default:
8660 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8661 width, stride);
8662 stride = 256;
8663 /* fallthrough */
8664 case 256:
8665 case 512:
8666 case 1024:
8667 case 2048:
8668 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008669 }
8670
Ville Syrjälädc41c152014-08-13 11:57:05 +03008671 cntl |= CURSOR_ENABLE |
8672 CURSOR_GAMMA_ENABLE |
8673 CURSOR_FORMAT_ARGB |
8674 CURSOR_STRIDE(stride);
8675
8676 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008677 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008678
Ville Syrjälädc41c152014-08-13 11:57:05 +03008679 if (intel_crtc->cursor_cntl != 0 &&
8680 (intel_crtc->cursor_base != base ||
8681 intel_crtc->cursor_size != size ||
8682 intel_crtc->cursor_cntl != cntl)) {
8683 /* On these chipsets we can only modify the base/size/stride
8684 * whilst the cursor is disabled.
8685 */
8686 I915_WRITE(_CURACNTR, 0);
8687 POSTING_READ(_CURACNTR);
8688 intel_crtc->cursor_cntl = 0;
8689 }
8690
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008691 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03008692 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008693 intel_crtc->cursor_base = base;
8694 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03008695
8696 if (intel_crtc->cursor_size != size) {
8697 I915_WRITE(CURSIZE, size);
8698 intel_crtc->cursor_size = size;
8699 }
8700
Chris Wilson4b0e3332014-05-30 16:35:26 +03008701 if (intel_crtc->cursor_cntl != cntl) {
8702 I915_WRITE(_CURACNTR, cntl);
8703 POSTING_READ(_CURACNTR);
8704 intel_crtc->cursor_cntl = cntl;
8705 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008706}
8707
8708static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
8709{
8710 struct drm_device *dev = crtc->dev;
8711 struct drm_i915_private *dev_priv = dev->dev_private;
8712 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8713 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008714 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01008715
Chris Wilson4b0e3332014-05-30 16:35:26 +03008716 cntl = 0;
8717 if (base) {
8718 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08008719 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b02014-03-10 17:06:23 +05308720 case 64:
8721 cntl |= CURSOR_MODE_64_ARGB_AX;
8722 break;
8723 case 128:
8724 cntl |= CURSOR_MODE_128_ARGB_AX;
8725 break;
8726 case 256:
8727 cntl |= CURSOR_MODE_256_ARGB_AX;
8728 break;
8729 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08008730 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b02014-03-10 17:06:23 +05308731 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01008732 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008733 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03008734
8735 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8736 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01008737 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008738
Matt Roper8e7d6882015-01-21 16:35:41 -08008739 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008740 cntl |= CURSOR_ROTATE_180;
8741
Chris Wilson4b0e3332014-05-30 16:35:26 +03008742 if (intel_crtc->cursor_cntl != cntl) {
8743 I915_WRITE(CURCNTR(pipe), cntl);
8744 POSTING_READ(CURCNTR(pipe));
8745 intel_crtc->cursor_cntl = cntl;
8746 }
8747
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008748 /* and commit changes on next vblank */
Ville Syrjälä5efb3e282014-04-09 13:28:53 +03008749 I915_WRITE(CURBASE(pipe), base);
8750 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008751
8752 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008753}
8754
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008755/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01008756static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8757 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008758{
8759 struct drm_device *dev = crtc->dev;
8760 struct drm_i915_private *dev_priv = dev->dev_private;
8761 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8762 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07008763 int x = crtc->cursor_x;
8764 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008765 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008766
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008767 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008768 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008769
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008770 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008771 base = 0;
8772
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008773 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008774 base = 0;
8775
8776 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08008777 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008778 base = 0;
8779
8780 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8781 x = -x;
8782 }
8783 pos |= x << CURSOR_X_SHIFT;
8784
8785 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08008786 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008787 base = 0;
8788
8789 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8790 y = -y;
8791 }
8792 pos |= y << CURSOR_Y_SHIFT;
8793
Chris Wilson4b0e3332014-05-30 16:35:26 +03008794 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008795 return;
8796
Ville Syrjälä5efb3e282014-04-09 13:28:53 +03008797 I915_WRITE(CURPOS(pipe), pos);
8798
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008799 /* ILK+ do this automagically */
8800 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08008801 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08008802 base += (intel_crtc->base.cursor->state->crtc_h *
8803 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008804 }
8805
Ville Syrjälä8ac54662014-08-12 19:39:54 +03008806 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e282014-04-09 13:28:53 +03008807 i845_update_cursor(crtc, base);
8808 else
8809 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008810}
8811
Ville Syrjälädc41c152014-08-13 11:57:05 +03008812static bool cursor_size_ok(struct drm_device *dev,
8813 uint32_t width, uint32_t height)
8814{
8815 if (width == 0 || height == 0)
8816 return false;
8817
8818 /*
8819 * 845g/865g are special in that they are only limited by
8820 * the width of their cursors, the height is arbitrary up to
8821 * the precision of the register. Everything else requires
8822 * square cursors, limited to a few power-of-two sizes.
8823 */
8824 if (IS_845G(dev) || IS_I865G(dev)) {
8825 if ((width & 63) != 0)
8826 return false;
8827
8828 if (width > (IS_845G(dev) ? 64 : 512))
8829 return false;
8830
8831 if (height > 1023)
8832 return false;
8833 } else {
8834 switch (width | height) {
8835 case 256:
8836 case 128:
8837 if (IS_GEN2(dev))
8838 return false;
8839 case 64:
8840 break;
8841 default:
8842 return false;
8843 }
8844 }
8845
8846 return true;
8847}
8848
Jesse Barnes79e53942008-11-07 14:24:08 -08008849static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01008850 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08008851{
James Simmons72034252010-08-03 01:33:19 +01008852 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08008853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008854
James Simmons72034252010-08-03 01:33:19 +01008855 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008856 intel_crtc->lut_r[i] = red[i] >> 8;
8857 intel_crtc->lut_g[i] = green[i] >> 8;
8858 intel_crtc->lut_b[i] = blue[i] >> 8;
8859 }
8860
8861 intel_crtc_load_lut(crtc);
8862}
8863
Jesse Barnes79e53942008-11-07 14:24:08 -08008864/* VESA 640x480x72Hz mode to set on the pipe */
8865static struct drm_display_mode load_detect_mode = {
8866 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8867 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8868};
8869
Daniel Vettera8bb6812014-02-10 18:00:39 +01008870struct drm_framebuffer *
8871__intel_framebuffer_create(struct drm_device *dev,
8872 struct drm_mode_fb_cmd2 *mode_cmd,
8873 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01008874{
8875 struct intel_framebuffer *intel_fb;
8876 int ret;
8877
8878 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8879 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03008880 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01008881 return ERR_PTR(-ENOMEM);
8882 }
8883
8884 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008885 if (ret)
8886 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01008887
8888 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008889err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03008890 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008891 kfree(intel_fb);
8892
8893 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01008894}
8895
Daniel Vetterb5ea6422014-03-02 21:18:00 +01008896static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01008897intel_framebuffer_create(struct drm_device *dev,
8898 struct drm_mode_fb_cmd2 *mode_cmd,
8899 struct drm_i915_gem_object *obj)
8900{
8901 struct drm_framebuffer *fb;
8902 int ret;
8903
8904 ret = i915_mutex_lock_interruptible(dev);
8905 if (ret)
8906 return ERR_PTR(ret);
8907 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8908 mutex_unlock(&dev->struct_mutex);
8909
8910 return fb;
8911}
8912
Chris Wilsond2dff872011-04-19 08:36:26 +01008913static u32
8914intel_framebuffer_pitch_for_width(int width, int bpp)
8915{
8916 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8917 return ALIGN(pitch, 64);
8918}
8919
8920static u32
8921intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8922{
8923 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a26b2014-07-01 20:39:41 +02008924 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01008925}
8926
8927static struct drm_framebuffer *
8928intel_framebuffer_create_for_mode(struct drm_device *dev,
8929 struct drm_display_mode *mode,
8930 int depth, int bpp)
8931{
8932 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00008933 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01008934
8935 obj = i915_gem_alloc_object(dev,
8936 intel_framebuffer_size_for_mode(mode, bpp));
8937 if (obj == NULL)
8938 return ERR_PTR(-ENOMEM);
8939
8940 mode_cmd.width = mode->hdisplay;
8941 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008942 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8943 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00008944 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01008945
8946 return intel_framebuffer_create(dev, &mode_cmd, obj);
8947}
8948
8949static struct drm_framebuffer *
8950mode_fits_in_fbdev(struct drm_device *dev,
8951 struct drm_display_mode *mode)
8952{
Daniel Vetter4520f532013-10-09 09:18:51 +02008953#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01008954 struct drm_i915_private *dev_priv = dev->dev_private;
8955 struct drm_i915_gem_object *obj;
8956 struct drm_framebuffer *fb;
8957
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008958 if (!dev_priv->fbdev)
8959 return NULL;
8960
8961 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01008962 return NULL;
8963
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008964 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008965 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01008966
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008967 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008968 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8969 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01008970 return NULL;
8971
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008972 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01008973 return NULL;
8974
8975 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02008976#else
8977 return NULL;
8978#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01008979}
8980
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008981bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01008982 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05008983 struct intel_load_detect_pipe *old,
8984 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08008985{
8986 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008987 struct intel_encoder *intel_encoder =
8988 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08008989 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008990 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08008991 struct drm_crtc *crtc = NULL;
8992 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02008993 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05008994 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02008995 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02008996 struct drm_connector_state *connector_state;
Rob Clark51fd3712013-11-19 12:10:12 -05008997 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008998
Chris Wilsond2dff872011-04-19 08:36:26 +01008999 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009000 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009001 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009002
Rob Clark51fd3712013-11-19 12:10:12 -05009003retry:
9004 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9005 if (ret)
9006 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009007
Jesse Barnes79e53942008-11-07 14:24:08 -08009008 /*
9009 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009010 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009011 * - if the connector already has an assigned crtc, use it (but make
9012 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009013 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009014 * - try to find the first unused crtc that can drive this connector,
9015 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009016 */
9017
9018 /* See if we already have a CRTC for this connector */
9019 if (encoder->crtc) {
9020 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009021
Rob Clark51fd3712013-11-19 12:10:12 -05009022 ret = drm_modeset_lock(&crtc->mutex, ctx);
9023 if (ret)
9024 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009025 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9026 if (ret)
9027 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01009028
Daniel Vetter24218aa2012-08-12 19:27:11 +02009029 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009030 old->load_detect_temp = false;
9031
9032 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009033 if (connector->dpms != DRM_MODE_DPMS_ON)
9034 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01009035
Chris Wilson71731882011-04-19 23:10:58 +01009036 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08009037 }
9038
9039 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009040 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009041 i++;
9042 if (!(encoder->possible_crtcs & (1 << i)))
9043 continue;
Matt Roper83d65732015-02-25 13:12:16 -08009044 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +03009045 continue;
9046 /* This can occur when applying the pipe A quirk on resume. */
9047 if (to_intel_crtc(possible_crtc)->new_enabled)
9048 continue;
9049
9050 crtc = possible_crtc;
9051 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009052 }
9053
9054 /*
9055 * If we didn't find an unused CRTC, don't use any.
9056 */
9057 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01009058 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05009059 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08009060 }
9061
Rob Clark51fd3712013-11-19 12:10:12 -05009062 ret = drm_modeset_lock(&crtc->mutex, ctx);
9063 if (ret)
9064 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009065 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9066 if (ret)
9067 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02009068 intel_encoder->new_crtc = to_intel_crtc(crtc);
9069 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009070
9071 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009072 intel_crtc->new_enabled = true;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009073 intel_crtc->new_config = intel_crtc->config;
Daniel Vetter24218aa2012-08-12 19:27:11 +02009074 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009075 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01009076 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08009077
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009078 state = drm_atomic_state_alloc(dev);
9079 if (!state)
9080 return false;
9081
9082 state->acquire_ctx = ctx;
9083
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009084 connector_state = drm_atomic_get_connector_state(state, connector);
9085 if (IS_ERR(connector_state)) {
9086 ret = PTR_ERR(connector_state);
9087 goto fail;
9088 }
9089
9090 connector_state->crtc = crtc;
9091 connector_state->best_encoder = &intel_encoder->base;
9092
Chris Wilson64927112011-04-20 07:25:26 +01009093 if (!mode)
9094 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08009095
Chris Wilsond2dff872011-04-19 08:36:26 +01009096 /* We need a framebuffer large enough to accommodate all accesses
9097 * that the plane may generate whilst we perform load detection.
9098 * We can not rely on the fbcon either being present (we get called
9099 * during its initialisation to detect all boot displays, or it may
9100 * not even exist) or that it is large enough to satisfy the
9101 * requested mode.
9102 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02009103 fb = mode_fits_in_fbdev(dev, mode);
9104 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009105 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009106 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9107 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009108 } else
9109 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009110 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009111 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009112 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009113 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009114
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009115 if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
Chris Wilson64927112011-04-20 07:25:26 +01009116 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01009117 if (old->release_fb)
9118 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009119 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009120 }
Daniel Vetter9128b042015-03-03 17:31:21 +01009121 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +01009122
Jesse Barnes79e53942008-11-07 14:24:08 -08009123 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009124 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01009125 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009126
9127 fail:
Matt Roper83d65732015-02-25 13:12:16 -08009128 intel_crtc->new_enabled = crtc->state->enable;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009129 if (intel_crtc->new_enabled)
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009130 intel_crtc->new_config = intel_crtc->config;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009131 else
9132 intel_crtc->new_config = NULL;
Rob Clark51fd3712013-11-19 12:10:12 -05009133fail_unlock:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009134 if (state) {
9135 drm_atomic_state_free(state);
9136 state = NULL;
9137 }
9138
Rob Clark51fd3712013-11-19 12:10:12 -05009139 if (ret == -EDEADLK) {
9140 drm_modeset_backoff(ctx);
9141 goto retry;
9142 }
9143
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009144 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009145}
9146
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009147void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +02009148 struct intel_load_detect_pipe *old,
9149 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009150{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009151 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009152 struct intel_encoder *intel_encoder =
9153 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01009154 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01009155 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009157 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009158 struct drm_connector_state *connector_state;
Jesse Barnes79e53942008-11-07 14:24:08 -08009159
Chris Wilsond2dff872011-04-19 08:36:26 +01009160 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009161 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009162 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009163
Chris Wilson8261b192011-04-19 23:18:09 +01009164 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009165 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009166 if (!state)
9167 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009168
9169 state->acquire_ctx = ctx;
9170
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009171 connector_state = drm_atomic_get_connector_state(state, connector);
9172 if (IS_ERR(connector_state))
9173 goto fail;
9174
Daniel Vetterfc303102012-07-09 10:40:58 +02009175 to_intel_connector(connector)->new_encoder = NULL;
9176 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009177 intel_crtc->new_enabled = false;
9178 intel_crtc->new_config = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009179
9180 connector_state->best_encoder = NULL;
9181 connector_state->crtc = NULL;
9182
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009183 intel_set_mode(crtc, NULL, 0, 0, NULL, state);
9184
9185 drm_atomic_state_free(state);
Chris Wilsond2dff872011-04-19 08:36:26 +01009186
Daniel Vetter36206362012-12-10 20:42:17 +01009187 if (old->release_fb) {
9188 drm_framebuffer_unregister_private(old->release_fb);
9189 drm_framebuffer_unreference(old->release_fb);
9190 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009191
Chris Wilson0622a532011-04-21 09:32:11 +01009192 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009193 }
9194
Eric Anholtc751ce42010-03-25 11:48:48 -07009195 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009196 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9197 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009198
9199 return;
9200fail:
9201 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9202 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -08009203}
9204
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009205static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009206 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009207{
9208 struct drm_i915_private *dev_priv = dev->dev_private;
9209 u32 dpll = pipe_config->dpll_hw_state.dpll;
9210
9211 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009212 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009213 else if (HAS_PCH_SPLIT(dev))
9214 return 120000;
9215 else if (!IS_GEN2(dev))
9216 return 96000;
9217 else
9218 return 48000;
9219}
9220
Jesse Barnes79e53942008-11-07 14:24:08 -08009221/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009222static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009223 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08009224{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009225 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08009226 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009227 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009228 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08009229 u32 fp;
9230 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009231 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08009232
9233 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03009234 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009235 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03009236 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009237
9238 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009239 if (IS_PINEVIEW(dev)) {
9240 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9241 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08009242 } else {
9243 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9244 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9245 }
9246
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009247 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009248 if (IS_PINEVIEW(dev))
9249 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9250 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08009251 else
9252 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08009253 DPLL_FPA01_P1_POST_DIV_SHIFT);
9254
9255 switch (dpll & DPLL_MODE_MASK) {
9256 case DPLLB_MODE_DAC_SERIAL:
9257 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9258 5 : 10;
9259 break;
9260 case DPLLB_MODE_LVDS:
9261 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9262 7 : 14;
9263 break;
9264 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08009265 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08009266 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009267 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009268 }
9269
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009270 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009271 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009272 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009273 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009274 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02009275 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009276 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08009277
9278 if (is_lvds) {
9279 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
9280 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009281
9282 if (lvds & LVDS_CLKB_POWER_UP)
9283 clock.p2 = 7;
9284 else
9285 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08009286 } else {
9287 if (dpll & PLL_P1_DIVIDE_BY_TWO)
9288 clock.p1 = 2;
9289 else {
9290 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
9291 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
9292 }
9293 if (dpll & PLL_P2_DIVIDE_BY_4)
9294 clock.p2 = 4;
9295 else
9296 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08009297 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009298
9299 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009300 }
9301
Ville Syrjälä18442d02013-09-13 16:00:08 +03009302 /*
9303 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01009304 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03009305 * encoder's get_config() function.
9306 */
9307 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009308}
9309
Ville Syrjälä6878da02013-09-13 15:59:11 +03009310int intel_dotclock_calculate(int link_freq,
9311 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009312{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009313 /*
9314 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009315 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009316 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009317 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009318 *
9319 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009320 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08009321 */
9322
Ville Syrjälä6878da02013-09-13 15:59:11 +03009323 if (!m_n->link_n)
9324 return 0;
9325
9326 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
9327}
9328
Ville Syrjälä18442d02013-09-13 16:00:08 +03009329static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009330 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03009331{
9332 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009333
9334 /* read out port_clock from the DPLL */
9335 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03009336
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009337 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03009338 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01009339 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03009340 * agree once we know their relationship in the encoder's
9341 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009342 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02009343 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03009344 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
9345 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08009346}
9347
9348/** Returns the currently programmed mode of the given pipe. */
9349struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9350 struct drm_crtc *crtc)
9351{
Jesse Barnes548f2452011-02-17 10:40:53 -08009352 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08009353 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009354 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009355 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009356 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02009357 int htot = I915_READ(HTOTAL(cpu_transcoder));
9358 int hsync = I915_READ(HSYNC(cpu_transcoder));
9359 int vtot = I915_READ(VTOTAL(cpu_transcoder));
9360 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03009361 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08009362
9363 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
9364 if (!mode)
9365 return NULL;
9366
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009367 /*
9368 * Construct a pipe_config sufficient for getting the clock info
9369 * back out of crtc_clock_get.
9370 *
9371 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
9372 * to use a real value here instead.
9373 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03009374 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009375 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009376 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
9377 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
9378 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009379 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
9380
Ville Syrjälä773ae032013-09-23 17:48:20 +03009381 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08009382 mode->hdisplay = (htot & 0xffff) + 1;
9383 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
9384 mode->hsync_start = (hsync & 0xffff) + 1;
9385 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
9386 mode->vdisplay = (vtot & 0xffff) + 1;
9387 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
9388 mode->vsync_start = (vsync & 0xffff) + 1;
9389 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
9390
9391 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08009392
9393 return mode;
9394}
9395
Jesse Barnes652c3932009-08-17 13:31:43 -07009396static void intel_decrease_pllclock(struct drm_crtc *crtc)
9397{
9398 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03009399 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07009400 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009401
Sonika Jindalbaff2962014-07-22 11:16:35 +05309402 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07009403 return;
9404
9405 if (!dev_priv->lvds_downclock_avail)
9406 return;
9407
9408 /*
9409 * Since this is called by a timer, we should never get here in
9410 * the manual case.
9411 */
9412 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01009413 int pipe = intel_crtc->pipe;
9414 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02009415 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01009416
Zhao Yakui44d98a62009-10-09 11:39:40 +08009417 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009418
Sean Paul8ac5a6d2012-02-13 13:14:51 -05009419 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009420
Chris Wilson074b5e12012-05-02 12:07:06 +01009421 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07009422 dpll |= DISPLAY_RATE_SELECT_FPA1;
9423 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009424 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009425 dpll = I915_READ(dpll_reg);
9426 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08009427 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009428 }
9429
9430}
9431
Chris Wilsonf047e392012-07-21 12:31:41 +01009432void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07009433{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009434 struct drm_i915_private *dev_priv = dev->dev_private;
9435
Chris Wilsonf62a0072014-02-21 17:55:39 +00009436 if (dev_priv->mm.busy)
9437 return;
9438
Paulo Zanoni43694d62014-03-07 20:08:08 -03009439 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009440 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +00009441 if (INTEL_INFO(dev)->gen >= 6)
9442 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +00009443 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +01009444}
9445
9446void intel_mark_idle(struct drm_device *dev)
9447{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009448 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00009449 struct drm_crtc *crtc;
9450
Chris Wilsonf62a0072014-02-21 17:55:39 +00009451 if (!dev_priv->mm.busy)
9452 return;
9453
9454 dev_priv->mm.busy = false;
9455
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009456 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07009457 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +00009458 continue;
9459
9460 intel_decrease_pllclock(crtc);
9461 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009462
Damien Lespiau3d13ef22014-02-07 19:12:47 +00009463 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009464 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03009465
Paulo Zanoni43694d62014-03-07 20:08:08 -03009466 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01009467}
9468
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009469static void intel_crtc_set_state(struct intel_crtc *crtc,
9470 struct intel_crtc_state *crtc_state)
9471{
9472 kfree(crtc->config);
9473 crtc->config = crtc_state;
Ander Conselvan de Oliveira16f3f652015-01-15 14:55:27 +02009474 crtc->base.state = &crtc_state->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009475}
9476
Jesse Barnes79e53942008-11-07 14:24:08 -08009477static void intel_crtc_destroy(struct drm_crtc *crtc)
9478{
9479 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009480 struct drm_device *dev = crtc->dev;
9481 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +02009482
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009483 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009484 work = intel_crtc->unpin_work;
9485 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009486 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009487
9488 if (work) {
9489 cancel_work_sync(&work->work);
9490 kfree(work);
9491 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009492
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009493 intel_crtc_set_state(intel_crtc, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08009494 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009495
Jesse Barnes79e53942008-11-07 14:24:08 -08009496 kfree(intel_crtc);
9497}
9498
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009499static void intel_unpin_work_fn(struct work_struct *__work)
9500{
9501 struct intel_unpin_work *work =
9502 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009503 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +02009504 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009505
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009506 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00009507 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +00009508 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00009509
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02009510 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +00009511
9512 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +00009513 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009514 mutex_unlock(&dev->struct_mutex);
9515
Daniel Vetterf99d7062014-06-19 16:01:59 +02009516 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +00009517 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +02009518
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009519 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9520 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9521
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009522 kfree(work);
9523}
9524
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009525static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01009526 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009527{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009528 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9529 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009530 unsigned long flags;
9531
9532 /* Ignore early vblank irqs */
9533 if (intel_crtc == NULL)
9534 return;
9535
Daniel Vetterf3260382014-09-15 14:55:23 +02009536 /*
9537 * This is called both by irq handlers and the reset code (to complete
9538 * lost pageflips) so needs the full irqsave spinlocks.
9539 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009540 spin_lock_irqsave(&dev->event_lock, flags);
9541 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00009542
9543 /* Ensure we don't miss a work->pending update ... */
9544 smp_rmb();
9545
9546 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009547 spin_unlock_irqrestore(&dev->event_lock, flags);
9548 return;
9549 }
9550
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009551 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01009552
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009553 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009554}
9555
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009556void intel_finish_page_flip(struct drm_device *dev, int pipe)
9557{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009558 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009559 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9560
Mario Kleiner49b14a52010-12-09 07:00:07 +01009561 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009562}
9563
9564void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9565{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009566 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009567 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9568
Mario Kleiner49b14a52010-12-09 07:00:07 +01009569 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009570}
9571
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009572/* Is 'a' after or equal to 'b'? */
9573static bool g4x_flip_count_after_eq(u32 a, u32 b)
9574{
9575 return !((a - b) & 0x80000000);
9576}
9577
9578static bool page_flip_finished(struct intel_crtc *crtc)
9579{
9580 struct drm_device *dev = crtc->base.dev;
9581 struct drm_i915_private *dev_priv = dev->dev_private;
9582
Ville Syrjäläbdfa7542014-05-27 21:33:09 +03009583 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
9584 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
9585 return true;
9586
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009587 /*
9588 * The relevant registers doen't exist on pre-ctg.
9589 * As the flip done interrupt doesn't trigger for mmio
9590 * flips on gmch platforms, a flip count check isn't
9591 * really needed there. But since ctg has the registers,
9592 * include it in the check anyway.
9593 */
9594 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9595 return true;
9596
9597 /*
9598 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9599 * used the same base address. In that case the mmio flip might
9600 * have completed, but the CS hasn't even executed the flip yet.
9601 *
9602 * A flip count check isn't enough as the CS might have updated
9603 * the base address just after start of vblank, but before we
9604 * managed to process the interrupt. This means we'd complete the
9605 * CS flip too soon.
9606 *
9607 * Combining both checks should get us a good enough result. It may
9608 * still happen that the CS flip has been executed, but has not
9609 * yet actually completed. But in case the base address is the same
9610 * anyway, we don't really care.
9611 */
9612 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9613 crtc->unpin_work->gtt_offset &&
9614 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9615 crtc->unpin_work->flip_count);
9616}
9617
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009618void intel_prepare_page_flip(struct drm_device *dev, int plane)
9619{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009620 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009621 struct intel_crtc *intel_crtc =
9622 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9623 unsigned long flags;
9624
Daniel Vetterf3260382014-09-15 14:55:23 +02009625
9626 /*
9627 * This is called both by irq handlers and the reset code (to complete
9628 * lost pageflips) so needs the full irqsave spinlocks.
9629 *
9630 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +00009631 * generate a page-flip completion irq, i.e. every modeset
9632 * is also accompanied by a spurious intel_prepare_page_flip().
9633 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009634 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009635 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +00009636 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009637 spin_unlock_irqrestore(&dev->event_lock, flags);
9638}
9639
Robin Schroereba905b2014-05-18 02:24:50 +02009640static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +00009641{
9642 /* Ensure that the work item is consistent when activating it ... */
9643 smp_wmb();
9644 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9645 /* and that it is marked active as soon as the irq could fire. */
9646 smp_wmb();
9647}
9648
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009649static int intel_gen2_queue_flip(struct drm_device *dev,
9650 struct drm_crtc *crtc,
9651 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009652 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009653 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009654 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009655{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009656 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009657 u32 flip_mask;
9658 int ret;
9659
Daniel Vetter6d90c952012-04-26 23:28:05 +02009660 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009661 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009662 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009663
9664 /* Can't queue multiple flips, so wait for the previous
9665 * one to finish before executing the next.
9666 */
9667 if (intel_crtc->plane)
9668 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9669 else
9670 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009671 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9672 intel_ring_emit(ring, MI_NOOP);
9673 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9674 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9675 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009676 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009677 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00009678
9679 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009680 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009681 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009682}
9683
9684static int intel_gen3_queue_flip(struct drm_device *dev,
9685 struct drm_crtc *crtc,
9686 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009687 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009688 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009689 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009690{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009691 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009692 u32 flip_mask;
9693 int ret;
9694
Daniel Vetter6d90c952012-04-26 23:28:05 +02009695 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009696 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009697 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009698
9699 if (intel_crtc->plane)
9700 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9701 else
9702 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009703 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9704 intel_ring_emit(ring, MI_NOOP);
9705 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9706 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9707 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009708 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009709 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009710
Chris Wilsone7d841c2012-12-03 11:36:30 +00009711 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009712 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009713 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009714}
9715
9716static int intel_gen4_queue_flip(struct drm_device *dev,
9717 struct drm_crtc *crtc,
9718 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009719 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009720 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009721 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009722{
9723 struct drm_i915_private *dev_priv = dev->dev_private;
9724 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9725 uint32_t pf, pipesrc;
9726 int ret;
9727
Daniel Vetter6d90c952012-04-26 23:28:05 +02009728 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009729 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009730 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009731
9732 /* i965+ uses the linear or tiled offsets from the
9733 * Display Registers (which do not change across a page-flip)
9734 * so we need only reprogram the base address.
9735 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02009736 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9737 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9738 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009739 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +02009740 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009741
9742 /* XXX Enabling the panel-fitter across page-flip is so far
9743 * untested on non-native modes, so ignore it for now.
9744 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9745 */
9746 pf = 0;
9747 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009748 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009749
9750 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009751 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009752 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009753}
9754
9755static int intel_gen6_queue_flip(struct drm_device *dev,
9756 struct drm_crtc *crtc,
9757 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009758 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009759 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009760 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009761{
9762 struct drm_i915_private *dev_priv = dev->dev_private;
9763 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9764 uint32_t pf, pipesrc;
9765 int ret;
9766
Daniel Vetter6d90c952012-04-26 23:28:05 +02009767 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009768 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009769 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009770
Daniel Vetter6d90c952012-04-26 23:28:05 +02009771 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9772 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9773 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009774 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009775
Chris Wilson99d9acd2012-04-17 20:37:00 +01009776 /* Contrary to the suggestions in the documentation,
9777 * "Enable Panel Fitter" does not seem to be required when page
9778 * flipping with a non-native mode, and worse causes a normal
9779 * modeset to fail.
9780 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9781 */
9782 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009783 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009784 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009785
9786 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009787 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009788 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009789}
9790
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009791static int intel_gen7_queue_flip(struct drm_device *dev,
9792 struct drm_crtc *crtc,
9793 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009794 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009795 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009796 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009797{
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009798 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009799 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01009800 int len, ret;
9801
Robin Schroereba905b2014-05-18 02:24:50 +02009802 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009803 case PLANE_A:
9804 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9805 break;
9806 case PLANE_B:
9807 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9808 break;
9809 case PLANE_C:
9810 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9811 break;
9812 default:
9813 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009814 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009815 }
9816
Chris Wilsonffe74d72013-08-26 20:58:12 +01009817 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +01009818 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +01009819 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +01009820 /*
9821 * On Gen 8, SRM is now taking an extra dword to accommodate
9822 * 48bits addresses, and we need a NOOP for the batch size to
9823 * stay even.
9824 */
9825 if (IS_GEN8(dev))
9826 len += 2;
9827 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009828
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009829 /*
9830 * BSpec MI_DISPLAY_FLIP for IVB:
9831 * "The full packet must be contained within the same cache line."
9832 *
9833 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9834 * cacheline, if we ever start emitting more commands before
9835 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9836 * then do the cacheline alignment, and finally emit the
9837 * MI_DISPLAY_FLIP.
9838 */
9839 ret = intel_ring_cacheline_align(ring);
9840 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009841 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009842
Chris Wilsonffe74d72013-08-26 20:58:12 +01009843 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009844 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009845 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009846
Chris Wilsonffe74d72013-08-26 20:58:12 +01009847 /* Unmask the flip-done completion message. Note that the bspec says that
9848 * we should do this for both the BCS and RCS, and that we must not unmask
9849 * more than one flip event at any time (or ensure that one flip message
9850 * can be sent by waiting for flip-done prior to queueing new flips).
9851 * Experimentation says that BCS works despite DERRMR masking all
9852 * flip-done completion events and that unmasking all planes at once
9853 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9854 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9855 */
9856 if (ring->id == RCS) {
9857 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9858 intel_ring_emit(ring, DERRMR);
9859 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9860 DERRMR_PIPEB_PRI_FLIP_DONE |
9861 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +01009862 if (IS_GEN8(dev))
9863 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9864 MI_SRM_LRM_GLOBAL_GTT);
9865 else
9866 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9867 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +01009868 intel_ring_emit(ring, DERRMR);
9869 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +01009870 if (IS_GEN8(dev)) {
9871 intel_ring_emit(ring, 0);
9872 intel_ring_emit(ring, MI_NOOP);
9873 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009874 }
9875
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009876 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009877 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009878 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009879 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00009880
9881 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009882 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009883 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009884}
9885
Sourab Gupta84c33a642014-06-02 16:47:17 +05309886static bool use_mmio_flip(struct intel_engine_cs *ring,
9887 struct drm_i915_gem_object *obj)
9888{
9889 /*
9890 * This is not being used for older platforms, because
9891 * non-availability of flip done interrupt forces us to use
9892 * CS flips. Older platforms derive flip done using some clever
9893 * tricks involving the flip_pending status bits and vblank irqs.
9894 * So using MMIO flips there would disrupt this mechanism.
9895 */
9896
Chris Wilson8e09bf82014-07-08 10:40:30 +01009897 if (ring == NULL)
9898 return true;
9899
Sourab Gupta84c33a642014-06-02 16:47:17 +05309900 if (INTEL_INFO(ring->dev)->gen < 5)
9901 return false;
9902
9903 if (i915.use_mmio_flip < 0)
9904 return false;
9905 else if (i915.use_mmio_flip > 0)
9906 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +01009907 else if (i915.enable_execlists)
9908 return true;
Sourab Gupta84c33a642014-06-02 16:47:17 +05309909 else
John Harrison41c52412014-11-24 18:49:43 +00009910 return ring != i915_gem_request_get_ring(obj->last_read_req);
Sourab Gupta84c33a642014-06-02 16:47:17 +05309911}
9912
Damien Lespiauff944562014-11-20 14:58:16 +00009913static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
9914{
9915 struct drm_device *dev = intel_crtc->base.dev;
9916 struct drm_i915_private *dev_priv = dev->dev_private;
9917 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
9918 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
9919 struct drm_i915_gem_object *obj = intel_fb->obj;
9920 const enum pipe pipe = intel_crtc->pipe;
9921 u32 ctl, stride;
9922
9923 ctl = I915_READ(PLANE_CTL(pipe, 0));
9924 ctl &= ~PLANE_CTL_TILED_MASK;
9925 if (obj->tiling_mode == I915_TILING_X)
9926 ctl |= PLANE_CTL_TILED_X;
9927
9928 /*
9929 * The stride is either expressed as a multiple of 64 bytes chunks for
9930 * linear buffers or in number of tiles for tiled buffers.
9931 */
9932 stride = fb->pitches[0] >> 6;
9933 if (obj->tiling_mode == I915_TILING_X)
9934 stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
9935
9936 /*
9937 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
9938 * PLANE_SURF updates, the update is then guaranteed to be atomic.
9939 */
9940 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
9941 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
9942
9943 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
9944 POSTING_READ(PLANE_SURF(pipe, 0));
9945}
9946
9947static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a642014-06-02 16:47:17 +05309948{
9949 struct drm_device *dev = intel_crtc->base.dev;
9950 struct drm_i915_private *dev_priv = dev->dev_private;
9951 struct intel_framebuffer *intel_fb =
9952 to_intel_framebuffer(intel_crtc->base.primary->fb);
9953 struct drm_i915_gem_object *obj = intel_fb->obj;
9954 u32 dspcntr;
9955 u32 reg;
9956
Sourab Gupta84c33a642014-06-02 16:47:17 +05309957 reg = DSPCNTR(intel_crtc->plane);
9958 dspcntr = I915_READ(reg);
9959
Damien Lespiauc5d97472014-10-25 00:11:11 +01009960 if (obj->tiling_mode != I915_TILING_NONE)
9961 dspcntr |= DISPPLANE_TILED;
9962 else
9963 dspcntr &= ~DISPPLANE_TILED;
9964
Sourab Gupta84c33a642014-06-02 16:47:17 +05309965 I915_WRITE(reg, dspcntr);
9966
9967 I915_WRITE(DSPSURF(intel_crtc->plane),
9968 intel_crtc->unpin_work->gtt_offset);
9969 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +02009970
Damien Lespiauff944562014-11-20 14:58:16 +00009971}
9972
9973/*
9974 * XXX: This is the temporary way to update the plane registers until we get
9975 * around to using the usual plane update functions for MMIO flips
9976 */
9977static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9978{
9979 struct drm_device *dev = intel_crtc->base.dev;
9980 bool atomic_update;
9981 u32 start_vbl_count;
9982
9983 intel_mark_page_flip_active(intel_crtc);
9984
9985 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
9986
9987 if (INTEL_INFO(dev)->gen >= 9)
9988 skl_do_mmio_flip(intel_crtc);
9989 else
9990 /* use_mmio_flip() retricts MMIO flips to ilk+ */
9991 ilk_do_mmio_flip(intel_crtc);
9992
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +02009993 if (atomic_update)
9994 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a642014-06-02 16:47:17 +05309995}
9996
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +02009997static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a642014-06-02 16:47:17 +05309998{
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009999 struct intel_crtc *crtc =
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010000 container_of(work, struct intel_crtc, mmio_flip.work);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010001 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a642014-06-02 16:47:17 +053010002
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010003 mmio_flip = &crtc->mmio_flip;
10004 if (mmio_flip->req)
John Harrison9c654812014-11-24 18:49:35 +000010005 WARN_ON(__i915_wait_request(mmio_flip->req,
10006 crtc->reset_counter,
10007 false, NULL, NULL) != 0);
Sourab Gupta84c33a642014-06-02 16:47:17 +053010008
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010009 intel_do_mmio_flip(crtc);
10010 if (mmio_flip->req) {
10011 mutex_lock(&crtc->base.dev->struct_mutex);
John Harrison146d84f2014-12-05 13:49:33 +000010012 i915_gem_request_assign(&mmio_flip->req, NULL);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010013 mutex_unlock(&crtc->base.dev->struct_mutex);
10014 }
Sourab Gupta84c33a642014-06-02 16:47:17 +053010015}
10016
10017static int intel_queue_mmio_flip(struct drm_device *dev,
10018 struct drm_crtc *crtc,
10019 struct drm_framebuffer *fb,
10020 struct drm_i915_gem_object *obj,
10021 struct intel_engine_cs *ring,
10022 uint32_t flags)
10023{
Sourab Gupta84c33a642014-06-02 16:47:17 +053010024 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sourab Gupta84c33a642014-06-02 16:47:17 +053010025
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010026 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
10027 obj->last_write_req);
Sourab Gupta84c33a642014-06-02 16:47:17 +053010028
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020010029 schedule_work(&intel_crtc->mmio_flip.work);
10030
Sourab Gupta84c33a642014-06-02 16:47:17 +053010031 return 0;
10032}
10033
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010034static int intel_default_queue_flip(struct drm_device *dev,
10035 struct drm_crtc *crtc,
10036 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010037 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010038 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010039 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010040{
10041 return -ENODEV;
10042}
10043
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010044static bool __intel_pageflip_stall_check(struct drm_device *dev,
10045 struct drm_crtc *crtc)
10046{
10047 struct drm_i915_private *dev_priv = dev->dev_private;
10048 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10049 struct intel_unpin_work *work = intel_crtc->unpin_work;
10050 u32 addr;
10051
10052 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10053 return true;
10054
10055 if (!work->enable_stall_check)
10056 return false;
10057
10058 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010010059 if (work->flip_queued_req &&
10060 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010061 return false;
10062
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010063 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010064 }
10065
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010066 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010067 return false;
10068
10069 /* Potential stall - if we see that the flip has happened,
10070 * assume a missed interrupt. */
10071 if (INTEL_INFO(dev)->gen >= 4)
10072 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10073 else
10074 addr = I915_READ(DSPADDR(intel_crtc->plane));
10075
10076 /* There is a potential issue here with a false positive after a flip
10077 * to the same address. We could address this by checking for a
10078 * non-incrementing frame counter.
10079 */
10080 return addr == work->gtt_offset;
10081}
10082
10083void intel_check_page_flip(struct drm_device *dev, int pipe)
10084{
10085 struct drm_i915_private *dev_priv = dev->dev_private;
10086 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10087 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterf3260382014-09-15 14:55:23 +020010088
Dave Gordon6c51d462015-03-06 15:34:26 +000010089 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010090
10091 if (crtc == NULL)
10092 return;
10093
Daniel Vetterf3260382014-09-15 14:55:23 +020010094 spin_lock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010095 if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
10096 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010097 intel_crtc->unpin_work->flip_queued_vblank,
10098 drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010099 page_flip_completed(intel_crtc);
10100 }
Daniel Vetterf3260382014-09-15 14:55:23 +020010101 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010102}
10103
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010104static int intel_crtc_page_flip(struct drm_crtc *crtc,
10105 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010106 struct drm_pending_vblank_event *event,
10107 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010108{
10109 struct drm_device *dev = crtc->dev;
10110 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070010111 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070010112 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010113 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080010114 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020010115 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010116 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010117 struct intel_engine_cs *ring;
Chris Wilson52e68632010-08-08 10:15:59 +010010118 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010119
Matt Roper2ff8fde2014-07-08 07:50:07 -070010120 /*
10121 * drm_mode_page_flip_ioctl() should already catch this, but double
10122 * check to be safe. In the future we may enable pageflipping from
10123 * a disabled primary plane.
10124 */
10125 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10126 return -EBUSY;
10127
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010128 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070010129 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010130 return -EINVAL;
10131
10132 /*
10133 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10134 * Note that pitch changes could also affect these register.
10135 */
10136 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070010137 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10138 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010139 return -EINVAL;
10140
Chris Wilsonf900db42014-02-20 09:26:13 +000010141 if (i915_terminally_wedged(&dev_priv->gpu_error))
10142 goto out_hang;
10143
Daniel Vetterb14c5672013-09-19 12:18:32 +020010144 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010145 if (work == NULL)
10146 return -ENOMEM;
10147
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010148 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010149 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010150 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010151 INIT_WORK(&work->work, intel_unpin_work_fn);
10152
Daniel Vetter87b6b102014-05-15 15:33:46 +020010153 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010154 if (ret)
10155 goto free_work;
10156
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010157 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010158 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010159 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010160 /* Before declaring the flip queue wedged, check if
10161 * the hardware completed the operation behind our backs.
10162 */
10163 if (__intel_pageflip_stall_check(dev, crtc)) {
10164 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10165 page_flip_completed(intel_crtc);
10166 } else {
10167 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010168 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010010169
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010170 drm_crtc_vblank_put(crtc);
10171 kfree(work);
10172 return -EBUSY;
10173 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010174 }
10175 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010176 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010177
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010178 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10179 flush_workqueue(dev_priv->wq);
10180
Jesse Barnes75dfca82010-02-10 15:09:44 -080010181 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010182 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010183 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010184
Matt Roperf4510a22014-04-01 15:22:40 -070010185 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010186 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080010187
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010188 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010189
Chris Wilson89ed88b2015-02-16 14:31:49 +000010190 ret = i915_mutex_lock_interruptible(dev);
10191 if (ret)
10192 goto cleanup;
10193
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010194 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020010195 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010196
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010197 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020010198 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010199
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010200 if (IS_VALLEYVIEW(dev)) {
10201 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010202 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010010203 /* vlv: DISPLAY_FLIP fails to change tiling */
10204 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000010205 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010010206 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010207 } else if (INTEL_INFO(dev)->gen >= 7) {
John Harrison41c52412014-11-24 18:49:43 +000010208 ring = i915_gem_request_get_ring(obj->last_read_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010209 if (ring == NULL || ring->id != RCS)
10210 ring = &dev_priv->ring[BCS];
10211 } else {
10212 ring = &dev_priv->ring[RCS];
10213 }
10214
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010215 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
10216 crtc->primary->state, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010217 if (ret)
10218 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010219
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000010220 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
10221 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010222
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010223 if (use_mmio_flip(ring, obj)) {
Sourab Gupta84c33a642014-06-02 16:47:17 +053010224 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10225 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010226 if (ret)
10227 goto cleanup_unpin;
10228
John Harrisonf06cc1b2014-11-24 18:49:37 +000010229 i915_gem_request_assign(&work->flip_queued_req,
10230 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010231 } else {
Sourab Gupta84c33a642014-06-02 16:47:17 +053010232 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010233 page_flip_flags);
10234 if (ret)
10235 goto cleanup_unpin;
10236
John Harrisonf06cc1b2014-11-24 18:49:37 +000010237 i915_gem_request_assign(&work->flip_queued_req,
10238 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010239 }
10240
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010241 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010242 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010243
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010244 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020010245 INTEL_FRONTBUFFER_PRIMARY(pipe));
10246
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010247 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010248 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010249 mutex_unlock(&dev->struct_mutex);
10250
Jesse Barnese5510fa2010-07-01 16:48:37 -070010251 trace_i915_flip_request(intel_crtc->plane, obj);
10252
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010253 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010010254
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010255cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010256 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010257cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010258 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010259 mutex_unlock(&dev->struct_mutex);
10260cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070010261 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010262 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010010263
Chris Wilson89ed88b2015-02-16 14:31:49 +000010264 drm_gem_object_unreference_unlocked(&obj->base);
10265 drm_framebuffer_unreference(work->old_fb);
10266
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010267 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010268 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010269 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010270
Daniel Vetter87b6b102014-05-15 15:33:46 +020010271 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010272free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010010273 kfree(work);
10274
Chris Wilsonf900db42014-02-20 09:26:13 +000010275 if (ret == -EIO) {
10276out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080010277 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010278 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010279 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020010280 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010281 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010282 }
Chris Wilsonf900db42014-02-20 09:26:13 +000010283 }
Chris Wilson96b099f2010-06-07 14:03:04 +010010284 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010285}
10286
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010287static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010288 .mode_set_base_atomic = intel_pipe_set_base_atomic,
10289 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080010290 .atomic_begin = intel_begin_crtc_commit,
10291 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010292};
10293
Daniel Vetter9a935852012-07-05 22:34:27 +020010294/**
10295 * intel_modeset_update_staged_output_state
10296 *
10297 * Updates the staged output configuration state, e.g. after we've read out the
10298 * current hw state.
10299 */
10300static void intel_modeset_update_staged_output_state(struct drm_device *dev)
10301{
Ville Syrjälä76688512014-01-10 11:28:06 +020010302 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010303 struct intel_encoder *encoder;
10304 struct intel_connector *connector;
10305
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010306 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010307 connector->new_encoder =
10308 to_intel_encoder(connector->base.encoder);
10309 }
10310
Damien Lespiaub2784e12014-08-05 11:29:37 +010010311 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010312 encoder->new_crtc =
10313 to_intel_crtc(encoder->base.crtc);
10314 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010315
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010316 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010317 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010318
10319 if (crtc->new_enabled)
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010320 crtc->new_config = crtc->config;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010321 else
10322 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010323 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010324}
10325
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010326/* Transitional helper to copy current connector/encoder state to
10327 * connector->state. This is needed so that code that is partially
10328 * converted to atomic does the right thing.
10329 */
10330static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
10331{
10332 struct intel_connector *connector;
10333
10334 for_each_intel_connector(dev, connector) {
10335 if (connector->base.encoder) {
10336 connector->base.state->best_encoder =
10337 connector->base.encoder;
10338 connector->base.state->crtc =
10339 connector->base.encoder->crtc;
10340 } else {
10341 connector->base.state->best_encoder = NULL;
10342 connector->base.state->crtc = NULL;
10343 }
10344 }
10345}
10346
Daniel Vetter9a935852012-07-05 22:34:27 +020010347/**
10348 * intel_modeset_commit_output_state
10349 *
10350 * This function copies the stage display pipe configuration to the real one.
10351 */
10352static void intel_modeset_commit_output_state(struct drm_device *dev)
10353{
Ville Syrjälä76688512014-01-10 11:28:06 +020010354 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010355 struct intel_encoder *encoder;
10356 struct intel_connector *connector;
10357
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010358 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010359 connector->base.encoder = &connector->new_encoder->base;
10360 }
10361
Damien Lespiaub2784e12014-08-05 11:29:37 +010010362 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010363 encoder->base.crtc = &encoder->new_crtc->base;
10364 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010365
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010366 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010367 crtc->base.state->enable = crtc->new_enabled;
Ville Syrjälä76688512014-01-10 11:28:06 +020010368 crtc->base.enabled = crtc->new_enabled;
10369 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010370
10371 intel_modeset_update_connector_atomic_state(dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020010372}
10373
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010374static void
Robin Schroereba905b2014-05-18 02:24:50 +020010375connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010376 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010377{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010378 int bpp = pipe_config->pipe_bpp;
10379
10380 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10381 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010382 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010383
10384 /* Don't use an invalid EDID bpc value */
10385 if (connector->base.display_info.bpc &&
10386 connector->base.display_info.bpc * 3 < bpp) {
10387 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10388 bpp, connector->base.display_info.bpc*3);
10389 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10390 }
10391
10392 /* Clamp bpp to 8 on screens without EDID 1.4 */
10393 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10394 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10395 bpp);
10396 pipe_config->pipe_bpp = 24;
10397 }
10398}
10399
10400static int
10401compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10402 struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010403 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010404{
10405 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010406 struct drm_atomic_state *state;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010407 struct intel_connector *connector;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010408 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010409
Daniel Vetterd42264b2013-03-28 16:38:08 +010010410 switch (fb->pixel_format) {
10411 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010412 bpp = 8*3; /* since we go through a colormap */
10413 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010414 case DRM_FORMAT_XRGB1555:
10415 case DRM_FORMAT_ARGB1555:
10416 /* checked in intel_framebuffer_init already */
10417 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10418 return -EINVAL;
10419 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010420 bpp = 6*3; /* min is 18bpp */
10421 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010422 case DRM_FORMAT_XBGR8888:
10423 case DRM_FORMAT_ABGR8888:
10424 /* checked in intel_framebuffer_init already */
10425 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10426 return -EINVAL;
10427 case DRM_FORMAT_XRGB8888:
10428 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010429 bpp = 8*3;
10430 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010431 case DRM_FORMAT_XRGB2101010:
10432 case DRM_FORMAT_ARGB2101010:
10433 case DRM_FORMAT_XBGR2101010:
10434 case DRM_FORMAT_ABGR2101010:
10435 /* checked in intel_framebuffer_init already */
10436 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +010010437 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010438 bpp = 10*3;
10439 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +010010440 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010441 default:
10442 DRM_DEBUG_KMS("unsupported depth\n");
10443 return -EINVAL;
10444 }
10445
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010446 pipe_config->pipe_bpp = bpp;
10447
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010448 state = pipe_config->base.state;
10449
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010450 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010451 for (i = 0; i < state->num_connector; i++) {
10452 if (!state->connectors[i])
10453 continue;
10454
10455 connector = to_intel_connector(state->connectors[i]);
10456 if (state->connector_states[i]->crtc != &crtc->base)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010457 continue;
10458
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010459 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010460 }
10461
10462 return bpp;
10463}
10464
Daniel Vetter644db712013-09-19 14:53:58 +020010465static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10466{
10467 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10468 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010010469 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020010470 mode->crtc_hdisplay, mode->crtc_hsync_start,
10471 mode->crtc_hsync_end, mode->crtc_htotal,
10472 mode->crtc_vdisplay, mode->crtc_vsync_start,
10473 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10474}
10475
Daniel Vetterc0b03412013-05-28 12:05:54 +020010476static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010477 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020010478 const char *context)
10479{
10480 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10481 context, pipe_name(crtc->pipe));
10482
10483 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10484 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10485 pipe_config->pipe_bpp, pipe_config->dither);
10486 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10487 pipe_config->has_pch_encoder,
10488 pipe_config->fdi_lanes,
10489 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10490 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10491 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010492 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10493 pipe_config->has_dp_encoder,
10494 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10495 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10496 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010497
10498 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10499 pipe_config->has_dp_encoder,
10500 pipe_config->dp_m2_n2.gmch_m,
10501 pipe_config->dp_m2_n2.gmch_n,
10502 pipe_config->dp_m2_n2.link_m,
10503 pipe_config->dp_m2_n2.link_n,
10504 pipe_config->dp_m2_n2.tu);
10505
Daniel Vetter55072d12014-11-20 16:10:28 +010010506 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
10507 pipe_config->has_audio,
10508 pipe_config->has_infoframe);
10509
Daniel Vetterc0b03412013-05-28 12:05:54 +020010510 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010511 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010512 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010513 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
10514 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030010515 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010516 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10517 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010518 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10519 pipe_config->gmch_pfit.control,
10520 pipe_config->gmch_pfit.pgm_ratios,
10521 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010522 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020010523 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010524 pipe_config->pch_pfit.size,
10525 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010526 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030010527 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010528}
10529
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010530static bool encoders_cloneable(const struct intel_encoder *a,
10531 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010532{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010533 /* masks could be asymmetric, so check both ways */
10534 return a == b || (a->cloneable & (1 << b->type) &&
10535 b->cloneable & (1 << a->type));
10536}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010537
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010538static bool check_single_encoder_cloning(struct intel_crtc *crtc,
10539 struct intel_encoder *encoder)
10540{
10541 struct drm_device *dev = crtc->base.dev;
10542 struct intel_encoder *source_encoder;
10543
Damien Lespiaub2784e12014-08-05 11:29:37 +010010544 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010545 if (source_encoder->new_crtc != crtc)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010546 continue;
10547
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010548 if (!encoders_cloneable(encoder, source_encoder))
10549 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010550 }
10551
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010552 return true;
10553}
10554
10555static bool check_encoder_cloning(struct intel_crtc *crtc)
10556{
10557 struct drm_device *dev = crtc->base.dev;
10558 struct intel_encoder *encoder;
10559
Damien Lespiaub2784e12014-08-05 11:29:37 +010010560 for_each_intel_encoder(dev, encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010561 if (encoder->new_crtc != crtc)
10562 continue;
10563
10564 if (!check_single_encoder_cloning(crtc, encoder))
10565 return false;
10566 }
10567
10568 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010569}
10570
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010571static bool check_digital_port_conflicts(struct drm_device *dev)
10572{
10573 struct intel_connector *connector;
10574 unsigned int used_ports = 0;
10575
10576 /*
10577 * Walk the connector list instead of the encoder
10578 * list to detect the problem on ddi platforms
10579 * where there's just one encoder per digital port.
10580 */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010581 for_each_intel_connector(dev, connector) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010582 struct intel_encoder *encoder = connector->new_encoder;
10583
10584 if (!encoder)
10585 continue;
10586
10587 WARN_ON(!encoder->new_crtc);
10588
10589 switch (encoder->type) {
10590 unsigned int port_mask;
10591 case INTEL_OUTPUT_UNKNOWN:
10592 if (WARN_ON(!HAS_DDI(dev)))
10593 break;
10594 case INTEL_OUTPUT_DISPLAYPORT:
10595 case INTEL_OUTPUT_HDMI:
10596 case INTEL_OUTPUT_EDP:
10597 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
10598
10599 /* the same port mustn't appear more than once */
10600 if (used_ports & port_mask)
10601 return false;
10602
10603 used_ports |= port_mask;
10604 default:
10605 break;
10606 }
10607 }
10608
10609 return true;
10610}
10611
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010612static void
10613clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
10614{
10615 struct drm_crtc_state tmp_state;
10616
10617 /* Clear only the intel specific part of the crtc state */
10618 tmp_state = crtc_state->base;
10619 memset(crtc_state, 0, sizeof *crtc_state);
10620 crtc_state->base = tmp_state;
10621}
10622
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010623static struct intel_crtc_state *
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010624intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010625 struct drm_framebuffer *fb,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010626 struct drm_display_mode *mode,
10627 struct drm_atomic_state *state)
Daniel Vetter7758a112012-07-08 19:40:39 +020010628{
10629 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +020010630 struct intel_encoder *encoder;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020010631 struct intel_connector *connector;
10632 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010633 struct intel_crtc_state *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +010010634 int plane_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020010635 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010010636 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020010637
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010638 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010639 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10640 return ERR_PTR(-EINVAL);
10641 }
10642
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010643 if (!check_digital_port_conflicts(dev)) {
10644 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
10645 return ERR_PTR(-EINVAL);
10646 }
10647
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010648 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
10649 if (IS_ERR(pipe_config))
10650 return pipe_config;
10651
10652 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020010653
Matt Roper07878242015-02-25 11:43:26 -080010654 pipe_config->base.crtc = crtc;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010655 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
10656 drm_mode_copy(&pipe_config->base.mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010657
Daniel Vettere143a212013-07-04 12:01:15 +020010658 pipe_config->cpu_transcoder =
10659 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010660 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010661
Imre Deak2960bc92013-07-30 13:36:32 +030010662 /*
10663 * Sanitize sync polarity flags based on requested ones. If neither
10664 * positive or negative polarity is requested, treat this as meaning
10665 * negative polarity.
10666 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010667 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030010668 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010669 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030010670
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010671 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030010672 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010673 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030010674
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010675 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10676 * plane pixel format and any sink constraints into account. Returns the
10677 * source plane bpp so that dithering can be selected on mismatches
10678 * after encoders and crtc also have had their say. */
10679 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10680 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010681 if (plane_bpp < 0)
10682 goto fail;
10683
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030010684 /*
10685 * Determine the real pipe dimensions. Note that stereo modes can
10686 * increase the actual pipe size due to the frame doubling and
10687 * insertion of additional space for blanks between the frame. This
10688 * is stored in the crtc timings. We use the requested mode to do this
10689 * computation to clearly distinguish it from the adjusted mode, which
10690 * can be changed by the connectors in the below retry loop.
10691 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010692 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080010693 &pipe_config->pipe_src_w,
10694 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030010695
Daniel Vettere29c22c2013-02-21 00:00:16 +010010696encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020010697 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020010698 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020010699 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010700
Daniel Vetter135c81b2013-07-21 21:37:09 +020010701 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010702 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
10703 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020010704
Daniel Vetter7758a112012-07-08 19:40:39 +020010705 /* Pass our mode to the connectors and the CRTC to give them a chance to
10706 * adjust it according to limitations or connector properties, and also
10707 * a chance to reject the mode entirely.
10708 */
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020010709 for (i = 0; i < state->num_connector; i++) {
10710 connector = to_intel_connector(state->connectors[i]);
10711 if (!connector)
Daniel Vetter7758a112012-07-08 19:40:39 +020010712 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +010010713
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020010714 connector_state = state->connector_states[i];
10715 if (connector_state->crtc != crtc)
10716 continue;
10717
10718 encoder = to_intel_encoder(connector_state->best_encoder);
10719
Daniel Vetterefea6e82013-07-21 21:36:59 +020010720 if (!(encoder->compute_config(encoder, pipe_config))) {
10721 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020010722 goto fail;
10723 }
10724 }
10725
Daniel Vetterff9a6752013-06-01 17:16:21 +020010726 /* Set default port clock if not overwritten by the encoder. Needs to be
10727 * done afterwards in case the encoder adjusts the mode. */
10728 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010729 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010010730 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010731
Daniel Vettera43f6e02013-06-07 23:10:32 +020010732 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010010733 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020010734 DRM_DEBUG_KMS("CRTC fixup failed\n");
10735 goto fail;
10736 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010010737
10738 if (ret == RETRY) {
10739 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10740 ret = -EINVAL;
10741 goto fail;
10742 }
10743
10744 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10745 retry = false;
10746 goto encoder_retry;
10747 }
10748
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010749 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10750 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10751 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10752
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010753 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020010754fail:
Daniel Vettere29c22c2013-02-21 00:00:16 +010010755 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +020010756}
10757
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010758/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10759 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10760static void
10761intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10762 unsigned *prepare_pipes, unsigned *disable_pipes)
10763{
10764 struct intel_crtc *intel_crtc;
10765 struct drm_device *dev = crtc->dev;
10766 struct intel_encoder *encoder;
10767 struct intel_connector *connector;
10768 struct drm_crtc *tmp_crtc;
10769
10770 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
10771
10772 /* Check which crtcs have changed outputs connected to them, these need
10773 * to be part of the prepare_pipes mask. We don't (yet) support global
10774 * modeset across multiple crtcs, so modeset_pipes will only have one
10775 * bit set at most. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010776 for_each_intel_connector(dev, connector) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010777 if (connector->base.encoder == &connector->new_encoder->base)
10778 continue;
10779
10780 if (connector->base.encoder) {
10781 tmp_crtc = connector->base.encoder->crtc;
10782
10783 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10784 }
10785
10786 if (connector->new_encoder)
10787 *prepare_pipes |=
10788 1 << connector->new_encoder->new_crtc->pipe;
10789 }
10790
Damien Lespiaub2784e12014-08-05 11:29:37 +010010791 for_each_intel_encoder(dev, encoder) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010792 if (encoder->base.crtc == &encoder->new_crtc->base)
10793 continue;
10794
10795 if (encoder->base.crtc) {
10796 tmp_crtc = encoder->base.crtc;
10797
10798 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10799 }
10800
10801 if (encoder->new_crtc)
10802 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
10803 }
10804
Ville Syrjälä76688512014-01-10 11:28:06 +020010805 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010806 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010807 if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010808 continue;
10809
Ville Syrjälä76688512014-01-10 11:28:06 +020010810 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010811 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +020010812 else
10813 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010814 }
10815
10816
10817 /* set_mode is also used to update properties on life display pipes. */
10818 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +020010819 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010820 *prepare_pipes |= 1 << intel_crtc->pipe;
10821
Daniel Vetterb6c51642013-04-12 18:48:43 +020010822 /*
10823 * For simplicity do a full modeset on any pipe where the output routing
10824 * changed. We could be more clever, but that would require us to be
10825 * more careful with calling the relevant encoder->mode_set functions.
10826 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010827 if (*prepare_pipes)
10828 *modeset_pipes = *prepare_pipes;
10829
10830 /* ... and mask these out. */
10831 *modeset_pipes &= ~(*disable_pipes);
10832 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020010833
10834 /*
10835 * HACK: We don't (yet) fully support global modesets. intel_set_config
10836 * obies this rule, but the modeset restore mode of
10837 * intel_modeset_setup_hw_state does not.
10838 */
10839 *modeset_pipes &= 1 << intel_crtc->pipe;
10840 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020010841
10842 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10843 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010844}
10845
Daniel Vetterea9d7582012-07-10 10:42:52 +020010846static bool intel_crtc_in_use(struct drm_crtc *crtc)
10847{
10848 struct drm_encoder *encoder;
10849 struct drm_device *dev = crtc->dev;
10850
10851 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10852 if (encoder->crtc == crtc)
10853 return true;
10854
10855 return false;
10856}
10857
10858static void
10859intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10860{
Daniel Vetterba41c0de2014-11-03 15:04:55 +010010861 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020010862 struct intel_encoder *intel_encoder;
10863 struct intel_crtc *intel_crtc;
10864 struct drm_connector *connector;
10865
Daniel Vetterba41c0de2014-11-03 15:04:55 +010010866 intel_shared_dpll_commit(dev_priv);
10867
Damien Lespiaub2784e12014-08-05 11:29:37 +010010868 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020010869 if (!intel_encoder->base.crtc)
10870 continue;
10871
10872 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10873
10874 if (prepare_pipes & (1 << intel_crtc->pipe))
10875 intel_encoder->connectors_active = false;
10876 }
10877
10878 intel_modeset_commit_output_state(dev);
10879
Ville Syrjälä76688512014-01-10 11:28:06 +020010880 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010881 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010882 WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010883 WARN_ON(intel_crtc->new_config &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010884 intel_crtc->new_config != intel_crtc->config);
Matt Roper83d65732015-02-25 13:12:16 -080010885 WARN_ON(intel_crtc->base.state->enable != !!intel_crtc->new_config);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010886 }
10887
10888 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10889 if (!connector->encoder || !connector->encoder->crtc)
10890 continue;
10891
10892 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10893
10894 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020010895 struct drm_property *dpms_property =
10896 dev->mode_config.dpms_property;
10897
Daniel Vetterea9d7582012-07-10 10:42:52 +020010898 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050010899 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020010900 dpms_property,
10901 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010902
10903 intel_encoder = to_intel_encoder(connector->encoder);
10904 intel_encoder->connectors_active = true;
10905 }
10906 }
10907
10908}
10909
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010910static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010911{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010912 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010913
10914 if (clock1 == clock2)
10915 return true;
10916
10917 if (!clock1 || !clock2)
10918 return false;
10919
10920 diff = abs(clock1 - clock2);
10921
10922 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10923 return true;
10924
10925 return false;
10926}
10927
Daniel Vetter25c5b262012-07-08 22:08:04 +020010928#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10929 list_for_each_entry((intel_crtc), \
10930 &(dev)->mode_config.crtc_list, \
10931 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020010932 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020010933
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010934static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010935intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010936 struct intel_crtc_state *current_config,
10937 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010938{
Daniel Vetter66e985c2013-06-05 13:34:20 +020010939#define PIPE_CONF_CHECK_X(name) \
10940 if (current_config->name != pipe_config->name) { \
10941 DRM_ERROR("mismatch in " #name " " \
10942 "(expected 0x%08x, found 0x%08x)\n", \
10943 current_config->name, \
10944 pipe_config->name); \
10945 return false; \
10946 }
10947
Daniel Vetter08a24032013-04-19 11:25:34 +020010948#define PIPE_CONF_CHECK_I(name) \
10949 if (current_config->name != pipe_config->name) { \
10950 DRM_ERROR("mismatch in " #name " " \
10951 "(expected %i, found %i)\n", \
10952 current_config->name, \
10953 pipe_config->name); \
10954 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010010955 }
10956
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010957/* This is required for BDW+ where there is only one set of registers for
10958 * switching between high and low RR.
10959 * This macro can be used whenever a comparison has to be made between one
10960 * hw state and multiple sw state variables.
10961 */
10962#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10963 if ((current_config->name != pipe_config->name) && \
10964 (current_config->alt_name != pipe_config->name)) { \
10965 DRM_ERROR("mismatch in " #name " " \
10966 "(expected %i or %i, found %i)\n", \
10967 current_config->name, \
10968 current_config->alt_name, \
10969 pipe_config->name); \
10970 return false; \
10971 }
10972
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010973#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10974 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070010975 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010976 "(expected %i, found %i)\n", \
10977 current_config->name & (mask), \
10978 pipe_config->name & (mask)); \
10979 return false; \
10980 }
10981
Ville Syrjälä5e550652013-09-06 23:29:07 +030010982#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10983 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10984 DRM_ERROR("mismatch in " #name " " \
10985 "(expected %i, found %i)\n", \
10986 current_config->name, \
10987 pipe_config->name); \
10988 return false; \
10989 }
10990
Daniel Vetterbb760062013-06-06 14:55:52 +020010991#define PIPE_CONF_QUIRK(quirk) \
10992 ((current_config->quirks | pipe_config->quirks) & (quirk))
10993
Daniel Vettereccb1402013-05-22 00:50:22 +020010994 PIPE_CONF_CHECK_I(cpu_transcoder);
10995
Daniel Vetter08a24032013-04-19 11:25:34 +020010996 PIPE_CONF_CHECK_I(has_pch_encoder);
10997 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020010998 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10999 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11000 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11001 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11002 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020011003
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011004 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011005
11006 if (INTEL_INFO(dev)->gen < 8) {
11007 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11008 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11009 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11010 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11011 PIPE_CONF_CHECK_I(dp_m_n.tu);
11012
11013 if (current_config->has_drrs) {
11014 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11015 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11016 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11017 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11018 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11019 }
11020 } else {
11021 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11022 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11023 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11024 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11025 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11026 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011027
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011028 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11029 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11030 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11031 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11032 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11033 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011034
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011035 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11036 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11037 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11038 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11039 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11040 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011041
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011042 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020011043 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020011044 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11045 IS_VALLEYVIEW(dev))
11046 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080011047 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011048
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011049 PIPE_CONF_CHECK_I(has_audio);
11050
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011051 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011052 DRM_MODE_FLAG_INTERLACE);
11053
Daniel Vetterbb760062013-06-06 14:55:52 +020011054 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011055 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011056 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011057 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011058 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011059 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011060 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011061 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011062 DRM_MODE_FLAG_NVSYNC);
11063 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011064
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011065 PIPE_CONF_CHECK_I(pipe_src_w);
11066 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011067
Daniel Vetter99535992014-04-13 12:00:33 +020011068 /*
11069 * FIXME: BIOS likes to set up a cloned config with lvds+external
11070 * screen. Since we don't yet re-compute the pipe config when moving
11071 * just the lvds port away to another pipe the sw tracking won't match.
11072 *
11073 * Proper atomic modesets with recomputed global state will fix this.
11074 * Until then just don't check gmch state for inherited modes.
11075 */
11076 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11077 PIPE_CONF_CHECK_I(gmch_pfit.control);
11078 /* pfit ratios are autocomputed by the hw on gen4+ */
11079 if (INTEL_INFO(dev)->gen < 4)
11080 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11081 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11082 }
11083
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011084 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11085 if (current_config->pch_pfit.enabled) {
11086 PIPE_CONF_CHECK_I(pch_pfit.pos);
11087 PIPE_CONF_CHECK_I(pch_pfit.size);
11088 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011089
Jesse Barnese59150d2014-01-07 13:30:45 -080011090 /* BDW+ don't expose a synchronous way to read the state */
11091 if (IS_HASWELL(dev))
11092 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011093
Ville Syrjälä282740f2013-09-04 18:30:03 +030011094 PIPE_CONF_CHECK_I(double_wide);
11095
Daniel Vetter26804af2014-06-25 22:01:55 +030011096 PIPE_CONF_CHECK_X(ddi_pll_sel);
11097
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011098 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011099 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011100 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011101 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11102 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011103 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011104 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11105 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11106 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011107
Ville Syrjälä42571aef2013-09-06 23:29:00 +030011108 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11109 PIPE_CONF_CHECK_I(pipe_bpp);
11110
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011111 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011112 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011113
Daniel Vetter66e985c2013-06-05 13:34:20 +020011114#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011115#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011116#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011117#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011118#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011119#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011120
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011121 return true;
11122}
11123
Damien Lespiau08db6652014-11-04 17:06:52 +000011124static void check_wm_state(struct drm_device *dev)
11125{
11126 struct drm_i915_private *dev_priv = dev->dev_private;
11127 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11128 struct intel_crtc *intel_crtc;
11129 int plane;
11130
11131 if (INTEL_INFO(dev)->gen < 9)
11132 return;
11133
11134 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11135 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11136
11137 for_each_intel_crtc(dev, intel_crtc) {
11138 struct skl_ddb_entry *hw_entry, *sw_entry;
11139 const enum pipe pipe = intel_crtc->pipe;
11140
11141 if (!intel_crtc->active)
11142 continue;
11143
11144 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000011145 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000011146 hw_entry = &hw_ddb.plane[pipe][plane];
11147 sw_entry = &sw_ddb->plane[pipe][plane];
11148
11149 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11150 continue;
11151
11152 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11153 "(expected (%u,%u), found (%u,%u))\n",
11154 pipe_name(pipe), plane + 1,
11155 sw_entry->start, sw_entry->end,
11156 hw_entry->start, hw_entry->end);
11157 }
11158
11159 /* cursor */
11160 hw_entry = &hw_ddb.cursor[pipe];
11161 sw_entry = &sw_ddb->cursor[pipe];
11162
11163 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11164 continue;
11165
11166 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11167 "(expected (%u,%u), found (%u,%u))\n",
11168 pipe_name(pipe),
11169 sw_entry->start, sw_entry->end,
11170 hw_entry->start, hw_entry->end);
11171 }
11172}
11173
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011174static void
11175check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011176{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011177 struct intel_connector *connector;
11178
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011179 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011180 /* This also checks the encoder/connector hw state with the
11181 * ->get_hw_state callbacks. */
11182 intel_connector_check_state(connector);
11183
Rob Clarke2c719b2014-12-15 13:56:32 -050011184 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011185 "connector's staged encoder doesn't match current encoder\n");
11186 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011187}
11188
11189static void
11190check_encoder_state(struct drm_device *dev)
11191{
11192 struct intel_encoder *encoder;
11193 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011194
Damien Lespiaub2784e12014-08-05 11:29:37 +010011195 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011196 bool enabled = false;
11197 bool active = false;
11198 enum pipe pipe, tracked_pipe;
11199
11200 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11201 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011202 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011203
Rob Clarke2c719b2014-12-15 13:56:32 -050011204 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011205 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011206 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011207 "encoder's active_connectors set, but no crtc\n");
11208
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011209 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011210 if (connector->base.encoder != &encoder->base)
11211 continue;
11212 enabled = true;
11213 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11214 active = true;
11215 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011216 /*
11217 * for MST connectors if we unplug the connector is gone
11218 * away but the encoder is still connected to a crtc
11219 * until a modeset happens in response to the hotplug.
11220 */
11221 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
11222 continue;
11223
Rob Clarke2c719b2014-12-15 13:56:32 -050011224 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011225 "encoder's enabled state mismatch "
11226 "(expected %i, found %i)\n",
11227 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050011228 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011229 "active encoder with no crtc\n");
11230
Rob Clarke2c719b2014-12-15 13:56:32 -050011231 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011232 "encoder's computed active state doesn't match tracked active state "
11233 "(expected %i, found %i)\n", active, encoder->connectors_active);
11234
11235 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050011236 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011237 "encoder's hw state doesn't match sw tracking "
11238 "(expected %i, found %i)\n",
11239 encoder->connectors_active, active);
11240
11241 if (!encoder->base.crtc)
11242 continue;
11243
11244 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050011245 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011246 "active encoder's pipe doesn't match"
11247 "(expected %i, found %i)\n",
11248 tracked_pipe, pipe);
11249
11250 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011251}
11252
11253static void
11254check_crtc_state(struct drm_device *dev)
11255{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011256 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011257 struct intel_crtc *crtc;
11258 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011259 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011260
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011261 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011262 bool enabled = false;
11263 bool active = false;
11264
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011265 memset(&pipe_config, 0, sizeof(pipe_config));
11266
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011267 DRM_DEBUG_KMS("[CRTC:%d]\n",
11268 crtc->base.base.id);
11269
Matt Roper83d65732015-02-25 13:12:16 -080011270 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011271 "active crtc, but not enabled in sw tracking\n");
11272
Damien Lespiaub2784e12014-08-05 11:29:37 +010011273 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011274 if (encoder->base.crtc != &crtc->base)
11275 continue;
11276 enabled = true;
11277 if (encoder->connectors_active)
11278 active = true;
11279 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020011280
Rob Clarke2c719b2014-12-15 13:56:32 -050011281 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011282 "crtc's computed active state doesn't match tracked active state "
11283 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080011284 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011285 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080011286 "(expected %i, found %i)\n", enabled,
11287 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011288
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011289 active = dev_priv->display.get_pipe_config(crtc,
11290 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020011291
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030011292 /* hw state is inconsistent with the pipe quirk */
11293 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
11294 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020011295 active = crtc->active;
11296
Damien Lespiaub2784e12014-08-05 11:29:37 +010011297 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030011298 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020011299 if (encoder->base.crtc != &crtc->base)
11300 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010011301 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020011302 encoder->get_config(encoder, &pipe_config);
11303 }
11304
Rob Clarke2c719b2014-12-15 13:56:32 -050011305 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011306 "crtc active state doesn't match with hw state "
11307 "(expected %i, found %i)\n", crtc->active, active);
11308
Daniel Vetterc0b03412013-05-28 12:05:54 +020011309 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011310 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050011311 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020011312 intel_dump_pipe_config(crtc, &pipe_config,
11313 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011314 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011315 "[sw state]");
11316 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011317 }
11318}
11319
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011320static void
11321check_shared_dpll_state(struct drm_device *dev)
11322{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011323 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011324 struct intel_crtc *crtc;
11325 struct intel_dpll_hw_state dpll_hw_state;
11326 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020011327
11328 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11329 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11330 int enabled_crtcs = 0, active_crtcs = 0;
11331 bool active;
11332
11333 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
11334
11335 DRM_DEBUG_KMS("%s\n", pll->name);
11336
11337 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
11338
Rob Clarke2c719b2014-12-15 13:56:32 -050011339 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020011340 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011341 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050011342 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020011343 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011344 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020011345 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011346 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020011347 "pll on state mismatch (expected %i, found %i)\n",
11348 pll->on, active);
11349
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011350 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011351 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020011352 enabled_crtcs++;
11353 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11354 active_crtcs++;
11355 }
Rob Clarke2c719b2014-12-15 13:56:32 -050011356 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020011357 "pll active crtcs mismatch (expected %i, found %i)\n",
11358 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050011359 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020011360 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011361 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011362
Rob Clarke2c719b2014-12-15 13:56:32 -050011363 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020011364 sizeof(dpll_hw_state)),
11365 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020011366 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011367}
11368
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011369void
11370intel_modeset_check_state(struct drm_device *dev)
11371{
Damien Lespiau08db6652014-11-04 17:06:52 +000011372 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011373 check_connector_state(dev);
11374 check_encoder_state(dev);
11375 check_crtc_state(dev);
11376 check_shared_dpll_state(dev);
11377}
11378
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011379void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030011380 int dotclock)
11381{
11382 /*
11383 * FDI already provided one idea for the dotclock.
11384 * Yell if the encoder disagrees.
11385 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011386 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030011387 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011388 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030011389}
11390
Ville Syrjälä80715b22014-05-15 20:23:23 +030011391static void update_scanline_offset(struct intel_crtc *crtc)
11392{
11393 struct drm_device *dev = crtc->base.dev;
11394
11395 /*
11396 * The scanline counter increments at the leading edge of hsync.
11397 *
11398 * On most platforms it starts counting from vtotal-1 on the
11399 * first active line. That means the scanline counter value is
11400 * always one less than what we would expect. Ie. just after
11401 * start of vblank, which also occurs at start of hsync (on the
11402 * last active line), the scanline counter will read vblank_start-1.
11403 *
11404 * On gen2 the scanline counter starts counting from 1 instead
11405 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11406 * to keep the value positive), instead of adding one.
11407 *
11408 * On HSW+ the behaviour of the scanline counter depends on the output
11409 * type. For DP ports it behaves like most other platforms, but on HDMI
11410 * there's an extra 1 line difference. So we need to add two instead of
11411 * one to the value.
11412 */
11413 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011414 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030011415 int vtotal;
11416
11417 vtotal = mode->crtc_vtotal;
11418 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
11419 vtotal /= 2;
11420
11421 crtc->scanline_offset = vtotal - 1;
11422 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030011423 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030011424 crtc->scanline_offset = 2;
11425 } else
11426 crtc->scanline_offset = 1;
11427}
11428
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011429static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011430intel_modeset_compute_config(struct drm_crtc *crtc,
11431 struct drm_display_mode *mode,
11432 struct drm_framebuffer *fb,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011433 struct drm_atomic_state *state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011434 unsigned *modeset_pipes,
11435 unsigned *prepare_pipes,
11436 unsigned *disable_pipes)
11437{
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011438 struct drm_device *dev = crtc->dev;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011439 struct intel_crtc_state *pipe_config = NULL;
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011440 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011441 int ret = 0;
11442
11443 ret = drm_atomic_add_affected_connectors(state, crtc);
11444 if (ret)
11445 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011446
11447 intel_modeset_affected_pipes(crtc, modeset_pipes,
11448 prepare_pipes, disable_pipes);
11449
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011450 for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
11451 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
11452 if (IS_ERR(pipe_config))
11453 return pipe_config;
11454
11455 pipe_config->base.enable = false;
11456 }
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011457
11458 /*
11459 * Note this needs changes when we start tracking multiple modes
11460 * and crtcs. At that point we'll need to compute the whole config
11461 * (i.e. one pipe_config for each crtc) rather than just the one
11462 * for this crtc.
11463 */
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011464 for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
11465 /* FIXME: For now we still expect modeset_pipes has at most
11466 * one bit set. */
11467 if (WARN_ON(&intel_crtc->base != crtc))
11468 continue;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011469
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011470 pipe_config = intel_modeset_pipe_config(crtc, fb, mode, state);
11471 if (IS_ERR(pipe_config))
11472 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011473
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011474 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
11475 "[modeset]");
11476 }
11477
11478 return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011479}
11480
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011481static int __intel_set_mode_setup_plls(struct drm_device *dev,
11482 unsigned modeset_pipes,
11483 unsigned disable_pipes)
11484{
11485 struct drm_i915_private *dev_priv = to_i915(dev);
11486 unsigned clear_pipes = modeset_pipes | disable_pipes;
11487 struct intel_crtc *intel_crtc;
11488 int ret = 0;
11489
11490 if (!dev_priv->display.crtc_compute_clock)
11491 return 0;
11492
11493 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
11494 if (ret)
11495 goto done;
11496
11497 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
11498 struct intel_crtc_state *state = intel_crtc->new_config;
11499 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11500 state);
11501 if (ret) {
11502 intel_shared_dpll_abort_config(dev_priv);
11503 goto done;
11504 }
11505 }
11506
11507done:
11508 return ret;
11509}
11510
Daniel Vetterf30da182013-04-11 20:22:50 +020011511static int __intel_set_mode(struct drm_crtc *crtc,
11512 struct drm_display_mode *mode,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011513 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011514 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011515 unsigned modeset_pipes,
11516 unsigned prepare_pipes,
11517 unsigned disable_pipes)
Daniel Vettera6778b32012-07-02 09:56:42 +020011518{
11519 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030011520 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011521 struct drm_display_mode *saved_mode;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011522 struct intel_crtc_state *crtc_state_copy = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020011523 struct intel_crtc *intel_crtc;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011524 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020011525
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011526 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011527 if (!saved_mode)
11528 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020011529
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011530 crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
11531 if (!crtc_state_copy) {
11532 ret = -ENOMEM;
11533 goto done;
11534 }
11535
Tim Gardner3ac18232012-12-07 07:54:26 -070011536 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020011537
Ville Syrjäläb9950a12014-11-21 21:00:36 +020011538 if (modeset_pipes)
11539 to_intel_crtc(crtc)->new_config = pipe_config;
11540
Jesse Barnes30a970c2013-11-04 13:48:12 -080011541 /*
11542 * See if the config requires any additional preparation, e.g.
11543 * to adjust global state with pipes off. We need to do this
11544 * here so we can get the modeset_pipe updated config for the new
11545 * mode set on this crtc. For other crtcs we need to use the
11546 * adjusted_mode bits in the crtc directly.
11547 */
Ville Syrjäläc164f832013-11-05 22:34:12 +020011548 if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +020011549 valleyview_modeset_global_pipes(dev, &prepare_pipes);
Jesse Barnes30a970c2013-11-04 13:48:12 -080011550
Ville Syrjäläc164f832013-11-05 22:34:12 +020011551 /* may have added more to prepare_pipes than we should */
11552 prepare_pipes &= ~disable_pipes;
11553 }
11554
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011555 ret = __intel_set_mode_setup_plls(dev, modeset_pipes, disable_pipes);
11556 if (ret)
11557 goto done;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +020011558
Daniel Vetter460da9162013-03-27 00:44:51 +010011559 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
11560 intel_crtc_disable(&intel_crtc->base);
11561
Daniel Vetterea9d7582012-07-10 10:42:52 +020011562 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011563 if (intel_crtc->base.state->enable)
Daniel Vetterea9d7582012-07-10 10:42:52 +020011564 dev_priv->display.crtc_disable(&intel_crtc->base);
11565 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011566
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020011567 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
11568 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011569 *
11570 * Note we'll need to fix this up when we start tracking multiple
11571 * pipes; here we assume a single modeset_pipe and only track the
11572 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020011573 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011574 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020011575 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011576 /* mode_set/enable/disable functions rely on a correct pipe
11577 * config. */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020011578 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020011579
11580 /*
11581 * Calculate and store various constants which
11582 * are later needed by vblank and swap-completion
11583 * timestamping. They are derived from true hwmode.
11584 */
11585 drm_calc_timestamping_constants(crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011586 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011587 }
Daniel Vetter7758a112012-07-08 19:40:39 +020011588
Daniel Vetterea9d7582012-07-10 10:42:52 +020011589 /* Only after disabling all output pipelines that will be changed can we
11590 * update the the output configuration. */
11591 intel_modeset_update_state(dev, prepare_pipes);
11592
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +020011593 modeset_update_crtc_power_domains(pipe_config->base.state);
Daniel Vetter47fab732012-10-26 10:58:18 +020011594
Daniel Vettera6778b32012-07-02 09:56:42 +020011595 /* Set up the DPLL and any encoders state that needs to adjust or depend
11596 * on the DPLL.
11597 */
Daniel Vetter25c5b262012-07-08 22:08:04 +020011598 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Gustavo Padovan455a6802014-12-01 15:40:11 -080011599 struct drm_plane *primary = intel_crtc->base.primary;
11600 int vdisplay, hdisplay;
Daniel Vetter4c107942014-04-24 23:55:05 +020011601
Gustavo Padovan455a6802014-12-01 15:40:11 -080011602 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
11603 ret = primary->funcs->update_plane(primary, &intel_crtc->base,
11604 fb, 0, 0,
11605 hdisplay, vdisplay,
11606 x << 16, y << 16,
11607 hdisplay << 16, vdisplay << 16);
Daniel Vettera6778b32012-07-02 09:56:42 +020011608 }
11609
11610 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030011611 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11612 update_scanline_offset(intel_crtc);
11613
Daniel Vetter25c5b262012-07-08 22:08:04 +020011614 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030011615 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011616
Daniel Vettera6778b32012-07-02 09:56:42 +020011617 /* FIXME: add subpixel order */
11618done:
Matt Roper83d65732015-02-25 13:12:16 -080011619 if (ret && crtc->state->enable)
Tim Gardner3ac18232012-12-07 07:54:26 -070011620 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020011621
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011622 if (ret == 0 && pipe_config) {
11623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11624
11625 /* The pipe_config will be freed with the atomic state, so
11626 * make a copy. */
11627 memcpy(crtc_state_copy, intel_crtc->config,
11628 sizeof *crtc_state_copy);
11629 intel_crtc->config = crtc_state_copy;
11630 intel_crtc->base.state = &crtc_state_copy->base;
11631
11632 if (modeset_pipes)
11633 intel_crtc->new_config = intel_crtc->config;
11634 } else {
11635 kfree(crtc_state_copy);
11636 }
11637
Tim Gardner3ac18232012-12-07 07:54:26 -070011638 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020011639 return ret;
11640}
11641
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011642static int intel_set_mode_pipes(struct drm_crtc *crtc,
11643 struct drm_display_mode *mode,
11644 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011645 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011646 unsigned modeset_pipes,
11647 unsigned prepare_pipes,
11648 unsigned disable_pipes)
11649{
11650 int ret;
11651
11652 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
11653 prepare_pipes, disable_pipes);
11654
11655 if (ret == 0)
11656 intel_modeset_check_state(crtc->dev);
11657
11658 return ret;
11659}
11660
Damien Lespiaue7457a92013-08-08 22:28:59 +010011661static int intel_set_mode(struct drm_crtc *crtc,
11662 struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011663 int x, int y, struct drm_framebuffer *fb,
11664 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020011665{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011666 struct intel_crtc_state *pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011667 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011668 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020011669
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011670 pipe_config = intel_modeset_compute_config(crtc, mode, fb, state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011671 &modeset_pipes,
11672 &prepare_pipes,
11673 &disable_pipes);
Daniel Vetterf30da182013-04-11 20:22:50 +020011674
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011675 if (IS_ERR(pipe_config)) {
11676 ret = PTR_ERR(pipe_config);
11677 goto out;
11678 }
Daniel Vetterf30da182013-04-11 20:22:50 +020011679
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011680 ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
11681 modeset_pipes, prepare_pipes,
11682 disable_pipes);
11683 if (ret)
11684 goto out;
11685
11686out:
11687 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020011688}
11689
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011690void intel_crtc_restore_mode(struct drm_crtc *crtc)
11691{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011692 struct drm_device *dev = crtc->dev;
11693 struct drm_atomic_state *state;
11694 struct intel_encoder *encoder;
11695 struct intel_connector *connector;
11696 struct drm_connector_state *connector_state;
11697
11698 state = drm_atomic_state_alloc(dev);
11699 if (!state) {
11700 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
11701 crtc->base.id);
11702 return;
11703 }
11704
11705 state->acquire_ctx = dev->mode_config.acquire_ctx;
11706
11707 /* The force restore path in the HW readout code relies on the staged
11708 * config still keeping the user requested config while the actual
11709 * state has been overwritten by the configuration read from HW. We
11710 * need to copy the staged config to the atomic state, otherwise the
11711 * mode set will just reapply the state the HW is already in. */
11712 for_each_intel_encoder(dev, encoder) {
11713 if (&encoder->new_crtc->base != crtc)
11714 continue;
11715
11716 for_each_intel_connector(dev, connector) {
11717 if (connector->new_encoder != encoder)
11718 continue;
11719
11720 connector_state = drm_atomic_get_connector_state(state, &connector->base);
11721 if (IS_ERR(connector_state)) {
11722 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
11723 connector->base.base.id,
11724 connector->base.name,
11725 PTR_ERR(connector_state));
11726 continue;
11727 }
11728
11729 connector_state->crtc = crtc;
11730 connector_state->best_encoder = &encoder->base;
11731 }
11732 }
11733
11734 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
11735 state);
11736
11737 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011738}
11739
Daniel Vetter25c5b262012-07-08 22:08:04 +020011740#undef for_each_intel_crtc_masked
11741
Daniel Vetterd9e55602012-07-04 22:16:09 +020011742static void intel_set_config_free(struct intel_set_config *config)
11743{
11744 if (!config)
11745 return;
11746
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011747 kfree(config->save_connector_encoders);
11748 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020011749 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020011750 kfree(config);
11751}
11752
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011753static int intel_set_config_save_state(struct drm_device *dev,
11754 struct intel_set_config *config)
11755{
Ville Syrjälä76688512014-01-10 11:28:06 +020011756 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011757 struct drm_encoder *encoder;
11758 struct drm_connector *connector;
11759 int count;
11760
Ville Syrjälä76688512014-01-10 11:28:06 +020011761 config->save_crtc_enabled =
11762 kcalloc(dev->mode_config.num_crtc,
11763 sizeof(bool), GFP_KERNEL);
11764 if (!config->save_crtc_enabled)
11765 return -ENOMEM;
11766
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011767 config->save_encoder_crtcs =
11768 kcalloc(dev->mode_config.num_encoder,
11769 sizeof(struct drm_crtc *), GFP_KERNEL);
11770 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011771 return -ENOMEM;
11772
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011773 config->save_connector_encoders =
11774 kcalloc(dev->mode_config.num_connector,
11775 sizeof(struct drm_encoder *), GFP_KERNEL);
11776 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011777 return -ENOMEM;
11778
11779 /* Copy data. Note that driver private data is not affected.
11780 * Should anything bad happen only the expected state is
11781 * restored, not the drivers personal bookkeeping.
11782 */
11783 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010011784 for_each_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011785 config->save_crtc_enabled[count++] = crtc->state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011786 }
11787
11788 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011789 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011790 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011791 }
11792
11793 count = 0;
11794 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011795 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011796 }
11797
11798 return 0;
11799}
11800
11801static void intel_set_config_restore_state(struct drm_device *dev,
11802 struct intel_set_config *config)
11803{
Ville Syrjälä76688512014-01-10 11:28:06 +020011804 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011805 struct intel_encoder *encoder;
11806 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011807 int count;
11808
11809 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011810 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011811 crtc->new_enabled = config->save_crtc_enabled[count++];
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011812
11813 if (crtc->new_enabled)
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011814 crtc->new_config = crtc->config;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011815 else
11816 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020011817 }
11818
11819 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010011820 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011821 encoder->new_crtc =
11822 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011823 }
11824
11825 count = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011826 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011827 connector->new_encoder =
11828 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011829 }
11830}
11831
Imre Deake3de42b2013-05-03 19:44:07 +020011832static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010011833is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020011834{
11835 int i;
11836
Chris Wilson2e57f472013-07-17 12:14:40 +010011837 if (set->num_connectors == 0)
11838 return false;
11839
11840 if (WARN_ON(set->connectors == NULL))
11841 return false;
11842
11843 for (i = 0; i < set->num_connectors; i++)
11844 if (set->connectors[i]->encoder &&
11845 set->connectors[i]->encoder->crtc == set->crtc &&
11846 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020011847 return true;
11848
11849 return false;
11850}
11851
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011852static void
11853intel_set_config_compute_mode_changes(struct drm_mode_set *set,
11854 struct intel_set_config *config)
11855{
11856
11857 /* We should be able to check here if the fb has the same properties
11858 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010011859 if (is_crtc_connector_off(set)) {
11860 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070011861 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070011862 /*
11863 * If we have no fb, we can only flip as long as the crtc is
11864 * active, otherwise we need a full mode set. The crtc may
11865 * be active if we've only disabled the primary plane, or
11866 * in fastboot situations.
11867 */
Matt Roperf4510a22014-04-01 15:22:40 -070011868 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030011869 struct intel_crtc *intel_crtc =
11870 to_intel_crtc(set->crtc);
11871
Matt Roper3b150f02014-05-29 08:06:53 -070011872 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030011873 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
11874 config->fb_changed = true;
11875 } else {
11876 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
11877 config->mode_changed = true;
11878 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011879 } else if (set->fb == NULL) {
11880 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010011881 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070011882 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011883 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020011884 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011885 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020011886 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011887 }
11888
Daniel Vetter835c5872012-07-10 18:11:08 +020011889 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011890 config->fb_changed = true;
11891
11892 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
11893 DRM_DEBUG_KMS("modes are different, full mode set\n");
11894 drm_mode_debug_printmodeline(&set->crtc->mode);
11895 drm_mode_debug_printmodeline(set->mode);
11896 config->mode_changed = true;
11897 }
Chris Wilsona1d95702013-08-13 18:48:47 +010011898
11899 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
11900 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011901}
11902
Daniel Vetter2e431052012-07-04 22:42:15 +020011903static int
Daniel Vetter9a935852012-07-05 22:34:27 +020011904intel_modeset_stage_output_state(struct drm_device *dev,
11905 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020011906 struct intel_set_config *config,
11907 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020011908{
Daniel Vetter9a935852012-07-05 22:34:27 +020011909 struct intel_connector *connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020011910 struct drm_connector_state *connector_state;
Daniel Vetter9a935852012-07-05 22:34:27 +020011911 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020011912 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030011913 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020011914
Damien Lespiau9abdda72013-02-13 13:29:23 +000011915 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020011916 * of connectors. For paranoia, double-check this. */
11917 WARN_ON(!set->fb && (set->num_connectors != 0));
11918 WARN_ON(set->fb && (set->num_connectors == 0));
11919
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011920 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011921 /* Otherwise traverse passed in connector list and get encoders
11922 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020011923 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011924 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100011925 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020011926 break;
11927 }
11928 }
11929
Daniel Vetter9a935852012-07-05 22:34:27 +020011930 /* If we disable the crtc, disable all its connectors. Also, if
11931 * the connector is on the changing crtc but not on the new
11932 * connector list, disable it. */
11933 if ((!set->fb || ro == set->num_connectors) &&
11934 connector->base.encoder &&
11935 connector->base.encoder->crtc == set->crtc) {
11936 connector->new_encoder = NULL;
11937
11938 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11939 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011940 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020011941 }
11942
11943
11944 if (&connector->new_encoder->base != connector->base.encoder) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020011945 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
11946 connector->base.base.id,
11947 connector->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011948 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020011949 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011950 }
11951 /* connector->new_encoder is now updated for all connectors. */
11952
11953 /* Update crtc of enabled connectors. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011954 for_each_intel_connector(dev, connector) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011955 struct drm_crtc *new_crtc;
11956
Daniel Vetter9a935852012-07-05 22:34:27 +020011957 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020011958 continue;
11959
Daniel Vetter9a935852012-07-05 22:34:27 +020011960 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020011961
11962 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011963 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020011964 new_crtc = set->crtc;
11965 }
11966
11967 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010011968 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11969 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011970 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020011971 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011972 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020011973
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020011974 connector_state =
11975 drm_atomic_get_connector_state(state, &connector->base);
11976 if (IS_ERR(connector_state))
11977 return PTR_ERR(connector_state);
11978
11979 connector_state->crtc = new_crtc;
11980 connector_state->best_encoder = &connector->new_encoder->base;
11981
Daniel Vetter9a935852012-07-05 22:34:27 +020011982 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11983 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011984 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020011985 new_crtc->base.id);
11986 }
11987
11988 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010011989 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011990 int num_connectors = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011991 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011992 if (connector->new_encoder == encoder) {
11993 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011994 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020011995 }
11996 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011997
11998 if (num_connectors == 0)
11999 encoder->new_crtc = NULL;
12000 else if (num_connectors > 1)
12001 return -EINVAL;
12002
Daniel Vetter9a935852012-07-05 22:34:27 +020012003 /* Only now check for crtc changes so we don't miss encoders
12004 * that will be disabled. */
12005 if (&encoder->new_crtc->base != encoder->base.crtc) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012006 DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
12007 encoder->base.base.id,
12008 encoder->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012009 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020012010 }
12011 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012012 /* Now we've also updated encoder->new_crtc for all encoders. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012013 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012014 connector_state =
12015 drm_atomic_get_connector_state(state, &connector->base);
Ander Conselvan de Oliveira9d918c12015-03-27 15:33:51 +020012016 if (IS_ERR(connector_state))
12017 return PTR_ERR(connector_state);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012018
12019 if (connector->new_encoder) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012020 if (connector->new_encoder != connector->encoder)
12021 connector->encoder = connector->new_encoder;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012022 } else {
12023 connector_state->crtc = NULL;
12024 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012025 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012026 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012027 crtc->new_enabled = false;
12028
Damien Lespiaub2784e12014-08-05 11:29:37 +010012029 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012030 if (encoder->new_crtc == crtc) {
12031 crtc->new_enabled = true;
12032 break;
12033 }
12034 }
12035
Matt Roper83d65732015-02-25 13:12:16 -080012036 if (crtc->new_enabled != crtc->base.state->enable) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012037 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
12038 crtc->base.base.id,
Ville Syrjälä76688512014-01-10 11:28:06 +020012039 crtc->new_enabled ? "en" : "dis");
12040 config->mode_changed = true;
12041 }
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020012042
12043 if (crtc->new_enabled)
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012044 crtc->new_config = crtc->config;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020012045 else
12046 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020012047 }
12048
Daniel Vetter2e431052012-07-04 22:42:15 +020012049 return 0;
12050}
12051
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012052static void disable_crtc_nofb(struct intel_crtc *crtc)
12053{
12054 struct drm_device *dev = crtc->base.dev;
12055 struct intel_encoder *encoder;
12056 struct intel_connector *connector;
12057
12058 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
12059 pipe_name(crtc->pipe));
12060
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012061 for_each_intel_connector(dev, connector) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012062 if (connector->new_encoder &&
12063 connector->new_encoder->new_crtc == crtc)
12064 connector->new_encoder = NULL;
12065 }
12066
Damien Lespiaub2784e12014-08-05 11:29:37 +010012067 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012068 if (encoder->new_crtc == crtc)
12069 encoder->new_crtc = NULL;
12070 }
12071
12072 crtc->new_enabled = false;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020012073 crtc->new_config = NULL;
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012074}
12075
Daniel Vetter2e431052012-07-04 22:42:15 +020012076static int intel_crtc_set_config(struct drm_mode_set *set)
12077{
12078 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020012079 struct drm_mode_set save_set;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012080 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020012081 struct intel_set_config *config;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012082 struct intel_crtc_state *pipe_config;
Jesse Barnes50f52752014-11-07 13:11:00 -080012083 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Daniel Vetter2e431052012-07-04 22:42:15 +020012084 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020012085
Daniel Vetter8d3e3752012-07-05 16:09:09 +020012086 BUG_ON(!set);
12087 BUG_ON(!set->crtc);
12088 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020012089
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010012090 /* Enforce sane interface api - has been abused by the fb helper. */
12091 BUG_ON(!set->mode && set->fb);
12092 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f72012-07-10 17:53:42 +020012093
Daniel Vetter2e431052012-07-04 22:42:15 +020012094 if (set->fb) {
12095 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12096 set->crtc->base.id, set->fb->base.id,
12097 (int)set->num_connectors, set->x, set->y);
12098 } else {
12099 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020012100 }
12101
12102 dev = set->crtc->dev;
12103
12104 ret = -ENOMEM;
12105 config = kzalloc(sizeof(*config), GFP_KERNEL);
12106 if (!config)
12107 goto out_config;
12108
12109 ret = intel_set_config_save_state(dev, config);
12110 if (ret)
12111 goto out_config;
12112
12113 save_set.crtc = set->crtc;
12114 save_set.mode = &set->crtc->mode;
12115 save_set.x = set->crtc->x;
12116 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070012117 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020012118
12119 /* Compute whether we need a full modeset, only an fb base update or no
12120 * change at all. In the future we might also check whether only the
12121 * mode changed, e.g. for LVDS where we only change the panel fitter in
12122 * such cases. */
12123 intel_set_config_compute_mode_changes(set, config);
12124
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012125 state = drm_atomic_state_alloc(dev);
12126 if (!state) {
12127 ret = -ENOMEM;
12128 goto out_config;
12129 }
12130
12131 state->acquire_ctx = dev->mode_config.acquire_ctx;
12132
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012133 ret = intel_modeset_stage_output_state(dev, set, config, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020012134 if (ret)
12135 goto fail;
12136
Jesse Barnes50f52752014-11-07 13:11:00 -080012137 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012138 set->fb, state,
Jesse Barnes50f52752014-11-07 13:11:00 -080012139 &modeset_pipes,
12140 &prepare_pipes,
12141 &disable_pipes);
Jesse Barnes20664592014-11-05 14:26:09 -080012142 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080012143 ret = PTR_ERR(pipe_config);
Jesse Barnes50f52752014-11-07 13:11:00 -080012144 goto fail;
Jesse Barnes20664592014-11-05 14:26:09 -080012145 } else if (pipe_config) {
Ville Syrjäläb9950a12014-11-21 21:00:36 +020012146 if (pipe_config->has_audio !=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012147 to_intel_crtc(set->crtc)->config->has_audio)
Jesse Barnes20664592014-11-05 14:26:09 -080012148 config->mode_changed = true;
12149
Jesse Barnesaf15d2c2014-12-01 09:54:28 -080012150 /*
12151 * Note we have an issue here with infoframes: current code
12152 * only updates them on the full mode set path per hw
12153 * requirements. So here we should be checking for any
12154 * required changes and forcing a mode set.
12155 */
Jesse Barnes20664592014-11-05 14:26:09 -080012156 }
Jesse Barnes50f52752014-11-07 13:11:00 -080012157
Jesse Barnes1f9954d2014-11-05 14:26:10 -080012158 intel_update_pipe_size(to_intel_crtc(set->crtc));
12159
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012160 if (config->mode_changed) {
Jesse Barnes50f52752014-11-07 13:11:00 -080012161 ret = intel_set_mode_pipes(set->crtc, set->mode,
12162 set->x, set->y, set->fb, pipe_config,
12163 modeset_pipes, prepare_pipes,
12164 disable_pipes);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012165 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070012166 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080012167 struct drm_plane *primary = set->crtc->primary;
12168 int vdisplay, hdisplay;
Matt Roper3b150f02014-05-29 08:06:53 -070012169
Gustavo Padovan455a6802014-12-01 15:40:11 -080012170 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
12171 ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
12172 0, 0, hdisplay, vdisplay,
12173 set->x << 16, set->y << 16,
12174 hdisplay << 16, vdisplay << 16);
Matt Roper3b150f02014-05-29 08:06:53 -070012175
12176 /*
12177 * We need to make sure the primary plane is re-enabled if it
12178 * has previously been turned off.
12179 */
12180 if (!intel_crtc->primary_enabled && ret == 0) {
12181 WARN_ON(!intel_crtc->active);
Ville Syrjäläfdd508a62014-08-08 21:51:11 +030012182 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012183 }
12184
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012185 /*
12186 * In the fastboot case this may be our only check of the
12187 * state after boot. It would be better to only do it on
12188 * the first update, but we don't have a nice way of doing that
12189 * (and really, set_config isn't used much for high freq page
12190 * flipping, so increasing its cost here shouldn't be a big
12191 * deal).
12192 */
Jani Nikulad330a952014-01-21 11:24:25 +020012193 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012194 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020012195 }
12196
Chris Wilson2d05eae2013-05-03 17:36:25 +010012197 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020012198 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12199 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020012200fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010012201 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012202
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012203 drm_atomic_state_clear(state);
12204
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012205 /*
12206 * HACK: if the pipe was on, but we didn't have a framebuffer,
12207 * force the pipe off to avoid oopsing in the modeset code
12208 * due to fb==NULL. This should only happen during boot since
12209 * we don't yet reconstruct the FB from the hardware state.
12210 */
12211 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
12212 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
12213
Chris Wilson2d05eae2013-05-03 17:36:25 +010012214 /* Try to restore the config */
12215 if (config->mode_changed &&
12216 intel_set_mode(save_set.crtc, save_set.mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012217 save_set.x, save_set.y, save_set.fb,
12218 state))
Chris Wilson2d05eae2013-05-03 17:36:25 +010012219 DRM_ERROR("failed to restore config after modeset failure\n");
12220 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012221
Daniel Vetterd9e55602012-07-04 22:16:09 +020012222out_config:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012223 if (state)
12224 drm_atomic_state_free(state);
12225
Daniel Vetterd9e55602012-07-04 22:16:09 +020012226 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012227 return ret;
12228}
12229
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012230static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012231 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020012232 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012233 .destroy = intel_crtc_destroy,
12234 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080012235 .atomic_duplicate_state = intel_crtc_duplicate_state,
12236 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012237};
12238
Daniel Vetter53589012013-06-05 13:34:16 +020012239static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
12240 struct intel_shared_dpll *pll,
12241 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012242{
Daniel Vetter53589012013-06-05 13:34:16 +020012243 uint32_t val;
12244
Daniel Vetterf458ebb2014-09-30 10:56:39 +020012245 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030012246 return false;
12247
Daniel Vetter53589012013-06-05 13:34:16 +020012248 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020012249 hw_state->dpll = val;
12250 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
12251 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020012252
12253 return val & DPLL_VCO_ENABLE;
12254}
12255
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012256static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
12257 struct intel_shared_dpll *pll)
12258{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012259 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
12260 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012261}
12262
Daniel Vettere7b903d2013-06-05 13:34:14 +020012263static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
12264 struct intel_shared_dpll *pll)
12265{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012266 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020012267 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020012268
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012269 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012270
12271 /* Wait for the clocks to stabilize. */
12272 POSTING_READ(PCH_DPLL(pll->id));
12273 udelay(150);
12274
12275 /* The pixel multiplier can only be updated once the
12276 * DPLL is enabled and the clocks are stable.
12277 *
12278 * So write it again.
12279 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012280 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012281 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012282 udelay(200);
12283}
12284
12285static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
12286 struct intel_shared_dpll *pll)
12287{
12288 struct drm_device *dev = dev_priv->dev;
12289 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020012290
12291 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012292 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020012293 if (intel_crtc_to_shared_dpll(crtc) == pll)
12294 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
12295 }
12296
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012297 I915_WRITE(PCH_DPLL(pll->id), 0);
12298 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012299 udelay(200);
12300}
12301
Daniel Vetter46edb022013-06-05 13:34:12 +020012302static char *ibx_pch_dpll_names[] = {
12303 "PCH DPLL A",
12304 "PCH DPLL B",
12305};
12306
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012307static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012308{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012309 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012310 int i;
12311
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012312 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012313
Daniel Vettere72f9fb2013-06-05 13:34:06 +020012314 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020012315 dev_priv->shared_dplls[i].id = i;
12316 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012317 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020012318 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
12319 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020012320 dev_priv->shared_dplls[i].get_hw_state =
12321 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012322 }
12323}
12324
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012325static void intel_shared_dpll_init(struct drm_device *dev)
12326{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012327 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012328
Daniel Vetter9cd86932014-06-25 22:01:57 +030012329 if (HAS_DDI(dev))
12330 intel_ddi_pll_init(dev);
12331 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012332 ibx_pch_dpll_init(dev);
12333 else
12334 dev_priv->num_shared_dpll = 0;
12335
12336 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012337}
12338
Matt Roper6beb8c232014-12-01 15:40:14 -080012339/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000012340 * intel_wm_need_update - Check whether watermarks need updating
12341 * @plane: drm plane
12342 * @state: new plane state
12343 *
12344 * Check current plane state versus the new one to determine whether
12345 * watermarks need to be recalculated.
12346 *
12347 * Returns true or false.
12348 */
12349bool intel_wm_need_update(struct drm_plane *plane,
12350 struct drm_plane_state *state)
12351{
12352 /* Update watermarks on tiling changes. */
12353 if (!plane->state->fb || !state->fb ||
12354 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
12355 plane->state->rotation != state->rotation)
12356 return true;
12357
12358 return false;
12359}
12360
12361/**
Matt Roper6beb8c232014-12-01 15:40:14 -080012362 * intel_prepare_plane_fb - Prepare fb for usage on plane
12363 * @plane: drm plane to prepare for
12364 * @fb: framebuffer to prepare for presentation
12365 *
12366 * Prepares a framebuffer for usage on a display plane. Generally this
12367 * involves pinning the underlying object and updating the frontbuffer tracking
12368 * bits. Some older platforms need special physical address handling for
12369 * cursor planes.
12370 *
12371 * Returns 0 on success, negative error code on failure.
12372 */
12373int
12374intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000012375 struct drm_framebuffer *fb,
12376 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070012377{
12378 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080012379 struct intel_plane *intel_plane = to_intel_plane(plane);
12380 enum pipe pipe = intel_plane->pipe;
12381 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12382 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
12383 unsigned frontbuffer_bits = 0;
12384 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070012385
Matt Roperea2c67b2014-12-23 10:41:52 -080012386 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070012387 return 0;
12388
Matt Roper6beb8c232014-12-01 15:40:14 -080012389 switch (plane->type) {
12390 case DRM_PLANE_TYPE_PRIMARY:
12391 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
12392 break;
12393 case DRM_PLANE_TYPE_CURSOR:
12394 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
12395 break;
12396 case DRM_PLANE_TYPE_OVERLAY:
12397 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
12398 break;
12399 }
Matt Roper465c1202014-05-29 08:06:54 -070012400
Matt Roper4c345742014-07-09 16:22:10 -070012401 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070012402
Matt Roper6beb8c232014-12-01 15:40:14 -080012403 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
12404 INTEL_INFO(dev)->cursor_needs_physical) {
12405 int align = IS_I830(dev) ? 16 * 1024 : 256;
12406 ret = i915_gem_object_attach_phys(obj, align);
12407 if (ret)
12408 DRM_DEBUG_KMS("failed to attach phys object\n");
12409 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000012410 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080012411 }
12412
12413 if (ret == 0)
12414 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
12415
12416 mutex_unlock(&dev->struct_mutex);
12417
12418 return ret;
12419}
12420
Matt Roper38f3ce32014-12-02 07:45:25 -080012421/**
12422 * intel_cleanup_plane_fb - Cleans up an fb after plane use
12423 * @plane: drm plane to clean up for
12424 * @fb: old framebuffer that was on plane
12425 *
12426 * Cleans up a framebuffer that has just been removed from a plane.
12427 */
12428void
12429intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000012430 struct drm_framebuffer *fb,
12431 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080012432{
12433 struct drm_device *dev = plane->dev;
12434 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12435
12436 if (WARN_ON(!obj))
12437 return;
12438
12439 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
12440 !INTEL_INFO(dev)->cursor_needs_physical) {
12441 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000012442 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080012443 mutex_unlock(&dev->struct_mutex);
12444 }
Matt Roper465c1202014-05-29 08:06:54 -070012445}
12446
12447static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012448intel_check_primary_plane(struct drm_plane *plane,
12449 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070012450{
Matt Roper32b7eee2014-12-24 07:59:06 -080012451 struct drm_device *dev = plane->dev;
12452 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080012453 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080012454 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080012455 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012456 struct drm_rect *dest = &state->dst;
12457 struct drm_rect *src = &state->src;
12458 const struct drm_rect *clip = &state->clip;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012459 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012460
Matt Roperea2c67b2014-12-23 10:41:52 -080012461 crtc = crtc ? crtc : plane->crtc;
12462 intel_crtc = to_intel_crtc(crtc);
12463
Matt Roperc59cb172014-12-01 15:40:16 -080012464 ret = drm_plane_helper_check_update(plane, crtc, fb,
12465 src, dest, clip,
12466 DRM_PLANE_HELPER_NO_SCALING,
12467 DRM_PLANE_HELPER_NO_SCALING,
12468 false, true, &state->visible);
12469 if (ret)
12470 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012471
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012472 if (intel_crtc->active) {
Matt Roper32b7eee2014-12-24 07:59:06 -080012473 intel_crtc->atomic.wait_for_flips = true;
12474
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012475 /*
12476 * FBC does not work on some platforms for rotated
12477 * planes, so disable it when rotation is not 0 and
12478 * update it when rotation is set back to 0.
12479 *
12480 * FIXME: This is redundant with the fbc update done in
12481 * the primary plane enable function except that that
12482 * one is done too late. We eventually need to unify
12483 * this.
12484 */
12485 if (intel_crtc->primary_enabled &&
12486 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020012487 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080012488 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080012489 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012490 }
12491
12492 if (state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080012493 /*
12494 * BDW signals flip done immediately if the plane
12495 * is disabled, even if the plane enable is already
12496 * armed to occur at the next vblank :(
12497 */
12498 if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
12499 intel_crtc->atomic.wait_vblank = true;
12500 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012501
Matt Roper32b7eee2014-12-24 07:59:06 -080012502 intel_crtc->atomic.fb_bits |=
12503 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
12504
12505 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000012506
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000012507 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000012508 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080012509 }
12510
12511 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070012512}
12513
Sonika Jindal48404c12014-08-22 14:06:04 +053012514static void
12515intel_commit_primary_plane(struct drm_plane *plane,
12516 struct intel_plane_state *state)
12517{
Matt Roper2b875c22014-12-01 15:40:13 -080012518 struct drm_crtc *crtc = state->base.crtc;
12519 struct drm_framebuffer *fb = state->base.fb;
12520 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053012521 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080012522 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053012523 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080012524
Matt Roperea2c67b2014-12-23 10:41:52 -080012525 crtc = crtc ? crtc : plane->crtc;
12526 intel_crtc = to_intel_crtc(crtc);
12527
Matt Ropercf4c7c12014-12-04 10:27:42 -080012528 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053012529 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070012530 crtc->y = src->y1 >> 16;
12531
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012532 if (intel_crtc->active) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012533 if (state->visible) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012534 /* FIXME: kill this fastboot hack */
12535 intel_update_pipe_size(intel_crtc);
12536
12537 intel_crtc->primary_enabled = true;
12538
12539 dev_priv->display.update_primary_plane(crtc, plane->fb,
12540 crtc->x, crtc->y);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012541 } else {
12542 /*
12543 * If clipping results in a non-visible primary plane,
12544 * we'll disable the primary plane. Note that this is
12545 * a bit different than what happens if userspace
12546 * explicitly disables the plane by passing fb=0
12547 * because plane->fb still gets set and pinned.
12548 */
12549 intel_disable_primary_hw_plane(plane, crtc);
12550 }
Matt Roper32b7eee2014-12-24 07:59:06 -080012551 }
12552}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012553
Matt Roper32b7eee2014-12-24 07:59:06 -080012554static void intel_begin_crtc_commit(struct drm_crtc *crtc)
12555{
12556 struct drm_device *dev = crtc->dev;
12557 struct drm_i915_private *dev_priv = dev->dev_private;
12558 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080012559 struct intel_plane *intel_plane;
12560 struct drm_plane *p;
12561 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012562
Matt Roperea2c67b2014-12-23 10:41:52 -080012563 /* Track fb's for any planes being disabled */
12564 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
12565 intel_plane = to_intel_plane(p);
12566
12567 if (intel_crtc->atomic.disabled_planes &
12568 (1 << drm_plane_index(p))) {
12569 switch (p->type) {
12570 case DRM_PLANE_TYPE_PRIMARY:
12571 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
12572 break;
12573 case DRM_PLANE_TYPE_CURSOR:
12574 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
12575 break;
12576 case DRM_PLANE_TYPE_OVERLAY:
12577 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
12578 break;
12579 }
12580
12581 mutex_lock(&dev->struct_mutex);
12582 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
12583 mutex_unlock(&dev->struct_mutex);
12584 }
12585 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012586
Matt Roper32b7eee2014-12-24 07:59:06 -080012587 if (intel_crtc->atomic.wait_for_flips)
12588 intel_crtc_wait_for_pending_flips(crtc);
12589
12590 if (intel_crtc->atomic.disable_fbc)
12591 intel_fbc_disable(dev);
12592
12593 if (intel_crtc->atomic.pre_disable_primary)
12594 intel_pre_disable_primary(crtc);
12595
12596 if (intel_crtc->atomic.update_wm)
12597 intel_update_watermarks(crtc);
12598
12599 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080012600
12601 /* Perform vblank evasion around commit operation */
12602 if (intel_crtc->active)
12603 intel_crtc->atomic.evade =
12604 intel_pipe_update_start(intel_crtc,
12605 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080012606}
12607
12608static void intel_finish_crtc_commit(struct drm_crtc *crtc)
12609{
12610 struct drm_device *dev = crtc->dev;
12611 struct drm_i915_private *dev_priv = dev->dev_private;
12612 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12613 struct drm_plane *p;
12614
Matt Roperc34c9ee2014-12-23 10:41:50 -080012615 if (intel_crtc->atomic.evade)
12616 intel_pipe_update_end(intel_crtc,
12617 intel_crtc->atomic.start_vbl_count);
12618
Matt Roper32b7eee2014-12-24 07:59:06 -080012619 intel_runtime_pm_put(dev_priv);
12620
12621 if (intel_crtc->atomic.wait_vblank)
12622 intel_wait_for_vblank(dev, intel_crtc->pipe);
12623
12624 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
12625
12626 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012627 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020012628 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012629 mutex_unlock(&dev->struct_mutex);
12630 }
Matt Roper465c1202014-05-29 08:06:54 -070012631
Matt Roper32b7eee2014-12-24 07:59:06 -080012632 if (intel_crtc->atomic.post_enable_primary)
12633 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012634
Matt Roper32b7eee2014-12-24 07:59:06 -080012635 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
12636 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
12637 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
12638 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012639
Matt Roper32b7eee2014-12-24 07:59:06 -080012640 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012641}
12642
Matt Ropercf4c7c12014-12-04 10:27:42 -080012643/**
Matt Roper4a3b8762014-12-23 10:41:51 -080012644 * intel_plane_destroy - destroy a plane
12645 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080012646 *
Matt Roper4a3b8762014-12-23 10:41:51 -080012647 * Common destruction function for all types of planes (primary, cursor,
12648 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080012649 */
Matt Roper4a3b8762014-12-23 10:41:51 -080012650void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070012651{
12652 struct intel_plane *intel_plane = to_intel_plane(plane);
12653 drm_plane_cleanup(plane);
12654 kfree(intel_plane);
12655}
12656
Matt Roper65a3fea2015-01-21 16:35:42 -080012657const struct drm_plane_funcs intel_plane_funcs = {
Daniel Vetterff42e092015-03-02 16:35:20 +010012658 .update_plane = drm_plane_helper_update,
12659 .disable_plane = drm_plane_helper_disable,
Matt Roper3d7d6512014-06-10 08:28:13 -070012660 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080012661 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080012662 .atomic_get_property = intel_plane_atomic_get_property,
12663 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080012664 .atomic_duplicate_state = intel_plane_duplicate_state,
12665 .atomic_destroy_state = intel_plane_destroy_state,
12666
Matt Roper465c1202014-05-29 08:06:54 -070012667};
12668
12669static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
12670 int pipe)
12671{
12672 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080012673 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070012674 const uint32_t *intel_primary_formats;
12675 int num_formats;
12676
12677 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
12678 if (primary == NULL)
12679 return NULL;
12680
Matt Roper8e7d6882015-01-21 16:35:41 -080012681 state = intel_create_plane_state(&primary->base);
12682 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080012683 kfree(primary);
12684 return NULL;
12685 }
Matt Roper8e7d6882015-01-21 16:35:41 -080012686 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080012687
Matt Roper465c1202014-05-29 08:06:54 -070012688 primary->can_scale = false;
12689 primary->max_downscale = 1;
12690 primary->pipe = pipe;
12691 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080012692 primary->check_plane = intel_check_primary_plane;
12693 primary->commit_plane = intel_commit_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070012694 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
12695 primary->plane = !pipe;
12696
12697 if (INTEL_INFO(dev)->gen <= 3) {
12698 intel_primary_formats = intel_primary_formats_gen2;
12699 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
12700 } else {
12701 intel_primary_formats = intel_primary_formats_gen4;
12702 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
12703 }
12704
12705 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080012706 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070012707 intel_primary_formats, num_formats,
12708 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053012709
12710 if (INTEL_INFO(dev)->gen >= 4) {
12711 if (!dev->mode_config.rotation_property)
12712 dev->mode_config.rotation_property =
12713 drm_mode_create_rotation_property(dev,
12714 BIT(DRM_ROTATE_0) |
12715 BIT(DRM_ROTATE_180));
12716 if (dev->mode_config.rotation_property)
12717 drm_object_attach_property(&primary->base.base,
12718 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080012719 state->base.rotation);
Sonika Jindal48404c12014-08-22 14:06:04 +053012720 }
12721
Matt Roperea2c67b2014-12-23 10:41:52 -080012722 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
12723
Matt Roper465c1202014-05-29 08:06:54 -070012724 return &primary->base;
12725}
12726
Matt Roper3d7d6512014-06-10 08:28:13 -070012727static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030012728intel_check_cursor_plane(struct drm_plane *plane,
12729 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070012730{
Matt Roper2b875c22014-12-01 15:40:13 -080012731 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080012732 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080012733 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012734 struct drm_rect *dest = &state->dst;
12735 struct drm_rect *src = &state->src;
12736 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012737 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080012738 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012739 unsigned stride;
12740 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012741
Matt Roperea2c67b2014-12-23 10:41:52 -080012742 crtc = crtc ? crtc : plane->crtc;
12743 intel_crtc = to_intel_crtc(crtc);
12744
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012745 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030012746 src, dest, clip,
12747 DRM_PLANE_HELPER_NO_SCALING,
12748 DRM_PLANE_HELPER_NO_SCALING,
12749 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012750 if (ret)
12751 return ret;
12752
12753
12754 /* if we want to turn off the cursor ignore width and height */
12755 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080012756 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012757
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012758 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080012759 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
12760 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
12761 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012762 return -EINVAL;
12763 }
12764
Matt Roperea2c67b2014-12-23 10:41:52 -080012765 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
12766 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012767 DRM_DEBUG_KMS("buffer is too small\n");
12768 return -ENOMEM;
12769 }
12770
Ville Syrjälä3a656b52015-03-09 21:08:37 +020012771 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012772 DRM_DEBUG_KMS("cursor cannot be tiled\n");
12773 ret = -EINVAL;
12774 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012775
Matt Roper32b7eee2014-12-24 07:59:06 -080012776finish:
12777 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020012778 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080012779 intel_crtc->atomic.update_wm = true;
12780
12781 intel_crtc->atomic.fb_bits |=
12782 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
12783 }
12784
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012785 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012786}
12787
Matt Roperf4a2cf22014-12-01 15:40:12 -080012788static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030012789intel_commit_cursor_plane(struct drm_plane *plane,
12790 struct intel_plane_state *state)
12791{
Matt Roper2b875c22014-12-01 15:40:13 -080012792 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080012793 struct drm_device *dev = plane->dev;
12794 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080012795 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080012796 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070012797
Matt Roperea2c67b2014-12-23 10:41:52 -080012798 crtc = crtc ? crtc : plane->crtc;
12799 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070012800
Matt Roperea2c67b2014-12-23 10:41:52 -080012801 plane->fb = state->base.fb;
12802 crtc->cursor_x = state->base.crtc_x;
12803 crtc->cursor_y = state->base.crtc_y;
12804
Gustavo Padovana912f122014-12-01 15:40:10 -080012805 if (intel_crtc->cursor_bo == obj)
12806 goto update;
12807
Matt Roperf4a2cf22014-12-01 15:40:12 -080012808 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080012809 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080012810 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080012811 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080012812 else
Gustavo Padovana912f122014-12-01 15:40:10 -080012813 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080012814
Gustavo Padovana912f122014-12-01 15:40:10 -080012815 intel_crtc->cursor_addr = addr;
12816 intel_crtc->cursor_bo = obj;
12817update:
Gustavo Padovana912f122014-12-01 15:40:10 -080012818
Matt Roper32b7eee2014-12-24 07:59:06 -080012819 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030012820 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070012821}
Gustavo Padovan852e7872014-09-05 17:22:31 -030012822
Matt Roper3d7d6512014-06-10 08:28:13 -070012823static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
12824 int pipe)
12825{
12826 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080012827 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070012828
12829 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
12830 if (cursor == NULL)
12831 return NULL;
12832
Matt Roper8e7d6882015-01-21 16:35:41 -080012833 state = intel_create_plane_state(&cursor->base);
12834 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080012835 kfree(cursor);
12836 return NULL;
12837 }
Matt Roper8e7d6882015-01-21 16:35:41 -080012838 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080012839
Matt Roper3d7d6512014-06-10 08:28:13 -070012840 cursor->can_scale = false;
12841 cursor->max_downscale = 1;
12842 cursor->pipe = pipe;
12843 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080012844 cursor->check_plane = intel_check_cursor_plane;
12845 cursor->commit_plane = intel_commit_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070012846
12847 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080012848 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070012849 intel_cursor_formats,
12850 ARRAY_SIZE(intel_cursor_formats),
12851 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070012852
12853 if (INTEL_INFO(dev)->gen >= 4) {
12854 if (!dev->mode_config.rotation_property)
12855 dev->mode_config.rotation_property =
12856 drm_mode_create_rotation_property(dev,
12857 BIT(DRM_ROTATE_0) |
12858 BIT(DRM_ROTATE_180));
12859 if (dev->mode_config.rotation_property)
12860 drm_object_attach_property(&cursor->base.base,
12861 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080012862 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070012863 }
12864
Matt Roperea2c67b2014-12-23 10:41:52 -080012865 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
12866
Matt Roper3d7d6512014-06-10 08:28:13 -070012867 return &cursor->base;
12868}
12869
Hannes Ederb358d0a2008-12-18 21:18:47 +010012870static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080012871{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012872 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080012873 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012874 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070012875 struct drm_plane *primary = NULL;
12876 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070012877 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080012878
Daniel Vetter955382f2013-09-19 14:05:45 +020012879 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080012880 if (intel_crtc == NULL)
12881 return;
12882
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012883 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
12884 if (!crtc_state)
12885 goto fail;
12886 intel_crtc_set_state(intel_crtc, crtc_state);
Matt Roper07878242015-02-25 11:43:26 -080012887 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012888
Matt Roper465c1202014-05-29 08:06:54 -070012889 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070012890 if (!primary)
12891 goto fail;
12892
12893 cursor = intel_cursor_plane_create(dev, pipe);
12894 if (!cursor)
12895 goto fail;
12896
Matt Roper465c1202014-05-29 08:06:54 -070012897 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070012898 cursor, &intel_crtc_funcs);
12899 if (ret)
12900 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080012901
12902 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080012903 for (i = 0; i < 256; i++) {
12904 intel_crtc->lut_r[i] = i;
12905 intel_crtc->lut_g[i] = i;
12906 intel_crtc->lut_b[i] = i;
12907 }
12908
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020012909 /*
12910 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020012911 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020012912 */
Jesse Barnes80824002009-09-10 15:28:06 -070012913 intel_crtc->pipe = pipe;
12914 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010012915 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080012916 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010012917 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070012918 }
12919
Chris Wilson4b0e3332014-05-30 16:35:26 +030012920 intel_crtc->cursor_base = ~0;
12921 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030012922 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030012923
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080012924 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
12925 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
12926 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
12927 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
12928
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020012929 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
12930
Jesse Barnes79e53942008-11-07 14:24:08 -080012931 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020012932
12933 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070012934 return;
12935
12936fail:
12937 if (primary)
12938 drm_plane_cleanup(primary);
12939 if (cursor)
12940 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012941 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070012942 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080012943}
12944
Jesse Barnes752aa882013-10-31 18:55:49 +020012945enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
12946{
12947 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012948 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020012949
Rob Clark51fd3712013-11-19 12:10:12 -050012950 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020012951
Ville Syrjäläd3babd32014-11-07 11:16:01 +020012952 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020012953 return INVALID_PIPE;
12954
12955 return to_intel_crtc(encoder->crtc)->pipe;
12956}
12957
Carl Worth08d7b3d2009-04-29 14:43:54 -070012958int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000012959 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070012960{
Carl Worth08d7b3d2009-04-29 14:43:54 -070012961 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040012962 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020012963 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012964
Rob Clark7707e652014-07-17 23:30:04 -040012965 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070012966
Rob Clark7707e652014-07-17 23:30:04 -040012967 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070012968 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030012969 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012970 }
12971
Rob Clark7707e652014-07-17 23:30:04 -040012972 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020012973 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012974
Daniel Vetterc05422d2009-08-11 16:05:30 +020012975 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012976}
12977
Daniel Vetter66a92782012-07-12 20:08:18 +020012978static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080012979{
Daniel Vetter66a92782012-07-12 20:08:18 +020012980 struct drm_device *dev = encoder->base.dev;
12981 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080012982 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080012983 int entry = 0;
12984
Damien Lespiaub2784e12014-08-05 11:29:37 +010012985 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012986 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020012987 index_mask |= (1 << entry);
12988
Jesse Barnes79e53942008-11-07 14:24:08 -080012989 entry++;
12990 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010012991
Jesse Barnes79e53942008-11-07 14:24:08 -080012992 return index_mask;
12993}
12994
Chris Wilson4d302442010-12-14 19:21:29 +000012995static bool has_edp_a(struct drm_device *dev)
12996{
12997 struct drm_i915_private *dev_priv = dev->dev_private;
12998
12999 if (!IS_MOBILE(dev))
13000 return false;
13001
13002 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13003 return false;
13004
Damien Lespiaue3589902014-02-07 19:12:50 +000013005 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013006 return false;
13007
13008 return true;
13009}
13010
Jesse Barnes84b4e042014-06-25 08:24:29 -070013011static bool intel_crt_present(struct drm_device *dev)
13012{
13013 struct drm_i915_private *dev_priv = dev->dev_private;
13014
Damien Lespiau884497e2013-12-03 13:56:23 +000013015 if (INTEL_INFO(dev)->gen >= 9)
13016 return false;
13017
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013018 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013019 return false;
13020
13021 if (IS_CHERRYVIEW(dev))
13022 return false;
13023
13024 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13025 return false;
13026
13027 return true;
13028}
13029
Jesse Barnes79e53942008-11-07 14:24:08 -080013030static void intel_setup_outputs(struct drm_device *dev)
13031{
Eric Anholt725e30a2009-01-22 13:01:02 -080013032 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013033 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013034 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013035
Daniel Vetterc9093352013-06-06 22:22:47 +020013036 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013037
Jesse Barnes84b4e042014-06-25 08:24:29 -070013038 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013039 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013040
Paulo Zanoniaffa9352012-11-23 15:30:39 -020013041 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013042 int found;
13043
Jesse Barnesde31fac2015-03-06 15:53:32 -080013044 /*
13045 * Haswell uses DDI functions to detect digital outputs.
13046 * On SKL pre-D0 the strap isn't connected, so we assume
13047 * it's there.
13048 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013049 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013050 /* WaIgnoreDDIAStrap: skl */
13051 if (found ||
13052 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013053 intel_ddi_init(dev, PORT_A);
13054
13055 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13056 * register */
13057 found = I915_READ(SFUSE_STRAP);
13058
13059 if (found & SFUSE_STRAP_DDIB_DETECTED)
13060 intel_ddi_init(dev, PORT_B);
13061 if (found & SFUSE_STRAP_DDIC_DETECTED)
13062 intel_ddi_init(dev, PORT_C);
13063 if (found & SFUSE_STRAP_DDID_DETECTED)
13064 intel_ddi_init(dev, PORT_D);
13065 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013066 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013067 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013068
13069 if (has_edp_a(dev))
13070 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013071
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013072 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013073 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013074 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013075 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013076 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013077 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013078 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013079 }
13080
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013081 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013082 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013083
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013084 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013085 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013086
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013087 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013088 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013089
Daniel Vetter270b3042012-10-27 15:52:05 +020013090 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013091 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013092 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013093 /*
13094 * The DP_DETECTED bit is the latched state of the DDC
13095 * SDA pin at boot. However since eDP doesn't require DDC
13096 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13097 * eDP ports may have been muxed to an alternate function.
13098 * Thus we can't rely on the DP_DETECTED bit alone to detect
13099 * eDP ports. Consult the VBT as well as DP_DETECTED to
13100 * detect eDP ports.
13101 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013102 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13103 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013104 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13105 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013106 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13107 intel_dp_is_edp(dev, PORT_B))
13108 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013109
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013110 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13111 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070013112 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13113 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013114 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13115 intel_dp_is_edp(dev, PORT_C))
13116 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053013117
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013118 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013119 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013120 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
13121 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013122 /* eDP not supported on port D, so don't check VBT */
13123 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
13124 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013125 }
13126
Jani Nikula3cfca972013-08-27 15:12:26 +030013127 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080013128 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013129 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080013130
Paulo Zanonie2debe92013-02-18 19:00:27 -030013131 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013132 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013133 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013134 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
13135 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013136 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013137 }
Ma Ling27185ae2009-08-24 13:50:23 +080013138
Imre Deake7281ea2013-05-08 13:14:08 +030013139 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013140 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080013141 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013142
13143 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013144
Paulo Zanonie2debe92013-02-18 19:00:27 -030013145 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013146 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013147 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013148 }
Ma Ling27185ae2009-08-24 13:50:23 +080013149
Paulo Zanonie2debe92013-02-18 19:00:27 -030013150 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013151
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013152 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
13153 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013154 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013155 }
Imre Deake7281ea2013-05-08 13:14:08 +030013156 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013157 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080013158 }
Ma Ling27185ae2009-08-24 13:50:23 +080013159
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013160 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030013161 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013162 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070013163 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013164 intel_dvo_init(dev);
13165
Zhenyu Wang103a1962009-11-27 11:44:36 +080013166 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013167 intel_tv_init(dev);
13168
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080013169 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070013170
Damien Lespiaub2784e12014-08-05 11:29:37 +010013171 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010013172 encoder->base.possible_crtcs = encoder->crtc_mask;
13173 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020013174 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080013175 }
Chris Wilson47356eb2011-01-11 17:06:04 +000013176
Paulo Zanonidde86e22012-12-01 12:04:25 -020013177 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020013178
13179 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013180}
13181
13182static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
13183{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013184 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080013185 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080013186
Daniel Vetteref2d6332014-02-10 18:00:38 +010013187 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013188 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010013189 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013190 drm_gem_object_unreference(&intel_fb->obj->base);
13191 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013192 kfree(intel_fb);
13193}
13194
13195static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000013196 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080013197 unsigned int *handle)
13198{
13199 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000013200 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080013201
Chris Wilson05394f32010-11-08 19:18:58 +000013202 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080013203}
13204
13205static const struct drm_framebuffer_funcs intel_fb_funcs = {
13206 .destroy = intel_user_framebuffer_destroy,
13207 .create_handle = intel_user_framebuffer_create_handle,
13208};
13209
Damien Lespiaub3218032015-02-27 11:15:18 +000013210static
13211u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
13212 uint32_t pixel_format)
13213{
13214 u32 gen = INTEL_INFO(dev)->gen;
13215
13216 if (gen >= 9) {
13217 /* "The stride in bytes must not exceed the of the size of 8K
13218 * pixels and 32K bytes."
13219 */
13220 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
13221 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
13222 return 32*1024;
13223 } else if (gen >= 4) {
13224 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13225 return 16*1024;
13226 else
13227 return 32*1024;
13228 } else if (gen >= 3) {
13229 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13230 return 8*1024;
13231 else
13232 return 16*1024;
13233 } else {
13234 /* XXX DSPC is limited to 4k tiled */
13235 return 8*1024;
13236 }
13237}
13238
Daniel Vetterb5ea6422014-03-02 21:18:00 +010013239static int intel_framebuffer_init(struct drm_device *dev,
13240 struct intel_framebuffer *intel_fb,
13241 struct drm_mode_fb_cmd2 *mode_cmd,
13242 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080013243{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000013244 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080013245 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000013246 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080013247
Daniel Vetterdd4916c2013-10-09 21:23:51 +020013248 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
13249
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013250 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
13251 /* Enforce that fb modifier and tiling mode match, but only for
13252 * X-tiled. This is needed for FBC. */
13253 if (!!(obj->tiling_mode == I915_TILING_X) !=
13254 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
13255 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
13256 return -EINVAL;
13257 }
13258 } else {
13259 if (obj->tiling_mode == I915_TILING_X)
13260 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
13261 else if (obj->tiling_mode == I915_TILING_Y) {
13262 DRM_DEBUG("No Y tiling for legacy addfb\n");
13263 return -EINVAL;
13264 }
13265 }
13266
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000013267 /* Passed in modifier sanity checking. */
13268 switch (mode_cmd->modifier[0]) {
13269 case I915_FORMAT_MOD_Y_TILED:
13270 case I915_FORMAT_MOD_Yf_TILED:
13271 if (INTEL_INFO(dev)->gen < 9) {
13272 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
13273 mode_cmd->modifier[0]);
13274 return -EINVAL;
13275 }
13276 case DRM_FORMAT_MOD_NONE:
13277 case I915_FORMAT_MOD_X_TILED:
13278 break;
13279 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070013280 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
13281 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010013282 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013283 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013284
Damien Lespiaub3218032015-02-27 11:15:18 +000013285 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
13286 mode_cmd->pixel_format);
13287 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
13288 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
13289 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010013290 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013291 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013292
Damien Lespiaub3218032015-02-27 11:15:18 +000013293 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
13294 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010013295 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000013296 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
13297 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013298 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010013299 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013300 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013301 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013302
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013303 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013304 mode_cmd->pitches[0] != obj->stride) {
13305 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
13306 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013307 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013308 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013309
Ville Syrjälä57779d02012-10-31 17:50:14 +020013310 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013311 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020013312 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020013313 case DRM_FORMAT_RGB565:
13314 case DRM_FORMAT_XRGB8888:
13315 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020013316 break;
13317 case DRM_FORMAT_XRGB1555:
13318 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013319 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013320 DRM_DEBUG("unsupported pixel format: %s\n",
13321 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013322 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013323 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020013324 break;
13325 case DRM_FORMAT_XBGR8888:
13326 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020013327 case DRM_FORMAT_XRGB2101010:
13328 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020013329 case DRM_FORMAT_XBGR2101010:
13330 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013331 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013332 DRM_DEBUG("unsupported pixel format: %s\n",
13333 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013334 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013335 }
Jesse Barnesb5626742011-06-24 12:19:27 -070013336 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020013337 case DRM_FORMAT_YUYV:
13338 case DRM_FORMAT_UYVY:
13339 case DRM_FORMAT_YVYU:
13340 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013341 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013342 DRM_DEBUG("unsupported pixel format: %s\n",
13343 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013344 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013345 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013346 break;
13347 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013348 DRM_DEBUG("unsupported pixel format: %s\n",
13349 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010013350 return -EINVAL;
13351 }
13352
Ville Syrjälä90f9a332012-10-31 17:50:19 +020013353 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
13354 if (mode_cmd->offsets[0] != 0)
13355 return -EINVAL;
13356
Damien Lespiauec2c9812015-01-20 12:51:45 +000013357 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000013358 mode_cmd->pixel_format,
13359 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020013360 /* FIXME drm helper for size checks (especially planar formats)? */
13361 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
13362 return -EINVAL;
13363
Daniel Vetterc7d73f62012-12-13 23:38:38 +010013364 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
13365 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020013366 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010013367
Jesse Barnes79e53942008-11-07 14:24:08 -080013368 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
13369 if (ret) {
13370 DRM_ERROR("framebuffer init failed %d\n", ret);
13371 return ret;
13372 }
13373
Jesse Barnes79e53942008-11-07 14:24:08 -080013374 return 0;
13375}
13376
Jesse Barnes79e53942008-11-07 14:24:08 -080013377static struct drm_framebuffer *
13378intel_user_framebuffer_create(struct drm_device *dev,
13379 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013380 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080013381{
Chris Wilson05394f32010-11-08 19:18:58 +000013382 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080013383
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013384 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
13385 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000013386 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010013387 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080013388
Chris Wilsond2dff872011-04-19 08:36:26 +010013389 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080013390}
13391
Daniel Vetter4520f532013-10-09 09:18:51 +020013392#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020013393static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020013394{
13395}
13396#endif
13397
Jesse Barnes79e53942008-11-07 14:24:08 -080013398static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080013399 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020013400 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080013401 .atomic_check = intel_atomic_check,
13402 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080013403};
13404
Jesse Barnese70236a2009-09-21 10:42:27 -070013405/* Set up chip specific display functions */
13406static void intel_init_display(struct drm_device *dev)
13407{
13408 struct drm_i915_private *dev_priv = dev->dev_private;
13409
Daniel Vetteree9300b2013-06-03 22:40:22 +020013410 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
13411 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030013412 else if (IS_CHERRYVIEW(dev))
13413 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020013414 else if (IS_VALLEYVIEW(dev))
13415 dev_priv->display.find_dpll = vlv_find_best_dpll;
13416 else if (IS_PINEVIEW(dev))
13417 dev_priv->display.find_dpll = pnv_find_best_dpll;
13418 else
13419 dev_priv->display.find_dpll = i9xx_find_best_dpll;
13420
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000013421 if (INTEL_INFO(dev)->gen >= 9) {
13422 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013423 dev_priv->display.get_initial_plane_config =
13424 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000013425 dev_priv->display.crtc_compute_clock =
13426 haswell_crtc_compute_clock;
13427 dev_priv->display.crtc_enable = haswell_crtc_enable;
13428 dev_priv->display.crtc_disable = haswell_crtc_disable;
13429 dev_priv->display.off = ironlake_crtc_off;
13430 dev_priv->display.update_primary_plane =
13431 skylake_update_primary_plane;
13432 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013433 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013434 dev_priv->display.get_initial_plane_config =
13435 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020013436 dev_priv->display.crtc_compute_clock =
13437 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020013438 dev_priv->display.crtc_enable = haswell_crtc_enable;
13439 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030013440 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000013441 dev_priv->display.update_primary_plane =
13442 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030013443 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013444 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013445 dev_priv->display.get_initial_plane_config =
13446 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020013447 dev_priv->display.crtc_compute_clock =
13448 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020013449 dev_priv->display.crtc_enable = ironlake_crtc_enable;
13450 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013451 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070013452 dev_priv->display.update_primary_plane =
13453 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070013454 } else if (IS_VALLEYVIEW(dev)) {
13455 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013456 dev_priv->display.get_initial_plane_config =
13457 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020013458 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070013459 dev_priv->display.crtc_enable = valleyview_crtc_enable;
13460 dev_priv->display.crtc_disable = i9xx_crtc_disable;
13461 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070013462 dev_priv->display.update_primary_plane =
13463 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070013464 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013465 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013466 dev_priv->display.get_initial_plane_config =
13467 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020013468 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020013469 dev_priv->display.crtc_enable = i9xx_crtc_enable;
13470 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013471 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070013472 dev_priv->display.update_primary_plane =
13473 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070013474 }
Jesse Barnese70236a2009-09-21 10:42:27 -070013475
Jesse Barnese70236a2009-09-21 10:42:27 -070013476 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070013477 if (IS_VALLEYVIEW(dev))
13478 dev_priv->display.get_display_clock_speed =
13479 valleyview_get_display_clock_speed;
13480 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070013481 dev_priv->display.get_display_clock_speed =
13482 i945_get_display_clock_speed;
13483 else if (IS_I915G(dev))
13484 dev_priv->display.get_display_clock_speed =
13485 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020013486 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070013487 dev_priv->display.get_display_clock_speed =
13488 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020013489 else if (IS_PINEVIEW(dev))
13490 dev_priv->display.get_display_clock_speed =
13491 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070013492 else if (IS_I915GM(dev))
13493 dev_priv->display.get_display_clock_speed =
13494 i915gm_get_display_clock_speed;
13495 else if (IS_I865G(dev))
13496 dev_priv->display.get_display_clock_speed =
13497 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020013498 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070013499 dev_priv->display.get_display_clock_speed =
13500 i855_get_display_clock_speed;
13501 else /* 852, 830 */
13502 dev_priv->display.get_display_clock_speed =
13503 i830_get_display_clock_speed;
13504
Jani Nikula7c10a2b2014-10-27 16:26:43 +020013505 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013506 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013507 } else if (IS_GEN6(dev)) {
13508 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013509 } else if (IS_IVYBRIDGE(dev)) {
13510 /* FIXME: detect B0+ stepping and use auto training */
13511 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030013512 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013513 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080013514 } else if (IS_VALLEYVIEW(dev)) {
13515 dev_priv->display.modeset_global_resources =
13516 valleyview_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070013517 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070013518
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070013519 switch (INTEL_INFO(dev)->gen) {
13520 case 2:
13521 dev_priv->display.queue_flip = intel_gen2_queue_flip;
13522 break;
13523
13524 case 3:
13525 dev_priv->display.queue_flip = intel_gen3_queue_flip;
13526 break;
13527
13528 case 4:
13529 case 5:
13530 dev_priv->display.queue_flip = intel_gen4_queue_flip;
13531 break;
13532
13533 case 6:
13534 dev_priv->display.queue_flip = intel_gen6_queue_flip;
13535 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070013536 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070013537 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070013538 dev_priv->display.queue_flip = intel_gen7_queue_flip;
13539 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000013540 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000013541 /* Drop through - unsupported since execlist only. */
13542 default:
13543 /* Default just returns -ENODEV to indicate unsupported */
13544 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070013545 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020013546
13547 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030013548
13549 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070013550}
13551
Jesse Barnesb690e962010-07-19 13:53:12 -070013552/*
13553 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
13554 * resume, or other times. This quirk makes sure that's the case for
13555 * affected systems.
13556 */
Akshay Joshi0206e352011-08-16 15:34:10 -040013557static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070013558{
13559 struct drm_i915_private *dev_priv = dev->dev_private;
13560
13561 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013562 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070013563}
13564
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030013565static void quirk_pipeb_force(struct drm_device *dev)
13566{
13567 struct drm_i915_private *dev_priv = dev->dev_private;
13568
13569 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
13570 DRM_INFO("applying pipe b force quirk\n");
13571}
13572
Keith Packard435793d2011-07-12 14:56:22 -070013573/*
13574 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
13575 */
13576static void quirk_ssc_force_disable(struct drm_device *dev)
13577{
13578 struct drm_i915_private *dev_priv = dev->dev_private;
13579 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013580 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070013581}
13582
Carsten Emde4dca20e2012-03-15 15:56:26 +010013583/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010013584 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
13585 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010013586 */
13587static void quirk_invert_brightness(struct drm_device *dev)
13588{
13589 struct drm_i915_private *dev_priv = dev->dev_private;
13590 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013591 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070013592}
13593
Scot Doyle9c72cc62014-07-03 23:27:50 +000013594/* Some VBT's incorrectly indicate no backlight is present */
13595static void quirk_backlight_present(struct drm_device *dev)
13596{
13597 struct drm_i915_private *dev_priv = dev->dev_private;
13598 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
13599 DRM_INFO("applying backlight present quirk\n");
13600}
13601
Jesse Barnesb690e962010-07-19 13:53:12 -070013602struct intel_quirk {
13603 int device;
13604 int subsystem_vendor;
13605 int subsystem_device;
13606 void (*hook)(struct drm_device *dev);
13607};
13608
Egbert Eich5f85f172012-10-14 15:46:38 +020013609/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
13610struct intel_dmi_quirk {
13611 void (*hook)(struct drm_device *dev);
13612 const struct dmi_system_id (*dmi_id_list)[];
13613};
13614
13615static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
13616{
13617 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
13618 return 1;
13619}
13620
13621static const struct intel_dmi_quirk intel_dmi_quirks[] = {
13622 {
13623 .dmi_id_list = &(const struct dmi_system_id[]) {
13624 {
13625 .callback = intel_dmi_reverse_brightness,
13626 .ident = "NCR Corporation",
13627 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
13628 DMI_MATCH(DMI_PRODUCT_NAME, ""),
13629 },
13630 },
13631 { } /* terminating entry */
13632 },
13633 .hook = quirk_invert_brightness,
13634 },
13635};
13636
Ben Widawskyc43b5632012-04-16 14:07:40 -070013637static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070013638 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040013639 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070013640
Jesse Barnesb690e962010-07-19 13:53:12 -070013641 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
13642 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
13643
Jesse Barnesb690e962010-07-19 13:53:12 -070013644 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
13645 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
13646
Ville Syrjälä5f080c02014-08-15 01:22:06 +030013647 /* 830 needs to leave pipe A & dpll A up */
13648 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
13649
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030013650 /* 830 needs to leave pipe B & dpll B up */
13651 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
13652
Keith Packard435793d2011-07-12 14:56:22 -070013653 /* Lenovo U160 cannot use SSC on LVDS */
13654 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020013655
13656 /* Sony Vaio Y cannot use SSC on LVDS */
13657 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010013658
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010013659 /* Acer Aspire 5734Z must invert backlight brightness */
13660 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
13661
13662 /* Acer/eMachines G725 */
13663 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
13664
13665 /* Acer/eMachines e725 */
13666 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
13667
13668 /* Acer/Packard Bell NCL20 */
13669 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
13670
13671 /* Acer Aspire 4736Z */
13672 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020013673
13674 /* Acer Aspire 5336 */
13675 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000013676
13677 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
13678 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000013679
Scot Doyledfb3d47b2014-08-21 16:08:02 +000013680 /* Acer C720 Chromebook (Core i3 4005U) */
13681 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
13682
jens steinb2a96012014-10-28 20:25:53 +010013683 /* Apple Macbook 2,1 (Core 2 T7400) */
13684 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
13685
Scot Doyled4967d82014-07-03 23:27:52 +000013686 /* Toshiba CB35 Chromebook (Celeron 2955U) */
13687 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000013688
13689 /* HP Chromebook 14 (Celeron 2955U) */
13690 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020013691
13692 /* Dell Chromebook 11 */
13693 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070013694};
13695
13696static void intel_init_quirks(struct drm_device *dev)
13697{
13698 struct pci_dev *d = dev->pdev;
13699 int i;
13700
13701 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
13702 struct intel_quirk *q = &intel_quirks[i];
13703
13704 if (d->device == q->device &&
13705 (d->subsystem_vendor == q->subsystem_vendor ||
13706 q->subsystem_vendor == PCI_ANY_ID) &&
13707 (d->subsystem_device == q->subsystem_device ||
13708 q->subsystem_device == PCI_ANY_ID))
13709 q->hook(dev);
13710 }
Egbert Eich5f85f172012-10-14 15:46:38 +020013711 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
13712 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
13713 intel_dmi_quirks[i].hook(dev);
13714 }
Jesse Barnesb690e962010-07-19 13:53:12 -070013715}
13716
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013717/* Disable the VGA plane that we never use */
13718static void i915_disable_vga(struct drm_device *dev)
13719{
13720 struct drm_i915_private *dev_priv = dev->dev_private;
13721 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020013722 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013723
Ville Syrjälä2b37c612014-01-22 21:32:38 +020013724 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013725 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070013726 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013727 sr1 = inb(VGA_SR_DATA);
13728 outb(sr1 | 1<<5, VGA_SR_DATA);
13729 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
13730 udelay(300);
13731
Ville Syrjälä01f5a622014-12-16 18:38:37 +020013732 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013733 POSTING_READ(vga_reg);
13734}
13735
Daniel Vetterf8175862012-04-10 15:50:11 +020013736void intel_modeset_init_hw(struct drm_device *dev)
13737{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030013738 intel_prepare_ddi(dev);
13739
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030013740 if (IS_VALLEYVIEW(dev))
13741 vlv_update_cdclk(dev);
13742
Daniel Vetterf8175862012-04-10 15:50:11 +020013743 intel_init_clock_gating(dev);
13744
Daniel Vetter8090c6b2012-06-24 16:42:32 +020013745 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020013746}
13747
Jesse Barnes79e53942008-11-07 14:24:08 -080013748void intel_modeset_init(struct drm_device *dev)
13749{
Jesse Barnes652c3932009-08-17 13:31:43 -070013750 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000013751 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000013752 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080013753 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080013754
13755 drm_mode_config_init(dev);
13756
13757 dev->mode_config.min_width = 0;
13758 dev->mode_config.min_height = 0;
13759
Dave Airlie019d96c2011-09-29 16:20:42 +010013760 dev->mode_config.preferred_depth = 24;
13761 dev->mode_config.prefer_shadow = 1;
13762
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000013763 dev->mode_config.allow_fb_modifiers = true;
13764
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020013765 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080013766
Jesse Barnesb690e962010-07-19 13:53:12 -070013767 intel_init_quirks(dev);
13768
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030013769 intel_init_pm(dev);
13770
Ben Widawskye3c74752013-04-05 13:12:39 -070013771 if (INTEL_INFO(dev)->num_pipes == 0)
13772 return;
13773
Jesse Barnese70236a2009-09-21 10:42:27 -070013774 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020013775 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070013776
Chris Wilsona6c45cf2010-09-17 00:32:17 +010013777 if (IS_GEN2(dev)) {
13778 dev->mode_config.max_width = 2048;
13779 dev->mode_config.max_height = 2048;
13780 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070013781 dev->mode_config.max_width = 4096;
13782 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080013783 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010013784 dev->mode_config.max_width = 8192;
13785 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080013786 }
Damien Lespiau068be562014-03-28 14:17:49 +000013787
Ville Syrjälädc41c152014-08-13 11:57:05 +030013788 if (IS_845G(dev) || IS_I865G(dev)) {
13789 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
13790 dev->mode_config.cursor_height = 1023;
13791 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000013792 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
13793 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
13794 } else {
13795 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
13796 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
13797 }
13798
Ben Widawsky5d4545a2013-01-17 12:45:15 -080013799 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080013800
Zhao Yakui28c97732009-10-09 11:39:41 +080013801 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070013802 INTEL_INFO(dev)->num_pipes,
13803 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080013804
Damien Lespiau055e3932014-08-18 13:49:10 +010013805 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000013806 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000013807 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000013808 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f38512013-04-02 11:22:20 -070013809 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030013810 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000013811 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f38512013-04-02 11:22:20 -070013812 }
Jesse Barnes79e53942008-11-07 14:24:08 -080013813 }
13814
Jesse Barnesf42bb702013-12-16 16:34:23 -080013815 intel_init_dpio(dev);
13816
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013817 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013818
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013819 /* Just disable it once at startup */
13820 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013821 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000013822
13823 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013824 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080013825
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013826 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080013827 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013828 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080013829
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013830 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080013831 if (!crtc->active)
13832 continue;
13833
Jesse Barnes46f297f2014-03-07 08:57:48 -080013834 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080013835 * Note that reserving the BIOS fb up front prevents us
13836 * from stuffing other stolen allocations like the ring
13837 * on top. This prevents some ugliness at boot time, and
13838 * can even allow for smooth boot transitions if the BIOS
13839 * fb is large enough for the active pipe configuration.
13840 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013841 if (dev_priv->display.get_initial_plane_config) {
13842 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080013843 &crtc->plane_config);
13844 /*
13845 * If the fb is shared between multiple heads, we'll
13846 * just get the first one.
13847 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010013848 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080013849 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080013850 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010013851}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080013852
Daniel Vetter7fad7982012-07-04 17:51:47 +020013853static void intel_enable_pipe_a(struct drm_device *dev)
13854{
13855 struct intel_connector *connector;
13856 struct drm_connector *crt = NULL;
13857 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030013858 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020013859
13860 /* We can't just switch on the pipe A, we need to set things up with a
13861 * proper mode and output configuration. As a gross hack, enable pipe A
13862 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013863 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020013864 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
13865 crt = &connector->base;
13866 break;
13867 }
13868 }
13869
13870 if (!crt)
13871 return;
13872
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030013873 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020013874 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020013875}
13876
Daniel Vetterfa555832012-10-10 23:14:00 +020013877static bool
13878intel_check_plane_mapping(struct intel_crtc *crtc)
13879{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070013880 struct drm_device *dev = crtc->base.dev;
13881 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020013882 u32 reg, val;
13883
Ben Widawsky7eb552a2013-03-13 14:05:41 -070013884 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020013885 return true;
13886
13887 reg = DSPCNTR(!crtc->plane);
13888 val = I915_READ(reg);
13889
13890 if ((val & DISPLAY_PLANE_ENABLE) &&
13891 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
13892 return false;
13893
13894 return true;
13895}
13896
Daniel Vetter24929352012-07-02 20:28:59 +020013897static void intel_sanitize_crtc(struct intel_crtc *crtc)
13898{
13899 struct drm_device *dev = crtc->base.dev;
13900 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020013901 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020013902
Daniel Vetter24929352012-07-02 20:28:59 +020013903 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013904 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020013905 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
13906
Ville Syrjäläd3eaf8842014-05-20 17:20:05 +030013907 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010013908 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030013909 if (crtc->active) {
13910 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010013911 drm_crtc_vblank_on(&crtc->base);
13912 }
Ville Syrjäläd3eaf8842014-05-20 17:20:05 +030013913
Daniel Vetter24929352012-07-02 20:28:59 +020013914 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020013915 * disable the crtc (and hence change the state) if it is wrong. Note
13916 * that gen4+ has a fixed plane -> pipe mapping. */
13917 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020013918 struct intel_connector *connector;
13919 bool plane;
13920
Daniel Vetter24929352012-07-02 20:28:59 +020013921 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
13922 crtc->base.base.id);
13923
13924 /* Pipe has the wrong plane attached and the plane is active.
13925 * Temporarily change the plane mapping and disable everything
13926 * ... */
13927 plane = crtc->plane;
13928 crtc->plane = !plane;
Daniel Vetter9c8958b2014-07-14 19:35:31 +020013929 crtc->primary_enabled = true;
Daniel Vetter24929352012-07-02 20:28:59 +020013930 dev_priv->display.crtc_disable(&crtc->base);
13931 crtc->plane = plane;
13932
13933 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013934 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020013935 if (connector->encoder->base.crtc != &crtc->base)
13936 continue;
13937
Egbert Eich7f1950f2014-04-25 10:56:22 +020013938 connector->base.dpms = DRM_MODE_DPMS_OFF;
13939 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020013940 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020013941 /* multiple connectors may have the same encoder:
13942 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013943 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020013944 if (connector->encoder->base.crtc == &crtc->base) {
13945 connector->encoder->base.crtc = NULL;
13946 connector->encoder->connectors_active = false;
13947 }
Daniel Vetter24929352012-07-02 20:28:59 +020013948
13949 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080013950 crtc->base.state->enable = false;
Daniel Vetter24929352012-07-02 20:28:59 +020013951 crtc->base.enabled = false;
13952 }
Daniel Vetter24929352012-07-02 20:28:59 +020013953
Daniel Vetter7fad7982012-07-04 17:51:47 +020013954 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
13955 crtc->pipe == PIPE_A && !crtc->active) {
13956 /* BIOS forgot to enable pipe A, this mostly happens after
13957 * resume. Force-enable the pipe to fix this, the update_dpms
13958 * call below we restore the pipe to the right state, but leave
13959 * the required bits on. */
13960 intel_enable_pipe_a(dev);
13961 }
13962
Daniel Vetter24929352012-07-02 20:28:59 +020013963 /* Adjust the state of the output pipe according to whether we
13964 * have active connectors/encoders. */
13965 intel_crtc_update_dpms(&crtc->base);
13966
Matt Roper83d65732015-02-25 13:12:16 -080013967 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020013968 struct intel_encoder *encoder;
13969
13970 /* This can happen either due to bugs in the get_hw_state
13971 * functions or because the pipe is force-enabled due to the
13972 * pipe A quirk. */
13973 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
13974 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080013975 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020013976 crtc->active ? "enabled" : "disabled");
13977
Matt Roper83d65732015-02-25 13:12:16 -080013978 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020013979 crtc->base.enabled = crtc->active;
13980
13981 /* Because we only establish the connector -> encoder ->
13982 * crtc links if something is active, this means the
13983 * crtc is now deactivated. Break the links. connector
13984 * -> encoder links are only establish when things are
13985 * actually up, hence no need to break them. */
13986 WARN_ON(crtc->active);
13987
13988 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
13989 WARN_ON(encoder->connectors_active);
13990 encoder->base.crtc = NULL;
13991 }
13992 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020013993
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030013994 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010013995 /*
13996 * We start out with underrun reporting disabled to avoid races.
13997 * For correct bookkeeping mark this on active crtcs.
13998 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020013999 * Also on gmch platforms we dont have any hardware bits to
14000 * disable the underrun reporting. Which means we need to start
14001 * out with underrun reporting disabled also on inactive pipes,
14002 * since otherwise we'll complain about the garbage we read when
14003 * e.g. coming up after runtime pm.
14004 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014005 * No protection against concurrent access is required - at
14006 * worst a fifo underrun happens which also sets this to false.
14007 */
14008 crtc->cpu_fifo_underrun_disabled = true;
14009 crtc->pch_fifo_underrun_disabled = true;
14010 }
Daniel Vetter24929352012-07-02 20:28:59 +020014011}
14012
14013static void intel_sanitize_encoder(struct intel_encoder *encoder)
14014{
14015 struct intel_connector *connector;
14016 struct drm_device *dev = encoder->base.dev;
14017
14018 /* We need to check both for a crtc link (meaning that the
14019 * encoder is active and trying to read from a pipe) and the
14020 * pipe itself being active. */
14021 bool has_active_crtc = encoder->base.crtc &&
14022 to_intel_crtc(encoder->base.crtc)->active;
14023
14024 if (encoder->connectors_active && !has_active_crtc) {
14025 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14026 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014027 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014028
14029 /* Connector is active, but has no active pipe. This is
14030 * fallout from our resume register restoring. Disable
14031 * the encoder manually again. */
14032 if (encoder->base.crtc) {
14033 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14034 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014035 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014036 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014037 if (encoder->post_disable)
14038 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014039 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014040 encoder->base.crtc = NULL;
14041 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014042
14043 /* Inconsistent output/port/pipe state happens presumably due to
14044 * a bug in one of the get_hw_state functions. Or someplace else
14045 * in our code, like the register restore mess on resume. Clamp
14046 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014047 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014048 if (connector->encoder != encoder)
14049 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014050 connector->base.dpms = DRM_MODE_DPMS_OFF;
14051 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014052 }
14053 }
14054 /* Enabled encoders without active connectors will be fixed in
14055 * the crtc fixup. */
14056}
14057
Imre Deak04098752014-02-18 00:02:16 +020014058void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014059{
14060 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014061 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014062
Imre Deak04098752014-02-18 00:02:16 +020014063 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14064 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14065 i915_disable_vga(dev);
14066 }
14067}
14068
14069void i915_redisable_vga(struct drm_device *dev)
14070{
14071 struct drm_i915_private *dev_priv = dev->dev_private;
14072
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014073 /* This function can be called both from intel_modeset_setup_hw_state or
14074 * at a very early point in our resume sequence, where the power well
14075 * structures are not yet restored. Since this function is at a very
14076 * paranoid "someone might have enabled VGA while we were not looking"
14077 * level, just check if the power well is enabled instead of trying to
14078 * follow the "don't touch the power well if we don't need it" policy
14079 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014080 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014081 return;
14082
Imre Deak04098752014-02-18 00:02:16 +020014083 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014084}
14085
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014086static bool primary_get_hw_state(struct intel_crtc *crtc)
14087{
14088 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14089
14090 if (!crtc->active)
14091 return false;
14092
14093 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
14094}
14095
Daniel Vetter30e984d2013-06-05 13:34:17 +020014096static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020014097{
14098 struct drm_i915_private *dev_priv = dev->dev_private;
14099 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020014100 struct intel_crtc *crtc;
14101 struct intel_encoder *encoder;
14102 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020014103 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020014104
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014105 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014106 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020014107
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014108 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020014109
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014110 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014111 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014112
Matt Roper83d65732015-02-25 13:12:16 -080014113 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014114 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014115 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020014116
14117 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
14118 crtc->base.base.id,
14119 crtc->active ? "enabled" : "disabled");
14120 }
14121
Daniel Vetter53589012013-06-05 13:34:16 +020014122 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14123 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14124
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014125 pll->on = pll->get_hw_state(dev_priv, pll,
14126 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020014127 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014128 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014129 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014130 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020014131 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014132 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014133 }
Daniel Vetter53589012013-06-05 13:34:16 +020014134 }
Daniel Vetter53589012013-06-05 13:34:16 +020014135
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014136 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014137 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014138
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014139 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014140 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020014141 }
14142
Damien Lespiaub2784e12014-08-05 11:29:37 +010014143 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014144 pipe = 0;
14145
14146 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070014147 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14148 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014149 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014150 } else {
14151 encoder->base.crtc = NULL;
14152 }
14153
14154 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014155 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020014156 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014157 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014158 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014159 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020014160 }
14161
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014162 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014163 if (connector->get_hw_state(connector)) {
14164 connector->base.dpms = DRM_MODE_DPMS_ON;
14165 connector->encoder->connectors_active = true;
14166 connector->base.encoder = &connector->encoder->base;
14167 } else {
14168 connector->base.dpms = DRM_MODE_DPMS_OFF;
14169 connector->base.encoder = NULL;
14170 }
14171 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
14172 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030014173 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014174 connector->base.encoder ? "enabled" : "disabled");
14175 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020014176}
14177
14178/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
14179 * and i915 state tracking structures. */
14180void intel_modeset_setup_hw_state(struct drm_device *dev,
14181 bool force_restore)
14182{
14183 struct drm_i915_private *dev_priv = dev->dev_private;
14184 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014185 struct intel_crtc *crtc;
14186 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020014187 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014188
14189 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020014190
Jesse Barnesbabea612013-06-26 18:57:38 +030014191 /*
14192 * Now that we have the config, copy it to each CRTC struct
14193 * Note that this could go away if we move to using crtc_config
14194 * checking everywhere.
14195 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014196 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020014197 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014198 intel_mode_from_pipe_config(&crtc->base.mode,
14199 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030014200 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
14201 crtc->base.base.id);
14202 drm_mode_debug_printmodeline(&crtc->base.mode);
14203 }
14204 }
14205
Daniel Vetter24929352012-07-02 20:28:59 +020014206 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010014207 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014208 intel_sanitize_encoder(encoder);
14209 }
14210
Damien Lespiau055e3932014-08-18 13:49:10 +010014211 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020014212 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14213 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014214 intel_dump_pipe_config(crtc, crtc->config,
14215 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020014216 }
Daniel Vetter9a935852012-07-05 22:34:27 +020014217
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020014218 intel_modeset_update_connector_atomic_state(dev);
14219
Daniel Vetter35c95372013-07-17 06:55:04 +020014220 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14221 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14222
14223 if (!pll->on || pll->active)
14224 continue;
14225
14226 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
14227
14228 pll->disable(dev_priv, pll);
14229 pll->on = false;
14230 }
14231
Pradeep Bhat30789992014-11-04 17:06:45 +000014232 if (IS_GEN9(dev))
14233 skl_wm_get_hw_state(dev);
14234 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030014235 ilk_wm_get_hw_state(dev);
14236
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010014237 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030014238 i915_redisable_vga(dev);
14239
Daniel Vetterf30da182013-04-11 20:22:50 +020014240 /*
14241 * We need to use raw interfaces for restoring state to avoid
14242 * checking (bogus) intermediate states.
14243 */
Damien Lespiau055e3932014-08-18 13:49:10 +010014244 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070014245 struct drm_crtc *crtc =
14246 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020014247
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014248 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010014249 }
14250 } else {
14251 intel_modeset_update_staged_output_state(dev);
14252 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020014253
14254 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010014255}
14256
14257void intel_modeset_gem_init(struct drm_device *dev)
14258{
Jesse Barnes92122782014-10-09 12:57:42 -070014259 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080014260 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070014261 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010014262 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080014263
Imre Deakae484342014-03-31 15:10:44 +030014264 mutex_lock(&dev->struct_mutex);
14265 intel_init_gt_powersave(dev);
14266 mutex_unlock(&dev->struct_mutex);
14267
Jesse Barnes92122782014-10-09 12:57:42 -070014268 /*
14269 * There may be no VBT; and if the BIOS enabled SSC we can
14270 * just keep using it to avoid unnecessary flicker. Whereas if the
14271 * BIOS isn't using it, don't assume it will work even if the VBT
14272 * indicates as much.
14273 */
14274 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
14275 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14276 DREF_SSC1_ENABLE);
14277
Chris Wilson1833b132012-05-09 11:56:28 +010014278 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020014279
14280 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080014281
14282 /*
14283 * Make sure any fbs we allocated at startup are properly
14284 * pinned & fenced. When we do the allocation it's too early
14285 * for this.
14286 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010014287 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070014288 obj = intel_fb_obj(c->primary->fb);
14289 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080014290 continue;
14291
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010014292 mutex_lock(&dev->struct_mutex);
14293 ret = intel_pin_and_fence_fb_obj(c->primary,
14294 c->primary->fb,
14295 c->primary->state,
14296 NULL);
14297 mutex_unlock(&dev->struct_mutex);
14298 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080014299 DRM_ERROR("failed to pin boot fb on pipe %d\n",
14300 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100014301 drm_framebuffer_unreference(c->primary->fb);
14302 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080014303 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080014304 }
14305 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020014306
14307 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014308}
14309
Imre Deak4932e2c2014-02-11 17:12:48 +020014310void intel_connector_unregister(struct intel_connector *intel_connector)
14311{
14312 struct drm_connector *connector = &intel_connector->base;
14313
14314 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010014315 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020014316}
14317
Jesse Barnes79e53942008-11-07 14:24:08 -080014318void intel_modeset_cleanup(struct drm_device *dev)
14319{
Jesse Barnes652c3932009-08-17 13:31:43 -070014320 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030014321 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070014322
Imre Deak2eb52522014-11-19 15:30:05 +020014323 intel_disable_gt_powersave(dev);
14324
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020014325 intel_backlight_unregister(dev);
14326
Daniel Vetterfd0c0642013-04-24 11:13:35 +020014327 /*
14328 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020014329 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020014330 * experience fancy races otherwise.
14331 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020014332 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070014333
Daniel Vetterfd0c0642013-04-24 11:13:35 +020014334 /*
14335 * Due to the hpd irq storm handling the hotplug work can re-arm the
14336 * poll handlers. Hence disable polling after hpd handling is shut down.
14337 */
Keith Packardf87ea762010-10-03 19:36:26 -070014338 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020014339
Jesse Barnes652c3932009-08-17 13:31:43 -070014340 mutex_lock(&dev->struct_mutex);
14341
Jesse Barnes723bfd72010-10-07 16:01:13 -070014342 intel_unregister_dsm_handler();
14343
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014344 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014345
Kristian Høgsberg69341a52009-11-11 12:19:17 -050014346 mutex_unlock(&dev->struct_mutex);
14347
Chris Wilson1630fe72011-07-08 12:22:42 +010014348 /* flush any delayed tasks or pending work */
14349 flush_scheduled_work();
14350
Jani Nikuladb31af1d2013-11-08 16:48:53 +020014351 /* destroy the backlight and sysfs files before encoders/connectors */
14352 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020014353 struct intel_connector *intel_connector;
14354
14355 intel_connector = to_intel_connector(connector);
14356 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020014357 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030014358
Jesse Barnes79e53942008-11-07 14:24:08 -080014359 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010014360
14361 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030014362
14363 mutex_lock(&dev->struct_mutex);
14364 intel_cleanup_gt_powersave(dev);
14365 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014366}
14367
Dave Airlie28d52042009-09-21 14:33:58 +100014368/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080014369 * Return which encoder is currently attached for connector.
14370 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010014371struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080014372{
Chris Wilsondf0e9242010-09-09 16:20:55 +010014373 return &intel_attached_encoder(connector)->base;
14374}
Jesse Barnes79e53942008-11-07 14:24:08 -080014375
Chris Wilsondf0e9242010-09-09 16:20:55 +010014376void intel_connector_attach_encoder(struct intel_connector *connector,
14377 struct intel_encoder *encoder)
14378{
14379 connector->encoder = encoder;
14380 drm_mode_connector_attach_encoder(&connector->base,
14381 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080014382}
Dave Airlie28d52042009-09-21 14:33:58 +100014383
14384/*
14385 * set vga decode state - true == enable VGA decode
14386 */
14387int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
14388{
14389 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000014390 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100014391 u16 gmch_ctrl;
14392
Chris Wilson75fa0412014-02-07 18:37:02 -020014393 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
14394 DRM_ERROR("failed to read control word\n");
14395 return -EIO;
14396 }
14397
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020014398 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
14399 return 0;
14400
Dave Airlie28d52042009-09-21 14:33:58 +100014401 if (state)
14402 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
14403 else
14404 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020014405
14406 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
14407 DRM_ERROR("failed to write control word\n");
14408 return -EIO;
14409 }
14410
Dave Airlie28d52042009-09-21 14:33:58 +100014411 return 0;
14412}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014413
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014414struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030014415
14416 u32 power_well_driver;
14417
Chris Wilson63b66e52013-08-08 15:12:06 +020014418 int num_transcoders;
14419
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014420 struct intel_cursor_error_state {
14421 u32 control;
14422 u32 position;
14423 u32 base;
14424 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010014425 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014426
14427 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020014428 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014429 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030014430 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010014431 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014432
14433 struct intel_plane_error_state {
14434 u32 control;
14435 u32 stride;
14436 u32 size;
14437 u32 pos;
14438 u32 addr;
14439 u32 surface;
14440 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010014441 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020014442
14443 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020014444 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020014445 enum transcoder cpu_transcoder;
14446
14447 u32 conf;
14448
14449 u32 htotal;
14450 u32 hblank;
14451 u32 hsync;
14452 u32 vtotal;
14453 u32 vblank;
14454 u32 vsync;
14455 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014456};
14457
14458struct intel_display_error_state *
14459intel_display_capture_error_state(struct drm_device *dev)
14460{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014461 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014462 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020014463 int transcoders[] = {
14464 TRANSCODER_A,
14465 TRANSCODER_B,
14466 TRANSCODER_C,
14467 TRANSCODER_EDP,
14468 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014469 int i;
14470
Chris Wilson63b66e52013-08-08 15:12:06 +020014471 if (INTEL_INFO(dev)->num_pipes == 0)
14472 return NULL;
14473
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020014474 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014475 if (error == NULL)
14476 return NULL;
14477
Imre Deak190be112013-11-25 17:15:31 +020014478 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030014479 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
14480
Damien Lespiau055e3932014-08-18 13:49:10 +010014481 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020014482 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014483 __intel_display_power_is_enabled(dev_priv,
14484 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020014485 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020014486 continue;
14487
Ville Syrjälä5efb3e282014-04-09 13:28:53 +030014488 error->cursor[i].control = I915_READ(CURCNTR(i));
14489 error->cursor[i].position = I915_READ(CURPOS(i));
14490 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014491
14492 error->plane[i].control = I915_READ(DSPCNTR(i));
14493 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014494 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030014495 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014496 error->plane[i].pos = I915_READ(DSPPOS(i));
14497 }
Paulo Zanonica291362013-03-06 20:03:14 -030014498 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
14499 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014500 if (INTEL_INFO(dev)->gen >= 4) {
14501 error->plane[i].surface = I915_READ(DSPSURF(i));
14502 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
14503 }
14504
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014505 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030014506
Sonika Jindal3abfce72014-07-21 15:23:43 +053014507 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e12014-04-18 15:55:04 +030014508 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020014509 }
14510
14511 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
14512 if (HAS_DDI(dev_priv->dev))
14513 error->num_transcoders++; /* Account for eDP. */
14514
14515 for (i = 0; i < error->num_transcoders; i++) {
14516 enum transcoder cpu_transcoder = transcoders[i];
14517
Imre Deakddf9c532013-11-27 22:02:02 +020014518 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014519 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020014520 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020014521 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020014522 continue;
14523
Chris Wilson63b66e52013-08-08 15:12:06 +020014524 error->transcoder[i].cpu_transcoder = cpu_transcoder;
14525
14526 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
14527 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
14528 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
14529 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
14530 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
14531 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
14532 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014533 }
14534
14535 return error;
14536}
14537
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014538#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
14539
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014540void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014541intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014542 struct drm_device *dev,
14543 struct intel_display_error_state *error)
14544{
Damien Lespiau055e3932014-08-18 13:49:10 +010014545 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014546 int i;
14547
Chris Wilson63b66e52013-08-08 15:12:06 +020014548 if (!error)
14549 return;
14550
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014551 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020014552 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014553 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030014554 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010014555 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014556 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020014557 err_printf(m, " Power: %s\n",
14558 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014559 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030014560 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014561
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014562 err_printf(m, "Plane [%d]:\n", i);
14563 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
14564 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014565 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014566 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
14567 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014568 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030014569 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014570 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014571 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014572 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
14573 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014574 }
14575
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014576 err_printf(m, "Cursor [%d]:\n", i);
14577 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
14578 err_printf(m, " POS: %08x\n", error->cursor[i].position);
14579 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014580 }
Chris Wilson63b66e52013-08-08 15:12:06 +020014581
14582 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010014583 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020014584 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020014585 err_printf(m, " Power: %s\n",
14586 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020014587 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
14588 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
14589 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
14590 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
14591 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
14592 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
14593 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
14594 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014595}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014596
14597void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
14598{
14599 struct intel_crtc *crtc;
14600
14601 for_each_intel_crtc(dev, crtc) {
14602 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014603
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020014604 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014605
14606 work = crtc->unpin_work;
14607
14608 if (work && work->event &&
14609 work->event->base.file_priv == file) {
14610 kfree(work->event);
14611 work->event = NULL;
14612 }
14613
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020014614 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014615 }
14616}