blob: a0be6ab7ba7b99e77bda50ce1dd14a16a53d6556 [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ä5efb3e22014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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äfdd508a2014-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
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002341 return 0;
2342}
2343
Chris Wilson127bd2a2010-07-23 23:32:05 +01002344int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002345intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2346 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002347 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002348 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002349{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002350 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002351 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002352 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002353 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002354 u32 alignment;
2355 int ret;
2356
Matt Roperebcdd392014-07-09 16:22:11 -07002357 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2358
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002359 switch (fb->modifier[0]) {
2360 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002361 if (INTEL_INFO(dev)->gen >= 9)
2362 alignment = 256 * 1024;
2363 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002364 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002365 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002366 alignment = 4 * 1024;
2367 else
2368 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002369 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002370 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002371 if (INTEL_INFO(dev)->gen >= 9)
2372 alignment = 256 * 1024;
2373 else {
2374 /* pin() will align the object as required by fence */
2375 alignment = 0;
2376 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002377 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002378 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002379 case I915_FORMAT_MOD_Yf_TILED:
2380 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2381 "Y tiling bo slipped through, driver bug!\n"))
2382 return -EINVAL;
2383 alignment = 1 * 1024 * 1024;
2384 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002385 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002386 MISSING_CASE(fb->modifier[0]);
2387 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002388 }
2389
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002390 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2391 if (ret)
2392 return ret;
2393
Chris Wilson693db182013-03-05 14:52:39 +00002394 /* Note that the w/a also requires 64 PTE of padding following the
2395 * bo. We currently fill all unused PTE with the shadow page and so
2396 * we should always have valid PTE following the scanout preventing
2397 * the VT-d warning.
2398 */
2399 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2400 alignment = 256 * 1024;
2401
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002402 /*
2403 * Global gtt pte registers are special registers which actually forward
2404 * writes to a chunk of system memory. Which means that there is no risk
2405 * that the register values disappear as soon as we call
2406 * intel_runtime_pm_put(), so it is correct to wrap only the
2407 * pin/unpin/fence and not more.
2408 */
2409 intel_runtime_pm_get(dev_priv);
2410
Chris Wilsonce453d82011-02-21 14:43:56 +00002411 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002412 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002413 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002414 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002415 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002416
2417 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2418 * fence, whereas 965+ only requires a fence if using
2419 * framebuffer compression. For simplicity, we always install
2420 * a fence as the cost is not that onerous.
2421 */
Chris Wilson06d98132012-04-17 15:31:24 +01002422 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002423 if (ret)
2424 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002425
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002426 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002427
Chris Wilsonce453d82011-02-21 14:43:56 +00002428 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002429 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002430 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002431
2432err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002433 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002434err_interruptible:
2435 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002436 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002437 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002438}
2439
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002440static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2441 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002442{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002443 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002444 struct i915_ggtt_view view;
2445 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002446
Matt Roperebcdd392014-07-09 16:22:11 -07002447 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2448
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002449 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2450 WARN_ONCE(ret, "Couldn't get view from plane state!");
2451
Chris Wilson1690e1e2011-12-14 13:57:08 +01002452 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002453 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002454}
2455
Daniel Vetterc2c75132012-07-05 12:17:30 +02002456/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2457 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002458unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2459 unsigned int tiling_mode,
2460 unsigned int cpp,
2461 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002462{
Chris Wilsonbc752862013-02-21 20:04:31 +00002463 if (tiling_mode != I915_TILING_NONE) {
2464 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002465
Chris Wilsonbc752862013-02-21 20:04:31 +00002466 tile_rows = *y / 8;
2467 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002468
Chris Wilsonbc752862013-02-21 20:04:31 +00002469 tiles = *x / (512/cpp);
2470 *x %= 512/cpp;
2471
2472 return tile_rows * pitch * 8 + tiles * 4096;
2473 } else {
2474 unsigned int offset;
2475
2476 offset = *y * pitch + *x * cpp;
2477 *y = 0;
2478 *x = (offset & 4095) / cpp;
2479 return offset & -4096;
2480 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002481}
2482
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002483static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002484{
2485 switch (format) {
2486 case DISPPLANE_8BPP:
2487 return DRM_FORMAT_C8;
2488 case DISPPLANE_BGRX555:
2489 return DRM_FORMAT_XRGB1555;
2490 case DISPPLANE_BGRX565:
2491 return DRM_FORMAT_RGB565;
2492 default:
2493 case DISPPLANE_BGRX888:
2494 return DRM_FORMAT_XRGB8888;
2495 case DISPPLANE_RGBX888:
2496 return DRM_FORMAT_XBGR8888;
2497 case DISPPLANE_BGRX101010:
2498 return DRM_FORMAT_XRGB2101010;
2499 case DISPPLANE_RGBX101010:
2500 return DRM_FORMAT_XBGR2101010;
2501 }
2502}
2503
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002504static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2505{
2506 switch (format) {
2507 case PLANE_CTL_FORMAT_RGB_565:
2508 return DRM_FORMAT_RGB565;
2509 default:
2510 case PLANE_CTL_FORMAT_XRGB_8888:
2511 if (rgb_order) {
2512 if (alpha)
2513 return DRM_FORMAT_ABGR8888;
2514 else
2515 return DRM_FORMAT_XBGR8888;
2516 } else {
2517 if (alpha)
2518 return DRM_FORMAT_ARGB8888;
2519 else
2520 return DRM_FORMAT_XRGB8888;
2521 }
2522 case PLANE_CTL_FORMAT_XRGB_2101010:
2523 if (rgb_order)
2524 return DRM_FORMAT_XBGR2101010;
2525 else
2526 return DRM_FORMAT_XRGB2101010;
2527 }
2528}
2529
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002530static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002531intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2532 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002533{
2534 struct drm_device *dev = crtc->base.dev;
2535 struct drm_i915_gem_object *obj = NULL;
2536 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002537 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002538 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2539 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2540 PAGE_SIZE);
2541
2542 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002543
Chris Wilsonff2652e2014-03-10 08:07:02 +00002544 if (plane_config->size == 0)
2545 return false;
2546
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002547 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2548 base_aligned,
2549 base_aligned,
2550 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002551 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002552 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002553
Damien Lespiau49af4492015-01-20 12:51:44 +00002554 obj->tiling_mode = plane_config->tiling;
2555 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002556 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002557
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002558 mode_cmd.pixel_format = fb->pixel_format;
2559 mode_cmd.width = fb->width;
2560 mode_cmd.height = fb->height;
2561 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002562 mode_cmd.modifier[0] = fb->modifier[0];
2563 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002564
2565 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002566 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002567 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002568 DRM_DEBUG_KMS("intel fb init failed\n");
2569 goto out_unref_obj;
2570 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002571 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002572
Daniel Vetterf6936e22015-03-26 12:17:05 +01002573 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002574 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002575
2576out_unref_obj:
2577 drm_gem_object_unreference(&obj->base);
2578 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002579 return false;
2580}
2581
Matt Roperafd65eb2015-02-03 13:10:04 -08002582/* Update plane->state->fb to match plane->fb after driver-internal updates */
2583static void
2584update_state_fb(struct drm_plane *plane)
2585{
2586 if (plane->fb == plane->state->fb)
2587 return;
2588
2589 if (plane->state->fb)
2590 drm_framebuffer_unreference(plane->state->fb);
2591 plane->state->fb = plane->fb;
2592 if (plane->state->fb)
2593 drm_framebuffer_reference(plane->state->fb);
2594}
2595
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002596static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002597intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2598 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002599{
2600 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002601 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002602 struct drm_crtc *c;
2603 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002604 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002605 struct drm_plane *primary = intel_crtc->base.primary;
2606 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002607
Damien Lespiau2d140302015-02-05 17:22:18 +00002608 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002609 return;
2610
Daniel Vetterf6936e22015-03-26 12:17:05 +01002611 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002612 fb = &plane_config->fb->base;
2613 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002614 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002615
Damien Lespiau2d140302015-02-05 17:22:18 +00002616 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002617
2618 /*
2619 * Failed to alloc the obj, check to see if we should share
2620 * an fb with another CRTC instead
2621 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002622 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002623 i = to_intel_crtc(c);
2624
2625 if (c == &intel_crtc->base)
2626 continue;
2627
Matt Roper2ff8fde2014-07-08 07:50:07 -07002628 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002629 continue;
2630
Daniel Vetter88595ac2015-03-26 12:42:24 +01002631 fb = c->primary->fb;
2632 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002633 continue;
2634
Daniel Vetter88595ac2015-03-26 12:42:24 +01002635 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002636 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002637 drm_framebuffer_reference(fb);
2638 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002639 }
2640 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002641
2642 return;
2643
2644valid_fb:
2645 obj = intel_fb_obj(fb);
2646 if (obj->tiling_mode != I915_TILING_NONE)
2647 dev_priv->preserve_bios_swizzle = true;
2648
2649 primary->fb = fb;
2650 primary->state->crtc = &intel_crtc->base;
2651 primary->crtc = &intel_crtc->base;
2652 update_state_fb(primary);
2653 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002654}
2655
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002656static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2657 struct drm_framebuffer *fb,
2658 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002659{
2660 struct drm_device *dev = crtc->dev;
2661 struct drm_i915_private *dev_priv = dev->dev_private;
2662 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002663 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002664 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002665 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002666 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002667 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302668 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002669
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002670 if (!intel_crtc->primary_enabled) {
2671 I915_WRITE(reg, 0);
2672 if (INTEL_INFO(dev)->gen >= 4)
2673 I915_WRITE(DSPSURF(plane), 0);
2674 else
2675 I915_WRITE(DSPADDR(plane), 0);
2676 POSTING_READ(reg);
2677 return;
2678 }
2679
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002680 obj = intel_fb_obj(fb);
2681 if (WARN_ON(obj == NULL))
2682 return;
2683
2684 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2685
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002686 dspcntr = DISPPLANE_GAMMA_ENABLE;
2687
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002688 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002689
2690 if (INTEL_INFO(dev)->gen < 4) {
2691 if (intel_crtc->pipe == PIPE_B)
2692 dspcntr |= DISPPLANE_SEL_PIPE_B;
2693
2694 /* pipesrc and dspsize control the size that is scaled from,
2695 * which should always be the user's requested size.
2696 */
2697 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002698 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2699 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002700 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002701 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2702 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002703 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2704 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002705 I915_WRITE(PRIMPOS(plane), 0);
2706 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002707 }
2708
Ville Syrjälä57779d02012-10-31 17:50:14 +02002709 switch (fb->pixel_format) {
2710 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002711 dspcntr |= DISPPLANE_8BPP;
2712 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002713 case DRM_FORMAT_XRGB1555:
2714 case DRM_FORMAT_ARGB1555:
2715 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002716 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002717 case DRM_FORMAT_RGB565:
2718 dspcntr |= DISPPLANE_BGRX565;
2719 break;
2720 case DRM_FORMAT_XRGB8888:
2721 case DRM_FORMAT_ARGB8888:
2722 dspcntr |= DISPPLANE_BGRX888;
2723 break;
2724 case DRM_FORMAT_XBGR8888:
2725 case DRM_FORMAT_ABGR8888:
2726 dspcntr |= DISPPLANE_RGBX888;
2727 break;
2728 case DRM_FORMAT_XRGB2101010:
2729 case DRM_FORMAT_ARGB2101010:
2730 dspcntr |= DISPPLANE_BGRX101010;
2731 break;
2732 case DRM_FORMAT_XBGR2101010:
2733 case DRM_FORMAT_ABGR2101010:
2734 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002735 break;
2736 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002737 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002738 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002739
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002740 if (INTEL_INFO(dev)->gen >= 4 &&
2741 obj->tiling_mode != I915_TILING_NONE)
2742 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002743
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002744 if (IS_G4X(dev))
2745 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2746
Ville Syrjäläb98971272014-08-27 16:51:22 +03002747 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002748
Daniel Vetterc2c75132012-07-05 12:17:30 +02002749 if (INTEL_INFO(dev)->gen >= 4) {
2750 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002751 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002752 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002753 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002754 linear_offset -= intel_crtc->dspaddr_offset;
2755 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002756 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002757 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002758
Matt Roper8e7d6882015-01-21 16:35:41 -08002759 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302760 dspcntr |= DISPPLANE_ROTATE_180;
2761
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002762 x += (intel_crtc->config->pipe_src_w - 1);
2763 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302764
2765 /* Finding the last pixel of the last line of the display
2766 data and adding to linear_offset*/
2767 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002768 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2769 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302770 }
2771
2772 I915_WRITE(reg, dspcntr);
2773
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002774 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002775 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002776 I915_WRITE(DSPSURF(plane),
2777 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002778 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002779 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002780 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002781 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002782 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002783}
2784
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002785static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2786 struct drm_framebuffer *fb,
2787 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002788{
2789 struct drm_device *dev = crtc->dev;
2790 struct drm_i915_private *dev_priv = dev->dev_private;
2791 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002792 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002793 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002794 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002795 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002796 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302797 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002798
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002799 if (!intel_crtc->primary_enabled) {
2800 I915_WRITE(reg, 0);
2801 I915_WRITE(DSPSURF(plane), 0);
2802 POSTING_READ(reg);
2803 return;
2804 }
2805
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002806 obj = intel_fb_obj(fb);
2807 if (WARN_ON(obj == NULL))
2808 return;
2809
2810 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2811
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002812 dspcntr = DISPPLANE_GAMMA_ENABLE;
2813
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002814 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002815
2816 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2817 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2818
Ville Syrjälä57779d02012-10-31 17:50:14 +02002819 switch (fb->pixel_format) {
2820 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002821 dspcntr |= DISPPLANE_8BPP;
2822 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002823 case DRM_FORMAT_RGB565:
2824 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002825 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002826 case DRM_FORMAT_XRGB8888:
2827 case DRM_FORMAT_ARGB8888:
2828 dspcntr |= DISPPLANE_BGRX888;
2829 break;
2830 case DRM_FORMAT_XBGR8888:
2831 case DRM_FORMAT_ABGR8888:
2832 dspcntr |= DISPPLANE_RGBX888;
2833 break;
2834 case DRM_FORMAT_XRGB2101010:
2835 case DRM_FORMAT_ARGB2101010:
2836 dspcntr |= DISPPLANE_BGRX101010;
2837 break;
2838 case DRM_FORMAT_XBGR2101010:
2839 case DRM_FORMAT_ABGR2101010:
2840 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002841 break;
2842 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002843 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002844 }
2845
2846 if (obj->tiling_mode != I915_TILING_NONE)
2847 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002848
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002849 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002850 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002851
Ville Syrjäläb98971272014-08-27 16:51:22 +03002852 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002853 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002854 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002855 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002856 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002857 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002858 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302859 dspcntr |= DISPPLANE_ROTATE_180;
2860
2861 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002862 x += (intel_crtc->config->pipe_src_w - 1);
2863 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302864
2865 /* Finding the last pixel of the last line of the display
2866 data and adding to linear_offset*/
2867 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002868 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2869 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302870 }
2871 }
2872
2873 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002874
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002875 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002876 I915_WRITE(DSPSURF(plane),
2877 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002878 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002879 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2880 } else {
2881 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2882 I915_WRITE(DSPLINOFF(plane), linear_offset);
2883 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002884 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002885}
2886
Damien Lespiaub3218032015-02-27 11:15:18 +00002887u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2888 uint32_t pixel_format)
2889{
2890 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2891
2892 /*
2893 * The stride is either expressed as a multiple of 64 bytes
2894 * chunks for linear buffers or in number of tiles for tiled
2895 * buffers.
2896 */
2897 switch (fb_modifier) {
2898 case DRM_FORMAT_MOD_NONE:
2899 return 64;
2900 case I915_FORMAT_MOD_X_TILED:
2901 if (INTEL_INFO(dev)->gen == 2)
2902 return 128;
2903 return 512;
2904 case I915_FORMAT_MOD_Y_TILED:
2905 /* No need to check for old gens and Y tiling since this is
2906 * about the display engine and those will be blocked before
2907 * we get here.
2908 */
2909 return 128;
2910 case I915_FORMAT_MOD_Yf_TILED:
2911 if (bits_per_pixel == 8)
2912 return 64;
2913 else
2914 return 128;
2915 default:
2916 MISSING_CASE(fb_modifier);
2917 return 64;
2918 }
2919}
2920
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002921unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2922 struct drm_i915_gem_object *obj)
2923{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002924 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002925
2926 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002927 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002928
2929 return i915_gem_obj_ggtt_offset_view(obj, view);
2930}
2931
Damien Lespiau70d21f02013-07-03 21:06:04 +01002932static void skylake_update_primary_plane(struct drm_crtc *crtc,
2933 struct drm_framebuffer *fb,
2934 int x, int y)
2935{
2936 struct drm_device *dev = crtc->dev;
2937 struct drm_i915_private *dev_priv = dev->dev_private;
2938 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002939 struct drm_i915_gem_object *obj;
2940 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302941 u32 plane_ctl, stride_div, stride;
2942 u32 tile_height, plane_offset, plane_size;
2943 unsigned int rotation;
2944 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002945 unsigned long surf_addr;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302946 struct drm_plane *plane;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002947
2948 if (!intel_crtc->primary_enabled) {
2949 I915_WRITE(PLANE_CTL(pipe, 0), 0);
2950 I915_WRITE(PLANE_SURF(pipe, 0), 0);
2951 POSTING_READ(PLANE_CTL(pipe, 0));
2952 return;
2953 }
2954
2955 plane_ctl = PLANE_CTL_ENABLE |
2956 PLANE_CTL_PIPE_GAMMA_ENABLE |
2957 PLANE_CTL_PIPE_CSC_ENABLE;
2958
2959 switch (fb->pixel_format) {
2960 case DRM_FORMAT_RGB565:
2961 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
2962 break;
2963 case DRM_FORMAT_XRGB8888:
2964 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2965 break;
Jani Nikulaf75fb422015-02-10 13:15:49 +02002966 case DRM_FORMAT_ARGB8888:
2967 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2968 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2969 break;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002970 case DRM_FORMAT_XBGR8888:
2971 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2972 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2973 break;
Jani Nikulaf75fb422015-02-10 13:15:49 +02002974 case DRM_FORMAT_ABGR8888:
2975 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2976 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2977 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2978 break;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002979 case DRM_FORMAT_XRGB2101010:
2980 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2981 break;
2982 case DRM_FORMAT_XBGR2101010:
2983 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2984 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2985 break;
2986 default:
2987 BUG();
2988 }
2989
Daniel Vetter30af77c2015-02-10 17:16:11 +00002990 switch (fb->modifier[0]) {
2991 case DRM_FORMAT_MOD_NONE:
Damien Lespiau70d21f02013-07-03 21:06:04 +01002992 break;
Daniel Vetter30af77c2015-02-10 17:16:11 +00002993 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau70d21f02013-07-03 21:06:04 +01002994 plane_ctl |= PLANE_CTL_TILED_X;
Damien Lespiaub3218032015-02-27 11:15:18 +00002995 break;
2996 case I915_FORMAT_MOD_Y_TILED:
2997 plane_ctl |= PLANE_CTL_TILED_Y;
2998 break;
2999 case I915_FORMAT_MOD_Yf_TILED:
3000 plane_ctl |= PLANE_CTL_TILED_YF;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003001 break;
3002 default:
Damien Lespiaub3218032015-02-27 11:15:18 +00003003 MISSING_CASE(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003004 }
3005
3006 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303007
3008 plane = crtc->primary;
3009 rotation = plane->state->rotation;
3010 switch (rotation) {
3011 case BIT(DRM_ROTATE_90):
3012 plane_ctl |= PLANE_CTL_ROTATE_90;
3013 break;
3014
3015 case BIT(DRM_ROTATE_180):
Sonika Jindal1447dde2014-10-04 10:53:31 +01003016 plane_ctl |= PLANE_CTL_ROTATE_180;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303017 break;
3018
3019 case BIT(DRM_ROTATE_270):
3020 plane_ctl |= PLANE_CTL_ROTATE_270;
3021 break;
3022 }
Damien Lespiau70d21f02013-07-03 21:06:04 +01003023
Damien Lespiaub3218032015-02-27 11:15:18 +00003024 obj = intel_fb_obj(fb);
3025 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3026 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303027 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3028
3029 if (intel_rotation_90_or_270(rotation)) {
3030 /* stride = Surface height in tiles */
3031 tile_height = intel_tile_height(dev, fb->bits_per_pixel,
3032 fb->modifier[0]);
3033 stride = DIV_ROUND_UP(fb->height, tile_height);
3034 x_offset = stride * tile_height - y - (plane->state->src_h >> 16);
3035 y_offset = x;
3036 plane_size = ((plane->state->src_w >> 16) - 1) << 16 |
3037 ((plane->state->src_h >> 16) - 1);
3038 } else {
3039 stride = fb->pitches[0] / stride_div;
3040 x_offset = x;
3041 y_offset = y;
3042 plane_size = ((plane->state->src_h >> 16) - 1) << 16 |
3043 ((plane->state->src_w >> 16) - 1);
3044 }
3045 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003046
Damien Lespiau70d21f02013-07-03 21:06:04 +01003047 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003048 I915_WRITE(PLANE_POS(pipe, 0), 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303049 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3050 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3051 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003052 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003053
3054 POSTING_READ(PLANE_SURF(pipe, 0));
3055}
3056
Jesse Barnes17638cd2011-06-24 12:19:23 -07003057/* Assume fb object is pinned & idle & fenced and just update base pointers */
3058static int
3059intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3060 int x, int y, enum mode_set_atomic state)
3061{
3062 struct drm_device *dev = crtc->dev;
3063 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003064
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003065 if (dev_priv->display.disable_fbc)
3066 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003067
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003068 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3069
3070 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003071}
3072
Ville Syrjälä75147472014-11-24 18:28:11 +02003073static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003074{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003075 struct drm_crtc *crtc;
3076
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003077 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003078 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3079 enum plane plane = intel_crtc->plane;
3080
3081 intel_prepare_page_flip(dev, plane);
3082 intel_finish_page_flip_plane(dev, plane);
3083 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003084}
3085
3086static void intel_update_primary_planes(struct drm_device *dev)
3087{
3088 struct drm_i915_private *dev_priv = dev->dev_private;
3089 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003090
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003091 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003092 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3093
Rob Clark51fd3712013-11-19 12:10:12 -05003094 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003095 /*
3096 * FIXME: Once we have proper support for primary planes (and
3097 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003098 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003099 */
Matt Roperf4510a22014-04-01 15:22:40 -07003100 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003101 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003102 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003103 crtc->x,
3104 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003105 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003106 }
3107}
3108
Ville Syrjälä75147472014-11-24 18:28:11 +02003109void intel_prepare_reset(struct drm_device *dev)
3110{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003111 struct drm_i915_private *dev_priv = to_i915(dev);
3112 struct intel_crtc *crtc;
3113
Ville Syrjälä75147472014-11-24 18:28:11 +02003114 /* no reset support for gen2 */
3115 if (IS_GEN2(dev))
3116 return;
3117
3118 /* reset doesn't touch the display */
3119 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3120 return;
3121
3122 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003123
3124 /*
3125 * Disabling the crtcs gracefully seems nicer. Also the
3126 * g33 docs say we should at least disable all the planes.
3127 */
3128 for_each_intel_crtc(dev, crtc) {
3129 if (crtc->active)
3130 dev_priv->display.crtc_disable(&crtc->base);
3131 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003132}
3133
3134void intel_finish_reset(struct drm_device *dev)
3135{
3136 struct drm_i915_private *dev_priv = to_i915(dev);
3137
3138 /*
3139 * Flips in the rings will be nuked by the reset,
3140 * so complete all pending flips so that user space
3141 * will get its events and not get stuck.
3142 */
3143 intel_complete_page_flips(dev);
3144
3145 /* no reset support for gen2 */
3146 if (IS_GEN2(dev))
3147 return;
3148
3149 /* reset doesn't touch the display */
3150 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3151 /*
3152 * Flips in the rings have been nuked by the reset,
3153 * so update the base address of all primary
3154 * planes to the the last fb to make sure we're
3155 * showing the correct fb after a reset.
3156 */
3157 intel_update_primary_planes(dev);
3158 return;
3159 }
3160
3161 /*
3162 * The display has been reset as well,
3163 * so need a full re-initialization.
3164 */
3165 intel_runtime_pm_disable_interrupts(dev_priv);
3166 intel_runtime_pm_enable_interrupts(dev_priv);
3167
3168 intel_modeset_init_hw(dev);
3169
3170 spin_lock_irq(&dev_priv->irq_lock);
3171 if (dev_priv->display.hpd_irq_setup)
3172 dev_priv->display.hpd_irq_setup(dev);
3173 spin_unlock_irq(&dev_priv->irq_lock);
3174
3175 intel_modeset_setup_hw_state(dev, true);
3176
3177 intel_hpd_init(dev_priv);
3178
3179 drm_modeset_unlock_all(dev);
3180}
3181
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003182static int
Chris Wilson14667a42012-04-03 17:58:35 +01003183intel_finish_fb(struct drm_framebuffer *old_fb)
3184{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003185 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01003186 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3187 bool was_interruptible = dev_priv->mm.interruptible;
3188 int ret;
3189
Chris Wilson14667a42012-04-03 17:58:35 +01003190 /* Big Hammer, we also need to ensure that any pending
3191 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3192 * current scanout is retired before unpinning the old
3193 * framebuffer.
3194 *
3195 * This should only fail upon a hung GPU, in which case we
3196 * can safely continue.
3197 */
3198 dev_priv->mm.interruptible = false;
3199 ret = i915_gem_object_finish_gpu(obj);
3200 dev_priv->mm.interruptible = was_interruptible;
3201
3202 return ret;
3203}
3204
Chris Wilson7d5e3792014-03-04 13:15:08 +00003205static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3206{
3207 struct drm_device *dev = crtc->dev;
3208 struct drm_i915_private *dev_priv = dev->dev_private;
3209 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003210 bool pending;
3211
3212 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3213 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3214 return false;
3215
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003216 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003217 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003218 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003219
3220 return pending;
3221}
3222
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003223static void intel_update_pipe_size(struct intel_crtc *crtc)
3224{
3225 struct drm_device *dev = crtc->base.dev;
3226 struct drm_i915_private *dev_priv = dev->dev_private;
3227 const struct drm_display_mode *adjusted_mode;
3228
3229 if (!i915.fastboot)
3230 return;
3231
3232 /*
3233 * Update pipe size and adjust fitter if needed: the reason for this is
3234 * that in compute_mode_changes we check the native mode (not the pfit
3235 * mode) to see if we can flip rather than do a full mode set. In the
3236 * fastboot case, we'll flip, but if we don't update the pipesrc and
3237 * pfit state, we'll end up with a big fb scanned out into the wrong
3238 * sized surface.
3239 *
3240 * To fix this properly, we need to hoist the checks up into
3241 * compute_mode_changes (or above), check the actual pfit state and
3242 * whether the platform allows pfit disable with pipe active, and only
3243 * then update the pipesrc and pfit state, even on the flip path.
3244 */
3245
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003246 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003247
3248 I915_WRITE(PIPESRC(crtc->pipe),
3249 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3250 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003251 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003252 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3253 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003254 I915_WRITE(PF_CTL(crtc->pipe), 0);
3255 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3256 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3257 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003258 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3259 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003260}
3261
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003262static void intel_fdi_normal_train(struct drm_crtc *crtc)
3263{
3264 struct drm_device *dev = crtc->dev;
3265 struct drm_i915_private *dev_priv = dev->dev_private;
3266 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3267 int pipe = intel_crtc->pipe;
3268 u32 reg, temp;
3269
3270 /* enable normal train */
3271 reg = FDI_TX_CTL(pipe);
3272 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003273 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003274 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3275 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003276 } else {
3277 temp &= ~FDI_LINK_TRAIN_NONE;
3278 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003279 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003280 I915_WRITE(reg, temp);
3281
3282 reg = FDI_RX_CTL(pipe);
3283 temp = I915_READ(reg);
3284 if (HAS_PCH_CPT(dev)) {
3285 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3286 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3287 } else {
3288 temp &= ~FDI_LINK_TRAIN_NONE;
3289 temp |= FDI_LINK_TRAIN_NONE;
3290 }
3291 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3292
3293 /* wait one idle pattern time */
3294 POSTING_READ(reg);
3295 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003296
3297 /* IVB wants error correction enabled */
3298 if (IS_IVYBRIDGE(dev))
3299 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3300 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003301}
3302
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003303/* The FDI link training functions for ILK/Ibexpeak. */
3304static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3305{
3306 struct drm_device *dev = crtc->dev;
3307 struct drm_i915_private *dev_priv = dev->dev_private;
3308 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3309 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003310 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003311
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003312 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003313 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003314
Adam Jacksone1a44742010-06-25 15:32:14 -04003315 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3316 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003317 reg = FDI_RX_IMR(pipe);
3318 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003319 temp &= ~FDI_RX_SYMBOL_LOCK;
3320 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003321 I915_WRITE(reg, temp);
3322 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003323 udelay(150);
3324
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003325 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003326 reg = FDI_TX_CTL(pipe);
3327 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003328 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003329 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003330 temp &= ~FDI_LINK_TRAIN_NONE;
3331 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003332 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003333
Chris Wilson5eddb702010-09-11 13:48:45 +01003334 reg = FDI_RX_CTL(pipe);
3335 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003336 temp &= ~FDI_LINK_TRAIN_NONE;
3337 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003338 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3339
3340 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003341 udelay(150);
3342
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003343 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003344 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3345 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3346 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003347
Chris Wilson5eddb702010-09-11 13:48:45 +01003348 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003349 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003350 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003351 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3352
3353 if ((temp & FDI_RX_BIT_LOCK)) {
3354 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003355 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003356 break;
3357 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003358 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003359 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003360 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003361
3362 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003363 reg = FDI_TX_CTL(pipe);
3364 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003365 temp &= ~FDI_LINK_TRAIN_NONE;
3366 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003367 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003368
Chris Wilson5eddb702010-09-11 13:48:45 +01003369 reg = FDI_RX_CTL(pipe);
3370 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003371 temp &= ~FDI_LINK_TRAIN_NONE;
3372 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003373 I915_WRITE(reg, temp);
3374
3375 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003376 udelay(150);
3377
Chris Wilson5eddb702010-09-11 13:48:45 +01003378 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003379 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003380 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003381 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3382
3383 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003384 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003385 DRM_DEBUG_KMS("FDI train 2 done.\n");
3386 break;
3387 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003388 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003389 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003390 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003391
3392 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003393
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003394}
3395
Akshay Joshi0206e352011-08-16 15:34:10 -04003396static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003397 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3398 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3399 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3400 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3401};
3402
3403/* The FDI link training functions for SNB/Cougarpoint. */
3404static void gen6_fdi_link_train(struct drm_crtc *crtc)
3405{
3406 struct drm_device *dev = crtc->dev;
3407 struct drm_i915_private *dev_priv = dev->dev_private;
3408 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3409 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003410 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003411
Adam Jacksone1a44742010-06-25 15:32:14 -04003412 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3413 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003414 reg = FDI_RX_IMR(pipe);
3415 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003416 temp &= ~FDI_RX_SYMBOL_LOCK;
3417 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003418 I915_WRITE(reg, temp);
3419
3420 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003421 udelay(150);
3422
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003423 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003424 reg = FDI_TX_CTL(pipe);
3425 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003426 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003427 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003428 temp &= ~FDI_LINK_TRAIN_NONE;
3429 temp |= FDI_LINK_TRAIN_PATTERN_1;
3430 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3431 /* SNB-B */
3432 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003433 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003434
Daniel Vetterd74cf322012-10-26 10:58:13 +02003435 I915_WRITE(FDI_RX_MISC(pipe),
3436 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3437
Chris Wilson5eddb702010-09-11 13:48:45 +01003438 reg = FDI_RX_CTL(pipe);
3439 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003440 if (HAS_PCH_CPT(dev)) {
3441 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3442 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3443 } else {
3444 temp &= ~FDI_LINK_TRAIN_NONE;
3445 temp |= FDI_LINK_TRAIN_PATTERN_1;
3446 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003447 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3448
3449 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003450 udelay(150);
3451
Akshay Joshi0206e352011-08-16 15:34:10 -04003452 for (i = 0; i < 4; i++) {
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_VOL_EMP_MASK;
3456 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003457 I915_WRITE(reg, temp);
3458
3459 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003460 udelay(500);
3461
Sean Paulfa37d392012-03-02 12:53:39 -05003462 for (retry = 0; retry < 5; retry++) {
3463 reg = FDI_RX_IIR(pipe);
3464 temp = I915_READ(reg);
3465 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3466 if (temp & FDI_RX_BIT_LOCK) {
3467 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3468 DRM_DEBUG_KMS("FDI train 1 done.\n");
3469 break;
3470 }
3471 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003472 }
Sean Paulfa37d392012-03-02 12:53:39 -05003473 if (retry < 5)
3474 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003475 }
3476 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003477 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003478
3479 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003480 reg = FDI_TX_CTL(pipe);
3481 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003482 temp &= ~FDI_LINK_TRAIN_NONE;
3483 temp |= FDI_LINK_TRAIN_PATTERN_2;
3484 if (IS_GEN6(dev)) {
3485 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3486 /* SNB-B */
3487 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3488 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003489 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003490
Chris Wilson5eddb702010-09-11 13:48:45 +01003491 reg = FDI_RX_CTL(pipe);
3492 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003493 if (HAS_PCH_CPT(dev)) {
3494 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3495 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3496 } else {
3497 temp &= ~FDI_LINK_TRAIN_NONE;
3498 temp |= FDI_LINK_TRAIN_PATTERN_2;
3499 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003500 I915_WRITE(reg, temp);
3501
3502 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003503 udelay(150);
3504
Akshay Joshi0206e352011-08-16 15:34:10 -04003505 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003506 reg = FDI_TX_CTL(pipe);
3507 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003508 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3509 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003510 I915_WRITE(reg, temp);
3511
3512 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003513 udelay(500);
3514
Sean Paulfa37d392012-03-02 12:53:39 -05003515 for (retry = 0; retry < 5; retry++) {
3516 reg = FDI_RX_IIR(pipe);
3517 temp = I915_READ(reg);
3518 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3519 if (temp & FDI_RX_SYMBOL_LOCK) {
3520 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3521 DRM_DEBUG_KMS("FDI train 2 done.\n");
3522 break;
3523 }
3524 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003525 }
Sean Paulfa37d392012-03-02 12:53:39 -05003526 if (retry < 5)
3527 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003528 }
3529 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003530 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003531
3532 DRM_DEBUG_KMS("FDI train done.\n");
3533}
3534
Jesse Barnes357555c2011-04-28 15:09:55 -07003535/* Manual link training for Ivy Bridge A0 parts */
3536static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3537{
3538 struct drm_device *dev = crtc->dev;
3539 struct drm_i915_private *dev_priv = dev->dev_private;
3540 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3541 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003542 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003543
3544 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3545 for train result */
3546 reg = FDI_RX_IMR(pipe);
3547 temp = I915_READ(reg);
3548 temp &= ~FDI_RX_SYMBOL_LOCK;
3549 temp &= ~FDI_RX_BIT_LOCK;
3550 I915_WRITE(reg, temp);
3551
3552 POSTING_READ(reg);
3553 udelay(150);
3554
Daniel Vetter01a415f2012-10-27 15:58:40 +02003555 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3556 I915_READ(FDI_RX_IIR(pipe)));
3557
Jesse Barnes139ccd32013-08-19 11:04:55 -07003558 /* Try each vswing and preemphasis setting twice before moving on */
3559 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3560 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003561 reg = FDI_TX_CTL(pipe);
3562 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003563 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3564 temp &= ~FDI_TX_ENABLE;
3565 I915_WRITE(reg, temp);
3566
3567 reg = FDI_RX_CTL(pipe);
3568 temp = I915_READ(reg);
3569 temp &= ~FDI_LINK_TRAIN_AUTO;
3570 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3571 temp &= ~FDI_RX_ENABLE;
3572 I915_WRITE(reg, temp);
3573
3574 /* enable CPU FDI TX and PCH FDI RX */
3575 reg = FDI_TX_CTL(pipe);
3576 temp = I915_READ(reg);
3577 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003578 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003579 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003580 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003581 temp |= snb_b_fdi_train_param[j/2];
3582 temp |= FDI_COMPOSITE_SYNC;
3583 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3584
3585 I915_WRITE(FDI_RX_MISC(pipe),
3586 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3587
3588 reg = FDI_RX_CTL(pipe);
3589 temp = I915_READ(reg);
3590 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3591 temp |= FDI_COMPOSITE_SYNC;
3592 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3593
3594 POSTING_READ(reg);
3595 udelay(1); /* should be 0.5us */
3596
3597 for (i = 0; i < 4; i++) {
3598 reg = FDI_RX_IIR(pipe);
3599 temp = I915_READ(reg);
3600 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3601
3602 if (temp & FDI_RX_BIT_LOCK ||
3603 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3604 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3605 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3606 i);
3607 break;
3608 }
3609 udelay(1); /* should be 0.5us */
3610 }
3611 if (i == 4) {
3612 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3613 continue;
3614 }
3615
3616 /* Train 2 */
3617 reg = FDI_TX_CTL(pipe);
3618 temp = I915_READ(reg);
3619 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3620 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3621 I915_WRITE(reg, temp);
3622
3623 reg = FDI_RX_CTL(pipe);
3624 temp = I915_READ(reg);
3625 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3626 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003627 I915_WRITE(reg, temp);
3628
3629 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003630 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003631
Jesse Barnes139ccd32013-08-19 11:04:55 -07003632 for (i = 0; i < 4; i++) {
3633 reg = FDI_RX_IIR(pipe);
3634 temp = I915_READ(reg);
3635 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003636
Jesse Barnes139ccd32013-08-19 11:04:55 -07003637 if (temp & FDI_RX_SYMBOL_LOCK ||
3638 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3639 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3640 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3641 i);
3642 goto train_done;
3643 }
3644 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003645 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003646 if (i == 4)
3647 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003648 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003649
Jesse Barnes139ccd32013-08-19 11:04:55 -07003650train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003651 DRM_DEBUG_KMS("FDI train done.\n");
3652}
3653
Daniel Vetter88cefb62012-08-12 19:27:14 +02003654static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003655{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003656 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003657 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003658 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003659 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003660
Jesse Barnesc64e3112010-09-10 11:27:03 -07003661
Jesse Barnes0e23b992010-09-10 11:10:00 -07003662 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003663 reg = FDI_RX_CTL(pipe);
3664 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003665 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003666 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003667 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003668 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3669
3670 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003671 udelay(200);
3672
3673 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003674 temp = I915_READ(reg);
3675 I915_WRITE(reg, temp | FDI_PCDCLK);
3676
3677 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003678 udelay(200);
3679
Paulo Zanoni20749732012-11-23 15:30:38 -02003680 /* Enable CPU FDI TX PLL, always on for Ironlake */
3681 reg = FDI_TX_CTL(pipe);
3682 temp = I915_READ(reg);
3683 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3684 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003685
Paulo Zanoni20749732012-11-23 15:30:38 -02003686 POSTING_READ(reg);
3687 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003688 }
3689}
3690
Daniel Vetter88cefb62012-08-12 19:27:14 +02003691static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3692{
3693 struct drm_device *dev = intel_crtc->base.dev;
3694 struct drm_i915_private *dev_priv = dev->dev_private;
3695 int pipe = intel_crtc->pipe;
3696 u32 reg, temp;
3697
3698 /* Switch from PCDclk to Rawclk */
3699 reg = FDI_RX_CTL(pipe);
3700 temp = I915_READ(reg);
3701 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3702
3703 /* Disable CPU FDI TX PLL */
3704 reg = FDI_TX_CTL(pipe);
3705 temp = I915_READ(reg);
3706 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3707
3708 POSTING_READ(reg);
3709 udelay(100);
3710
3711 reg = FDI_RX_CTL(pipe);
3712 temp = I915_READ(reg);
3713 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3714
3715 /* Wait for the clocks to turn off. */
3716 POSTING_READ(reg);
3717 udelay(100);
3718}
3719
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003720static void ironlake_fdi_disable(struct drm_crtc *crtc)
3721{
3722 struct drm_device *dev = crtc->dev;
3723 struct drm_i915_private *dev_priv = dev->dev_private;
3724 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3725 int pipe = intel_crtc->pipe;
3726 u32 reg, temp;
3727
3728 /* disable CPU FDI tx and PCH FDI rx */
3729 reg = FDI_TX_CTL(pipe);
3730 temp = I915_READ(reg);
3731 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3732 POSTING_READ(reg);
3733
3734 reg = FDI_RX_CTL(pipe);
3735 temp = I915_READ(reg);
3736 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003737 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003738 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3739
3740 POSTING_READ(reg);
3741 udelay(100);
3742
3743 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003744 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003745 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003746
3747 /* still set train pattern 1 */
3748 reg = FDI_TX_CTL(pipe);
3749 temp = I915_READ(reg);
3750 temp &= ~FDI_LINK_TRAIN_NONE;
3751 temp |= FDI_LINK_TRAIN_PATTERN_1;
3752 I915_WRITE(reg, temp);
3753
3754 reg = FDI_RX_CTL(pipe);
3755 temp = I915_READ(reg);
3756 if (HAS_PCH_CPT(dev)) {
3757 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3758 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3759 } else {
3760 temp &= ~FDI_LINK_TRAIN_NONE;
3761 temp |= FDI_LINK_TRAIN_PATTERN_1;
3762 }
3763 /* BPC in FDI rx is consistent with that in PIPECONF */
3764 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003765 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003766 I915_WRITE(reg, temp);
3767
3768 POSTING_READ(reg);
3769 udelay(100);
3770}
3771
Chris Wilson5dce5b932014-01-20 10:17:36 +00003772bool intel_has_pending_fb_unpin(struct drm_device *dev)
3773{
3774 struct intel_crtc *crtc;
3775
3776 /* Note that we don't need to be called with mode_config.lock here
3777 * as our list of CRTC objects is static for the lifetime of the
3778 * device and so cannot disappear as we iterate. Similarly, we can
3779 * happily treat the predicates as racy, atomic checks as userspace
3780 * cannot claim and pin a new fb without at least acquring the
3781 * struct_mutex and so serialising with us.
3782 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003783 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003784 if (atomic_read(&crtc->unpin_work_count) == 0)
3785 continue;
3786
3787 if (crtc->unpin_work)
3788 intel_wait_for_vblank(dev, crtc->pipe);
3789
3790 return true;
3791 }
3792
3793 return false;
3794}
3795
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003796static void page_flip_completed(struct intel_crtc *intel_crtc)
3797{
3798 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3799 struct intel_unpin_work *work = intel_crtc->unpin_work;
3800
3801 /* ensure that the unpin work is consistent wrt ->pending. */
3802 smp_rmb();
3803 intel_crtc->unpin_work = NULL;
3804
3805 if (work->event)
3806 drm_send_vblank_event(intel_crtc->base.dev,
3807 intel_crtc->pipe,
3808 work->event);
3809
3810 drm_crtc_vblank_put(&intel_crtc->base);
3811
3812 wake_up_all(&dev_priv->pending_flip_queue);
3813 queue_work(dev_priv->wq, &work->work);
3814
3815 trace_i915_flip_complete(intel_crtc->plane,
3816 work->pending_flip_obj);
3817}
3818
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003819void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003820{
Chris Wilson0f911282012-04-17 10:05:38 +01003821 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003822 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003823
Daniel Vetter2c10d572012-12-20 21:24:07 +01003824 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003825 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3826 !intel_crtc_has_pending_flip(crtc),
3827 60*HZ) == 0)) {
3828 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003829
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003830 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003831 if (intel_crtc->unpin_work) {
3832 WARN_ONCE(1, "Removing stuck page flip\n");
3833 page_flip_completed(intel_crtc);
3834 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003835 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003836 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003837
Chris Wilson975d5682014-08-20 13:13:34 +01003838 if (crtc->primary->fb) {
3839 mutex_lock(&dev->struct_mutex);
3840 intel_finish_fb(crtc->primary->fb);
3841 mutex_unlock(&dev->struct_mutex);
3842 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003843}
3844
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003845/* Program iCLKIP clock to the desired frequency */
3846static void lpt_program_iclkip(struct drm_crtc *crtc)
3847{
3848 struct drm_device *dev = crtc->dev;
3849 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003850 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003851 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3852 u32 temp;
3853
Daniel Vetter09153002012-12-12 14:06:44 +01003854 mutex_lock(&dev_priv->dpio_lock);
3855
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003856 /* It is necessary to ungate the pixclk gate prior to programming
3857 * the divisors, and gate it back when it is done.
3858 */
3859 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3860
3861 /* Disable SSCCTL */
3862 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003863 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3864 SBI_SSCCTL_DISABLE,
3865 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003866
3867 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003868 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003869 auxdiv = 1;
3870 divsel = 0x41;
3871 phaseinc = 0x20;
3872 } else {
3873 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003874 * but the adjusted_mode->crtc_clock in in KHz. To get the
3875 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003876 * convert the virtual clock precision to KHz here for higher
3877 * precision.
3878 */
3879 u32 iclk_virtual_root_freq = 172800 * 1000;
3880 u32 iclk_pi_range = 64;
3881 u32 desired_divisor, msb_divisor_value, pi_value;
3882
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003883 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003884 msb_divisor_value = desired_divisor / iclk_pi_range;
3885 pi_value = desired_divisor % iclk_pi_range;
3886
3887 auxdiv = 0;
3888 divsel = msb_divisor_value - 2;
3889 phaseinc = pi_value;
3890 }
3891
3892 /* This should not happen with any sane values */
3893 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3894 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3895 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3896 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3897
3898 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 +03003899 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003900 auxdiv,
3901 divsel,
3902 phasedir,
3903 phaseinc);
3904
3905 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003906 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003907 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3908 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3909 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3910 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3911 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3912 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003913 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003914
3915 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003916 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003917 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3918 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003919 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003920
3921 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003922 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003923 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003924 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003925
3926 /* Wait for initialization time */
3927 udelay(24);
3928
3929 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003930
3931 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003932}
3933
Daniel Vetter275f01b22013-05-03 11:49:47 +02003934static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3935 enum pipe pch_transcoder)
3936{
3937 struct drm_device *dev = crtc->base.dev;
3938 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003939 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003940
3941 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3942 I915_READ(HTOTAL(cpu_transcoder)));
3943 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3944 I915_READ(HBLANK(cpu_transcoder)));
3945 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3946 I915_READ(HSYNC(cpu_transcoder)));
3947
3948 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3949 I915_READ(VTOTAL(cpu_transcoder)));
3950 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3951 I915_READ(VBLANK(cpu_transcoder)));
3952 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3953 I915_READ(VSYNC(cpu_transcoder)));
3954 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3955 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3956}
3957
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003958static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003959{
3960 struct drm_i915_private *dev_priv = dev->dev_private;
3961 uint32_t temp;
3962
3963 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003964 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003965 return;
3966
3967 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3968 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3969
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003970 temp &= ~FDI_BC_BIFURCATION_SELECT;
3971 if (enable)
3972 temp |= FDI_BC_BIFURCATION_SELECT;
3973
3974 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003975 I915_WRITE(SOUTH_CHICKEN1, temp);
3976 POSTING_READ(SOUTH_CHICKEN1);
3977}
3978
3979static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3980{
3981 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003982
3983 switch (intel_crtc->pipe) {
3984 case PIPE_A:
3985 break;
3986 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003987 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003988 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003989 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003990 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003991
3992 break;
3993 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003994 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003995
3996 break;
3997 default:
3998 BUG();
3999 }
4000}
4001
Jesse Barnesf67a5592011-01-05 10:31:48 -08004002/*
4003 * Enable PCH resources required for PCH ports:
4004 * - PCH PLLs
4005 * - FDI training & RX/TX
4006 * - update transcoder timings
4007 * - DP transcoding bits
4008 * - transcoder
4009 */
4010static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004011{
4012 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004013 struct drm_i915_private *dev_priv = dev->dev_private;
4014 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4015 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004016 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004017
Daniel Vetterab9412b2013-05-03 11:49:46 +02004018 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004019
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004020 if (IS_IVYBRIDGE(dev))
4021 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4022
Daniel Vettercd986ab2012-10-26 10:58:12 +02004023 /* Write the TU size bits before fdi link training, so that error
4024 * detection works. */
4025 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4026 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4027
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004028 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004029 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004030
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004031 /* We need to program the right clock selection before writing the pixel
4032 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004033 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004034 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004035
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004036 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004037 temp |= TRANS_DPLL_ENABLE(pipe);
4038 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004039 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004040 temp |= sel;
4041 else
4042 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004043 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004044 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004045
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004046 /* XXX: pch pll's can be enabled any time before we enable the PCH
4047 * transcoder, and we actually should do this to not upset any PCH
4048 * transcoder that already use the clock when we share it.
4049 *
4050 * Note that enable_shared_dpll tries to do the right thing, but
4051 * get_shared_dpll unconditionally resets the pll - we need that to have
4052 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004053 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004054
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004055 /* set transcoder timing, panel must allow it */
4056 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004057 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004058
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004059 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004060
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004061 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004062 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004063 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004064 reg = TRANS_DP_CTL(pipe);
4065 temp = I915_READ(reg);
4066 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004067 TRANS_DP_SYNC_MASK |
4068 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01004069 temp |= (TRANS_DP_OUTPUT_ENABLE |
4070 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004071 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004072
4073 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004074 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004075 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004076 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004077
4078 switch (intel_trans_dp_port_sel(crtc)) {
4079 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004080 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004081 break;
4082 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004083 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004084 break;
4085 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004086 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004087 break;
4088 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004089 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004090 }
4091
Chris Wilson5eddb702010-09-11 13:48:45 +01004092 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004093 }
4094
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004095 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004096}
4097
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004098static void lpt_pch_enable(struct drm_crtc *crtc)
4099{
4100 struct drm_device *dev = crtc->dev;
4101 struct drm_i915_private *dev_priv = dev->dev_private;
4102 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004103 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004104
Daniel Vetterab9412b2013-05-03 11:49:46 +02004105 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004106
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004107 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004108
Paulo Zanoni0540e482012-10-31 18:12:40 -02004109 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004110 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004111
Paulo Zanoni937bb612012-10-31 18:12:47 -02004112 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004113}
4114
Daniel Vetter716c2e52014-06-25 22:02:02 +03004115void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004116{
Daniel Vettere2b78262013-06-07 23:10:03 +02004117 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004118
4119 if (pll == NULL)
4120 return;
4121
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004122 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004123 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004124 return;
4125 }
4126
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004127 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4128 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004129 WARN_ON(pll->on);
4130 WARN_ON(pll->active);
4131 }
4132
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004133 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004134}
4135
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004136struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4137 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004138{
Daniel Vettere2b78262013-06-07 23:10:03 +02004139 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004140 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004141 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004142
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004143 if (HAS_PCH_IBX(dev_priv->dev)) {
4144 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004145 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004146 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004147
Daniel Vetter46edb022013-06-05 13:34:12 +02004148 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4149 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004150
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004151 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004152
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004153 goto found;
4154 }
4155
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004156 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4157 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004158
4159 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004160 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004161 continue;
4162
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004163 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004164 &pll->new_config->hw_state,
4165 sizeof(pll->new_config->hw_state)) == 0) {
4166 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004167 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004168 pll->new_config->crtc_mask,
4169 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004170 goto found;
4171 }
4172 }
4173
4174 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004175 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4176 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004177 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004178 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4179 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004180 goto found;
4181 }
4182 }
4183
4184 return NULL;
4185
4186found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004187 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004188 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004189
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004190 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004191 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4192 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004193
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004194 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004195
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004196 return pll;
4197}
4198
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004199/**
4200 * intel_shared_dpll_start_config - start a new PLL staged config
4201 * @dev_priv: DRM device
4202 * @clear_pipes: mask of pipes that will have their PLLs freed
4203 *
4204 * Starts a new PLL staged config, copying the current config but
4205 * releasing the references of pipes specified in clear_pipes.
4206 */
4207static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4208 unsigned clear_pipes)
4209{
4210 struct intel_shared_dpll *pll;
4211 enum intel_dpll_id i;
4212
4213 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4214 pll = &dev_priv->shared_dplls[i];
4215
4216 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4217 GFP_KERNEL);
4218 if (!pll->new_config)
4219 goto cleanup;
4220
4221 pll->new_config->crtc_mask &= ~clear_pipes;
4222 }
4223
4224 return 0;
4225
4226cleanup:
4227 while (--i >= 0) {
4228 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004229 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004230 pll->new_config = NULL;
4231 }
4232
4233 return -ENOMEM;
4234}
4235
4236static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4237{
4238 struct intel_shared_dpll *pll;
4239 enum intel_dpll_id i;
4240
4241 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4242 pll = &dev_priv->shared_dplls[i];
4243
4244 WARN_ON(pll->new_config == &pll->config);
4245
4246 pll->config = *pll->new_config;
4247 kfree(pll->new_config);
4248 pll->new_config = NULL;
4249 }
4250}
4251
4252static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4253{
4254 struct intel_shared_dpll *pll;
4255 enum intel_dpll_id i;
4256
4257 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4258 pll = &dev_priv->shared_dplls[i];
4259
4260 WARN_ON(pll->new_config == &pll->config);
4261
4262 kfree(pll->new_config);
4263 pll->new_config = NULL;
4264 }
4265}
4266
Daniel Vettera1520312013-05-03 11:49:50 +02004267static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004268{
4269 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004270 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004271 u32 temp;
4272
4273 temp = I915_READ(dslreg);
4274 udelay(500);
4275 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004276 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004277 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004278 }
4279}
4280
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004281static void skylake_pfit_enable(struct intel_crtc *crtc)
4282{
4283 struct drm_device *dev = crtc->base.dev;
4284 struct drm_i915_private *dev_priv = dev->dev_private;
4285 int pipe = crtc->pipe;
4286
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004287 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004288 I915_WRITE(PS_CTL(pipe), PS_ENABLE);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004289 I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4290 I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004291 }
4292}
4293
Jesse Barnesb074cec2013-04-25 12:55:02 -07004294static void ironlake_pfit_enable(struct intel_crtc *crtc)
4295{
4296 struct drm_device *dev = crtc->base.dev;
4297 struct drm_i915_private *dev_priv = dev->dev_private;
4298 int pipe = crtc->pipe;
4299
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004300 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004301 /* Force use of hard-coded filter coefficients
4302 * as some pre-programmed values are broken,
4303 * e.g. x201.
4304 */
4305 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4306 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4307 PF_PIPE_SEL_IVB(pipe));
4308 else
4309 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004310 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4311 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004312 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004313}
4314
Matt Roper4a3b8762014-12-23 10:41:51 -08004315static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004316{
4317 struct drm_device *dev = crtc->dev;
4318 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004319 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004320 struct intel_plane *intel_plane;
4321
Matt Roperaf2b6532014-04-01 15:22:32 -07004322 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4323 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004324 if (intel_plane->pipe == pipe)
4325 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004326 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004327}
4328
Matt Roper0d703d42015-03-04 10:49:04 -08004329/*
4330 * Disable a plane internally without actually modifying the plane's state.
4331 * This will allow us to easily restore the plane later by just reprogramming
4332 * its state.
4333 */
4334static void disable_plane_internal(struct drm_plane *plane)
4335{
4336 struct intel_plane *intel_plane = to_intel_plane(plane);
4337 struct drm_plane_state *state =
4338 plane->funcs->atomic_duplicate_state(plane);
4339 struct intel_plane_state *intel_state = to_intel_plane_state(state);
4340
4341 intel_state->visible = false;
4342 intel_plane->commit_plane(plane, intel_state);
4343
4344 intel_plane_destroy_state(plane, state);
4345}
4346
Matt Roper4a3b8762014-12-23 10:41:51 -08004347static void intel_disable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004348{
4349 struct drm_device *dev = crtc->dev;
4350 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004351 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004352 struct intel_plane *intel_plane;
4353
Matt Roperaf2b6532014-04-01 15:22:32 -07004354 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4355 intel_plane = to_intel_plane(plane);
Matt Roper0d703d42015-03-04 10:49:04 -08004356 if (plane->fb && intel_plane->pipe == pipe)
4357 disable_plane_internal(plane);
Matt Roperaf2b6532014-04-01 15:22:32 -07004358 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004359}
4360
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004361void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004362{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004363 struct drm_device *dev = crtc->base.dev;
4364 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004365
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004366 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004367 return;
4368
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004369 /* We can only enable IPS after we enable a plane and wait for a vblank */
4370 intel_wait_for_vblank(dev, crtc->pipe);
4371
Paulo Zanonid77e4532013-09-24 13:52:55 -03004372 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004373 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004374 mutex_lock(&dev_priv->rps.hw_lock);
4375 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4376 mutex_unlock(&dev_priv->rps.hw_lock);
4377 /* Quoting Art Runyan: "its not safe to expect any particular
4378 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004379 * mailbox." Moreover, the mailbox may return a bogus state,
4380 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004381 */
4382 } else {
4383 I915_WRITE(IPS_CTL, IPS_ENABLE);
4384 /* The bit only becomes 1 in the next vblank, so this wait here
4385 * is essentially intel_wait_for_vblank. If we don't have this
4386 * and don't wait for vblanks until the end of crtc_enable, then
4387 * the HW state readout code will complain that the expected
4388 * IPS_CTL value is not the one we read. */
4389 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4390 DRM_ERROR("Timed out waiting for IPS enable\n");
4391 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004392}
4393
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004394void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004395{
4396 struct drm_device *dev = crtc->base.dev;
4397 struct drm_i915_private *dev_priv = dev->dev_private;
4398
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004399 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004400 return;
4401
4402 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004403 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004404 mutex_lock(&dev_priv->rps.hw_lock);
4405 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4406 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004407 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4408 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4409 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004410 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004411 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004412 POSTING_READ(IPS_CTL);
4413 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004414
4415 /* We need to wait for a vblank before we can disable the plane. */
4416 intel_wait_for_vblank(dev, crtc->pipe);
4417}
4418
4419/** Loads the palette/gamma unit for the CRTC with the prepared values */
4420static void intel_crtc_load_lut(struct drm_crtc *crtc)
4421{
4422 struct drm_device *dev = crtc->dev;
4423 struct drm_i915_private *dev_priv = dev->dev_private;
4424 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4425 enum pipe pipe = intel_crtc->pipe;
4426 int palreg = PALETTE(pipe);
4427 int i;
4428 bool reenable_ips = false;
4429
4430 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004431 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004432 return;
4433
4434 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004435 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004436 assert_dsi_pll_enabled(dev_priv);
4437 else
4438 assert_pll_enabled(dev_priv, pipe);
4439 }
4440
4441 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304442 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004443 palreg = LGC_PALETTE(pipe);
4444
4445 /* Workaround : Do not read or write the pipe palette/gamma data while
4446 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4447 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004448 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004449 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4450 GAMMA_MODE_MODE_SPLIT)) {
4451 hsw_disable_ips(intel_crtc);
4452 reenable_ips = true;
4453 }
4454
4455 for (i = 0; i < 256; i++) {
4456 I915_WRITE(palreg + 4 * i,
4457 (intel_crtc->lut_r[i] << 16) |
4458 (intel_crtc->lut_g[i] << 8) |
4459 intel_crtc->lut_b[i]);
4460 }
4461
4462 if (reenable_ips)
4463 hsw_enable_ips(intel_crtc);
4464}
4465
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004466static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4467{
4468 if (!enable && intel_crtc->overlay) {
4469 struct drm_device *dev = intel_crtc->base.dev;
4470 struct drm_i915_private *dev_priv = dev->dev_private;
4471
4472 mutex_lock(&dev->struct_mutex);
4473 dev_priv->mm.interruptible = false;
4474 (void) intel_overlay_switch_off(intel_crtc->overlay);
4475 dev_priv->mm.interruptible = true;
4476 mutex_unlock(&dev->struct_mutex);
4477 }
4478
4479 /* Let userspace switch the overlay on again. In most cases userspace
4480 * has to recompute where to put it anyway.
4481 */
4482}
4483
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004484static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004485{
4486 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004487 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4488 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004489
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004490 intel_enable_primary_hw_plane(crtc->primary, crtc);
Matt Roper4a3b8762014-12-23 10:41:51 -08004491 intel_enable_sprite_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004492 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004493 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004494
4495 hsw_enable_ips(intel_crtc);
4496
4497 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004498 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004499 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004500
4501 /*
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 from 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
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004509static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004510{
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);
4514 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004515
4516 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004517
Paulo Zanonie35fef22015-02-09 14:46:29 -02004518 if (dev_priv->fbc.crtc == intel_crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004519 intel_fbc_disable(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004520
4521 hsw_disable_ips(intel_crtc);
4522
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004523 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004524 intel_crtc_update_cursor(crtc, false);
Matt Roper4a3b8762014-12-23 10:41:51 -08004525 intel_disable_sprite_planes(crtc);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004526 intel_disable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004527
Daniel Vetterf99d7062014-06-19 16:01:59 +02004528 /*
4529 * FIXME: Once we grow proper nuclear flip support out of this we need
4530 * to compute the mask of flip planes precisely. For the time being
4531 * consider this a flip to a NULL plane.
4532 */
4533 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004534}
4535
Jesse Barnesf67a5592011-01-05 10:31:48 -08004536static void ironlake_crtc_enable(struct drm_crtc *crtc)
4537{
4538 struct drm_device *dev = crtc->dev;
4539 struct drm_i915_private *dev_priv = dev->dev_private;
4540 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004541 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004542 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004543
Matt Roper83d65732015-02-25 13:12:16 -08004544 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004545
Jesse Barnesf67a5592011-01-05 10:31:48 -08004546 if (intel_crtc->active)
4547 return;
4548
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004549 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004550 intel_prepare_shared_dpll(intel_crtc);
4551
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004552 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304553 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004554
4555 intel_set_pipe_timings(intel_crtc);
4556
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004557 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004558 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004559 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004560 }
4561
4562 ironlake_set_pipeconf(crtc);
4563
Jesse Barnesf67a5592011-01-05 10:31:48 -08004564 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004565
Daniel Vettera72e4c92014-09-30 10:56:47 +02004566 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4567 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004568
Daniel Vetterf6736a12013-06-05 13:34:30 +02004569 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004570 if (encoder->pre_enable)
4571 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004572
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004573 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004574 /* Note: FDI PLL enabling _must_ be done before we enable the
4575 * cpu pipes, hence this is separate from all the other fdi/pch
4576 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004577 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004578 } else {
4579 assert_fdi_tx_disabled(dev_priv, pipe);
4580 assert_fdi_rx_disabled(dev_priv, pipe);
4581 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004582
Jesse Barnesb074cec2013-04-25 12:55:02 -07004583 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004584
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004585 /*
4586 * On ILK+ LUT must be loaded before the pipe is running but with
4587 * clocks enabled
4588 */
4589 intel_crtc_load_lut(crtc);
4590
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004591 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004592 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004593
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004594 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004595 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004596
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004597 assert_vblank_disabled(crtc);
4598 drm_crtc_vblank_on(crtc);
4599
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004600 for_each_encoder_on_crtc(dev, crtc, encoder)
4601 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004602
4603 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004604 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004605
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004606 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004607}
4608
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004609/* IPS only exists on ULT machines and is tied to pipe A. */
4610static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4611{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004612 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004613}
4614
Paulo Zanonie4916942013-09-20 16:21:19 -03004615/*
4616 * This implements the workaround described in the "notes" section of the mode
4617 * set sequence documentation. When going from no pipes or single pipe to
4618 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4619 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4620 */
4621static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4622{
4623 struct drm_device *dev = crtc->base.dev;
4624 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4625
4626 /* We want to get the other_active_crtc only if there's only 1 other
4627 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004628 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004629 if (!crtc_it->active || crtc_it == crtc)
4630 continue;
4631
4632 if (other_active_crtc)
4633 return;
4634
4635 other_active_crtc = crtc_it;
4636 }
4637 if (!other_active_crtc)
4638 return;
4639
4640 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4641 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4642}
4643
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004644static void haswell_crtc_enable(struct drm_crtc *crtc)
4645{
4646 struct drm_device *dev = crtc->dev;
4647 struct drm_i915_private *dev_priv = dev->dev_private;
4648 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4649 struct intel_encoder *encoder;
4650 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004651
Matt Roper83d65732015-02-25 13:12:16 -08004652 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004653
4654 if (intel_crtc->active)
4655 return;
4656
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004657 if (intel_crtc_to_shared_dpll(intel_crtc))
4658 intel_enable_shared_dpll(intel_crtc);
4659
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004660 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304661 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004662
4663 intel_set_pipe_timings(intel_crtc);
4664
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004665 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4666 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4667 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004668 }
4669
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004670 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004671 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004672 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004673 }
4674
4675 haswell_set_pipeconf(crtc);
4676
4677 intel_set_pipe_csc(crtc);
4678
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004679 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004680
Daniel Vettera72e4c92014-09-30 10:56:47 +02004681 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004682 for_each_encoder_on_crtc(dev, crtc, encoder)
4683 if (encoder->pre_enable)
4684 encoder->pre_enable(encoder);
4685
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004686 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004687 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4688 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004689 dev_priv->display.fdi_link_train(crtc);
4690 }
4691
Paulo Zanoni1f544382012-10-24 11:32:00 -02004692 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004693
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004694 if (IS_SKYLAKE(dev))
4695 skylake_pfit_enable(intel_crtc);
4696 else
4697 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004698
4699 /*
4700 * On ILK+ LUT must be loaded before the pipe is running but with
4701 * clocks enabled
4702 */
4703 intel_crtc_load_lut(crtc);
4704
Paulo Zanoni1f544382012-10-24 11:32:00 -02004705 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004706 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004707
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004708 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004709 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004710
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004711 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004712 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004713
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004714 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004715 intel_ddi_set_vc_payload_alloc(crtc, true);
4716
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004717 assert_vblank_disabled(crtc);
4718 drm_crtc_vblank_on(crtc);
4719
Jani Nikula8807e552013-08-30 19:40:32 +03004720 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004721 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004722 intel_opregion_notify_encoder(encoder, true);
4723 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004724
Paulo Zanonie4916942013-09-20 16:21:19 -03004725 /* If we change the relative order between pipe/planes enabling, we need
4726 * to change the workaround. */
4727 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004728 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004729}
4730
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004731static void skylake_pfit_disable(struct intel_crtc *crtc)
4732{
4733 struct drm_device *dev = crtc->base.dev;
4734 struct drm_i915_private *dev_priv = dev->dev_private;
4735 int pipe = crtc->pipe;
4736
4737 /* To avoid upsetting the power well on haswell only disable the pfit if
4738 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004739 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004740 I915_WRITE(PS_CTL(pipe), 0);
4741 I915_WRITE(PS_WIN_POS(pipe), 0);
4742 I915_WRITE(PS_WIN_SZ(pipe), 0);
4743 }
4744}
4745
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004746static void ironlake_pfit_disable(struct intel_crtc *crtc)
4747{
4748 struct drm_device *dev = crtc->base.dev;
4749 struct drm_i915_private *dev_priv = dev->dev_private;
4750 int pipe = crtc->pipe;
4751
4752 /* To avoid upsetting the power well on haswell only disable the pfit if
4753 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004754 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004755 I915_WRITE(PF_CTL(pipe), 0);
4756 I915_WRITE(PF_WIN_POS(pipe), 0);
4757 I915_WRITE(PF_WIN_SZ(pipe), 0);
4758 }
4759}
4760
Jesse Barnes6be4a602010-09-10 10:26:01 -07004761static void ironlake_crtc_disable(struct drm_crtc *crtc)
4762{
4763 struct drm_device *dev = crtc->dev;
4764 struct drm_i915_private *dev_priv = dev->dev_private;
4765 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004766 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004767 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004768 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004769
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004770 if (!intel_crtc->active)
4771 return;
4772
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004773 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004774
Daniel Vetterea9d7582012-07-10 10:42:52 +02004775 for_each_encoder_on_crtc(dev, crtc, encoder)
4776 encoder->disable(encoder);
4777
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004778 drm_crtc_vblank_off(crtc);
4779 assert_vblank_disabled(crtc);
4780
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004781 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004782 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02004783
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004784 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004785
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004786 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004787
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004788 for_each_encoder_on_crtc(dev, crtc, encoder)
4789 if (encoder->post_disable)
4790 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004791
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004792 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004793 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004794
Daniel Vetterd925c592013-06-05 13:34:04 +02004795 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004796
Daniel Vetterd925c592013-06-05 13:34:04 +02004797 if (HAS_PCH_CPT(dev)) {
4798 /* disable TRANS_DP_CTL */
4799 reg = TRANS_DP_CTL(pipe);
4800 temp = I915_READ(reg);
4801 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4802 TRANS_DP_PORT_SEL_MASK);
4803 temp |= TRANS_DP_PORT_SEL_NONE;
4804 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004805
Daniel Vetterd925c592013-06-05 13:34:04 +02004806 /* disable DPLL_SEL */
4807 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004808 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004809 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004810 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004811
4812 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004813 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02004814
4815 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004816 }
4817
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004818 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004819 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004820
4821 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004822 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004823 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004824}
4825
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004826static void haswell_crtc_disable(struct drm_crtc *crtc)
4827{
4828 struct drm_device *dev = crtc->dev;
4829 struct drm_i915_private *dev_priv = dev->dev_private;
4830 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4831 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004832 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004833
4834 if (!intel_crtc->active)
4835 return;
4836
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004837 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03004838
Jani Nikula8807e552013-08-30 19:40:32 +03004839 for_each_encoder_on_crtc(dev, crtc, encoder) {
4840 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004841 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004842 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004843
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004844 drm_crtc_vblank_off(crtc);
4845 assert_vblank_disabled(crtc);
4846
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004847 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004848 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4849 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004850 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004851
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004852 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03004853 intel_ddi_set_vc_payload_alloc(crtc, false);
4854
Paulo Zanoniad80a812012-10-24 16:06:19 -02004855 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004856
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004857 if (IS_SKYLAKE(dev))
4858 skylake_pfit_disable(intel_crtc);
4859 else
4860 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004861
Paulo Zanoni1f544382012-10-24 11:32:00 -02004862 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004863
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004864 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02004865 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02004866 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02004867 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004868
Imre Deak97b040a2014-06-25 22:01:50 +03004869 for_each_encoder_on_crtc(dev, crtc, encoder)
4870 if (encoder->post_disable)
4871 encoder->post_disable(encoder);
4872
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004873 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004874 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004875
4876 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004877 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004878 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004879
4880 if (intel_crtc_to_shared_dpll(intel_crtc))
4881 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004882}
4883
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004884static void ironlake_crtc_off(struct drm_crtc *crtc)
4885{
4886 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004887 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004888}
4889
Paulo Zanoni6441ab52012-10-05 12:05:58 -03004890
Jesse Barnes2dd24552013-04-25 12:55:01 -07004891static void i9xx_pfit_enable(struct intel_crtc *crtc)
4892{
4893 struct drm_device *dev = crtc->base.dev;
4894 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004895 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07004896
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02004897 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07004898 return;
4899
Daniel Vetterc0b03412013-05-28 12:05:54 +02004900 /*
4901 * The panel fitter should only be adjusted whilst the pipe is disabled,
4902 * according to register description and PRM.
4903 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07004904 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4905 assert_pipe_disabled(dev_priv, crtc->pipe);
4906
Jesse Barnesb074cec2013-04-25 12:55:02 -07004907 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4908 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02004909
4910 /* Border color in case we don't scale up to the full screen. Black by
4911 * default, change to something else for debugging. */
4912 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07004913}
4914
Dave Airlied05410f2014-06-05 13:22:59 +10004915static enum intel_display_power_domain port_to_power_domain(enum port port)
4916{
4917 switch (port) {
4918 case PORT_A:
4919 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4920 case PORT_B:
4921 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4922 case PORT_C:
4923 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4924 case PORT_D:
4925 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4926 default:
4927 WARN_ON_ONCE(1);
4928 return POWER_DOMAIN_PORT_OTHER;
4929 }
4930}
4931
Imre Deak77d22dc2014-03-05 16:20:52 +02004932#define for_each_power_domain(domain, mask) \
4933 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4934 if ((1 << (domain)) & (mask))
4935
Imre Deak319be8a2014-03-04 19:22:57 +02004936enum intel_display_power_domain
4937intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02004938{
Imre Deak319be8a2014-03-04 19:22:57 +02004939 struct drm_device *dev = intel_encoder->base.dev;
4940 struct intel_digital_port *intel_dig_port;
4941
4942 switch (intel_encoder->type) {
4943 case INTEL_OUTPUT_UNKNOWN:
4944 /* Only DDI platforms should ever use this output type */
4945 WARN_ON_ONCE(!HAS_DDI(dev));
4946 case INTEL_OUTPUT_DISPLAYPORT:
4947 case INTEL_OUTPUT_HDMI:
4948 case INTEL_OUTPUT_EDP:
4949 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10004950 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10004951 case INTEL_OUTPUT_DP_MST:
4952 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4953 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02004954 case INTEL_OUTPUT_ANALOG:
4955 return POWER_DOMAIN_PORT_CRT;
4956 case INTEL_OUTPUT_DSI:
4957 return POWER_DOMAIN_PORT_DSI;
4958 default:
4959 return POWER_DOMAIN_PORT_OTHER;
4960 }
4961}
4962
4963static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4964{
4965 struct drm_device *dev = crtc->dev;
4966 struct intel_encoder *intel_encoder;
4967 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4968 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02004969 unsigned long mask;
4970 enum transcoder transcoder;
4971
4972 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4973
4974 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4975 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004976 if (intel_crtc->config->pch_pfit.enabled ||
4977 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02004978 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4979
Imre Deak319be8a2014-03-04 19:22:57 +02004980 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4981 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4982
Imre Deak77d22dc2014-03-05 16:20:52 +02004983 return mask;
4984}
4985
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02004986static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02004987{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02004988 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02004989 struct drm_i915_private *dev_priv = dev->dev_private;
4990 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4991 struct intel_crtc *crtc;
4992
4993 /*
4994 * First get all needed power domains, then put all unneeded, to avoid
4995 * any unnecessary toggling of the power wells.
4996 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004997 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004998 enum intel_display_power_domain domain;
4999
Matt Roper83d65732015-02-25 13:12:16 -08005000 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005001 continue;
5002
Imre Deak319be8a2014-03-04 19:22:57 +02005003 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005004
5005 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5006 intel_display_power_get(dev_priv, domain);
5007 }
5008
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005009 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005010 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005011
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005012 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005013 enum intel_display_power_domain domain;
5014
5015 for_each_power_domain(domain, crtc->enabled_power_domains)
5016 intel_display_power_put(dev_priv, domain);
5017
5018 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5019 }
5020
5021 intel_display_set_init_power(dev_priv, false);
5022}
5023
Ville Syrjälädfcab172014-06-13 13:37:47 +03005024/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005025static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005026{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005027 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005028
Jesse Barnes586f49d2013-11-04 16:06:59 -08005029 /* Obtain SKU information */
5030 mutex_lock(&dev_priv->dpio_lock);
5031 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5032 CCK_FUSE_HPLL_FREQ_MASK;
5033 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005034
Ville Syrjälädfcab172014-06-13 13:37:47 +03005035 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005036}
5037
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005038static void vlv_update_cdclk(struct drm_device *dev)
5039{
5040 struct drm_i915_private *dev_priv = dev->dev_private;
5041
5042 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03005043 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005044 dev_priv->vlv_cdclk_freq);
5045
5046 /*
5047 * Program the gmbus_freq based on the cdclk frequency.
5048 * BSpec erroneously claims we should aim for 4MHz, but
5049 * in fact 1MHz is the correct frequency.
5050 */
Ville Syrjälä6be1e3d2014-10-16 20:52:31 +03005051 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005052}
5053
Jesse Barnes30a970c2013-11-04 13:48:12 -08005054/* Adjust CDclk dividers to allow high res or save power if possible */
5055static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5056{
5057 struct drm_i915_private *dev_priv = dev->dev_private;
5058 u32 val, cmd;
5059
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005060 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005061
Ville Syrjälädfcab172014-06-13 13:37:47 +03005062 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005063 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005064 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005065 cmd = 1;
5066 else
5067 cmd = 0;
5068
5069 mutex_lock(&dev_priv->rps.hw_lock);
5070 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5071 val &= ~DSPFREQGUAR_MASK;
5072 val |= (cmd << DSPFREQGUAR_SHIFT);
5073 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5074 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5075 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5076 50)) {
5077 DRM_ERROR("timed out waiting for CDclk change\n");
5078 }
5079 mutex_unlock(&dev_priv->rps.hw_lock);
5080
Ville Syrjälädfcab172014-06-13 13:37:47 +03005081 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005082 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005083
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005084 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005085
5086 mutex_lock(&dev_priv->dpio_lock);
5087 /* adjust cdclk divider */
5088 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005089 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005090 val |= divider;
5091 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005092
5093 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5094 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5095 50))
5096 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005097 mutex_unlock(&dev_priv->dpio_lock);
5098 }
5099
5100 mutex_lock(&dev_priv->dpio_lock);
5101 /* adjust self-refresh exit latency value */
5102 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5103 val &= ~0x7f;
5104
5105 /*
5106 * For high bandwidth configs, we set a higher latency in the bunit
5107 * so that the core display fetch happens in time to avoid underruns.
5108 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005109 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005110 val |= 4500 / 250; /* 4.5 usec */
5111 else
5112 val |= 3000 / 250; /* 3.0 usec */
5113 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5114 mutex_unlock(&dev_priv->dpio_lock);
5115
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005116 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005117}
5118
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005119static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5120{
5121 struct drm_i915_private *dev_priv = dev->dev_private;
5122 u32 val, cmd;
5123
5124 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
5125
5126 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005127 case 333333:
5128 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005129 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005130 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005131 break;
5132 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005133 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005134 return;
5135 }
5136
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005137 /*
5138 * Specs are full of misinformation, but testing on actual
5139 * hardware has shown that we just need to write the desired
5140 * CCK divider into the Punit register.
5141 */
5142 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5143
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005144 mutex_lock(&dev_priv->rps.hw_lock);
5145 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5146 val &= ~DSPFREQGUAR_MASK_CHV;
5147 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5148 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5149 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5150 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5151 50)) {
5152 DRM_ERROR("timed out waiting for CDclk change\n");
5153 }
5154 mutex_unlock(&dev_priv->rps.hw_lock);
5155
5156 vlv_update_cdclk(dev);
5157}
5158
Jesse Barnes30a970c2013-11-04 13:48:12 -08005159static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5160 int max_pixclk)
5161{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005162 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005163 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005164
Jesse Barnes30a970c2013-11-04 13:48:12 -08005165 /*
5166 * Really only a few cases to deal with, as only 4 CDclks are supported:
5167 * 200MHz
5168 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005169 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005170 * 400MHz (VLV only)
5171 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5172 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005173 *
5174 * We seem to get an unstable or solid color picture at 200MHz.
5175 * Not sure what's wrong. For now use 200MHz only when all pipes
5176 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005177 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005178 if (!IS_CHERRYVIEW(dev_priv) &&
5179 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005180 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005181 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005182 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005183 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005184 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005185 else
5186 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005187}
5188
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005189/* compute the max pixel clock for new configuration */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005190static int intel_mode_max_pixclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005191{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005192 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005193 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005194 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005195 int max_pixclk = 0;
5196
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005197 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005198 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5199 if (IS_ERR(crtc_state))
5200 return PTR_ERR(crtc_state);
5201
5202 if (!crtc_state->base.enable)
5203 continue;
5204
5205 max_pixclk = max(max_pixclk,
5206 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005207 }
5208
5209 return max_pixclk;
5210}
5211
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005212static int valleyview_modeset_global_pipes(struct drm_atomic_state *state,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005213 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005214{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005215 struct drm_i915_private *dev_priv = to_i915(state->dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005216 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005217 int max_pixclk = intel_mode_max_pixclk(state);
5218
5219 if (max_pixclk < 0)
5220 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005221
Imre Deakd60c4472014-03-27 17:45:10 +02005222 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
5223 dev_priv->vlv_cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005224 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005225
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005226 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005227 for_each_intel_crtc(state->dev, intel_crtc)
Matt Roper83d65732015-02-25 13:12:16 -08005228 if (intel_crtc->base.state->enable)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005229 *prepare_pipes |= (1 << intel_crtc->pipe);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005230
5231 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005232}
5233
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005234static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5235{
5236 unsigned int credits, default_credits;
5237
5238 if (IS_CHERRYVIEW(dev_priv))
5239 default_credits = PFI_CREDIT(12);
5240 else
5241 default_credits = PFI_CREDIT(8);
5242
5243 if (DIV_ROUND_CLOSEST(dev_priv->vlv_cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
5244 /* CHV suggested value is 31 or 63 */
5245 if (IS_CHERRYVIEW(dev_priv))
5246 credits = PFI_CREDIT_31;
5247 else
5248 credits = PFI_CREDIT(15);
5249 } else {
5250 credits = default_credits;
5251 }
5252
5253 /*
5254 * WA - write default credits before re-programming
5255 * FIXME: should we also set the resend bit here?
5256 */
5257 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5258 default_credits);
5259
5260 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5261 credits | PFI_CREDIT_RESEND);
5262
5263 /*
5264 * FIXME is this guaranteed to clear
5265 * immediately or should we poll for it?
5266 */
5267 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5268}
5269
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005270static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005271{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005272 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005273 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005274 int max_pixclk = intel_mode_max_pixclk(state);
5275 int req_cdclk;
5276
5277 /* The only reason this can fail is if we fail to add the crtc_state
5278 * to the atomic state. But that can't happen since the call to
5279 * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
5280 * can't have failed otherwise the mode set would be aborted) added all
5281 * the states already. */
5282 if (WARN_ON(max_pixclk < 0))
5283 return;
5284
5285 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005286
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005287 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005288 /*
5289 * FIXME: We can end up here with all power domains off, yet
5290 * with a CDCLK frequency other than the minimum. To account
5291 * for this take the PIPE-A power domain, which covers the HW
5292 * blocks needed for the following programming. This can be
5293 * removed once it's guaranteed that we get here either with
5294 * the minimum CDCLK set, or the required power domains
5295 * enabled.
5296 */
5297 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5298
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005299 if (IS_CHERRYVIEW(dev))
5300 cherryview_set_cdclk(dev, req_cdclk);
5301 else
5302 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005303
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005304 vlv_program_pfi_credits(dev_priv);
5305
Imre Deak738c05c2014-11-19 16:25:37 +02005306 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005307 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005308}
5309
Jesse Barnes89b667f2013-04-18 14:51:36 -07005310static void valleyview_crtc_enable(struct drm_crtc *crtc)
5311{
5312 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005313 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005314 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5315 struct intel_encoder *encoder;
5316 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005317 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005318
Matt Roper83d65732015-02-25 13:12:16 -08005319 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005320
5321 if (intel_crtc->active)
5322 return;
5323
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005324 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305325
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005326 if (!is_dsi) {
5327 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005328 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005329 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005330 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005331 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005332
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005333 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305334 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005335
5336 intel_set_pipe_timings(intel_crtc);
5337
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005338 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5339 struct drm_i915_private *dev_priv = dev->dev_private;
5340
5341 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5342 I915_WRITE(CHV_CANVAS(pipe), 0);
5343 }
5344
Daniel Vetter5b18e572014-04-24 23:55:06 +02005345 i9xx_set_pipeconf(intel_crtc);
5346
Jesse Barnes89b667f2013-04-18 14:51:36 -07005347 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005348
Daniel Vettera72e4c92014-09-30 10:56:47 +02005349 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005350
Jesse Barnes89b667f2013-04-18 14:51:36 -07005351 for_each_encoder_on_crtc(dev, crtc, encoder)
5352 if (encoder->pre_pll_enable)
5353 encoder->pre_pll_enable(encoder);
5354
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005355 if (!is_dsi) {
5356 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005357 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005358 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005359 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005360 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005361
5362 for_each_encoder_on_crtc(dev, crtc, encoder)
5363 if (encoder->pre_enable)
5364 encoder->pre_enable(encoder);
5365
Jesse Barnes2dd24552013-04-25 12:55:01 -07005366 i9xx_pfit_enable(intel_crtc);
5367
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005368 intel_crtc_load_lut(crtc);
5369
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005370 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005371 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005372
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005373 assert_vblank_disabled(crtc);
5374 drm_crtc_vblank_on(crtc);
5375
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005376 for_each_encoder_on_crtc(dev, crtc, encoder)
5377 encoder->enable(encoder);
5378
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005379 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005380
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005381 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005382 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005383}
5384
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005385static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5386{
5387 struct drm_device *dev = crtc->base.dev;
5388 struct drm_i915_private *dev_priv = dev->dev_private;
5389
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005390 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5391 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005392}
5393
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005394static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005395{
5396 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005397 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005398 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005399 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005400 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005401
Matt Roper83d65732015-02-25 13:12:16 -08005402 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02005403
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005404 if (intel_crtc->active)
5405 return;
5406
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005407 i9xx_set_pll_dividers(intel_crtc);
5408
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005409 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305410 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005411
5412 intel_set_pipe_timings(intel_crtc);
5413
Daniel Vetter5b18e572014-04-24 23:55:06 +02005414 i9xx_set_pipeconf(intel_crtc);
5415
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005416 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005417
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005418 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005419 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005420
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005421 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005422 if (encoder->pre_enable)
5423 encoder->pre_enable(encoder);
5424
Daniel Vetterf6736a12013-06-05 13:34:30 +02005425 i9xx_enable_pll(intel_crtc);
5426
Jesse Barnes2dd24552013-04-25 12:55:01 -07005427 i9xx_pfit_enable(intel_crtc);
5428
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005429 intel_crtc_load_lut(crtc);
5430
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005431 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005432 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005433
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005434 assert_vblank_disabled(crtc);
5435 drm_crtc_vblank_on(crtc);
5436
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005437 for_each_encoder_on_crtc(dev, crtc, encoder)
5438 encoder->enable(encoder);
5439
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005440 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005441
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005442 /*
5443 * Gen2 reports pipe underruns whenever all planes are disabled.
5444 * So don't enable underrun reporting before at least some planes
5445 * are enabled.
5446 * FIXME: Need to fix the logic to work when we turn off all planes
5447 * but leave the pipe running.
5448 */
5449 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005450 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005451
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005452 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005453 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005454}
5455
Daniel Vetter87476d62013-04-11 16:29:06 +02005456static void i9xx_pfit_disable(struct intel_crtc *crtc)
5457{
5458 struct drm_device *dev = crtc->base.dev;
5459 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02005460
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005461 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005462 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02005463
5464 assert_pipe_disabled(dev_priv, crtc->pipe);
5465
Daniel Vetter328d8e82013-05-08 10:36:31 +02005466 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5467 I915_READ(PFIT_CONTROL));
5468 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02005469}
5470
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005471static void i9xx_crtc_disable(struct drm_crtc *crtc)
5472{
5473 struct drm_device *dev = crtc->dev;
5474 struct drm_i915_private *dev_priv = dev->dev_private;
5475 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005476 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005477 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005478
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005479 if (!intel_crtc->active)
5480 return;
5481
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005482 /*
5483 * Gen2 reports pipe underruns whenever all planes are disabled.
5484 * So diasble underrun reporting before all the planes get disabled.
5485 * FIXME: Need to fix the logic to work when we turn off all planes
5486 * but leave the pipe running.
5487 */
5488 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005489 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005490
Imre Deak564ed192014-06-13 14:54:21 +03005491 /*
5492 * Vblank time updates from the shadow to live plane control register
5493 * are blocked if the memory self-refresh mode is active at that
5494 * moment. So to make sure the plane gets truly disabled, disable
5495 * first the self-refresh mode. The self-refresh enable bit in turn
5496 * will be checked/applied by the HW only at the next frame start
5497 * event which is after the vblank start event, so we need to have a
5498 * wait-for-vblank between disabling the plane and the pipe.
5499 */
5500 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005501 intel_crtc_disable_planes(crtc);
5502
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005503 /*
5504 * On gen2 planes are double buffered but the pipe isn't, so we must
5505 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03005506 * We also need to wait on all gmch platforms because of the
5507 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005508 */
Imre Deak564ed192014-06-13 14:54:21 +03005509 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005510
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005511 for_each_encoder_on_crtc(dev, crtc, encoder)
5512 encoder->disable(encoder);
5513
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005514 drm_crtc_vblank_off(crtc);
5515 assert_vblank_disabled(crtc);
5516
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005517 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005518
Daniel Vetter87476d62013-04-11 16:29:06 +02005519 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005520
Jesse Barnes89b667f2013-04-18 14:51:36 -07005521 for_each_encoder_on_crtc(dev, crtc, encoder)
5522 if (encoder->post_disable)
5523 encoder->post_disable(encoder);
5524
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005525 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005526 if (IS_CHERRYVIEW(dev))
5527 chv_disable_pll(dev_priv, pipe);
5528 else if (IS_VALLEYVIEW(dev))
5529 vlv_disable_pll(dev_priv, pipe);
5530 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03005531 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005532 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005533
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005534 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005535 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005536
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005537 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005538 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005539
Daniel Vetterefa96242014-04-24 23:55:02 +02005540 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005541 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02005542 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005543}
5544
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005545static void i9xx_crtc_off(struct drm_crtc *crtc)
5546{
5547}
5548
Borun Fub04c5bd2014-07-12 10:02:27 +05305549/* Master function to enable/disable CRTC and corresponding power wells */
5550void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01005551{
Chris Wilsoncdd59982010-09-08 16:30:16 +01005552 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005553 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005554 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005555 enum intel_display_power_domain domain;
5556 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005557
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005558 if (enable) {
5559 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005560 domains = get_crtc_power_domains(crtc);
5561 for_each_power_domain(domain, domains)
5562 intel_display_power_get(dev_priv, domain);
5563 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005564
5565 dev_priv->display.crtc_enable(crtc);
5566 }
5567 } else {
5568 if (intel_crtc->active) {
5569 dev_priv->display.crtc_disable(crtc);
5570
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005571 domains = intel_crtc->enabled_power_domains;
5572 for_each_power_domain(domain, domains)
5573 intel_display_power_put(dev_priv, domain);
5574 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005575 }
5576 }
Borun Fub04c5bd2014-07-12 10:02:27 +05305577}
5578
5579/**
5580 * Sets the power management mode of the pipe and plane.
5581 */
5582void intel_crtc_update_dpms(struct drm_crtc *crtc)
5583{
5584 struct drm_device *dev = crtc->dev;
5585 struct intel_encoder *intel_encoder;
5586 bool enable = false;
5587
5588 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5589 enable |= intel_encoder->connectors_active;
5590
5591 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005592}
5593
Daniel Vetter976f8a22012-07-08 22:34:21 +02005594static void intel_crtc_disable(struct drm_crtc *crtc)
5595{
5596 struct drm_device *dev = crtc->dev;
5597 struct drm_connector *connector;
5598 struct drm_i915_private *dev_priv = dev->dev_private;
5599
5600 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08005601 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005602
5603 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005604 dev_priv->display.off(crtc);
5605
Matt Roper70a101f2015-04-08 18:56:53 -07005606 drm_plane_helper_disable(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005607
5608 /* Update computed state. */
5609 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5610 if (!connector->encoder || !connector->encoder->crtc)
5611 continue;
5612
5613 if (connector->encoder->crtc != crtc)
5614 continue;
5615
5616 connector->dpms = DRM_MODE_DPMS_OFF;
5617 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01005618 }
5619}
5620
Chris Wilsonea5b2132010-08-04 13:50:23 +01005621void intel_encoder_destroy(struct drm_encoder *encoder)
5622{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005623 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01005624
Chris Wilsonea5b2132010-08-04 13:50:23 +01005625 drm_encoder_cleanup(encoder);
5626 kfree(intel_encoder);
5627}
5628
Damien Lespiau92373292013-08-08 22:28:57 +01005629/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005630 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5631 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01005632static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005633{
5634 if (mode == DRM_MODE_DPMS_ON) {
5635 encoder->connectors_active = true;
5636
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005637 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005638 } else {
5639 encoder->connectors_active = false;
5640
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005641 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005642 }
5643}
5644
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005645/* Cross check the actual hw state with our own modeset state tracking (and it's
5646 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02005647static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005648{
5649 if (connector->get_hw_state(connector)) {
5650 struct intel_encoder *encoder = connector->encoder;
5651 struct drm_crtc *crtc;
5652 bool encoder_enabled;
5653 enum pipe pipe;
5654
5655 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5656 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03005657 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005658
Dave Airlie0e32b392014-05-02 14:02:48 +10005659 /* there is no real hw state for MST connectors */
5660 if (connector->mst_port)
5661 return;
5662
Rob Clarke2c719b2014-12-15 13:56:32 -05005663 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005664 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05005665 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005666 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005667
Dave Airlie36cd7442014-05-02 13:44:18 +10005668 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05005669 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10005670 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005671
Dave Airlie36cd7442014-05-02 13:44:18 +10005672 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05005673 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
5674 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10005675 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005676
Dave Airlie36cd7442014-05-02 13:44:18 +10005677 crtc = encoder->base.crtc;
5678
Matt Roper83d65732015-02-25 13:12:16 -08005679 I915_STATE_WARN(!crtc->state->enable,
5680 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05005681 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5682 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10005683 "encoder active on the wrong pipe\n");
5684 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005685 }
5686}
5687
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005688/* Even simpler default implementation, if there's really no special case to
5689 * consider. */
5690void intel_connector_dpms(struct drm_connector *connector, int mode)
5691{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005692 /* All the simple cases only support two dpms states. */
5693 if (mode != DRM_MODE_DPMS_ON)
5694 mode = DRM_MODE_DPMS_OFF;
5695
5696 if (mode == connector->dpms)
5697 return;
5698
5699 connector->dpms = mode;
5700
5701 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01005702 if (connector->encoder)
5703 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005704
Daniel Vetterb9805142012-08-31 17:37:33 +02005705 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005706}
5707
Daniel Vetterf0947c32012-07-02 13:10:34 +02005708/* Simple connector->get_hw_state implementation for encoders that support only
5709 * one connector and no cloning and hence the encoder state determines the state
5710 * of the connector. */
5711bool intel_connector_get_hw_state(struct intel_connector *connector)
5712{
Daniel Vetter24929352012-07-02 20:28:59 +02005713 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02005714 struct intel_encoder *encoder = connector->encoder;
5715
5716 return encoder->get_hw_state(encoder, &pipe);
5717}
5718
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005719static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02005720{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005721 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
5722 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02005723
5724 return 0;
5725}
5726
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005727static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005728 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005729{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005730 struct drm_atomic_state *state = pipe_config->base.state;
5731 struct intel_crtc *other_crtc;
5732 struct intel_crtc_state *other_crtc_state;
5733
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005734 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5735 pipe_name(pipe), pipe_config->fdi_lanes);
5736 if (pipe_config->fdi_lanes > 4) {
5737 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5738 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005739 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005740 }
5741
Paulo Zanonibafb6552013-11-02 21:07:44 -07005742 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005743 if (pipe_config->fdi_lanes > 2) {
5744 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5745 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005746 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005747 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005748 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005749 }
5750 }
5751
5752 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005753 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005754
5755 /* Ivybridge 3 pipe is really complicated */
5756 switch (pipe) {
5757 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005758 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005759 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005760 if (pipe_config->fdi_lanes <= 2)
5761 return 0;
5762
5763 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
5764 other_crtc_state =
5765 intel_atomic_get_crtc_state(state, other_crtc);
5766 if (IS_ERR(other_crtc_state))
5767 return PTR_ERR(other_crtc_state);
5768
5769 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005770 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5771 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005772 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005773 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005774 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005775 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02005776 if (pipe_config->fdi_lanes > 2) {
5777 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
5778 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005779 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02005780 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005781
5782 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
5783 other_crtc_state =
5784 intel_atomic_get_crtc_state(state, other_crtc);
5785 if (IS_ERR(other_crtc_state))
5786 return PTR_ERR(other_crtc_state);
5787
5788 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005789 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005790 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005791 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005792 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005793 default:
5794 BUG();
5795 }
5796}
5797
Daniel Vettere29c22c2013-02-21 00:00:16 +01005798#define RETRY 1
5799static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005800 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02005801{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005802 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02005803 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005804 int lane, link_bw, fdi_dotclock, ret;
5805 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005806
Daniel Vettere29c22c2013-02-21 00:00:16 +01005807retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02005808 /* FDI is a binary signal running at ~2.7GHz, encoding
5809 * each output octet as 10 bits. The actual frequency
5810 * is stored as a divider into a 100MHz clock, and the
5811 * mode pixel clock is stored in units of 1KHz.
5812 * Hence the bw of each lane in terms of the mode signal
5813 * is:
5814 */
5815 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5816
Damien Lespiau241bfc32013-09-25 16:45:37 +01005817 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005818
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005819 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005820 pipe_config->pipe_bpp);
5821
5822 pipe_config->fdi_lanes = lane;
5823
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005824 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005825 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005826
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005827 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5828 intel_crtc->pipe, pipe_config);
5829 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01005830 pipe_config->pipe_bpp -= 2*3;
5831 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5832 pipe_config->pipe_bpp);
5833 needs_recompute = true;
5834 pipe_config->bw_constrained = true;
5835
5836 goto retry;
5837 }
5838
5839 if (needs_recompute)
5840 return RETRY;
5841
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005842 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005843}
5844
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005845static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005846 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005847{
Jani Nikulad330a952014-01-21 11:24:25 +02005848 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03005849 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07005850 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005851}
5852
Daniel Vettera43f6e02013-06-07 23:10:32 +02005853static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005854 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08005855{
Daniel Vettera43f6e02013-06-07 23:10:32 +02005856 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02005857 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02005858 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01005859
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005860 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005861 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005862 int clock_limit =
5863 dev_priv->display.get_display_clock_speed(dev);
5864
5865 /*
5866 * Enable pixel doubling when the dot clock
5867 * is > 90% of the (display) core speed.
5868 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03005869 * GDG double wide on either pipe,
5870 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005871 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03005872 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01005873 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005874 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005875 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005876 }
5877
Damien Lespiau241bfc32013-09-25 16:45:37 +01005878 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005879 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005880 }
Chris Wilson89749352010-09-12 18:25:19 +01005881
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005882 /*
5883 * Pipe horizontal size must be even in:
5884 * - DVO ganged mode
5885 * - LVDS dual channel mode
5886 * - Double wide pipe
5887 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02005888 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005889 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5890 pipe_config->pipe_src_w &= ~1;
5891
Damien Lespiau8693a822013-05-03 18:48:11 +01005892 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5893 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03005894 */
5895 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5896 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005897 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03005898
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005899 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005900 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005901 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005902 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5903 * for lvds. */
5904 pipe_config->pipe_bpp = 8*3;
5905 }
5906
Damien Lespiauf5adf942013-06-24 18:29:34 +01005907 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005908 hsw_compute_ips_config(crtc, pipe_config);
5909
Daniel Vetter877d48d2013-04-19 11:24:43 +02005910 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02005911 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02005912
Daniel Vettere29c22c2013-02-21 00:00:16 +01005913 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005914}
5915
Ville Syrjälä1652d192015-03-31 14:12:01 +03005916static int skylake_get_display_clock_speed(struct drm_device *dev)
5917{
5918 struct drm_i915_private *dev_priv = to_i915(dev);
5919 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5920 uint32_t cdctl = I915_READ(CDCLK_CTL);
5921 uint32_t linkrate;
5922
5923 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
5924 WARN(1, "LCPLL1 not enabled\n");
5925 return 24000; /* 24MHz is the cd freq with NSSC ref */
5926 }
5927
5928 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
5929 return 540000;
5930
5931 linkrate = (I915_READ(DPLL_CTRL1) &
5932 DPLL_CRTL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
5933
5934 if (linkrate == DPLL_CRTL1_LINK_RATE_2160 ||
5935 linkrate == DPLL_CRTL1_LINK_RATE_1080) {
5936 /* vco 8640 */
5937 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
5938 case CDCLK_FREQ_450_432:
5939 return 432000;
5940 case CDCLK_FREQ_337_308:
5941 return 308570;
5942 case CDCLK_FREQ_675_617:
5943 return 617140;
5944 default:
5945 WARN(1, "Unknown cd freq selection\n");
5946 }
5947 } else {
5948 /* vco 8100 */
5949 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
5950 case CDCLK_FREQ_450_432:
5951 return 450000;
5952 case CDCLK_FREQ_337_308:
5953 return 337500;
5954 case CDCLK_FREQ_675_617:
5955 return 675000;
5956 default:
5957 WARN(1, "Unknown cd freq selection\n");
5958 }
5959 }
5960
5961 /* error case, do as if DPLL0 isn't enabled */
5962 return 24000;
5963}
5964
5965static int broadwell_get_display_clock_speed(struct drm_device *dev)
5966{
5967 struct drm_i915_private *dev_priv = dev->dev_private;
5968 uint32_t lcpll = I915_READ(LCPLL_CTL);
5969 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
5970
5971 if (lcpll & LCPLL_CD_SOURCE_FCLK)
5972 return 800000;
5973 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5974 return 450000;
5975 else if (freq == LCPLL_CLK_FREQ_450)
5976 return 450000;
5977 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
5978 return 540000;
5979 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
5980 return 337500;
5981 else
5982 return 675000;
5983}
5984
5985static int haswell_get_display_clock_speed(struct drm_device *dev)
5986{
5987 struct drm_i915_private *dev_priv = dev->dev_private;
5988 uint32_t lcpll = I915_READ(LCPLL_CTL);
5989 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
5990
5991 if (lcpll & LCPLL_CD_SOURCE_FCLK)
5992 return 800000;
5993 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5994 return 450000;
5995 else if (freq == LCPLL_CLK_FREQ_450)
5996 return 450000;
5997 else if (IS_HSW_ULT(dev))
5998 return 337500;
5999 else
6000 return 540000;
6001}
6002
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006003static int valleyview_get_display_clock_speed(struct drm_device *dev)
6004{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006005 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006006 u32 val;
6007 int divider;
6008
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006009 if (dev_priv->hpll_freq == 0)
6010 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6011
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006012 mutex_lock(&dev_priv->dpio_lock);
6013 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6014 mutex_unlock(&dev_priv->dpio_lock);
6015
6016 divider = val & DISPLAY_FREQUENCY_VALUES;
6017
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006018 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6019 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6020 "cdclk change in progress\n");
6021
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006022 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006023}
6024
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006025static int ilk_get_display_clock_speed(struct drm_device *dev)
6026{
6027 return 450000;
6028}
6029
Jesse Barnese70236a2009-09-21 10:42:27 -07006030static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006031{
Jesse Barnese70236a2009-09-21 10:42:27 -07006032 return 400000;
6033}
Jesse Barnes79e53942008-11-07 14:24:08 -08006034
Jesse Barnese70236a2009-09-21 10:42:27 -07006035static int i915_get_display_clock_speed(struct drm_device *dev)
6036{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006037 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006038}
Jesse Barnes79e53942008-11-07 14:24:08 -08006039
Jesse Barnese70236a2009-09-21 10:42:27 -07006040static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6041{
6042 return 200000;
6043}
Jesse Barnes79e53942008-11-07 14:24:08 -08006044
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006045static int pnv_get_display_clock_speed(struct drm_device *dev)
6046{
6047 u16 gcfgc = 0;
6048
6049 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6050
6051 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6052 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006053 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006054 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006055 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006056 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006057 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006058 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6059 return 200000;
6060 default:
6061 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6062 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006063 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006064 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006065 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006066 }
6067}
6068
Jesse Barnese70236a2009-09-21 10:42:27 -07006069static int i915gm_get_display_clock_speed(struct drm_device *dev)
6070{
6071 u16 gcfgc = 0;
6072
6073 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6074
6075 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006076 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006077 else {
6078 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6079 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006080 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006081 default:
6082 case GC_DISPLAY_CLOCK_190_200_MHZ:
6083 return 190000;
6084 }
6085 }
6086}
Jesse Barnes79e53942008-11-07 14:24:08 -08006087
Jesse Barnese70236a2009-09-21 10:42:27 -07006088static int i865_get_display_clock_speed(struct drm_device *dev)
6089{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006090 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006091}
6092
6093static int i855_get_display_clock_speed(struct drm_device *dev)
6094{
6095 u16 hpllcc = 0;
6096 /* Assume that the hardware is in the high speed state. This
6097 * should be the default.
6098 */
6099 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6100 case GC_CLOCK_133_200:
6101 case GC_CLOCK_100_200:
6102 return 200000;
6103 case GC_CLOCK_166_250:
6104 return 250000;
6105 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006106 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006107 }
6108
6109 /* Shouldn't happen */
6110 return 0;
6111}
6112
6113static int i830_get_display_clock_speed(struct drm_device *dev)
6114{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006115 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006116}
6117
Zhenyu Wang2c072452009-06-05 15:38:42 +08006118static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006119intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006120{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006121 while (*num > DATA_LINK_M_N_MASK ||
6122 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006123 *num >>= 1;
6124 *den >>= 1;
6125 }
6126}
6127
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006128static void compute_m_n(unsigned int m, unsigned int n,
6129 uint32_t *ret_m, uint32_t *ret_n)
6130{
6131 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6132 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6133 intel_reduce_m_n_ratio(ret_m, ret_n);
6134}
6135
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006136void
6137intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6138 int pixel_clock, int link_clock,
6139 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006140{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006141 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006142
6143 compute_m_n(bits_per_pixel * pixel_clock,
6144 link_clock * nlanes * 8,
6145 &m_n->gmch_m, &m_n->gmch_n);
6146
6147 compute_m_n(pixel_clock, link_clock,
6148 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006149}
6150
Chris Wilsona7615032011-01-12 17:04:08 +00006151static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6152{
Jani Nikulad330a952014-01-21 11:24:25 +02006153 if (i915.panel_use_ssc >= 0)
6154 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006155 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006156 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006157}
6158
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006159static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6160 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006161{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006162 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006163 struct drm_i915_private *dev_priv = dev->dev_private;
6164 int refclk;
6165
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006166 WARN_ON(!crtc_state->base.state);
6167
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006168 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006169 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006170 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006171 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006172 refclk = dev_priv->vbt.lvds_ssc_freq;
6173 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006174 } else if (!IS_GEN2(dev)) {
6175 refclk = 96000;
6176 } else {
6177 refclk = 48000;
6178 }
6179
6180 return refclk;
6181}
6182
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006183static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006184{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006185 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006186}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006187
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006188static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6189{
6190 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006191}
6192
Daniel Vetterf47709a2013-03-28 10:42:02 +01006193static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006194 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08006195 intel_clock_t *reduced_clock)
6196{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006197 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006198 u32 fp, fp2 = 0;
6199
6200 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006201 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006202 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006203 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006204 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006205 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006206 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006207 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006208 }
6209
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006210 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006211
Daniel Vetterf47709a2013-03-28 10:42:02 +01006212 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006213 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006214 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006215 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006216 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006217 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006218 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006219 }
6220}
6221
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006222static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6223 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006224{
6225 u32 reg_val;
6226
6227 /*
6228 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6229 * and set it to a reasonable value instead.
6230 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006231 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006232 reg_val &= 0xffffff00;
6233 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006234 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006235
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006236 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006237 reg_val &= 0x8cffffff;
6238 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006239 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006240
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006241 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006242 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006243 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006244
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006245 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006246 reg_val &= 0x00ffffff;
6247 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006248 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006249}
6250
Daniel Vetterb5518422013-05-03 11:49:48 +02006251static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6252 struct intel_link_m_n *m_n)
6253{
6254 struct drm_device *dev = crtc->base.dev;
6255 struct drm_i915_private *dev_priv = dev->dev_private;
6256 int pipe = crtc->pipe;
6257
Daniel Vettere3b95f12013-05-03 11:49:49 +02006258 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6259 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6260 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6261 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006262}
6263
6264static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006265 struct intel_link_m_n *m_n,
6266 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006267{
6268 struct drm_device *dev = crtc->base.dev;
6269 struct drm_i915_private *dev_priv = dev->dev_private;
6270 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006271 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006272
6273 if (INTEL_INFO(dev)->gen >= 5) {
6274 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6275 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6276 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6277 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006278 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6279 * for gen < 8) and if DRRS is supported (to make sure the
6280 * registers are not unnecessarily accessed).
6281 */
Durgadoss R44395bf2015-02-13 15:33:02 +05306282 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006283 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006284 I915_WRITE(PIPE_DATA_M2(transcoder),
6285 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6286 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6287 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6288 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6289 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006290 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006291 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6292 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6293 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6294 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006295 }
6296}
6297
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306298void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006299{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306300 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6301
6302 if (m_n == M1_N1) {
6303 dp_m_n = &crtc->config->dp_m_n;
6304 dp_m2_n2 = &crtc->config->dp_m2_n2;
6305 } else if (m_n == M2_N2) {
6306
6307 /*
6308 * M2_N2 registers are not supported. Hence m2_n2 divider value
6309 * needs to be programmed into M1_N1.
6310 */
6311 dp_m_n = &crtc->config->dp_m2_n2;
6312 } else {
6313 DRM_ERROR("Unsupported divider value\n");
6314 return;
6315 }
6316
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006317 if (crtc->config->has_pch_encoder)
6318 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006319 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306320 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006321}
6322
Ville Syrjäläd288f652014-10-28 13:20:22 +02006323static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006324 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006325{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006326 u32 dpll, dpll_md;
6327
6328 /*
6329 * Enable DPIO clock input. We should never disable the reference
6330 * clock for pipe B, since VGA hotplug / manual detection depends
6331 * on it.
6332 */
6333 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6334 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6335 /* We should never disable this, set it here for state tracking */
6336 if (crtc->pipe == PIPE_B)
6337 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6338 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006339 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006340
Ville Syrjäläd288f652014-10-28 13:20:22 +02006341 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006342 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006343 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006344}
6345
Ville Syrjäläd288f652014-10-28 13:20:22 +02006346static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006347 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006348{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006349 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006350 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006351 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006352 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006353 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006354 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006355
Daniel Vetter09153002012-12-12 14:06:44 +01006356 mutex_lock(&dev_priv->dpio_lock);
6357
Ville Syrjäläd288f652014-10-28 13:20:22 +02006358 bestn = pipe_config->dpll.n;
6359 bestm1 = pipe_config->dpll.m1;
6360 bestm2 = pipe_config->dpll.m2;
6361 bestp1 = pipe_config->dpll.p1;
6362 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006363
Jesse Barnes89b667f2013-04-18 14:51:36 -07006364 /* See eDP HDMI DPIO driver vbios notes doc */
6365
6366 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006367 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006368 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006369
6370 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006371 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006372
6373 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006374 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006375 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006376 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006377
6378 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006379 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006380
6381 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006382 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6383 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6384 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006385 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006386
6387 /*
6388 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6389 * but we don't support that).
6390 * Note: don't use the DAC post divider as it seems unstable.
6391 */
6392 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006393 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006394
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006395 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006396 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006397
Jesse Barnes89b667f2013-04-18 14:51:36 -07006398 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006399 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006400 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6401 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006402 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03006403 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006404 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006405 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006406 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006407
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02006408 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006409 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006410 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006411 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006412 0x0df40000);
6413 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006414 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006415 0x0df70000);
6416 } else { /* HDMI or VGA */
6417 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006418 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006419 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006420 0x0df70000);
6421 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006422 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006423 0x0df40000);
6424 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006425
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006426 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006427 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006428 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6429 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006430 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006431 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006432
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006433 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01006434 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006435}
6436
Ville Syrjäläd288f652014-10-28 13:20:22 +02006437static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006438 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006439{
Ville Syrjäläd288f652014-10-28 13:20:22 +02006440 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006441 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6442 DPLL_VCO_ENABLE;
6443 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006444 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006445
Ville Syrjäläd288f652014-10-28 13:20:22 +02006446 pipe_config->dpll_hw_state.dpll_md =
6447 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006448}
6449
Ville Syrjäläd288f652014-10-28 13:20:22 +02006450static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006451 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006452{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006453 struct drm_device *dev = crtc->base.dev;
6454 struct drm_i915_private *dev_priv = dev->dev_private;
6455 int pipe = crtc->pipe;
6456 int dpll_reg = DPLL(crtc->pipe);
6457 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306458 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006459 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306460 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306461 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006462
Ville Syrjäläd288f652014-10-28 13:20:22 +02006463 bestn = pipe_config->dpll.n;
6464 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6465 bestm1 = pipe_config->dpll.m1;
6466 bestm2 = pipe_config->dpll.m2 >> 22;
6467 bestp1 = pipe_config->dpll.p1;
6468 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306469 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306470 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306471 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006472
6473 /*
6474 * Enable Refclk and SSC
6475 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006476 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02006477 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006478
6479 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006480
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006481 /* p1 and p2 divider */
6482 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6483 5 << DPIO_CHV_S1_DIV_SHIFT |
6484 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6485 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6486 1 << DPIO_CHV_K_DIV_SHIFT);
6487
6488 /* Feedback post-divider - m2 */
6489 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6490
6491 /* Feedback refclk divider - n and m1 */
6492 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6493 DPIO_CHV_M1_DIV_BY_2 |
6494 1 << DPIO_CHV_N_DIV_SHIFT);
6495
6496 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306497 if (bestm2_frac)
6498 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006499
6500 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306501 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6502 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6503 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6504 if (bestm2_frac)
6505 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6506 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006507
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05306508 /* Program digital lock detect threshold */
6509 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6510 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6511 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6512 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6513 if (!bestm2_frac)
6514 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6515 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6516
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006517 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306518 if (vco == 5400000) {
6519 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6520 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6521 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6522 tribuf_calcntr = 0x9;
6523 } else if (vco <= 6200000) {
6524 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6525 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6526 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6527 tribuf_calcntr = 0x9;
6528 } else if (vco <= 6480000) {
6529 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6530 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6531 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6532 tribuf_calcntr = 0x8;
6533 } else {
6534 /* Not supported. Apply the same limits as in the max case */
6535 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6536 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6537 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6538 tribuf_calcntr = 0;
6539 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006540 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6541
Ville Syrjälä968040b2015-03-11 22:52:08 +02006542 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306543 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6544 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6545 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6546
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006547 /* AFC Recal */
6548 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6549 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6550 DPIO_AFC_RECAL);
6551
6552 mutex_unlock(&dev_priv->dpio_lock);
6553}
6554
Ville Syrjäläd288f652014-10-28 13:20:22 +02006555/**
6556 * vlv_force_pll_on - forcibly enable just the PLL
6557 * @dev_priv: i915 private structure
6558 * @pipe: pipe PLL to enable
6559 * @dpll: PLL configuration
6560 *
6561 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6562 * in cases where we need the PLL enabled even when @pipe is not going to
6563 * be enabled.
6564 */
6565void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
6566 const struct dpll *dpll)
6567{
6568 struct intel_crtc *crtc =
6569 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006570 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006571 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02006572 .pixel_multiplier = 1,
6573 .dpll = *dpll,
6574 };
6575
6576 if (IS_CHERRYVIEW(dev)) {
6577 chv_update_pll(crtc, &pipe_config);
6578 chv_prepare_pll(crtc, &pipe_config);
6579 chv_enable_pll(crtc, &pipe_config);
6580 } else {
6581 vlv_update_pll(crtc, &pipe_config);
6582 vlv_prepare_pll(crtc, &pipe_config);
6583 vlv_enable_pll(crtc, &pipe_config);
6584 }
6585}
6586
6587/**
6588 * vlv_force_pll_off - forcibly disable just the PLL
6589 * @dev_priv: i915 private structure
6590 * @pipe: pipe PLL to disable
6591 *
6592 * Disable the PLL for @pipe. To be used in cases where we need
6593 * the PLL enabled even when @pipe is not going to be enabled.
6594 */
6595void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
6596{
6597 if (IS_CHERRYVIEW(dev))
6598 chv_disable_pll(to_i915(dev), pipe);
6599 else
6600 vlv_disable_pll(to_i915(dev), pipe);
6601}
6602
Daniel Vetterf47709a2013-03-28 10:42:02 +01006603static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006604 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01006605 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006606 int num_connectors)
6607{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006608 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006609 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006610 u32 dpll;
6611 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006612 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006613
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006614 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306615
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006616 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
6617 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006618
6619 dpll = DPLL_VGA_MODE_DIS;
6620
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006621 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006622 dpll |= DPLLB_MODE_LVDS;
6623 else
6624 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006625
Daniel Vetteref1b4602013-06-01 17:17:04 +02006626 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006627 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02006628 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006629 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02006630
6631 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006632 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006633
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006634 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006635 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006636
6637 /* compute bitmask from p1 value */
6638 if (IS_PINEVIEW(dev))
6639 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6640 else {
6641 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6642 if (IS_G4X(dev) && reduced_clock)
6643 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6644 }
6645 switch (clock->p2) {
6646 case 5:
6647 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6648 break;
6649 case 7:
6650 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6651 break;
6652 case 10:
6653 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6654 break;
6655 case 14:
6656 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6657 break;
6658 }
6659 if (INTEL_INFO(dev)->gen >= 4)
6660 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6661
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006662 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006663 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006664 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006665 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6666 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6667 else
6668 dpll |= PLL_REF_INPUT_DREFCLK;
6669
6670 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006671 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006672
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006673 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006674 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02006675 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006676 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006677 }
6678}
6679
Daniel Vetterf47709a2013-03-28 10:42:02 +01006680static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006681 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01006682 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006683 int num_connectors)
6684{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006685 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006686 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006687 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006688 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006689
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006690 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306691
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006692 dpll = DPLL_VGA_MODE_DIS;
6693
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006694 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006695 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6696 } else {
6697 if (clock->p1 == 2)
6698 dpll |= PLL_P1_DIVIDE_BY_TWO;
6699 else
6700 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6701 if (clock->p2 == 4)
6702 dpll |= PLL_P2_DIVIDE_BY_4;
6703 }
6704
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006705 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02006706 dpll |= DPLL_DVO_2X_MODE;
6707
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006708 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006709 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6710 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6711 else
6712 dpll |= PLL_REF_INPUT_DREFCLK;
6713
6714 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006715 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006716}
6717
Daniel Vetter8a654f32013-06-01 17:16:22 +02006718static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006719{
6720 struct drm_device *dev = intel_crtc->base.dev;
6721 struct drm_i915_private *dev_priv = dev->dev_private;
6722 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006723 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02006724 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006725 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006726 uint32_t crtc_vtotal, crtc_vblank_end;
6727 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006728
6729 /* We need to be careful not to changed the adjusted mode, for otherwise
6730 * the hw state checker will get angry at the mismatch. */
6731 crtc_vtotal = adjusted_mode->crtc_vtotal;
6732 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006733
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006734 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006735 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006736 crtc_vtotal -= 1;
6737 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006738
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006739 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006740 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6741 else
6742 vsyncshift = adjusted_mode->crtc_hsync_start -
6743 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006744 if (vsyncshift < 0)
6745 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006746 }
6747
6748 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006749 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006750
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006751 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006752 (adjusted_mode->crtc_hdisplay - 1) |
6753 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006754 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006755 (adjusted_mode->crtc_hblank_start - 1) |
6756 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006757 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006758 (adjusted_mode->crtc_hsync_start - 1) |
6759 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6760
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006761 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006762 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006763 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006764 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006765 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006766 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006767 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006768 (adjusted_mode->crtc_vsync_start - 1) |
6769 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6770
Paulo Zanonib5e508d2012-10-24 11:34:43 -02006771 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6772 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6773 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6774 * bits. */
6775 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6776 (pipe == PIPE_B || pipe == PIPE_C))
6777 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6778
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006779 /* pipesrc controls the size that is scaled from, which should
6780 * always be the user's requested size.
6781 */
6782 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006783 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6784 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006785}
6786
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006787static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006788 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006789{
6790 struct drm_device *dev = crtc->base.dev;
6791 struct drm_i915_private *dev_priv = dev->dev_private;
6792 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6793 uint32_t tmp;
6794
6795 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006796 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6797 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006798 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006799 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6800 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006801 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006802 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6803 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006804
6805 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006806 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6807 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006808 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006809 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6810 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006811 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006812 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6813 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006814
6815 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006816 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6817 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
6818 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006819 }
6820
6821 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03006822 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6823 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6824
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006825 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
6826 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006827}
6828
Daniel Vetterf6a83282014-02-11 15:28:57 -08006829void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006830 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03006831{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006832 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
6833 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
6834 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
6835 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006836
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006837 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
6838 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
6839 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
6840 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006841
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006842 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006843
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006844 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
6845 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006846}
6847
Daniel Vetter84b046f2013-02-19 18:48:54 +01006848static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6849{
6850 struct drm_device *dev = intel_crtc->base.dev;
6851 struct drm_i915_private *dev_priv = dev->dev_private;
6852 uint32_t pipeconf;
6853
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006854 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006855
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03006856 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6857 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6858 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02006859
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006860 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006861 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006862
Daniel Vetterff9ce462013-04-24 14:57:17 +02006863 /* only g4x and later have fancy bpc/dither controls */
6864 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02006865 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006866 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02006867 pipeconf |= PIPECONF_DITHER_EN |
6868 PIPECONF_DITHER_TYPE_SP;
6869
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006870 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02006871 case 18:
6872 pipeconf |= PIPECONF_6BPC;
6873 break;
6874 case 24:
6875 pipeconf |= PIPECONF_8BPC;
6876 break;
6877 case 30:
6878 pipeconf |= PIPECONF_10BPC;
6879 break;
6880 default:
6881 /* Case prevented by intel_choose_pipe_bpp_dither. */
6882 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01006883 }
6884 }
6885
6886 if (HAS_PIPE_CXSR(dev)) {
6887 if (intel_crtc->lowfreq_avail) {
6888 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6889 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6890 } else {
6891 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01006892 }
6893 }
6894
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006895 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006896 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006897 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006898 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6899 else
6900 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6901 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01006902 pipeconf |= PIPECONF_PROGRESSIVE;
6903
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006904 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006905 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03006906
Daniel Vetter84b046f2013-02-19 18:48:54 +01006907 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6908 POSTING_READ(PIPECONF(intel_crtc->pipe));
6909}
6910
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006911static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
6912 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08006913{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006914 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08006915 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07006916 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07006917 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02006918 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006919 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01006920 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08006921 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02006922 struct drm_atomic_state *state = crtc_state->base.state;
6923 struct drm_connector_state *connector_state;
6924 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006925
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02006926 for (i = 0; i < state->num_connector; i++) {
6927 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006928 continue;
6929
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02006930 connector_state = state->connector_states[i];
6931 if (connector_state->crtc != &crtc->base)
6932 continue;
6933
6934 encoder = to_intel_encoder(connector_state->best_encoder);
6935
Chris Wilson5eddb702010-09-11 13:48:45 +01006936 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006937 case INTEL_OUTPUT_LVDS:
6938 is_lvds = true;
6939 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006940 case INTEL_OUTPUT_DSI:
6941 is_dsi = true;
6942 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02006943 default:
6944 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006945 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006946
Eric Anholtc751ce42010-03-25 11:48:48 -07006947 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08006948 }
6949
Jani Nikulaf2335332013-09-13 11:03:09 +03006950 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02006951 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006952
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006953 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006954 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03006955
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006956 /*
6957 * Returns a set of divisors for the desired target clock with
6958 * the given refclk, or FALSE. The returned values represent
6959 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6960 * 2) / p1 / p2.
6961 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006962 limit = intel_limit(crtc_state, refclk);
6963 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006964 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006965 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03006966 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006967 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6968 return -EINVAL;
6969 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006970
Jani Nikulaf2335332013-09-13 11:03:09 +03006971 if (is_lvds && dev_priv->lvds_downclock_avail) {
6972 /*
6973 * Ensure we match the reduced clock's P to the target
6974 * clock. If the clocks don't match, we can't switch
6975 * the display clock by using the FP0/FP1. In such case
6976 * we will disable the LVDS downclock feature.
6977 */
6978 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006979 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03006980 dev_priv->lvds_downclock,
6981 refclk, &clock,
6982 &reduced_clock);
6983 }
6984 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006985 crtc_state->dpll.n = clock.n;
6986 crtc_state->dpll.m1 = clock.m1;
6987 crtc_state->dpll.m2 = clock.m2;
6988 crtc_state->dpll.p1 = clock.p1;
6989 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006990 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006991
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006992 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006993 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306994 has_reduced_clock ? &reduced_clock : NULL,
6995 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006996 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006997 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006998 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006999 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007000 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007001 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007002 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007003 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007004 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007005
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007006 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007007}
7008
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007009static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007010 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007011{
7012 struct drm_device *dev = crtc->base.dev;
7013 struct drm_i915_private *dev_priv = dev->dev_private;
7014 uint32_t tmp;
7015
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007016 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7017 return;
7018
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007019 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007020 if (!(tmp & PFIT_ENABLE))
7021 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007022
Daniel Vetter06922822013-07-11 13:35:40 +02007023 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007024 if (INTEL_INFO(dev)->gen < 4) {
7025 if (crtc->pipe != PIPE_B)
7026 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007027 } else {
7028 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7029 return;
7030 }
7031
Daniel Vetter06922822013-07-11 13:35:40 +02007032 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007033 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7034 if (INTEL_INFO(dev)->gen < 5)
7035 pipe_config->gmch_pfit.lvds_border_bits =
7036 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7037}
7038
Jesse Barnesacbec812013-09-20 11:29:32 -07007039static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007040 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007041{
7042 struct drm_device *dev = crtc->base.dev;
7043 struct drm_i915_private *dev_priv = dev->dev_private;
7044 int pipe = pipe_config->cpu_transcoder;
7045 intel_clock_t clock;
7046 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007047 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007048
Shobhit Kumarf573de52014-07-30 20:32:37 +05307049 /* In case of MIPI DPLL will not even be used */
7050 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7051 return;
7052
Jesse Barnesacbec812013-09-20 11:29:32 -07007053 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007054 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07007055 mutex_unlock(&dev_priv->dpio_lock);
7056
7057 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7058 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7059 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7060 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7061 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7062
Ville Syrjäläf6466282013-10-14 14:50:31 +03007063 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007064
Ville Syrjäläf6466282013-10-14 14:50:31 +03007065 /* clock.dot is the fast clock */
7066 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007067}
7068
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007069static void
7070i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7071 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007072{
7073 struct drm_device *dev = crtc->base.dev;
7074 struct drm_i915_private *dev_priv = dev->dev_private;
7075 u32 val, base, offset;
7076 int pipe = crtc->pipe, plane = crtc->plane;
7077 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007078 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007079 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007080 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007081
Damien Lespiau42a7b082015-02-05 19:35:13 +00007082 val = I915_READ(DSPCNTR(plane));
7083 if (!(val & DISPLAY_PLANE_ENABLE))
7084 return;
7085
Damien Lespiaud9806c92015-01-21 14:07:19 +00007086 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007087 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007088 DRM_DEBUG_KMS("failed to alloc fb\n");
7089 return;
7090 }
7091
Damien Lespiau1b842c82015-01-21 13:50:54 +00007092 fb = &intel_fb->base;
7093
Daniel Vetter18c52472015-02-10 17:16:09 +00007094 if (INTEL_INFO(dev)->gen >= 4) {
7095 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007096 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007097 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7098 }
7099 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007100
7101 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007102 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007103 fb->pixel_format = fourcc;
7104 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007105
7106 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007107 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007108 offset = I915_READ(DSPTILEOFF(plane));
7109 else
7110 offset = I915_READ(DSPLINOFF(plane));
7111 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7112 } else {
7113 base = I915_READ(DSPADDR(plane));
7114 }
7115 plane_config->base = base;
7116
7117 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007118 fb->width = ((val >> 16) & 0xfff) + 1;
7119 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007120
7121 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007122 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007123
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007124 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007125 fb->pixel_format,
7126 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007127
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007128 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007129
Damien Lespiau2844a922015-01-20 12:51:48 +00007130 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7131 pipe_name(pipe), plane, fb->width, fb->height,
7132 fb->bits_per_pixel, base, fb->pitches[0],
7133 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007134
Damien Lespiau2d140302015-02-05 17:22:18 +00007135 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007136}
7137
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007138static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007139 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007140{
7141 struct drm_device *dev = crtc->base.dev;
7142 struct drm_i915_private *dev_priv = dev->dev_private;
7143 int pipe = pipe_config->cpu_transcoder;
7144 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7145 intel_clock_t clock;
7146 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7147 int refclk = 100000;
7148
7149 mutex_lock(&dev_priv->dpio_lock);
7150 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7151 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7152 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7153 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7154 mutex_unlock(&dev_priv->dpio_lock);
7155
7156 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7157 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7158 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7159 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7160 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7161
7162 chv_clock(refclk, &clock);
7163
7164 /* clock.dot is the fast clock */
7165 pipe_config->port_clock = clock.dot / 5;
7166}
7167
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007168static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007169 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007170{
7171 struct drm_device *dev = crtc->base.dev;
7172 struct drm_i915_private *dev_priv = dev->dev_private;
7173 uint32_t tmp;
7174
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007175 if (!intel_display_power_is_enabled(dev_priv,
7176 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007177 return false;
7178
Daniel Vettere143a212013-07-04 12:01:15 +02007179 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007180 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007181
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007182 tmp = I915_READ(PIPECONF(crtc->pipe));
7183 if (!(tmp & PIPECONF_ENABLE))
7184 return false;
7185
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007186 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7187 switch (tmp & PIPECONF_BPC_MASK) {
7188 case PIPECONF_6BPC:
7189 pipe_config->pipe_bpp = 18;
7190 break;
7191 case PIPECONF_8BPC:
7192 pipe_config->pipe_bpp = 24;
7193 break;
7194 case PIPECONF_10BPC:
7195 pipe_config->pipe_bpp = 30;
7196 break;
7197 default:
7198 break;
7199 }
7200 }
7201
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007202 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7203 pipe_config->limited_color_range = true;
7204
Ville Syrjälä282740f2013-09-04 18:30:03 +03007205 if (INTEL_INFO(dev)->gen < 4)
7206 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7207
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007208 intel_get_pipe_timings(crtc, pipe_config);
7209
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007210 i9xx_get_pfit_config(crtc, pipe_config);
7211
Daniel Vetter6c49f242013-06-06 12:45:25 +02007212 if (INTEL_INFO(dev)->gen >= 4) {
7213 tmp = I915_READ(DPLL_MD(crtc->pipe));
7214 pipe_config->pixel_multiplier =
7215 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7216 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007217 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02007218 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7219 tmp = I915_READ(DPLL(crtc->pipe));
7220 pipe_config->pixel_multiplier =
7221 ((tmp & SDVO_MULTIPLIER_MASK)
7222 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7223 } else {
7224 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7225 * port and will be fixed up in the encoder->get_config
7226 * function. */
7227 pipe_config->pixel_multiplier = 1;
7228 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007229 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7230 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007231 /*
7232 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7233 * on 830. Filter it out here so that we don't
7234 * report errors due to that.
7235 */
7236 if (IS_I830(dev))
7237 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7238
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007239 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7240 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007241 } else {
7242 /* Mask out read-only status bits. */
7243 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7244 DPLL_PORTC_READY_MASK |
7245 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007246 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007247
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007248 if (IS_CHERRYVIEW(dev))
7249 chv_crtc_clock_get(crtc, pipe_config);
7250 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07007251 vlv_crtc_clock_get(crtc, pipe_config);
7252 else
7253 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007254
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007255 return true;
7256}
7257
Paulo Zanonidde86e22012-12-01 12:04:25 -02007258static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007259{
7260 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007261 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007262 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007263 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007264 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007265 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007266 bool has_ck505 = false;
7267 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007268
7269 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01007270 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007271 switch (encoder->type) {
7272 case INTEL_OUTPUT_LVDS:
7273 has_panel = true;
7274 has_lvds = true;
7275 break;
7276 case INTEL_OUTPUT_EDP:
7277 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03007278 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007279 has_cpu_edp = true;
7280 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007281 default:
7282 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007283 }
7284 }
7285
Keith Packard99eb6a02011-09-26 14:29:12 -07007286 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007287 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007288 can_ssc = has_ck505;
7289 } else {
7290 has_ck505 = false;
7291 can_ssc = true;
7292 }
7293
Imre Deak2de69052013-05-08 13:14:04 +03007294 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7295 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007296
7297 /* Ironlake: try to setup display ref clock before DPLL
7298 * enabling. This is only under driver's control after
7299 * PCH B stepping, previous chipset stepping should be
7300 * ignoring this setting.
7301 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007302 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007303
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007304 /* As we must carefully and slowly disable/enable each source in turn,
7305 * compute the final state we want first and check if we need to
7306 * make any changes at all.
7307 */
7308 final = val;
7309 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007310 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007311 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007312 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007313 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7314
7315 final &= ~DREF_SSC_SOURCE_MASK;
7316 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7317 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007318
Keith Packard199e5d72011-09-22 12:01:57 -07007319 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007320 final |= DREF_SSC_SOURCE_ENABLE;
7321
7322 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7323 final |= DREF_SSC1_ENABLE;
7324
7325 if (has_cpu_edp) {
7326 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7327 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7328 else
7329 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7330 } else
7331 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7332 } else {
7333 final |= DREF_SSC_SOURCE_DISABLE;
7334 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7335 }
7336
7337 if (final == val)
7338 return;
7339
7340 /* Always enable nonspread source */
7341 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7342
7343 if (has_ck505)
7344 val |= DREF_NONSPREAD_CK505_ENABLE;
7345 else
7346 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7347
7348 if (has_panel) {
7349 val &= ~DREF_SSC_SOURCE_MASK;
7350 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007351
Keith Packard199e5d72011-09-22 12:01:57 -07007352 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007353 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007354 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007355 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007356 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007357 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007358
7359 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007360 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007361 POSTING_READ(PCH_DREF_CONTROL);
7362 udelay(200);
7363
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007364 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007365
7366 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007367 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007368 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007369 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007370 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007371 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007372 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007373 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007374 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007375
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007376 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007377 POSTING_READ(PCH_DREF_CONTROL);
7378 udelay(200);
7379 } else {
7380 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7381
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007382 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007383
7384 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007385 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007386
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007387 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007388 POSTING_READ(PCH_DREF_CONTROL);
7389 udelay(200);
7390
7391 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007392 val &= ~DREF_SSC_SOURCE_MASK;
7393 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007394
7395 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007396 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007397
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007398 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007399 POSTING_READ(PCH_DREF_CONTROL);
7400 udelay(200);
7401 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007402
7403 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007404}
7405
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007406static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007407{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007408 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007409
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007410 tmp = I915_READ(SOUTH_CHICKEN2);
7411 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7412 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007413
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007414 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7415 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7416 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007417
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007418 tmp = I915_READ(SOUTH_CHICKEN2);
7419 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7420 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007421
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007422 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7423 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7424 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007425}
7426
7427/* WaMPhyProgramming:hsw */
7428static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7429{
7430 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007431
7432 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7433 tmp &= ~(0xFF << 24);
7434 tmp |= (0x12 << 24);
7435 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7436
Paulo Zanonidde86e22012-12-01 12:04:25 -02007437 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7438 tmp |= (1 << 11);
7439 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7440
7441 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7442 tmp |= (1 << 11);
7443 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7444
Paulo Zanonidde86e22012-12-01 12:04:25 -02007445 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7446 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7447 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7448
7449 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7450 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7451 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7452
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007453 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7454 tmp &= ~(7 << 13);
7455 tmp |= (5 << 13);
7456 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007457
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007458 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7459 tmp &= ~(7 << 13);
7460 tmp |= (5 << 13);
7461 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007462
7463 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7464 tmp &= ~0xFF;
7465 tmp |= 0x1C;
7466 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7467
7468 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7469 tmp &= ~0xFF;
7470 tmp |= 0x1C;
7471 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7472
7473 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7474 tmp &= ~(0xFF << 16);
7475 tmp |= (0x1C << 16);
7476 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7477
7478 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7479 tmp &= ~(0xFF << 16);
7480 tmp |= (0x1C << 16);
7481 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7482
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007483 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7484 tmp |= (1 << 27);
7485 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007486
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007487 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7488 tmp |= (1 << 27);
7489 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007490
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007491 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7492 tmp &= ~(0xF << 28);
7493 tmp |= (4 << 28);
7494 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007495
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007496 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7497 tmp &= ~(0xF << 28);
7498 tmp |= (4 << 28);
7499 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007500}
7501
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007502/* Implements 3 different sequences from BSpec chapter "Display iCLK
7503 * Programming" based on the parameters passed:
7504 * - Sequence to enable CLKOUT_DP
7505 * - Sequence to enable CLKOUT_DP without spread
7506 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7507 */
7508static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
7509 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007510{
7511 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007512 uint32_t reg, tmp;
7513
7514 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7515 with_spread = true;
7516 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
7517 with_fdi, "LP PCH doesn't have FDI\n"))
7518 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007519
7520 mutex_lock(&dev_priv->dpio_lock);
7521
7522 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7523 tmp &= ~SBI_SSCCTL_DISABLE;
7524 tmp |= SBI_SSCCTL_PATHALT;
7525 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7526
7527 udelay(24);
7528
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007529 if (with_spread) {
7530 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7531 tmp &= ~SBI_SSCCTL_PATHALT;
7532 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007533
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007534 if (with_fdi) {
7535 lpt_reset_fdi_mphy(dev_priv);
7536 lpt_program_fdi_mphy(dev_priv);
7537 }
7538 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02007539
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007540 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7541 SBI_GEN0 : SBI_DBUFF0;
7542 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7543 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7544 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01007545
7546 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007547}
7548
Paulo Zanoni47701c32013-07-23 11:19:25 -03007549/* Sequence to disable CLKOUT_DP */
7550static void lpt_disable_clkout_dp(struct drm_device *dev)
7551{
7552 struct drm_i915_private *dev_priv = dev->dev_private;
7553 uint32_t reg, tmp;
7554
7555 mutex_lock(&dev_priv->dpio_lock);
7556
7557 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7558 SBI_GEN0 : SBI_DBUFF0;
7559 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7560 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7561 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7562
7563 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7564 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7565 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7566 tmp |= SBI_SSCCTL_PATHALT;
7567 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7568 udelay(32);
7569 }
7570 tmp |= SBI_SSCCTL_DISABLE;
7571 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7572 }
7573
7574 mutex_unlock(&dev_priv->dpio_lock);
7575}
7576
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007577static void lpt_init_pch_refclk(struct drm_device *dev)
7578{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007579 struct intel_encoder *encoder;
7580 bool has_vga = false;
7581
Damien Lespiaub2784e12014-08-05 11:29:37 +01007582 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007583 switch (encoder->type) {
7584 case INTEL_OUTPUT_ANALOG:
7585 has_vga = true;
7586 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007587 default:
7588 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007589 }
7590 }
7591
Paulo Zanoni47701c32013-07-23 11:19:25 -03007592 if (has_vga)
7593 lpt_enable_clkout_dp(dev, true, true);
7594 else
7595 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007596}
7597
Paulo Zanonidde86e22012-12-01 12:04:25 -02007598/*
7599 * Initialize reference clocks when the driver loads
7600 */
7601void intel_init_pch_refclk(struct drm_device *dev)
7602{
7603 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7604 ironlake_init_pch_refclk(dev);
7605 else if (HAS_PCH_LPT(dev))
7606 lpt_init_pch_refclk(dev);
7607}
7608
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007609static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007610{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007611 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007612 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007613 struct drm_atomic_state *state = crtc_state->base.state;
7614 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007615 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007616 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007617 bool is_lvds = false;
7618
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007619 for (i = 0; i < state->num_connector; i++) {
7620 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007621 continue;
7622
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007623 connector_state = state->connector_states[i];
7624 if (connector_state->crtc != crtc_state->base.crtc)
7625 continue;
7626
7627 encoder = to_intel_encoder(connector_state->best_encoder);
7628
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007629 switch (encoder->type) {
7630 case INTEL_OUTPUT_LVDS:
7631 is_lvds = true;
7632 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007633 default:
7634 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007635 }
7636 num_connectors++;
7637 }
7638
7639 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007640 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007641 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007642 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007643 }
7644
7645 return 120000;
7646}
7647
Daniel Vetter6ff93602013-04-19 11:24:36 +02007648static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03007649{
7650 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
7651 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7652 int pipe = intel_crtc->pipe;
7653 uint32_t val;
7654
Daniel Vetter78114072013-06-13 00:54:57 +02007655 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03007656
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007657 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03007658 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007659 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007660 break;
7661 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007662 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007663 break;
7664 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007665 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007666 break;
7667 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007668 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007669 break;
7670 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03007671 /* Case prevented by intel_choose_pipe_bpp_dither. */
7672 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03007673 }
7674
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007675 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03007676 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7677
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007678 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03007679 val |= PIPECONF_INTERLACED_ILK;
7680 else
7681 val |= PIPECONF_PROGRESSIVE;
7682
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007683 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007684 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007685
Paulo Zanonic8203562012-09-12 10:06:29 -03007686 I915_WRITE(PIPECONF(pipe), val);
7687 POSTING_READ(PIPECONF(pipe));
7688}
7689
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007690/*
7691 * Set up the pipe CSC unit.
7692 *
7693 * Currently only full range RGB to limited range RGB conversion
7694 * is supported, but eventually this should handle various
7695 * RGB<->YCbCr scenarios as well.
7696 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01007697static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007698{
7699 struct drm_device *dev = crtc->dev;
7700 struct drm_i915_private *dev_priv = dev->dev_private;
7701 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7702 int pipe = intel_crtc->pipe;
7703 uint16_t coeff = 0x7800; /* 1.0 */
7704
7705 /*
7706 * TODO: Check what kind of values actually come out of the pipe
7707 * with these coeff/postoff values and adjust to get the best
7708 * accuracy. Perhaps we even need to take the bpc value into
7709 * consideration.
7710 */
7711
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007712 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007713 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
7714
7715 /*
7716 * GY/GU and RY/RU should be the other way around according
7717 * to BSpec, but reality doesn't agree. Just set them up in
7718 * a way that results in the correct picture.
7719 */
7720 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
7721 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
7722
7723 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
7724 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
7725
7726 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
7727 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
7728
7729 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
7730 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
7731 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
7732
7733 if (INTEL_INFO(dev)->gen > 6) {
7734 uint16_t postoff = 0;
7735
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007736 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02007737 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007738
7739 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
7740 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
7741 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
7742
7743 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
7744 } else {
7745 uint32_t mode = CSC_MODE_YUV_TO_RGB;
7746
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007747 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007748 mode |= CSC_BLACK_SCREEN_OFFSET;
7749
7750 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
7751 }
7752}
7753
Daniel Vetter6ff93602013-04-19 11:24:36 +02007754static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007755{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007756 struct drm_device *dev = crtc->dev;
7757 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007758 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007759 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007760 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007761 uint32_t val;
7762
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007763 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007764
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007765 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007766 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7767
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007768 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007769 val |= PIPECONF_INTERLACED_ILK;
7770 else
7771 val |= PIPECONF_PROGRESSIVE;
7772
Paulo Zanoni702e7a52012-10-23 18:29:59 -02007773 I915_WRITE(PIPECONF(cpu_transcoder), val);
7774 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007775
7776 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
7777 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007778
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05307779 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007780 val = 0;
7781
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007782 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007783 case 18:
7784 val |= PIPEMISC_DITHER_6_BPC;
7785 break;
7786 case 24:
7787 val |= PIPEMISC_DITHER_8_BPC;
7788 break;
7789 case 30:
7790 val |= PIPEMISC_DITHER_10_BPC;
7791 break;
7792 case 36:
7793 val |= PIPEMISC_DITHER_12_BPC;
7794 break;
7795 default:
7796 /* Case prevented by pipe_config_set_bpp. */
7797 BUG();
7798 }
7799
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007800 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007801 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7802
7803 I915_WRITE(PIPEMISC(pipe), val);
7804 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007805}
7806
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007807static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007808 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007809 intel_clock_t *clock,
7810 bool *has_reduced_clock,
7811 intel_clock_t *reduced_clock)
7812{
7813 struct drm_device *dev = crtc->dev;
7814 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007815 int refclk;
7816 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02007817 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007818
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007819 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007820
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007821 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007822
7823 /*
7824 * Returns a set of divisors for the desired target clock with the given
7825 * refclk, or FALSE. The returned values represent the clock equation:
7826 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7827 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007828 limit = intel_limit(crtc_state, refclk);
7829 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007830 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007831 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007832 if (!ret)
7833 return false;
7834
7835 if (is_lvds && dev_priv->lvds_downclock_avail) {
7836 /*
7837 * Ensure we match the reduced clock's P to the target clock.
7838 * If the clocks don't match, we can't switch the display clock
7839 * by using the FP0/FP1. In such case we will disable the LVDS
7840 * downclock feature.
7841 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02007842 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007843 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007844 dev_priv->lvds_downclock,
7845 refclk, clock,
7846 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007847 }
7848
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007849 return true;
7850}
7851
Paulo Zanonid4b19312012-11-29 11:29:32 -02007852int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7853{
7854 /*
7855 * Account for spread spectrum to avoid
7856 * oversubscribing the link. Max center spread
7857 * is 2.5%; use 5% for safety's sake.
7858 */
7859 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02007860 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02007861}
7862
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007863static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02007864{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007865 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03007866}
7867
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007868static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007869 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007870 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007871 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007872{
7873 struct drm_crtc *crtc = &intel_crtc->base;
7874 struct drm_device *dev = crtc->dev;
7875 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007876 struct drm_atomic_state *state = crtc_state->base.state;
7877 struct drm_connector_state *connector_state;
7878 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007879 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007880 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02007881 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007882
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007883 for (i = 0; i < state->num_connector; i++) {
7884 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007885 continue;
7886
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007887 connector_state = state->connector_states[i];
7888 if (connector_state->crtc != crtc_state->base.crtc)
7889 continue;
7890
7891 encoder = to_intel_encoder(connector_state->best_encoder);
7892
7893 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007894 case INTEL_OUTPUT_LVDS:
7895 is_lvds = true;
7896 break;
7897 case INTEL_OUTPUT_SDVO:
7898 case INTEL_OUTPUT_HDMI:
7899 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007900 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007901 default:
7902 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007903 }
7904
7905 num_connectors++;
7906 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007907
Chris Wilsonc1858122010-12-03 21:35:48 +00007908 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07007909 factor = 21;
7910 if (is_lvds) {
7911 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007912 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02007913 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07007914 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007915 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07007916 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00007917
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007918 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02007919 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00007920
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007921 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7922 *fp2 |= FP_CB_TUNE;
7923
Chris Wilson5eddb702010-09-11 13:48:45 +01007924 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08007925
Eric Anholta07d6782011-03-30 13:01:08 -07007926 if (is_lvds)
7927 dpll |= DPLLB_MODE_LVDS;
7928 else
7929 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007930
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007931 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007932 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007933
7934 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007935 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007936 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007937 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08007938
Eric Anholta07d6782011-03-30 13:01:08 -07007939 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007940 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007941 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007942 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007943
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007944 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07007945 case 5:
7946 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7947 break;
7948 case 7:
7949 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7950 break;
7951 case 10:
7952 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7953 break;
7954 case 14:
7955 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7956 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007957 }
7958
Daniel Vetterb4c09f32013-04-30 14:01:42 +02007959 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007960 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08007961 else
7962 dpll |= PLL_REF_INPUT_DREFCLK;
7963
Daniel Vetter959e16d2013-06-05 13:34:21 +02007964 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007965}
7966
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007967static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
7968 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007969{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007970 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007971 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007972 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03007973 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01007974 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007975 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007976
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007977 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08007978
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007979 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7980 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
7981
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007982 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007983 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007984 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007985 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7986 return -EINVAL;
7987 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01007988 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007989 if (!crtc_state->clock_set) {
7990 crtc_state->dpll.n = clock.n;
7991 crtc_state->dpll.m1 = clock.m1;
7992 crtc_state->dpll.m2 = clock.m2;
7993 crtc_state->dpll.p1 = clock.p1;
7994 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007995 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007996
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007997 /* 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 +02007998 if (crtc_state->has_pch_encoder) {
7999 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008000 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008001 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008002
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008003 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008004 &fp, &reduced_clock,
8005 has_reduced_clock ? &fp2 : NULL);
8006
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008007 crtc_state->dpll_hw_state.dpll = dpll;
8008 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008009 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008010 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008011 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008012 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008013
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008014 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008015 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008016 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008017 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008018 return -EINVAL;
8019 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008020 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008021
Rodrigo Viviab585de2015-03-24 12:40:09 -07008022 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008023 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008024 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008025 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008026
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008027 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008028}
8029
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008030static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8031 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008032{
8033 struct drm_device *dev = crtc->base.dev;
8034 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008035 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008036
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008037 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8038 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8039 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8040 & ~TU_SIZE_MASK;
8041 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8042 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8043 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8044}
8045
8046static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8047 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008048 struct intel_link_m_n *m_n,
8049 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008050{
8051 struct drm_device *dev = crtc->base.dev;
8052 struct drm_i915_private *dev_priv = dev->dev_private;
8053 enum pipe pipe = crtc->pipe;
8054
8055 if (INTEL_INFO(dev)->gen >= 5) {
8056 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8057 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8058 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8059 & ~TU_SIZE_MASK;
8060 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8061 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8062 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008063 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8064 * gen < 8) and if DRRS is supported (to make sure the
8065 * registers are not unnecessarily read).
8066 */
8067 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008068 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008069 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8070 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8071 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8072 & ~TU_SIZE_MASK;
8073 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8074 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8075 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8076 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008077 } else {
8078 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8079 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8080 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8081 & ~TU_SIZE_MASK;
8082 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8083 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8084 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8085 }
8086}
8087
8088void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008089 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008090{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008091 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008092 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8093 else
8094 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008095 &pipe_config->dp_m_n,
8096 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008097}
8098
Daniel Vetter72419202013-04-04 13:28:53 +02008099static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008100 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008101{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008102 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008103 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008104}
8105
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008106static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008107 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008108{
8109 struct drm_device *dev = crtc->base.dev;
8110 struct drm_i915_private *dev_priv = dev->dev_private;
8111 uint32_t tmp;
8112
8113 tmp = I915_READ(PS_CTL(crtc->pipe));
8114
8115 if (tmp & PS_ENABLE) {
8116 pipe_config->pch_pfit.enabled = true;
8117 pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
8118 pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
8119 }
8120}
8121
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008122static void
8123skylake_get_initial_plane_config(struct intel_crtc *crtc,
8124 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008125{
8126 struct drm_device *dev = crtc->base.dev;
8127 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008128 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008129 int pipe = crtc->pipe;
8130 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008131 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008132 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008133 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008134
Damien Lespiaud9806c92015-01-21 14:07:19 +00008135 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008136 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008137 DRM_DEBUG_KMS("failed to alloc fb\n");
8138 return;
8139 }
8140
Damien Lespiau1b842c82015-01-21 13:50:54 +00008141 fb = &intel_fb->base;
8142
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008143 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008144 if (!(val & PLANE_CTL_ENABLE))
8145 goto error;
8146
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008147 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8148 fourcc = skl_format_to_fourcc(pixel_format,
8149 val & PLANE_CTL_ORDER_RGBX,
8150 val & PLANE_CTL_ALPHA_MASK);
8151 fb->pixel_format = fourcc;
8152 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8153
Damien Lespiau40f46282015-02-27 11:15:21 +00008154 tiling = val & PLANE_CTL_TILED_MASK;
8155 switch (tiling) {
8156 case PLANE_CTL_TILED_LINEAR:
8157 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8158 break;
8159 case PLANE_CTL_TILED_X:
8160 plane_config->tiling = I915_TILING_X;
8161 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8162 break;
8163 case PLANE_CTL_TILED_Y:
8164 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8165 break;
8166 case PLANE_CTL_TILED_YF:
8167 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8168 break;
8169 default:
8170 MISSING_CASE(tiling);
8171 goto error;
8172 }
8173
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008174 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8175 plane_config->base = base;
8176
8177 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8178
8179 val = I915_READ(PLANE_SIZE(pipe, 0));
8180 fb->height = ((val >> 16) & 0xfff) + 1;
8181 fb->width = ((val >> 0) & 0x1fff) + 1;
8182
8183 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00008184 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8185 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008186 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8187
8188 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008189 fb->pixel_format,
8190 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008191
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008192 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008193
8194 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8195 pipe_name(pipe), fb->width, fb->height,
8196 fb->bits_per_pixel, base, fb->pitches[0],
8197 plane_config->size);
8198
Damien Lespiau2d140302015-02-05 17:22:18 +00008199 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008200 return;
8201
8202error:
8203 kfree(fb);
8204}
8205
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008206static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008207 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008208{
8209 struct drm_device *dev = crtc->base.dev;
8210 struct drm_i915_private *dev_priv = dev->dev_private;
8211 uint32_t tmp;
8212
8213 tmp = I915_READ(PF_CTL(crtc->pipe));
8214
8215 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008216 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008217 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8218 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008219
8220 /* We currently do not free assignements of panel fitters on
8221 * ivb/hsw (since we don't use the higher upscaling modes which
8222 * differentiates them) so just WARN about this case for now. */
8223 if (IS_GEN7(dev)) {
8224 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8225 PF_PIPE_SEL_IVB(crtc->pipe));
8226 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008227 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008228}
8229
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008230static void
8231ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8232 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008233{
8234 struct drm_device *dev = crtc->base.dev;
8235 struct drm_i915_private *dev_priv = dev->dev_private;
8236 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008237 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008238 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008239 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008240 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008241 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008242
Damien Lespiau42a7b082015-02-05 19:35:13 +00008243 val = I915_READ(DSPCNTR(pipe));
8244 if (!(val & DISPLAY_PLANE_ENABLE))
8245 return;
8246
Damien Lespiaud9806c92015-01-21 14:07:19 +00008247 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008248 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008249 DRM_DEBUG_KMS("failed to alloc fb\n");
8250 return;
8251 }
8252
Damien Lespiau1b842c82015-01-21 13:50:54 +00008253 fb = &intel_fb->base;
8254
Daniel Vetter18c52472015-02-10 17:16:09 +00008255 if (INTEL_INFO(dev)->gen >= 4) {
8256 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008257 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008258 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8259 }
8260 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008261
8262 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008263 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008264 fb->pixel_format = fourcc;
8265 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008266
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008267 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008268 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008269 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008270 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00008271 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008272 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008273 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008274 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008275 }
8276 plane_config->base = base;
8277
8278 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008279 fb->width = ((val >> 16) & 0xfff) + 1;
8280 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008281
8282 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008283 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008284
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008285 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008286 fb->pixel_format,
8287 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008288
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008289 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008290
Damien Lespiau2844a922015-01-20 12:51:48 +00008291 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8292 pipe_name(pipe), fb->width, fb->height,
8293 fb->bits_per_pixel, base, fb->pitches[0],
8294 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008295
Damien Lespiau2d140302015-02-05 17:22:18 +00008296 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008297}
8298
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008299static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008300 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008301{
8302 struct drm_device *dev = crtc->base.dev;
8303 struct drm_i915_private *dev_priv = dev->dev_private;
8304 uint32_t tmp;
8305
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008306 if (!intel_display_power_is_enabled(dev_priv,
8307 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008308 return false;
8309
Daniel Vettere143a212013-07-04 12:01:15 +02008310 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008311 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008312
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008313 tmp = I915_READ(PIPECONF(crtc->pipe));
8314 if (!(tmp & PIPECONF_ENABLE))
8315 return false;
8316
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008317 switch (tmp & PIPECONF_BPC_MASK) {
8318 case PIPECONF_6BPC:
8319 pipe_config->pipe_bpp = 18;
8320 break;
8321 case PIPECONF_8BPC:
8322 pipe_config->pipe_bpp = 24;
8323 break;
8324 case PIPECONF_10BPC:
8325 pipe_config->pipe_bpp = 30;
8326 break;
8327 case PIPECONF_12BPC:
8328 pipe_config->pipe_bpp = 36;
8329 break;
8330 default:
8331 break;
8332 }
8333
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008334 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8335 pipe_config->limited_color_range = true;
8336
Daniel Vetterab9412b2013-05-03 11:49:46 +02008337 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008338 struct intel_shared_dpll *pll;
8339
Daniel Vetter88adfff2013-03-28 10:42:01 +01008340 pipe_config->has_pch_encoder = true;
8341
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008342 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8343 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8344 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008345
8346 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008347
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008348 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02008349 pipe_config->shared_dpll =
8350 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008351 } else {
8352 tmp = I915_READ(PCH_DPLL_SEL);
8353 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8354 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8355 else
8356 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8357 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008358
8359 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8360
8361 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8362 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008363
8364 tmp = pipe_config->dpll_hw_state.dpll;
8365 pipe_config->pixel_multiplier =
8366 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8367 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008368
8369 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008370 } else {
8371 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008372 }
8373
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008374 intel_get_pipe_timings(crtc, pipe_config);
8375
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008376 ironlake_get_pfit_config(crtc, pipe_config);
8377
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008378 return true;
8379}
8380
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008381static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8382{
8383 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008384 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008385
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008386 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008387 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008388 pipe_name(crtc->pipe));
8389
Rob Clarke2c719b2014-12-15 13:56:32 -05008390 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8391 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8392 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8393 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8394 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8395 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008396 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03008397 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05008398 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008399 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008400 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008401 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008402 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008403 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008404 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008405
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008406 /*
8407 * In theory we can still leave IRQs enabled, as long as only the HPD
8408 * interrupts remain enabled. We used to check for that, but since it's
8409 * gen-specific and since we only disable LCPLL after we fully disable
8410 * the interrupts, the check below should be enough.
8411 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008412 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008413}
8414
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008415static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8416{
8417 struct drm_device *dev = dev_priv->dev;
8418
8419 if (IS_HASWELL(dev))
8420 return I915_READ(D_COMP_HSW);
8421 else
8422 return I915_READ(D_COMP_BDW);
8423}
8424
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008425static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8426{
8427 struct drm_device *dev = dev_priv->dev;
8428
8429 if (IS_HASWELL(dev)) {
8430 mutex_lock(&dev_priv->rps.hw_lock);
8431 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8432 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03008433 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008434 mutex_unlock(&dev_priv->rps.hw_lock);
8435 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008436 I915_WRITE(D_COMP_BDW, val);
8437 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008438 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008439}
8440
8441/*
8442 * This function implements pieces of two sequences from BSpec:
8443 * - Sequence for display software to disable LCPLL
8444 * - Sequence for display software to allow package C8+
8445 * The steps implemented here are just the steps that actually touch the LCPLL
8446 * register. Callers should take care of disabling all the display engine
8447 * functions, doing the mode unset, fixing interrupts, etc.
8448 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008449static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8450 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008451{
8452 uint32_t val;
8453
8454 assert_can_disable_lcpll(dev_priv);
8455
8456 val = I915_READ(LCPLL_CTL);
8457
8458 if (switch_to_fclk) {
8459 val |= LCPLL_CD_SOURCE_FCLK;
8460 I915_WRITE(LCPLL_CTL, val);
8461
8462 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
8463 LCPLL_CD_SOURCE_FCLK_DONE, 1))
8464 DRM_ERROR("Switching to FCLK failed\n");
8465
8466 val = I915_READ(LCPLL_CTL);
8467 }
8468
8469 val |= LCPLL_PLL_DISABLE;
8470 I915_WRITE(LCPLL_CTL, val);
8471 POSTING_READ(LCPLL_CTL);
8472
8473 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
8474 DRM_ERROR("LCPLL still locked\n");
8475
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008476 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008477 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008478 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008479 ndelay(100);
8480
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008481 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8482 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008483 DRM_ERROR("D_COMP RCOMP still in progress\n");
8484
8485 if (allow_power_down) {
8486 val = I915_READ(LCPLL_CTL);
8487 val |= LCPLL_POWER_DOWN_ALLOW;
8488 I915_WRITE(LCPLL_CTL, val);
8489 POSTING_READ(LCPLL_CTL);
8490 }
8491}
8492
8493/*
8494 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8495 * source.
8496 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008497static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008498{
8499 uint32_t val;
8500
8501 val = I915_READ(LCPLL_CTL);
8502
8503 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8504 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8505 return;
8506
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008507 /*
8508 * Make sure we're not on PC8 state before disabling PC8, otherwise
8509 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008510 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02008511 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03008512
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008513 if (val & LCPLL_POWER_DOWN_ALLOW) {
8514 val &= ~LCPLL_POWER_DOWN_ALLOW;
8515 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02008516 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008517 }
8518
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008519 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008520 val |= D_COMP_COMP_FORCE;
8521 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008522 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008523
8524 val = I915_READ(LCPLL_CTL);
8525 val &= ~LCPLL_PLL_DISABLE;
8526 I915_WRITE(LCPLL_CTL, val);
8527
8528 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
8529 DRM_ERROR("LCPLL not locked yet\n");
8530
8531 if (val & LCPLL_CD_SOURCE_FCLK) {
8532 val = I915_READ(LCPLL_CTL);
8533 val &= ~LCPLL_CD_SOURCE_FCLK;
8534 I915_WRITE(LCPLL_CTL, val);
8535
8536 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
8537 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
8538 DRM_ERROR("Switching back to LCPLL failed\n");
8539 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03008540
Mika Kuoppala59bad942015-01-16 11:34:40 +02008541 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008542}
8543
Paulo Zanoni765dab672014-03-07 20:08:18 -03008544/*
8545 * Package states C8 and deeper are really deep PC states that can only be
8546 * reached when all the devices on the system allow it, so even if the graphics
8547 * device allows PC8+, it doesn't mean the system will actually get to these
8548 * states. Our driver only allows PC8+ when going into runtime PM.
8549 *
8550 * The requirements for PC8+ are that all the outputs are disabled, the power
8551 * well is disabled and most interrupts are disabled, and these are also
8552 * requirements for runtime PM. When these conditions are met, we manually do
8553 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8554 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8555 * hang the machine.
8556 *
8557 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8558 * the state of some registers, so when we come back from PC8+ we need to
8559 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8560 * need to take care of the registers kept by RC6. Notice that this happens even
8561 * if we don't put the device in PCI D3 state (which is what currently happens
8562 * because of the runtime PM support).
8563 *
8564 * For more, read "Display Sequences for Package C8" on the hardware
8565 * documentation.
8566 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008567void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008568{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008569 struct drm_device *dev = dev_priv->dev;
8570 uint32_t val;
8571
Paulo Zanonic67a4702013-08-19 13:18:09 -03008572 DRM_DEBUG_KMS("Enabling package C8+\n");
8573
Paulo Zanonic67a4702013-08-19 13:18:09 -03008574 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8575 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8576 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8577 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8578 }
8579
8580 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008581 hsw_disable_lcpll(dev_priv, true, true);
8582}
8583
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008584void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008585{
8586 struct drm_device *dev = dev_priv->dev;
8587 uint32_t val;
8588
Paulo Zanonic67a4702013-08-19 13:18:09 -03008589 DRM_DEBUG_KMS("Disabling package C8+\n");
8590
8591 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008592 lpt_init_pch_refclk(dev);
8593
8594 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8595 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8596 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8597 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8598 }
8599
8600 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008601}
8602
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008603static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8604 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008605{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008606 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008607 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03008608
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008609 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02008610
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008611 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008612}
8613
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008614static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8615 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008616 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008617{
Damien Lespiau3148ade2014-11-21 16:14:56 +00008618 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008619
8620 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
8621 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
8622
8623 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00008624 case SKL_DPLL0:
8625 /*
8626 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
8627 * of the shared DPLL framework and thus needs to be read out
8628 * separately
8629 */
8630 dpll_ctl1 = I915_READ(DPLL_CTRL1);
8631 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
8632 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008633 case SKL_DPLL1:
8634 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
8635 break;
8636 case SKL_DPLL2:
8637 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
8638 break;
8639 case SKL_DPLL3:
8640 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
8641 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008642 }
8643}
8644
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008645static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8646 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008647 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008648{
8649 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8650
8651 switch (pipe_config->ddi_pll_sel) {
8652 case PORT_CLK_SEL_WRPLL1:
8653 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
8654 break;
8655 case PORT_CLK_SEL_WRPLL2:
8656 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
8657 break;
8658 }
8659}
8660
Daniel Vetter26804af2014-06-25 22:01:55 +03008661static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008662 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03008663{
8664 struct drm_device *dev = crtc->base.dev;
8665 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03008666 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03008667 enum port port;
8668 uint32_t tmp;
8669
8670 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
8671
8672 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
8673
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008674 if (IS_SKYLAKE(dev))
8675 skylake_get_ddi_pll(dev_priv, port, pipe_config);
8676 else
8677 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03008678
Daniel Vetterd452c5b2014-07-04 11:27:39 -03008679 if (pipe_config->shared_dpll >= 0) {
8680 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8681
8682 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8683 &pipe_config->dpll_hw_state));
8684 }
8685
Daniel Vetter26804af2014-06-25 22:01:55 +03008686 /*
8687 * Haswell has only FDI/PCH transcoder A. It is which is connected to
8688 * DDI E. So just check whether this pipe is wired to DDI E and whether
8689 * the PCH transcoder is on.
8690 */
Damien Lespiauca370452013-12-03 13:56:24 +00008691 if (INTEL_INFO(dev)->gen < 9 &&
8692 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03008693 pipe_config->has_pch_encoder = true;
8694
8695 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
8696 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8697 FDI_DP_PORT_WIDTH_SHIFT) + 1;
8698
8699 ironlake_get_fdi_m_n_config(crtc, pipe_config);
8700 }
8701}
8702
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008703static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008704 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008705{
8706 struct drm_device *dev = crtc->base.dev;
8707 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008708 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008709 uint32_t tmp;
8710
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008711 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02008712 POWER_DOMAIN_PIPE(crtc->pipe)))
8713 return false;
8714
Daniel Vettere143a212013-07-04 12:01:15 +02008715 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008716 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
8717
Daniel Vettereccb1402013-05-22 00:50:22 +02008718 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8719 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8720 enum pipe trans_edp_pipe;
8721 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8722 default:
8723 WARN(1, "unknown pipe linked to edp transcoder\n");
8724 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8725 case TRANS_DDI_EDP_INPUT_A_ON:
8726 trans_edp_pipe = PIPE_A;
8727 break;
8728 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8729 trans_edp_pipe = PIPE_B;
8730 break;
8731 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8732 trans_edp_pipe = PIPE_C;
8733 break;
8734 }
8735
8736 if (trans_edp_pipe == crtc->pipe)
8737 pipe_config->cpu_transcoder = TRANSCODER_EDP;
8738 }
8739
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008740 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02008741 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03008742 return false;
8743
Daniel Vettereccb1402013-05-22 00:50:22 +02008744 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008745 if (!(tmp & PIPECONF_ENABLE))
8746 return false;
8747
Daniel Vetter26804af2014-06-25 22:01:55 +03008748 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008749
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008750 intel_get_pipe_timings(crtc, pipe_config);
8751
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008752 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008753 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
8754 if (IS_SKYLAKE(dev))
8755 skylake_get_pfit_config(crtc, pipe_config);
8756 else
8757 ironlake_get_pfit_config(crtc, pipe_config);
8758 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01008759
Jesse Barnese59150d2014-01-07 13:30:45 -08008760 if (IS_HASWELL(dev))
8761 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
8762 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008763
Clint Taylorebb69c92014-09-30 10:30:22 -07008764 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
8765 pipe_config->pixel_multiplier =
8766 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
8767 } else {
8768 pipe_config->pixel_multiplier = 1;
8769 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008770
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008771 return true;
8772}
8773
Chris Wilson560b85b2010-08-07 11:01:38 +01008774static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8775{
8776 struct drm_device *dev = crtc->dev;
8777 struct drm_i915_private *dev_priv = dev->dev_private;
8778 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03008779 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01008780
Ville Syrjälädc41c152014-08-13 11:57:05 +03008781 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08008782 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
8783 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03008784 unsigned int stride = roundup_pow_of_two(width) * 4;
8785
8786 switch (stride) {
8787 default:
8788 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8789 width, stride);
8790 stride = 256;
8791 /* fallthrough */
8792 case 256:
8793 case 512:
8794 case 1024:
8795 case 2048:
8796 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008797 }
8798
Ville Syrjälädc41c152014-08-13 11:57:05 +03008799 cntl |= CURSOR_ENABLE |
8800 CURSOR_GAMMA_ENABLE |
8801 CURSOR_FORMAT_ARGB |
8802 CURSOR_STRIDE(stride);
8803
8804 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008805 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008806
Ville Syrjälädc41c152014-08-13 11:57:05 +03008807 if (intel_crtc->cursor_cntl != 0 &&
8808 (intel_crtc->cursor_base != base ||
8809 intel_crtc->cursor_size != size ||
8810 intel_crtc->cursor_cntl != cntl)) {
8811 /* On these chipsets we can only modify the base/size/stride
8812 * whilst the cursor is disabled.
8813 */
8814 I915_WRITE(_CURACNTR, 0);
8815 POSTING_READ(_CURACNTR);
8816 intel_crtc->cursor_cntl = 0;
8817 }
8818
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008819 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03008820 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008821 intel_crtc->cursor_base = base;
8822 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03008823
8824 if (intel_crtc->cursor_size != size) {
8825 I915_WRITE(CURSIZE, size);
8826 intel_crtc->cursor_size = size;
8827 }
8828
Chris Wilson4b0e3332014-05-30 16:35:26 +03008829 if (intel_crtc->cursor_cntl != cntl) {
8830 I915_WRITE(_CURACNTR, cntl);
8831 POSTING_READ(_CURACNTR);
8832 intel_crtc->cursor_cntl = cntl;
8833 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008834}
8835
8836static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
8837{
8838 struct drm_device *dev = crtc->dev;
8839 struct drm_i915_private *dev_priv = dev->dev_private;
8840 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8841 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008842 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01008843
Chris Wilson4b0e3332014-05-30 16:35:26 +03008844 cntl = 0;
8845 if (base) {
8846 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08008847 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308848 case 64:
8849 cntl |= CURSOR_MODE_64_ARGB_AX;
8850 break;
8851 case 128:
8852 cntl |= CURSOR_MODE_128_ARGB_AX;
8853 break;
8854 case 256:
8855 cntl |= CURSOR_MODE_256_ARGB_AX;
8856 break;
8857 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08008858 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308859 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01008860 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008861 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03008862
8863 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8864 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01008865 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008866
Matt Roper8e7d6882015-01-21 16:35:41 -08008867 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008868 cntl |= CURSOR_ROTATE_180;
8869
Chris Wilson4b0e3332014-05-30 16:35:26 +03008870 if (intel_crtc->cursor_cntl != cntl) {
8871 I915_WRITE(CURCNTR(pipe), cntl);
8872 POSTING_READ(CURCNTR(pipe));
8873 intel_crtc->cursor_cntl = cntl;
8874 }
8875
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008876 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008877 I915_WRITE(CURBASE(pipe), base);
8878 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008879
8880 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008881}
8882
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008883/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01008884static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8885 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008886{
8887 struct drm_device *dev = crtc->dev;
8888 struct drm_i915_private *dev_priv = dev->dev_private;
8889 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8890 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07008891 int x = crtc->cursor_x;
8892 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008893 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008894
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008895 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008896 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008897
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008898 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008899 base = 0;
8900
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008901 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008902 base = 0;
8903
8904 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08008905 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008906 base = 0;
8907
8908 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8909 x = -x;
8910 }
8911 pos |= x << CURSOR_X_SHIFT;
8912
8913 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08008914 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008915 base = 0;
8916
8917 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8918 y = -y;
8919 }
8920 pos |= y << CURSOR_Y_SHIFT;
8921
Chris Wilson4b0e3332014-05-30 16:35:26 +03008922 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008923 return;
8924
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008925 I915_WRITE(CURPOS(pipe), pos);
8926
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008927 /* ILK+ do this automagically */
8928 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08008929 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08008930 base += (intel_crtc->base.cursor->state->crtc_h *
8931 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008932 }
8933
Ville Syrjälä8ac54662014-08-12 19:39:54 +03008934 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008935 i845_update_cursor(crtc, base);
8936 else
8937 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008938}
8939
Ville Syrjälädc41c152014-08-13 11:57:05 +03008940static bool cursor_size_ok(struct drm_device *dev,
8941 uint32_t width, uint32_t height)
8942{
8943 if (width == 0 || height == 0)
8944 return false;
8945
8946 /*
8947 * 845g/865g are special in that they are only limited by
8948 * the width of their cursors, the height is arbitrary up to
8949 * the precision of the register. Everything else requires
8950 * square cursors, limited to a few power-of-two sizes.
8951 */
8952 if (IS_845G(dev) || IS_I865G(dev)) {
8953 if ((width & 63) != 0)
8954 return false;
8955
8956 if (width > (IS_845G(dev) ? 64 : 512))
8957 return false;
8958
8959 if (height > 1023)
8960 return false;
8961 } else {
8962 switch (width | height) {
8963 case 256:
8964 case 128:
8965 if (IS_GEN2(dev))
8966 return false;
8967 case 64:
8968 break;
8969 default:
8970 return false;
8971 }
8972 }
8973
8974 return true;
8975}
8976
Jesse Barnes79e53942008-11-07 14:24:08 -08008977static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01008978 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08008979{
James Simmons72034252010-08-03 01:33:19 +01008980 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08008981 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008982
James Simmons72034252010-08-03 01:33:19 +01008983 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008984 intel_crtc->lut_r[i] = red[i] >> 8;
8985 intel_crtc->lut_g[i] = green[i] >> 8;
8986 intel_crtc->lut_b[i] = blue[i] >> 8;
8987 }
8988
8989 intel_crtc_load_lut(crtc);
8990}
8991
Jesse Barnes79e53942008-11-07 14:24:08 -08008992/* VESA 640x480x72Hz mode to set on the pipe */
8993static struct drm_display_mode load_detect_mode = {
8994 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8995 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8996};
8997
Daniel Vettera8bb6812014-02-10 18:00:39 +01008998struct drm_framebuffer *
8999__intel_framebuffer_create(struct drm_device *dev,
9000 struct drm_mode_fb_cmd2 *mode_cmd,
9001 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01009002{
9003 struct intel_framebuffer *intel_fb;
9004 int ret;
9005
9006 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9007 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009008 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01009009 return ERR_PTR(-ENOMEM);
9010 }
9011
9012 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009013 if (ret)
9014 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009015
9016 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009017err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009018 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009019 kfree(intel_fb);
9020
9021 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009022}
9023
Daniel Vetterb5ea6422014-03-02 21:18:00 +01009024static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01009025intel_framebuffer_create(struct drm_device *dev,
9026 struct drm_mode_fb_cmd2 *mode_cmd,
9027 struct drm_i915_gem_object *obj)
9028{
9029 struct drm_framebuffer *fb;
9030 int ret;
9031
9032 ret = i915_mutex_lock_interruptible(dev);
9033 if (ret)
9034 return ERR_PTR(ret);
9035 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9036 mutex_unlock(&dev->struct_mutex);
9037
9038 return fb;
9039}
9040
Chris Wilsond2dff872011-04-19 08:36:26 +01009041static u32
9042intel_framebuffer_pitch_for_width(int width, int bpp)
9043{
9044 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9045 return ALIGN(pitch, 64);
9046}
9047
9048static u32
9049intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9050{
9051 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009052 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009053}
9054
9055static struct drm_framebuffer *
9056intel_framebuffer_create_for_mode(struct drm_device *dev,
9057 struct drm_display_mode *mode,
9058 int depth, int bpp)
9059{
9060 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009061 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009062
9063 obj = i915_gem_alloc_object(dev,
9064 intel_framebuffer_size_for_mode(mode, bpp));
9065 if (obj == NULL)
9066 return ERR_PTR(-ENOMEM);
9067
9068 mode_cmd.width = mode->hdisplay;
9069 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009070 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9071 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009072 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009073
9074 return intel_framebuffer_create(dev, &mode_cmd, obj);
9075}
9076
9077static struct drm_framebuffer *
9078mode_fits_in_fbdev(struct drm_device *dev,
9079 struct drm_display_mode *mode)
9080{
Daniel Vetter4520f532013-10-09 09:18:51 +02009081#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01009082 struct drm_i915_private *dev_priv = dev->dev_private;
9083 struct drm_i915_gem_object *obj;
9084 struct drm_framebuffer *fb;
9085
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009086 if (!dev_priv->fbdev)
9087 return NULL;
9088
9089 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009090 return NULL;
9091
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009092 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009093 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009094
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009095 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009096 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9097 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01009098 return NULL;
9099
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009100 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009101 return NULL;
9102
9103 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009104#else
9105 return NULL;
9106#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009107}
9108
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009109bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01009110 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05009111 struct intel_load_detect_pipe *old,
9112 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009113{
9114 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009115 struct intel_encoder *intel_encoder =
9116 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009117 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009118 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009119 struct drm_crtc *crtc = NULL;
9120 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02009121 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009122 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009123 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009124 struct drm_connector_state *connector_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009125 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009126
Chris Wilsond2dff872011-04-19 08:36:26 +01009127 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009128 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009129 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009130
Rob Clark51fd3712013-11-19 12:10:12 -05009131retry:
9132 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9133 if (ret)
9134 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009135
Jesse Barnes79e53942008-11-07 14:24:08 -08009136 /*
9137 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009138 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009139 * - if the connector already has an assigned crtc, use it (but make
9140 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009141 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009142 * - try to find the first unused crtc that can drive this connector,
9143 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009144 */
9145
9146 /* See if we already have a CRTC for this connector */
9147 if (encoder->crtc) {
9148 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009149
Rob Clark51fd3712013-11-19 12:10:12 -05009150 ret = drm_modeset_lock(&crtc->mutex, ctx);
9151 if (ret)
9152 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009153 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9154 if (ret)
9155 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01009156
Daniel Vetter24218aa2012-08-12 19:27:11 +02009157 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009158 old->load_detect_temp = false;
9159
9160 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009161 if (connector->dpms != DRM_MODE_DPMS_ON)
9162 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01009163
Chris Wilson71731882011-04-19 23:10:58 +01009164 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08009165 }
9166
9167 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009168 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009169 i++;
9170 if (!(encoder->possible_crtcs & (1 << i)))
9171 continue;
Matt Roper83d65732015-02-25 13:12:16 -08009172 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +03009173 continue;
9174 /* This can occur when applying the pipe A quirk on resume. */
9175 if (to_intel_crtc(possible_crtc)->new_enabled)
9176 continue;
9177
9178 crtc = possible_crtc;
9179 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009180 }
9181
9182 /*
9183 * If we didn't find an unused CRTC, don't use any.
9184 */
9185 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01009186 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05009187 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08009188 }
9189
Rob Clark51fd3712013-11-19 12:10:12 -05009190 ret = drm_modeset_lock(&crtc->mutex, ctx);
9191 if (ret)
9192 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009193 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9194 if (ret)
9195 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02009196 intel_encoder->new_crtc = to_intel_crtc(crtc);
9197 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009198
9199 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009200 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +02009201 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009202 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01009203 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08009204
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009205 state = drm_atomic_state_alloc(dev);
9206 if (!state)
9207 return false;
9208
9209 state->acquire_ctx = ctx;
9210
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009211 connector_state = drm_atomic_get_connector_state(state, connector);
9212 if (IS_ERR(connector_state)) {
9213 ret = PTR_ERR(connector_state);
9214 goto fail;
9215 }
9216
9217 connector_state->crtc = crtc;
9218 connector_state->best_encoder = &intel_encoder->base;
9219
Chris Wilson64927112011-04-20 07:25:26 +01009220 if (!mode)
9221 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08009222
Chris Wilsond2dff872011-04-19 08:36:26 +01009223 /* We need a framebuffer large enough to accommodate all accesses
9224 * that the plane may generate whilst we perform load detection.
9225 * We can not rely on the fbcon either being present (we get called
9226 * during its initialisation to detect all boot displays, or it may
9227 * not even exist) or that it is large enough to satisfy the
9228 * requested mode.
9229 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02009230 fb = mode_fits_in_fbdev(dev, mode);
9231 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009232 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009233 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9234 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009235 } else
9236 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009237 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009238 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009239 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009240 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009241
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009242 if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
Chris Wilson64927112011-04-20 07:25:26 +01009243 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01009244 if (old->release_fb)
9245 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009246 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009247 }
Daniel Vetter9128b042015-03-03 17:31:21 +01009248 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +01009249
Jesse Barnes79e53942008-11-07 14:24:08 -08009250 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009251 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01009252 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009253
9254 fail:
Matt Roper83d65732015-02-25 13:12:16 -08009255 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -05009256fail_unlock:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009257 if (state) {
9258 drm_atomic_state_free(state);
9259 state = NULL;
9260 }
9261
Rob Clark51fd3712013-11-19 12:10:12 -05009262 if (ret == -EDEADLK) {
9263 drm_modeset_backoff(ctx);
9264 goto retry;
9265 }
9266
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009267 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009268}
9269
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009270void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +02009271 struct intel_load_detect_pipe *old,
9272 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009273{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009274 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009275 struct intel_encoder *intel_encoder =
9276 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01009277 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01009278 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009279 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009280 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009281 struct drm_connector_state *connector_state;
Jesse Barnes79e53942008-11-07 14:24:08 -08009282
Chris Wilsond2dff872011-04-19 08:36:26 +01009283 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009284 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009285 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009286
Chris Wilson8261b192011-04-19 23:18:09 +01009287 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009288 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009289 if (!state)
9290 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009291
9292 state->acquire_ctx = ctx;
9293
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009294 connector_state = drm_atomic_get_connector_state(state, connector);
9295 if (IS_ERR(connector_state))
9296 goto fail;
9297
Daniel Vetterfc303102012-07-09 10:40:58 +02009298 to_intel_connector(connector)->new_encoder = NULL;
9299 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009300 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009301
9302 connector_state->best_encoder = NULL;
9303 connector_state->crtc = NULL;
9304
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009305 intel_set_mode(crtc, NULL, 0, 0, NULL, state);
9306
9307 drm_atomic_state_free(state);
Chris Wilsond2dff872011-04-19 08:36:26 +01009308
Daniel Vetter36206362012-12-10 20:42:17 +01009309 if (old->release_fb) {
9310 drm_framebuffer_unregister_private(old->release_fb);
9311 drm_framebuffer_unreference(old->release_fb);
9312 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009313
Chris Wilson0622a532011-04-21 09:32:11 +01009314 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009315 }
9316
Eric Anholtc751ce42010-03-25 11:48:48 -07009317 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009318 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9319 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009320
9321 return;
9322fail:
9323 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9324 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -08009325}
9326
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009327static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009328 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009329{
9330 struct drm_i915_private *dev_priv = dev->dev_private;
9331 u32 dpll = pipe_config->dpll_hw_state.dpll;
9332
9333 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009334 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009335 else if (HAS_PCH_SPLIT(dev))
9336 return 120000;
9337 else if (!IS_GEN2(dev))
9338 return 96000;
9339 else
9340 return 48000;
9341}
9342
Jesse Barnes79e53942008-11-07 14:24:08 -08009343/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009344static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009345 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08009346{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009347 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08009348 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009349 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009350 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08009351 u32 fp;
9352 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009353 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08009354
9355 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03009356 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009357 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03009358 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009359
9360 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009361 if (IS_PINEVIEW(dev)) {
9362 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9363 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08009364 } else {
9365 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9366 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9367 }
9368
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009369 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009370 if (IS_PINEVIEW(dev))
9371 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9372 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08009373 else
9374 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08009375 DPLL_FPA01_P1_POST_DIV_SHIFT);
9376
9377 switch (dpll & DPLL_MODE_MASK) {
9378 case DPLLB_MODE_DAC_SERIAL:
9379 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9380 5 : 10;
9381 break;
9382 case DPLLB_MODE_LVDS:
9383 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9384 7 : 14;
9385 break;
9386 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08009387 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08009388 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009389 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009390 }
9391
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009392 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009393 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009394 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009395 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009396 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02009397 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009398 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08009399
9400 if (is_lvds) {
9401 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
9402 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009403
9404 if (lvds & LVDS_CLKB_POWER_UP)
9405 clock.p2 = 7;
9406 else
9407 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08009408 } else {
9409 if (dpll & PLL_P1_DIVIDE_BY_TWO)
9410 clock.p1 = 2;
9411 else {
9412 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
9413 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
9414 }
9415 if (dpll & PLL_P2_DIVIDE_BY_4)
9416 clock.p2 = 4;
9417 else
9418 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08009419 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009420
9421 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009422 }
9423
Ville Syrjälä18442d02013-09-13 16:00:08 +03009424 /*
9425 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01009426 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03009427 * encoder's get_config() function.
9428 */
9429 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009430}
9431
Ville Syrjälä6878da02013-09-13 15:59:11 +03009432int intel_dotclock_calculate(int link_freq,
9433 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009434{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009435 /*
9436 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009437 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009438 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009439 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009440 *
9441 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009442 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08009443 */
9444
Ville Syrjälä6878da02013-09-13 15:59:11 +03009445 if (!m_n->link_n)
9446 return 0;
9447
9448 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
9449}
9450
Ville Syrjälä18442d02013-09-13 16:00:08 +03009451static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009452 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03009453{
9454 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009455
9456 /* read out port_clock from the DPLL */
9457 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03009458
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009459 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03009460 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01009461 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03009462 * agree once we know their relationship in the encoder's
9463 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009464 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02009465 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03009466 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
9467 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08009468}
9469
9470/** Returns the currently programmed mode of the given pipe. */
9471struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9472 struct drm_crtc *crtc)
9473{
Jesse Barnes548f2452011-02-17 10:40:53 -08009474 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08009475 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009476 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009477 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009478 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02009479 int htot = I915_READ(HTOTAL(cpu_transcoder));
9480 int hsync = I915_READ(HSYNC(cpu_transcoder));
9481 int vtot = I915_READ(VTOTAL(cpu_transcoder));
9482 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03009483 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08009484
9485 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
9486 if (!mode)
9487 return NULL;
9488
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009489 /*
9490 * Construct a pipe_config sufficient for getting the clock info
9491 * back out of crtc_clock_get.
9492 *
9493 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
9494 * to use a real value here instead.
9495 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03009496 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009497 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009498 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
9499 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
9500 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009501 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
9502
Ville Syrjälä773ae032013-09-23 17:48:20 +03009503 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08009504 mode->hdisplay = (htot & 0xffff) + 1;
9505 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
9506 mode->hsync_start = (hsync & 0xffff) + 1;
9507 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
9508 mode->vdisplay = (vtot & 0xffff) + 1;
9509 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
9510 mode->vsync_start = (vsync & 0xffff) + 1;
9511 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
9512
9513 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08009514
9515 return mode;
9516}
9517
Jesse Barnes652c3932009-08-17 13:31:43 -07009518static void intel_decrease_pllclock(struct drm_crtc *crtc)
9519{
9520 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03009521 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07009522 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009523
Sonika Jindalbaff2962014-07-22 11:16:35 +05309524 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07009525 return;
9526
9527 if (!dev_priv->lvds_downclock_avail)
9528 return;
9529
9530 /*
9531 * Since this is called by a timer, we should never get here in
9532 * the manual case.
9533 */
9534 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01009535 int pipe = intel_crtc->pipe;
9536 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02009537 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01009538
Zhao Yakui44d98a62009-10-09 11:39:40 +08009539 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009540
Sean Paul8ac5a6d2012-02-13 13:14:51 -05009541 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009542
Chris Wilson074b5e12012-05-02 12:07:06 +01009543 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07009544 dpll |= DISPLAY_RATE_SELECT_FPA1;
9545 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009546 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009547 dpll = I915_READ(dpll_reg);
9548 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08009549 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009550 }
9551
9552}
9553
Chris Wilsonf047e392012-07-21 12:31:41 +01009554void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07009555{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009556 struct drm_i915_private *dev_priv = dev->dev_private;
9557
Chris Wilsonf62a0072014-02-21 17:55:39 +00009558 if (dev_priv->mm.busy)
9559 return;
9560
Paulo Zanoni43694d62014-03-07 20:08:08 -03009561 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009562 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +00009563 if (INTEL_INFO(dev)->gen >= 6)
9564 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +00009565 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +01009566}
9567
9568void intel_mark_idle(struct drm_device *dev)
9569{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009570 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00009571 struct drm_crtc *crtc;
9572
Chris Wilsonf62a0072014-02-21 17:55:39 +00009573 if (!dev_priv->mm.busy)
9574 return;
9575
9576 dev_priv->mm.busy = false;
9577
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009578 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07009579 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +00009580 continue;
9581
9582 intel_decrease_pllclock(crtc);
9583 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009584
Damien Lespiau3d13ef22014-02-07 19:12:47 +00009585 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009586 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03009587
Paulo Zanoni43694d62014-03-07 20:08:08 -03009588 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01009589}
9590
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009591static void intel_crtc_set_state(struct intel_crtc *crtc,
9592 struct intel_crtc_state *crtc_state)
9593{
9594 kfree(crtc->config);
9595 crtc->config = crtc_state;
Ander Conselvan de Oliveira16f3f652015-01-15 14:55:27 +02009596 crtc->base.state = &crtc_state->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009597}
9598
Jesse Barnes79e53942008-11-07 14:24:08 -08009599static void intel_crtc_destroy(struct drm_crtc *crtc)
9600{
9601 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009602 struct drm_device *dev = crtc->dev;
9603 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +02009604
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009605 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009606 work = intel_crtc->unpin_work;
9607 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009608 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009609
9610 if (work) {
9611 cancel_work_sync(&work->work);
9612 kfree(work);
9613 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009614
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009615 intel_crtc_set_state(intel_crtc, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08009616 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009617
Jesse Barnes79e53942008-11-07 14:24:08 -08009618 kfree(intel_crtc);
9619}
9620
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009621static void intel_unpin_work_fn(struct work_struct *__work)
9622{
9623 struct intel_unpin_work *work =
9624 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009625 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +02009626 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009627
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009628 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00009629 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +00009630 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00009631
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02009632 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +00009633
9634 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +00009635 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009636 mutex_unlock(&dev->struct_mutex);
9637
Daniel Vetterf99d7062014-06-19 16:01:59 +02009638 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +00009639 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +02009640
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009641 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9642 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9643
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009644 kfree(work);
9645}
9646
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009647static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01009648 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009649{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009650 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9651 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009652 unsigned long flags;
9653
9654 /* Ignore early vblank irqs */
9655 if (intel_crtc == NULL)
9656 return;
9657
Daniel Vetterf3260382014-09-15 14:55:23 +02009658 /*
9659 * This is called both by irq handlers and the reset code (to complete
9660 * lost pageflips) so needs the full irqsave spinlocks.
9661 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009662 spin_lock_irqsave(&dev->event_lock, flags);
9663 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00009664
9665 /* Ensure we don't miss a work->pending update ... */
9666 smp_rmb();
9667
9668 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009669 spin_unlock_irqrestore(&dev->event_lock, flags);
9670 return;
9671 }
9672
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009673 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01009674
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009675 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009676}
9677
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009678void intel_finish_page_flip(struct drm_device *dev, int pipe)
9679{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009680 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009681 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9682
Mario Kleiner49b14a52010-12-09 07:00:07 +01009683 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009684}
9685
9686void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9687{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009688 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009689 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9690
Mario Kleiner49b14a52010-12-09 07:00:07 +01009691 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009692}
9693
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009694/* Is 'a' after or equal to 'b'? */
9695static bool g4x_flip_count_after_eq(u32 a, u32 b)
9696{
9697 return !((a - b) & 0x80000000);
9698}
9699
9700static bool page_flip_finished(struct intel_crtc *crtc)
9701{
9702 struct drm_device *dev = crtc->base.dev;
9703 struct drm_i915_private *dev_priv = dev->dev_private;
9704
Ville Syrjäläbdfa7542014-05-27 21:33:09 +03009705 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
9706 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
9707 return true;
9708
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009709 /*
9710 * The relevant registers doen't exist on pre-ctg.
9711 * As the flip done interrupt doesn't trigger for mmio
9712 * flips on gmch platforms, a flip count check isn't
9713 * really needed there. But since ctg has the registers,
9714 * include it in the check anyway.
9715 */
9716 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9717 return true;
9718
9719 /*
9720 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9721 * used the same base address. In that case the mmio flip might
9722 * have completed, but the CS hasn't even executed the flip yet.
9723 *
9724 * A flip count check isn't enough as the CS might have updated
9725 * the base address just after start of vblank, but before we
9726 * managed to process the interrupt. This means we'd complete the
9727 * CS flip too soon.
9728 *
9729 * Combining both checks should get us a good enough result. It may
9730 * still happen that the CS flip has been executed, but has not
9731 * yet actually completed. But in case the base address is the same
9732 * anyway, we don't really care.
9733 */
9734 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9735 crtc->unpin_work->gtt_offset &&
9736 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9737 crtc->unpin_work->flip_count);
9738}
9739
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009740void intel_prepare_page_flip(struct drm_device *dev, int plane)
9741{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009742 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009743 struct intel_crtc *intel_crtc =
9744 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9745 unsigned long flags;
9746
Daniel Vetterf3260382014-09-15 14:55:23 +02009747
9748 /*
9749 * This is called both by irq handlers and the reset code (to complete
9750 * lost pageflips) so needs the full irqsave spinlocks.
9751 *
9752 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +00009753 * generate a page-flip completion irq, i.e. every modeset
9754 * is also accompanied by a spurious intel_prepare_page_flip().
9755 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009756 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009757 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +00009758 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009759 spin_unlock_irqrestore(&dev->event_lock, flags);
9760}
9761
Robin Schroereba905b2014-05-18 02:24:50 +02009762static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +00009763{
9764 /* Ensure that the work item is consistent when activating it ... */
9765 smp_wmb();
9766 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9767 /* and that it is marked active as soon as the irq could fire. */
9768 smp_wmb();
9769}
9770
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009771static int intel_gen2_queue_flip(struct drm_device *dev,
9772 struct drm_crtc *crtc,
9773 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009774 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009775 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009776 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009777{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009778 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009779 u32 flip_mask;
9780 int ret;
9781
Daniel Vetter6d90c952012-04-26 23:28:05 +02009782 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009783 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009784 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009785
9786 /* Can't queue multiple flips, so wait for the previous
9787 * one to finish before executing the next.
9788 */
9789 if (intel_crtc->plane)
9790 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9791 else
9792 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009793 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9794 intel_ring_emit(ring, MI_NOOP);
9795 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9796 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9797 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009798 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009799 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00009800
9801 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009802 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009803 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009804}
9805
9806static int intel_gen3_queue_flip(struct drm_device *dev,
9807 struct drm_crtc *crtc,
9808 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009809 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009810 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009811 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009812{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009813 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009814 u32 flip_mask;
9815 int ret;
9816
Daniel Vetter6d90c952012-04-26 23:28:05 +02009817 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009818 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009819 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009820
9821 if (intel_crtc->plane)
9822 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9823 else
9824 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009825 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9826 intel_ring_emit(ring, MI_NOOP);
9827 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9828 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9829 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009830 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009831 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009832
Chris Wilsone7d841c2012-12-03 11:36:30 +00009833 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009834 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009835 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009836}
9837
9838static int intel_gen4_queue_flip(struct drm_device *dev,
9839 struct drm_crtc *crtc,
9840 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009841 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009842 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009843 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009844{
9845 struct drm_i915_private *dev_priv = dev->dev_private;
9846 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9847 uint32_t pf, pipesrc;
9848 int ret;
9849
Daniel Vetter6d90c952012-04-26 23:28:05 +02009850 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009851 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009852 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009853
9854 /* i965+ uses the linear or tiled offsets from the
9855 * Display Registers (which do not change across a page-flip)
9856 * so we need only reprogram the base address.
9857 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02009858 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9859 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9860 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009861 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +02009862 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009863
9864 /* XXX Enabling the panel-fitter across page-flip is so far
9865 * untested on non-native modes, so ignore it for now.
9866 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9867 */
9868 pf = 0;
9869 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009870 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009871
9872 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009873 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009874 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009875}
9876
9877static int intel_gen6_queue_flip(struct drm_device *dev,
9878 struct drm_crtc *crtc,
9879 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009880 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009881 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009882 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009883{
9884 struct drm_i915_private *dev_priv = dev->dev_private;
9885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9886 uint32_t pf, pipesrc;
9887 int ret;
9888
Daniel Vetter6d90c952012-04-26 23:28:05 +02009889 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009890 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009891 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009892
Daniel Vetter6d90c952012-04-26 23:28:05 +02009893 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9894 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9895 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009896 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009897
Chris Wilson99d9acd2012-04-17 20:37:00 +01009898 /* Contrary to the suggestions in the documentation,
9899 * "Enable Panel Fitter" does not seem to be required when page
9900 * flipping with a non-native mode, and worse causes a normal
9901 * modeset to fail.
9902 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9903 */
9904 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009905 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009906 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009907
9908 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009909 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009910 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009911}
9912
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009913static int intel_gen7_queue_flip(struct drm_device *dev,
9914 struct drm_crtc *crtc,
9915 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009916 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009917 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009918 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009919{
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009920 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009921 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01009922 int len, ret;
9923
Robin Schroereba905b2014-05-18 02:24:50 +02009924 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009925 case PLANE_A:
9926 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9927 break;
9928 case PLANE_B:
9929 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9930 break;
9931 case PLANE_C:
9932 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9933 break;
9934 default:
9935 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009936 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009937 }
9938
Chris Wilsonffe74d72013-08-26 20:58:12 +01009939 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +01009940 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +01009941 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +01009942 /*
9943 * On Gen 8, SRM is now taking an extra dword to accommodate
9944 * 48bits addresses, and we need a NOOP for the batch size to
9945 * stay even.
9946 */
9947 if (IS_GEN8(dev))
9948 len += 2;
9949 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009950
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009951 /*
9952 * BSpec MI_DISPLAY_FLIP for IVB:
9953 * "The full packet must be contained within the same cache line."
9954 *
9955 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9956 * cacheline, if we ever start emitting more commands before
9957 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9958 * then do the cacheline alignment, and finally emit the
9959 * MI_DISPLAY_FLIP.
9960 */
9961 ret = intel_ring_cacheline_align(ring);
9962 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009963 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009964
Chris Wilsonffe74d72013-08-26 20:58:12 +01009965 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009966 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009967 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009968
Chris Wilsonffe74d72013-08-26 20:58:12 +01009969 /* Unmask the flip-done completion message. Note that the bspec says that
9970 * we should do this for both the BCS and RCS, and that we must not unmask
9971 * more than one flip event at any time (or ensure that one flip message
9972 * can be sent by waiting for flip-done prior to queueing new flips).
9973 * Experimentation says that BCS works despite DERRMR masking all
9974 * flip-done completion events and that unmasking all planes at once
9975 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9976 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9977 */
9978 if (ring->id == RCS) {
9979 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9980 intel_ring_emit(ring, DERRMR);
9981 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9982 DERRMR_PIPEB_PRI_FLIP_DONE |
9983 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +01009984 if (IS_GEN8(dev))
9985 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9986 MI_SRM_LRM_GLOBAL_GTT);
9987 else
9988 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9989 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +01009990 intel_ring_emit(ring, DERRMR);
9991 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +01009992 if (IS_GEN8(dev)) {
9993 intel_ring_emit(ring, 0);
9994 intel_ring_emit(ring, MI_NOOP);
9995 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009996 }
9997
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009998 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009999 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010000 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010001 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000010002
10003 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010004 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010005 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010006}
10007
Sourab Gupta84c33a62014-06-02 16:47:17 +053010008static bool use_mmio_flip(struct intel_engine_cs *ring,
10009 struct drm_i915_gem_object *obj)
10010{
10011 /*
10012 * This is not being used for older platforms, because
10013 * non-availability of flip done interrupt forces us to use
10014 * CS flips. Older platforms derive flip done using some clever
10015 * tricks involving the flip_pending status bits and vblank irqs.
10016 * So using MMIO flips there would disrupt this mechanism.
10017 */
10018
Chris Wilson8e09bf82014-07-08 10:40:30 +010010019 if (ring == NULL)
10020 return true;
10021
Sourab Gupta84c33a62014-06-02 16:47:17 +053010022 if (INTEL_INFO(ring->dev)->gen < 5)
10023 return false;
10024
10025 if (i915.use_mmio_flip < 0)
10026 return false;
10027 else if (i915.use_mmio_flip > 0)
10028 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010010029 else if (i915.enable_execlists)
10030 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010031 else
John Harrison41c52412014-11-24 18:49:43 +000010032 return ring != i915_gem_request_get_ring(obj->last_read_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010033}
10034
Damien Lespiauff944562014-11-20 14:58:16 +000010035static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10036{
10037 struct drm_device *dev = intel_crtc->base.dev;
10038 struct drm_i915_private *dev_priv = dev->dev_private;
10039 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
10040 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
10041 struct drm_i915_gem_object *obj = intel_fb->obj;
10042 const enum pipe pipe = intel_crtc->pipe;
10043 u32 ctl, stride;
10044
10045 ctl = I915_READ(PLANE_CTL(pipe, 0));
10046 ctl &= ~PLANE_CTL_TILED_MASK;
10047 if (obj->tiling_mode == I915_TILING_X)
10048 ctl |= PLANE_CTL_TILED_X;
10049
10050 /*
10051 * The stride is either expressed as a multiple of 64 bytes chunks for
10052 * linear buffers or in number of tiles for tiled buffers.
10053 */
10054 stride = fb->pitches[0] >> 6;
10055 if (obj->tiling_mode == I915_TILING_X)
10056 stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
10057
10058 /*
10059 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10060 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10061 */
10062 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10063 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10064
10065 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10066 POSTING_READ(PLANE_SURF(pipe, 0));
10067}
10068
10069static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010070{
10071 struct drm_device *dev = intel_crtc->base.dev;
10072 struct drm_i915_private *dev_priv = dev->dev_private;
10073 struct intel_framebuffer *intel_fb =
10074 to_intel_framebuffer(intel_crtc->base.primary->fb);
10075 struct drm_i915_gem_object *obj = intel_fb->obj;
10076 u32 dspcntr;
10077 u32 reg;
10078
Sourab Gupta84c33a62014-06-02 16:47:17 +053010079 reg = DSPCNTR(intel_crtc->plane);
10080 dspcntr = I915_READ(reg);
10081
Damien Lespiauc5d97472014-10-25 00:11:11 +010010082 if (obj->tiling_mode != I915_TILING_NONE)
10083 dspcntr |= DISPPLANE_TILED;
10084 else
10085 dspcntr &= ~DISPPLANE_TILED;
10086
Sourab Gupta84c33a62014-06-02 16:47:17 +053010087 I915_WRITE(reg, dspcntr);
10088
10089 I915_WRITE(DSPSURF(intel_crtc->plane),
10090 intel_crtc->unpin_work->gtt_offset);
10091 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010092
Damien Lespiauff944562014-11-20 14:58:16 +000010093}
10094
10095/*
10096 * XXX: This is the temporary way to update the plane registers until we get
10097 * around to using the usual plane update functions for MMIO flips
10098 */
10099static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10100{
10101 struct drm_device *dev = intel_crtc->base.dev;
10102 bool atomic_update;
10103 u32 start_vbl_count;
10104
10105 intel_mark_page_flip_active(intel_crtc);
10106
10107 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10108
10109 if (INTEL_INFO(dev)->gen >= 9)
10110 skl_do_mmio_flip(intel_crtc);
10111 else
10112 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10113 ilk_do_mmio_flip(intel_crtc);
10114
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010115 if (atomic_update)
10116 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010117}
10118
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010119static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010120{
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010121 struct intel_crtc *crtc =
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010122 container_of(work, struct intel_crtc, mmio_flip.work);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010123 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010124
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010125 mmio_flip = &crtc->mmio_flip;
10126 if (mmio_flip->req)
John Harrison9c654812014-11-24 18:49:35 +000010127 WARN_ON(__i915_wait_request(mmio_flip->req,
10128 crtc->reset_counter,
10129 false, NULL, NULL) != 0);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010130
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010131 intel_do_mmio_flip(crtc);
10132 if (mmio_flip->req) {
10133 mutex_lock(&crtc->base.dev->struct_mutex);
John Harrison146d84f2014-12-05 13:49:33 +000010134 i915_gem_request_assign(&mmio_flip->req, NULL);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010135 mutex_unlock(&crtc->base.dev->struct_mutex);
10136 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053010137}
10138
10139static int intel_queue_mmio_flip(struct drm_device *dev,
10140 struct drm_crtc *crtc,
10141 struct drm_framebuffer *fb,
10142 struct drm_i915_gem_object *obj,
10143 struct intel_engine_cs *ring,
10144 uint32_t flags)
10145{
Sourab Gupta84c33a62014-06-02 16:47:17 +053010146 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010147
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010148 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
10149 obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010150
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020010151 schedule_work(&intel_crtc->mmio_flip.work);
10152
Sourab Gupta84c33a62014-06-02 16:47:17 +053010153 return 0;
10154}
10155
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010156static int intel_default_queue_flip(struct drm_device *dev,
10157 struct drm_crtc *crtc,
10158 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010159 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010160 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010161 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010162{
10163 return -ENODEV;
10164}
10165
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010166static bool __intel_pageflip_stall_check(struct drm_device *dev,
10167 struct drm_crtc *crtc)
10168{
10169 struct drm_i915_private *dev_priv = dev->dev_private;
10170 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10171 struct intel_unpin_work *work = intel_crtc->unpin_work;
10172 u32 addr;
10173
10174 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10175 return true;
10176
10177 if (!work->enable_stall_check)
10178 return false;
10179
10180 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010010181 if (work->flip_queued_req &&
10182 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010183 return false;
10184
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010185 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010186 }
10187
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010188 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010189 return false;
10190
10191 /* Potential stall - if we see that the flip has happened,
10192 * assume a missed interrupt. */
10193 if (INTEL_INFO(dev)->gen >= 4)
10194 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10195 else
10196 addr = I915_READ(DSPADDR(intel_crtc->plane));
10197
10198 /* There is a potential issue here with a false positive after a flip
10199 * to the same address. We could address this by checking for a
10200 * non-incrementing frame counter.
10201 */
10202 return addr == work->gtt_offset;
10203}
10204
10205void intel_check_page_flip(struct drm_device *dev, int pipe)
10206{
10207 struct drm_i915_private *dev_priv = dev->dev_private;
10208 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10209 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010210 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020010211
Dave Gordon6c51d462015-03-06 15:34:26 +000010212 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010213
10214 if (crtc == NULL)
10215 return;
10216
Daniel Vetterf3260382014-09-15 14:55:23 +020010217 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010218 work = intel_crtc->unpin_work;
10219 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010220 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010010221 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010222 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010223 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010224 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010010225 if (work != NULL &&
10226 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10227 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020010228 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010229}
10230
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010231static int intel_crtc_page_flip(struct drm_crtc *crtc,
10232 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010233 struct drm_pending_vblank_event *event,
10234 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010235{
10236 struct drm_device *dev = crtc->dev;
10237 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070010238 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070010239 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010240 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080010241 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020010242 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010243 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010244 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010245 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010010246 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010247
Matt Roper2ff8fde2014-07-08 07:50:07 -070010248 /*
10249 * drm_mode_page_flip_ioctl() should already catch this, but double
10250 * check to be safe. In the future we may enable pageflipping from
10251 * a disabled primary plane.
10252 */
10253 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10254 return -EBUSY;
10255
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010256 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070010257 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010258 return -EINVAL;
10259
10260 /*
10261 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10262 * Note that pitch changes could also affect these register.
10263 */
10264 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070010265 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10266 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010267 return -EINVAL;
10268
Chris Wilsonf900db42014-02-20 09:26:13 +000010269 if (i915_terminally_wedged(&dev_priv->gpu_error))
10270 goto out_hang;
10271
Daniel Vetterb14c5672013-09-19 12:18:32 +020010272 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010273 if (work == NULL)
10274 return -ENOMEM;
10275
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010276 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010277 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010278 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010279 INIT_WORK(&work->work, intel_unpin_work_fn);
10280
Daniel Vetter87b6b102014-05-15 15:33:46 +020010281 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010282 if (ret)
10283 goto free_work;
10284
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010285 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010286 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010287 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010288 /* Before declaring the flip queue wedged, check if
10289 * the hardware completed the operation behind our backs.
10290 */
10291 if (__intel_pageflip_stall_check(dev, crtc)) {
10292 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10293 page_flip_completed(intel_crtc);
10294 } else {
10295 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010296 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010010297
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010298 drm_crtc_vblank_put(crtc);
10299 kfree(work);
10300 return -EBUSY;
10301 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010302 }
10303 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010304 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010305
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010306 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10307 flush_workqueue(dev_priv->wq);
10308
Jesse Barnes75dfca82010-02-10 15:09:44 -080010309 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010310 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010311 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010312
Matt Roperf4510a22014-04-01 15:22:40 -070010313 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010314 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080010315
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010316 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010317
Chris Wilson89ed88b2015-02-16 14:31:49 +000010318 ret = i915_mutex_lock_interruptible(dev);
10319 if (ret)
10320 goto cleanup;
10321
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010322 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020010323 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010324
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010325 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020010326 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010327
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010328 if (IS_VALLEYVIEW(dev)) {
10329 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010330 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010010331 /* vlv: DISPLAY_FLIP fails to change tiling */
10332 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000010333 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010010334 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010335 } else if (INTEL_INFO(dev)->gen >= 7) {
John Harrison41c52412014-11-24 18:49:43 +000010336 ring = i915_gem_request_get_ring(obj->last_read_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010337 if (ring == NULL || ring->id != RCS)
10338 ring = &dev_priv->ring[BCS];
10339 } else {
10340 ring = &dev_priv->ring[RCS];
10341 }
10342
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010343 mmio_flip = use_mmio_flip(ring, obj);
10344
10345 /* When using CS flips, we want to emit semaphores between rings.
10346 * However, when using mmio flips we will create a task to do the
10347 * synchronisation, so all we want here is to pin the framebuffer
10348 * into the display plane and skip any waits.
10349 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010350 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010351 crtc->primary->state,
10352 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010353 if (ret)
10354 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010355
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000010356 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
10357 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010358
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010359 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010360 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10361 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010362 if (ret)
10363 goto cleanup_unpin;
10364
John Harrisonf06cc1b2014-11-24 18:49:37 +000010365 i915_gem_request_assign(&work->flip_queued_req,
10366 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010367 } else {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010368 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010369 page_flip_flags);
10370 if (ret)
10371 goto cleanup_unpin;
10372
John Harrisonf06cc1b2014-11-24 18:49:37 +000010373 i915_gem_request_assign(&work->flip_queued_req,
10374 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010375 }
10376
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010377 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010378 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010379
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010380 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020010381 INTEL_FRONTBUFFER_PRIMARY(pipe));
10382
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010383 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010384 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010385 mutex_unlock(&dev->struct_mutex);
10386
Jesse Barnese5510fa2010-07-01 16:48:37 -070010387 trace_i915_flip_request(intel_crtc->plane, obj);
10388
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010389 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010010390
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010391cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010392 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010393cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010394 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010395 mutex_unlock(&dev->struct_mutex);
10396cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070010397 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010398 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010010399
Chris Wilson89ed88b2015-02-16 14:31:49 +000010400 drm_gem_object_unreference_unlocked(&obj->base);
10401 drm_framebuffer_unreference(work->old_fb);
10402
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010403 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010404 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010405 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010406
Daniel Vetter87b6b102014-05-15 15:33:46 +020010407 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010408free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010010409 kfree(work);
10410
Chris Wilsonf900db42014-02-20 09:26:13 +000010411 if (ret == -EIO) {
10412out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080010413 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010414 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010415 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020010416 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010417 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010418 }
Chris Wilsonf900db42014-02-20 09:26:13 +000010419 }
Chris Wilson96b099f2010-06-07 14:03:04 +010010420 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010421}
10422
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010423static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010424 .mode_set_base_atomic = intel_pipe_set_base_atomic,
10425 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080010426 .atomic_begin = intel_begin_crtc_commit,
10427 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010428};
10429
Daniel Vetter9a935852012-07-05 22:34:27 +020010430/**
10431 * intel_modeset_update_staged_output_state
10432 *
10433 * Updates the staged output configuration state, e.g. after we've read out the
10434 * current hw state.
10435 */
10436static void intel_modeset_update_staged_output_state(struct drm_device *dev)
10437{
Ville Syrjälä76688512014-01-10 11:28:06 +020010438 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010439 struct intel_encoder *encoder;
10440 struct intel_connector *connector;
10441
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010442 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010443 connector->new_encoder =
10444 to_intel_encoder(connector->base.encoder);
10445 }
10446
Damien Lespiaub2784e12014-08-05 11:29:37 +010010447 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010448 encoder->new_crtc =
10449 to_intel_crtc(encoder->base.crtc);
10450 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010451
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010452 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010453 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020010454 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010455}
10456
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010457/* Transitional helper to copy current connector/encoder state to
10458 * connector->state. This is needed so that code that is partially
10459 * converted to atomic does the right thing.
10460 */
10461static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
10462{
10463 struct intel_connector *connector;
10464
10465 for_each_intel_connector(dev, connector) {
10466 if (connector->base.encoder) {
10467 connector->base.state->best_encoder =
10468 connector->base.encoder;
10469 connector->base.state->crtc =
10470 connector->base.encoder->crtc;
10471 } else {
10472 connector->base.state->best_encoder = NULL;
10473 connector->base.state->crtc = NULL;
10474 }
10475 }
10476}
10477
Daniel Vetter9a935852012-07-05 22:34:27 +020010478/**
10479 * intel_modeset_commit_output_state
10480 *
10481 * This function copies the stage display pipe configuration to the real one.
10482 */
10483static void intel_modeset_commit_output_state(struct drm_device *dev)
10484{
Ville Syrjälä76688512014-01-10 11:28:06 +020010485 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010486 struct intel_encoder *encoder;
10487 struct intel_connector *connector;
10488
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010489 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010490 connector->base.encoder = &connector->new_encoder->base;
10491 }
10492
Damien Lespiaub2784e12014-08-05 11:29:37 +010010493 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010494 encoder->base.crtc = &encoder->new_crtc->base;
10495 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010496
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010497 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010498 crtc->base.state->enable = crtc->new_enabled;
Ville Syrjälä76688512014-01-10 11:28:06 +020010499 crtc->base.enabled = crtc->new_enabled;
10500 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010501
10502 intel_modeset_update_connector_atomic_state(dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020010503}
10504
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010505static void
Robin Schroereba905b2014-05-18 02:24:50 +020010506connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010507 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010508{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010509 int bpp = pipe_config->pipe_bpp;
10510
10511 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10512 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010513 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010514
10515 /* Don't use an invalid EDID bpc value */
10516 if (connector->base.display_info.bpc &&
10517 connector->base.display_info.bpc * 3 < bpp) {
10518 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10519 bpp, connector->base.display_info.bpc*3);
10520 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10521 }
10522
10523 /* Clamp bpp to 8 on screens without EDID 1.4 */
10524 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10525 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10526 bpp);
10527 pipe_config->pipe_bpp = 24;
10528 }
10529}
10530
10531static int
10532compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10533 struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010534 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010535{
10536 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010537 struct drm_atomic_state *state;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010538 struct intel_connector *connector;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010539 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010540
Daniel Vetterd42264b2013-03-28 16:38:08 +010010541 switch (fb->pixel_format) {
10542 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010543 bpp = 8*3; /* since we go through a colormap */
10544 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010545 case DRM_FORMAT_XRGB1555:
10546 case DRM_FORMAT_ARGB1555:
10547 /* checked in intel_framebuffer_init already */
10548 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10549 return -EINVAL;
10550 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010551 bpp = 6*3; /* min is 18bpp */
10552 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010553 case DRM_FORMAT_XBGR8888:
10554 case DRM_FORMAT_ABGR8888:
10555 /* checked in intel_framebuffer_init already */
10556 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10557 return -EINVAL;
10558 case DRM_FORMAT_XRGB8888:
10559 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010560 bpp = 8*3;
10561 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010562 case DRM_FORMAT_XRGB2101010:
10563 case DRM_FORMAT_ARGB2101010:
10564 case DRM_FORMAT_XBGR2101010:
10565 case DRM_FORMAT_ABGR2101010:
10566 /* checked in intel_framebuffer_init already */
10567 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +010010568 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010569 bpp = 10*3;
10570 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +010010571 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010572 default:
10573 DRM_DEBUG_KMS("unsupported depth\n");
10574 return -EINVAL;
10575 }
10576
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010577 pipe_config->pipe_bpp = bpp;
10578
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010579 state = pipe_config->base.state;
10580
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010581 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010582 for (i = 0; i < state->num_connector; i++) {
10583 if (!state->connectors[i])
10584 continue;
10585
10586 connector = to_intel_connector(state->connectors[i]);
10587 if (state->connector_states[i]->crtc != &crtc->base)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010588 continue;
10589
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010590 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010591 }
10592
10593 return bpp;
10594}
10595
Daniel Vetter644db712013-09-19 14:53:58 +020010596static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10597{
10598 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10599 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010010600 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020010601 mode->crtc_hdisplay, mode->crtc_hsync_start,
10602 mode->crtc_hsync_end, mode->crtc_htotal,
10603 mode->crtc_vdisplay, mode->crtc_vsync_start,
10604 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10605}
10606
Daniel Vetterc0b03412013-05-28 12:05:54 +020010607static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010608 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020010609 const char *context)
10610{
10611 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10612 context, pipe_name(crtc->pipe));
10613
10614 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10615 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10616 pipe_config->pipe_bpp, pipe_config->dither);
10617 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10618 pipe_config->has_pch_encoder,
10619 pipe_config->fdi_lanes,
10620 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10621 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10622 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010623 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10624 pipe_config->has_dp_encoder,
10625 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10626 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10627 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010628
10629 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10630 pipe_config->has_dp_encoder,
10631 pipe_config->dp_m2_n2.gmch_m,
10632 pipe_config->dp_m2_n2.gmch_n,
10633 pipe_config->dp_m2_n2.link_m,
10634 pipe_config->dp_m2_n2.link_n,
10635 pipe_config->dp_m2_n2.tu);
10636
Daniel Vetter55072d12014-11-20 16:10:28 +010010637 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
10638 pipe_config->has_audio,
10639 pipe_config->has_infoframe);
10640
Daniel Vetterc0b03412013-05-28 12:05:54 +020010641 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010642 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010643 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010644 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
10645 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030010646 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010647 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10648 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010649 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10650 pipe_config->gmch_pfit.control,
10651 pipe_config->gmch_pfit.pgm_ratios,
10652 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010653 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020010654 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010655 pipe_config->pch_pfit.size,
10656 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010657 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030010658 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010659}
10660
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010661static bool encoders_cloneable(const struct intel_encoder *a,
10662 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010663{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010664 /* masks could be asymmetric, so check both ways */
10665 return a == b || (a->cloneable & (1 << b->type) &&
10666 b->cloneable & (1 << a->type));
10667}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010668
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010669static bool check_single_encoder_cloning(struct drm_atomic_state *state,
10670 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010671 struct intel_encoder *encoder)
10672{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010673 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010674 struct drm_connector_state *connector_state;
10675 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010676
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010677 for (i = 0; i < state->num_connector; i++) {
10678 if (!state->connectors[i])
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010679 continue;
10680
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010681 connector_state = state->connector_states[i];
10682 if (connector_state->crtc != &crtc->base)
10683 continue;
10684
10685 source_encoder =
10686 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010687 if (!encoders_cloneable(encoder, source_encoder))
10688 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010689 }
10690
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010691 return true;
10692}
10693
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010694static bool check_encoder_cloning(struct drm_atomic_state *state,
10695 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010696{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010697 struct intel_encoder *encoder;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010698 struct drm_connector_state *connector_state;
10699 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010700
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010701 for (i = 0; i < state->num_connector; i++) {
10702 if (!state->connectors[i])
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010703 continue;
10704
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010705 connector_state = state->connector_states[i];
10706 if (connector_state->crtc != &crtc->base)
10707 continue;
10708
10709 encoder = to_intel_encoder(connector_state->best_encoder);
10710 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010711 return false;
10712 }
10713
10714 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010715}
10716
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010717static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010718{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010719 struct drm_device *dev = state->dev;
10720 struct intel_encoder *encoder;
10721 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010722 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010723 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010724
10725 /*
10726 * Walk the connector list instead of the encoder
10727 * list to detect the problem on ddi platforms
10728 * where there's just one encoder per digital port.
10729 */
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010730 for (i = 0; i < state->num_connector; i++) {
10731 if (!state->connectors[i])
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010732 continue;
10733
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010734 connector_state = state->connector_states[i];
10735 if (!connector_state->best_encoder)
10736 continue;
10737
10738 encoder = to_intel_encoder(connector_state->best_encoder);
10739
10740 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010741
10742 switch (encoder->type) {
10743 unsigned int port_mask;
10744 case INTEL_OUTPUT_UNKNOWN:
10745 if (WARN_ON(!HAS_DDI(dev)))
10746 break;
10747 case INTEL_OUTPUT_DISPLAYPORT:
10748 case INTEL_OUTPUT_HDMI:
10749 case INTEL_OUTPUT_EDP:
10750 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
10751
10752 /* the same port mustn't appear more than once */
10753 if (used_ports & port_mask)
10754 return false;
10755
10756 used_ports |= port_mask;
10757 default:
10758 break;
10759 }
10760 }
10761
10762 return true;
10763}
10764
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010765static void
10766clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
10767{
10768 struct drm_crtc_state tmp_state;
10769
10770 /* Clear only the intel specific part of the crtc state */
10771 tmp_state = crtc_state->base;
10772 memset(crtc_state, 0, sizeof *crtc_state);
10773 crtc_state->base = tmp_state;
10774}
10775
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010776static struct intel_crtc_state *
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010777intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010778 struct drm_framebuffer *fb,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010779 struct drm_display_mode *mode,
10780 struct drm_atomic_state *state)
Daniel Vetter7758a112012-07-08 19:40:39 +020010781{
Daniel Vetter7758a112012-07-08 19:40:39 +020010782 struct intel_encoder *encoder;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020010783 struct intel_connector *connector;
10784 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010785 struct intel_crtc_state *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +010010786 int plane_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020010787 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010010788 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020010789
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030010790 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010791 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10792 return ERR_PTR(-EINVAL);
10793 }
10794
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010795 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010796 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
10797 return ERR_PTR(-EINVAL);
10798 }
10799
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010800 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
10801 if (IS_ERR(pipe_config))
10802 return pipe_config;
10803
10804 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020010805
Matt Roper07878242015-02-25 11:43:26 -080010806 pipe_config->base.crtc = crtc;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010807 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
10808 drm_mode_copy(&pipe_config->base.mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010809
Daniel Vettere143a212013-07-04 12:01:15 +020010810 pipe_config->cpu_transcoder =
10811 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010812 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010813
Imre Deak2960bc92013-07-30 13:36:32 +030010814 /*
10815 * Sanitize sync polarity flags based on requested ones. If neither
10816 * positive or negative polarity is requested, treat this as meaning
10817 * negative polarity.
10818 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010819 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030010820 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010821 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030010822
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010823 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030010824 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010825 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030010826
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010827 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10828 * plane pixel format and any sink constraints into account. Returns the
10829 * source plane bpp so that dithering can be selected on mismatches
10830 * after encoders and crtc also have had their say. */
10831 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10832 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010833 if (plane_bpp < 0)
10834 goto fail;
10835
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030010836 /*
10837 * Determine the real pipe dimensions. Note that stereo modes can
10838 * increase the actual pipe size due to the frame doubling and
10839 * insertion of additional space for blanks between the frame. This
10840 * is stored in the crtc timings. We use the requested mode to do this
10841 * computation to clearly distinguish it from the adjusted mode, which
10842 * can be changed by the connectors in the below retry loop.
10843 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010844 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080010845 &pipe_config->pipe_src_w,
10846 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030010847
Daniel Vettere29c22c2013-02-21 00:00:16 +010010848encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020010849 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020010850 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020010851 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010852
Daniel Vetter135c81b2013-07-21 21:37:09 +020010853 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010854 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
10855 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020010856
Daniel Vetter7758a112012-07-08 19:40:39 +020010857 /* Pass our mode to the connectors and the CRTC to give them a chance to
10858 * adjust it according to limitations or connector properties, and also
10859 * a chance to reject the mode entirely.
10860 */
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020010861 for (i = 0; i < state->num_connector; i++) {
10862 connector = to_intel_connector(state->connectors[i]);
10863 if (!connector)
Daniel Vetter7758a112012-07-08 19:40:39 +020010864 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +010010865
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020010866 connector_state = state->connector_states[i];
10867 if (connector_state->crtc != crtc)
10868 continue;
10869
10870 encoder = to_intel_encoder(connector_state->best_encoder);
10871
Daniel Vetterefea6e82013-07-21 21:36:59 +020010872 if (!(encoder->compute_config(encoder, pipe_config))) {
10873 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020010874 goto fail;
10875 }
10876 }
10877
Daniel Vetterff9a6752013-06-01 17:16:21 +020010878 /* Set default port clock if not overwritten by the encoder. Needs to be
10879 * done afterwards in case the encoder adjusts the mode. */
10880 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010881 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010010882 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010883
Daniel Vettera43f6e02013-06-07 23:10:32 +020010884 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010010885 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020010886 DRM_DEBUG_KMS("CRTC fixup failed\n");
10887 goto fail;
10888 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010010889
10890 if (ret == RETRY) {
10891 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10892 ret = -EINVAL;
10893 goto fail;
10894 }
10895
10896 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10897 retry = false;
10898 goto encoder_retry;
10899 }
10900
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010901 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10902 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10903 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10904
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010905 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020010906fail:
Daniel Vettere29c22c2013-02-21 00:00:16 +010010907 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +020010908}
10909
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010910/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10911 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10912static void
10913intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10914 unsigned *prepare_pipes, unsigned *disable_pipes)
10915{
10916 struct intel_crtc *intel_crtc;
10917 struct drm_device *dev = crtc->dev;
10918 struct intel_encoder *encoder;
10919 struct intel_connector *connector;
10920 struct drm_crtc *tmp_crtc;
10921
10922 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
10923
10924 /* Check which crtcs have changed outputs connected to them, these need
10925 * to be part of the prepare_pipes mask. We don't (yet) support global
10926 * modeset across multiple crtcs, so modeset_pipes will only have one
10927 * bit set at most. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010928 for_each_intel_connector(dev, connector) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010929 if (connector->base.encoder == &connector->new_encoder->base)
10930 continue;
10931
10932 if (connector->base.encoder) {
10933 tmp_crtc = connector->base.encoder->crtc;
10934
10935 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10936 }
10937
10938 if (connector->new_encoder)
10939 *prepare_pipes |=
10940 1 << connector->new_encoder->new_crtc->pipe;
10941 }
10942
Damien Lespiaub2784e12014-08-05 11:29:37 +010010943 for_each_intel_encoder(dev, encoder) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010944 if (encoder->base.crtc == &encoder->new_crtc->base)
10945 continue;
10946
10947 if (encoder->base.crtc) {
10948 tmp_crtc = encoder->base.crtc;
10949
10950 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10951 }
10952
10953 if (encoder->new_crtc)
10954 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
10955 }
10956
Ville Syrjälä76688512014-01-10 11:28:06 +020010957 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010958 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010959 if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010960 continue;
10961
Ville Syrjälä76688512014-01-10 11:28:06 +020010962 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010963 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +020010964 else
10965 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010966 }
10967
10968
10969 /* set_mode is also used to update properties on life display pipes. */
10970 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +020010971 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010972 *prepare_pipes |= 1 << intel_crtc->pipe;
10973
Daniel Vetterb6c51642013-04-12 18:48:43 +020010974 /*
10975 * For simplicity do a full modeset on any pipe where the output routing
10976 * changed. We could be more clever, but that would require us to be
10977 * more careful with calling the relevant encoder->mode_set functions.
10978 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010979 if (*prepare_pipes)
10980 *modeset_pipes = *prepare_pipes;
10981
10982 /* ... and mask these out. */
10983 *modeset_pipes &= ~(*disable_pipes);
10984 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020010985
10986 /*
10987 * HACK: We don't (yet) fully support global modesets. intel_set_config
10988 * obies this rule, but the modeset restore mode of
10989 * intel_modeset_setup_hw_state does not.
10990 */
10991 *modeset_pipes &= 1 << intel_crtc->pipe;
10992 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020010993
10994 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10995 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010996}
10997
Daniel Vetterea9d7582012-07-10 10:42:52 +020010998static bool intel_crtc_in_use(struct drm_crtc *crtc)
10999{
11000 struct drm_encoder *encoder;
11001 struct drm_device *dev = crtc->dev;
11002
11003 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11004 if (encoder->crtc == crtc)
11005 return true;
11006
11007 return false;
11008}
11009
11010static void
11011intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
11012{
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011013 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011014 struct intel_encoder *intel_encoder;
11015 struct intel_crtc *intel_crtc;
11016 struct drm_connector *connector;
11017
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011018 intel_shared_dpll_commit(dev_priv);
11019
Damien Lespiaub2784e12014-08-05 11:29:37 +010011020 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020011021 if (!intel_encoder->base.crtc)
11022 continue;
11023
11024 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
11025
11026 if (prepare_pipes & (1 << intel_crtc->pipe))
11027 intel_encoder->connectors_active = false;
11028 }
11029
11030 intel_modeset_commit_output_state(dev);
11031
Ville Syrjälä76688512014-01-10 11:28:06 +020011032 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011033 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011034 WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
Daniel Vetterea9d7582012-07-10 10:42:52 +020011035 }
11036
11037 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11038 if (!connector->encoder || !connector->encoder->crtc)
11039 continue;
11040
11041 intel_crtc = to_intel_crtc(connector->encoder->crtc);
11042
11043 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020011044 struct drm_property *dpms_property =
11045 dev->mode_config.dpms_property;
11046
Daniel Vetterea9d7582012-07-10 10:42:52 +020011047 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050011048 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020011049 dpms_property,
11050 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011051
11052 intel_encoder = to_intel_encoder(connector->encoder);
11053 intel_encoder->connectors_active = true;
11054 }
11055 }
11056
11057}
11058
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011059static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011060{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011061 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011062
11063 if (clock1 == clock2)
11064 return true;
11065
11066 if (!clock1 || !clock2)
11067 return false;
11068
11069 diff = abs(clock1 - clock2);
11070
11071 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11072 return true;
11073
11074 return false;
11075}
11076
Daniel Vetter25c5b262012-07-08 22:08:04 +020011077#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11078 list_for_each_entry((intel_crtc), \
11079 &(dev)->mode_config.crtc_list, \
11080 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020011081 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020011082
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011083static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011084intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011085 struct intel_crtc_state *current_config,
11086 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011087{
Daniel Vetter66e985c2013-06-05 13:34:20 +020011088#define PIPE_CONF_CHECK_X(name) \
11089 if (current_config->name != pipe_config->name) { \
11090 DRM_ERROR("mismatch in " #name " " \
11091 "(expected 0x%08x, found 0x%08x)\n", \
11092 current_config->name, \
11093 pipe_config->name); \
11094 return false; \
11095 }
11096
Daniel Vetter08a24032013-04-19 11:25:34 +020011097#define PIPE_CONF_CHECK_I(name) \
11098 if (current_config->name != pipe_config->name) { \
11099 DRM_ERROR("mismatch in " #name " " \
11100 "(expected %i, found %i)\n", \
11101 current_config->name, \
11102 pipe_config->name); \
11103 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011104 }
11105
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011106/* This is required for BDW+ where there is only one set of registers for
11107 * switching between high and low RR.
11108 * This macro can be used whenever a comparison has to be made between one
11109 * hw state and multiple sw state variables.
11110 */
11111#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11112 if ((current_config->name != pipe_config->name) && \
11113 (current_config->alt_name != pipe_config->name)) { \
11114 DRM_ERROR("mismatch in " #name " " \
11115 "(expected %i or %i, found %i)\n", \
11116 current_config->name, \
11117 current_config->alt_name, \
11118 pipe_config->name); \
11119 return false; \
11120 }
11121
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011122#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11123 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070011124 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011125 "(expected %i, found %i)\n", \
11126 current_config->name & (mask), \
11127 pipe_config->name & (mask)); \
11128 return false; \
11129 }
11130
Ville Syrjälä5e550652013-09-06 23:29:07 +030011131#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11132 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11133 DRM_ERROR("mismatch in " #name " " \
11134 "(expected %i, found %i)\n", \
11135 current_config->name, \
11136 pipe_config->name); \
11137 return false; \
11138 }
11139
Daniel Vetterbb760062013-06-06 14:55:52 +020011140#define PIPE_CONF_QUIRK(quirk) \
11141 ((current_config->quirks | pipe_config->quirks) & (quirk))
11142
Daniel Vettereccb1402013-05-22 00:50:22 +020011143 PIPE_CONF_CHECK_I(cpu_transcoder);
11144
Daniel Vetter08a24032013-04-19 11:25:34 +020011145 PIPE_CONF_CHECK_I(has_pch_encoder);
11146 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020011147 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11148 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11149 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11150 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11151 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020011152
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011153 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011154
11155 if (INTEL_INFO(dev)->gen < 8) {
11156 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11157 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11158 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11159 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11160 PIPE_CONF_CHECK_I(dp_m_n.tu);
11161
11162 if (current_config->has_drrs) {
11163 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11164 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11165 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11166 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11167 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11168 }
11169 } else {
11170 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11171 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11172 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11173 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11174 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11175 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011176
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011177 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11178 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11179 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11180 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11181 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11182 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011183
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011184 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11185 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11186 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11187 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11188 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11189 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011190
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011191 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020011192 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020011193 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11194 IS_VALLEYVIEW(dev))
11195 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080011196 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011197
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011198 PIPE_CONF_CHECK_I(has_audio);
11199
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011200 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011201 DRM_MODE_FLAG_INTERLACE);
11202
Daniel Vetterbb760062013-06-06 14:55:52 +020011203 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011204 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011205 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011206 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011207 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011208 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011209 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011210 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011211 DRM_MODE_FLAG_NVSYNC);
11212 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011213
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011214 PIPE_CONF_CHECK_I(pipe_src_w);
11215 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011216
Daniel Vetter99535992014-04-13 12:00:33 +020011217 /*
11218 * FIXME: BIOS likes to set up a cloned config with lvds+external
11219 * screen. Since we don't yet re-compute the pipe config when moving
11220 * just the lvds port away to another pipe the sw tracking won't match.
11221 *
11222 * Proper atomic modesets with recomputed global state will fix this.
11223 * Until then just don't check gmch state for inherited modes.
11224 */
11225 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11226 PIPE_CONF_CHECK_I(gmch_pfit.control);
11227 /* pfit ratios are autocomputed by the hw on gen4+ */
11228 if (INTEL_INFO(dev)->gen < 4)
11229 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11230 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11231 }
11232
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011233 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11234 if (current_config->pch_pfit.enabled) {
11235 PIPE_CONF_CHECK_I(pch_pfit.pos);
11236 PIPE_CONF_CHECK_I(pch_pfit.size);
11237 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011238
Jesse Barnese59150d2014-01-07 13:30:45 -080011239 /* BDW+ don't expose a synchronous way to read the state */
11240 if (IS_HASWELL(dev))
11241 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011242
Ville Syrjälä282740f2013-09-04 18:30:03 +030011243 PIPE_CONF_CHECK_I(double_wide);
11244
Daniel Vetter26804af2014-06-25 22:01:55 +030011245 PIPE_CONF_CHECK_X(ddi_pll_sel);
11246
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011247 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011248 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011249 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011250 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11251 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011252 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011253 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11254 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11255 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011256
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011257 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11258 PIPE_CONF_CHECK_I(pipe_bpp);
11259
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011260 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011261 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011262
Daniel Vetter66e985c2013-06-05 13:34:20 +020011263#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011264#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011265#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011266#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011267#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011268#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011269
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011270 return true;
11271}
11272
Damien Lespiau08db6652014-11-04 17:06:52 +000011273static void check_wm_state(struct drm_device *dev)
11274{
11275 struct drm_i915_private *dev_priv = dev->dev_private;
11276 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11277 struct intel_crtc *intel_crtc;
11278 int plane;
11279
11280 if (INTEL_INFO(dev)->gen < 9)
11281 return;
11282
11283 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11284 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11285
11286 for_each_intel_crtc(dev, intel_crtc) {
11287 struct skl_ddb_entry *hw_entry, *sw_entry;
11288 const enum pipe pipe = intel_crtc->pipe;
11289
11290 if (!intel_crtc->active)
11291 continue;
11292
11293 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000011294 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000011295 hw_entry = &hw_ddb.plane[pipe][plane];
11296 sw_entry = &sw_ddb->plane[pipe][plane];
11297
11298 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11299 continue;
11300
11301 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11302 "(expected (%u,%u), found (%u,%u))\n",
11303 pipe_name(pipe), plane + 1,
11304 sw_entry->start, sw_entry->end,
11305 hw_entry->start, hw_entry->end);
11306 }
11307
11308 /* cursor */
11309 hw_entry = &hw_ddb.cursor[pipe];
11310 sw_entry = &sw_ddb->cursor[pipe];
11311
11312 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11313 continue;
11314
11315 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11316 "(expected (%u,%u), found (%u,%u))\n",
11317 pipe_name(pipe),
11318 sw_entry->start, sw_entry->end,
11319 hw_entry->start, hw_entry->end);
11320 }
11321}
11322
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011323static void
11324check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011325{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011326 struct intel_connector *connector;
11327
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011328 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011329 /* This also checks the encoder/connector hw state with the
11330 * ->get_hw_state callbacks. */
11331 intel_connector_check_state(connector);
11332
Rob Clarke2c719b2014-12-15 13:56:32 -050011333 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011334 "connector's staged encoder doesn't match current encoder\n");
11335 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011336}
11337
11338static void
11339check_encoder_state(struct drm_device *dev)
11340{
11341 struct intel_encoder *encoder;
11342 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011343
Damien Lespiaub2784e12014-08-05 11:29:37 +010011344 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011345 bool enabled = false;
11346 bool active = false;
11347 enum pipe pipe, tracked_pipe;
11348
11349 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11350 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011351 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011352
Rob Clarke2c719b2014-12-15 13:56:32 -050011353 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011354 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011355 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011356 "encoder's active_connectors set, but no crtc\n");
11357
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011358 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011359 if (connector->base.encoder != &encoder->base)
11360 continue;
11361 enabled = true;
11362 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11363 active = true;
11364 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011365 /*
11366 * for MST connectors if we unplug the connector is gone
11367 * away but the encoder is still connected to a crtc
11368 * until a modeset happens in response to the hotplug.
11369 */
11370 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
11371 continue;
11372
Rob Clarke2c719b2014-12-15 13:56:32 -050011373 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011374 "encoder's enabled state mismatch "
11375 "(expected %i, found %i)\n",
11376 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050011377 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011378 "active encoder with no crtc\n");
11379
Rob Clarke2c719b2014-12-15 13:56:32 -050011380 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011381 "encoder's computed active state doesn't match tracked active state "
11382 "(expected %i, found %i)\n", active, encoder->connectors_active);
11383
11384 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050011385 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011386 "encoder's hw state doesn't match sw tracking "
11387 "(expected %i, found %i)\n",
11388 encoder->connectors_active, active);
11389
11390 if (!encoder->base.crtc)
11391 continue;
11392
11393 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050011394 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011395 "active encoder's pipe doesn't match"
11396 "(expected %i, found %i)\n",
11397 tracked_pipe, pipe);
11398
11399 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011400}
11401
11402static void
11403check_crtc_state(struct drm_device *dev)
11404{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011405 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011406 struct intel_crtc *crtc;
11407 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011408 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011409
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011410 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011411 bool enabled = false;
11412 bool active = false;
11413
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011414 memset(&pipe_config, 0, sizeof(pipe_config));
11415
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011416 DRM_DEBUG_KMS("[CRTC:%d]\n",
11417 crtc->base.base.id);
11418
Matt Roper83d65732015-02-25 13:12:16 -080011419 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011420 "active crtc, but not enabled in sw tracking\n");
11421
Damien Lespiaub2784e12014-08-05 11:29:37 +010011422 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011423 if (encoder->base.crtc != &crtc->base)
11424 continue;
11425 enabled = true;
11426 if (encoder->connectors_active)
11427 active = true;
11428 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020011429
Rob Clarke2c719b2014-12-15 13:56:32 -050011430 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011431 "crtc's computed active state doesn't match tracked active state "
11432 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080011433 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011434 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080011435 "(expected %i, found %i)\n", enabled,
11436 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011437
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011438 active = dev_priv->display.get_pipe_config(crtc,
11439 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020011440
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030011441 /* hw state is inconsistent with the pipe quirk */
11442 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
11443 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020011444 active = crtc->active;
11445
Damien Lespiaub2784e12014-08-05 11:29:37 +010011446 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030011447 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020011448 if (encoder->base.crtc != &crtc->base)
11449 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010011450 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020011451 encoder->get_config(encoder, &pipe_config);
11452 }
11453
Rob Clarke2c719b2014-12-15 13:56:32 -050011454 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011455 "crtc active state doesn't match with hw state "
11456 "(expected %i, found %i)\n", crtc->active, active);
11457
Daniel Vetterc0b03412013-05-28 12:05:54 +020011458 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011459 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050011460 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020011461 intel_dump_pipe_config(crtc, &pipe_config,
11462 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011463 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011464 "[sw state]");
11465 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011466 }
11467}
11468
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011469static void
11470check_shared_dpll_state(struct drm_device *dev)
11471{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011472 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011473 struct intel_crtc *crtc;
11474 struct intel_dpll_hw_state dpll_hw_state;
11475 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020011476
11477 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11478 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11479 int enabled_crtcs = 0, active_crtcs = 0;
11480 bool active;
11481
11482 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
11483
11484 DRM_DEBUG_KMS("%s\n", pll->name);
11485
11486 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
11487
Rob Clarke2c719b2014-12-15 13:56:32 -050011488 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020011489 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011490 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050011491 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020011492 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011493 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020011494 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011495 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020011496 "pll on state mismatch (expected %i, found %i)\n",
11497 pll->on, active);
11498
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011499 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011500 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020011501 enabled_crtcs++;
11502 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11503 active_crtcs++;
11504 }
Rob Clarke2c719b2014-12-15 13:56:32 -050011505 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020011506 "pll active crtcs mismatch (expected %i, found %i)\n",
11507 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050011508 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020011509 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011510 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011511
Rob Clarke2c719b2014-12-15 13:56:32 -050011512 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020011513 sizeof(dpll_hw_state)),
11514 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020011515 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011516}
11517
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011518void
11519intel_modeset_check_state(struct drm_device *dev)
11520{
Damien Lespiau08db6652014-11-04 17:06:52 +000011521 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011522 check_connector_state(dev);
11523 check_encoder_state(dev);
11524 check_crtc_state(dev);
11525 check_shared_dpll_state(dev);
11526}
11527
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011528void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030011529 int dotclock)
11530{
11531 /*
11532 * FDI already provided one idea for the dotclock.
11533 * Yell if the encoder disagrees.
11534 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011535 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030011536 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011537 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030011538}
11539
Ville Syrjälä80715b22014-05-15 20:23:23 +030011540static void update_scanline_offset(struct intel_crtc *crtc)
11541{
11542 struct drm_device *dev = crtc->base.dev;
11543
11544 /*
11545 * The scanline counter increments at the leading edge of hsync.
11546 *
11547 * On most platforms it starts counting from vtotal-1 on the
11548 * first active line. That means the scanline counter value is
11549 * always one less than what we would expect. Ie. just after
11550 * start of vblank, which also occurs at start of hsync (on the
11551 * last active line), the scanline counter will read vblank_start-1.
11552 *
11553 * On gen2 the scanline counter starts counting from 1 instead
11554 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11555 * to keep the value positive), instead of adding one.
11556 *
11557 * On HSW+ the behaviour of the scanline counter depends on the output
11558 * type. For DP ports it behaves like most other platforms, but on HDMI
11559 * there's an extra 1 line difference. So we need to add two instead of
11560 * one to the value.
11561 */
11562 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011563 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030011564 int vtotal;
11565
11566 vtotal = mode->crtc_vtotal;
11567 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
11568 vtotal /= 2;
11569
11570 crtc->scanline_offset = vtotal - 1;
11571 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030011572 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030011573 crtc->scanline_offset = 2;
11574 } else
11575 crtc->scanline_offset = 1;
11576}
11577
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011578static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011579intel_modeset_compute_config(struct drm_crtc *crtc,
11580 struct drm_display_mode *mode,
11581 struct drm_framebuffer *fb,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011582 struct drm_atomic_state *state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011583 unsigned *modeset_pipes,
11584 unsigned *prepare_pipes,
11585 unsigned *disable_pipes)
11586{
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011587 struct drm_device *dev = crtc->dev;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011588 struct intel_crtc_state *pipe_config = NULL;
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011589 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011590 int ret = 0;
11591
11592 ret = drm_atomic_add_affected_connectors(state, crtc);
11593 if (ret)
11594 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011595
11596 intel_modeset_affected_pipes(crtc, modeset_pipes,
11597 prepare_pipes, disable_pipes);
11598
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011599 for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
11600 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
11601 if (IS_ERR(pipe_config))
11602 return pipe_config;
11603
11604 pipe_config->base.enable = false;
11605 }
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011606
11607 /*
11608 * Note this needs changes when we start tracking multiple modes
11609 * and crtcs. At that point we'll need to compute the whole config
11610 * (i.e. one pipe_config for each crtc) rather than just the one
11611 * for this crtc.
11612 */
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011613 for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
11614 /* FIXME: For now we still expect modeset_pipes has at most
11615 * one bit set. */
11616 if (WARN_ON(&intel_crtc->base != crtc))
11617 continue;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011618
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011619 pipe_config = intel_modeset_pipe_config(crtc, fb, mode, state);
11620 if (IS_ERR(pipe_config))
11621 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011622
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030011623 pipe_config->base.enable = true;
11624
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020011625 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
11626 "[modeset]");
11627 }
11628
11629 return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011630}
11631
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030011632static int __intel_set_mode_setup_plls(struct drm_atomic_state *state,
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011633 unsigned modeset_pipes,
11634 unsigned disable_pipes)
11635{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030011636 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011637 struct drm_i915_private *dev_priv = to_i915(dev);
11638 unsigned clear_pipes = modeset_pipes | disable_pipes;
11639 struct intel_crtc *intel_crtc;
11640 int ret = 0;
11641
11642 if (!dev_priv->display.crtc_compute_clock)
11643 return 0;
11644
11645 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
11646 if (ret)
11647 goto done;
11648
11649 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030011650 struct intel_crtc_state *crtc_state =
11651 intel_atomic_get_crtc_state(state, intel_crtc);
11652
11653 /* Modeset pipes should have a new state by now */
11654 if (WARN_ON(IS_ERR(crtc_state)))
11655 continue;
11656
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011657 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030011658 crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011659 if (ret) {
11660 intel_shared_dpll_abort_config(dev_priv);
11661 goto done;
11662 }
11663 }
11664
11665done:
11666 return ret;
11667}
11668
Daniel Vetterf30da182013-04-11 20:22:50 +020011669static int __intel_set_mode(struct drm_crtc *crtc,
11670 struct drm_display_mode *mode,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011671 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011672 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011673 unsigned modeset_pipes,
11674 unsigned prepare_pipes,
11675 unsigned disable_pipes)
Daniel Vettera6778b32012-07-02 09:56:42 +020011676{
11677 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030011678 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011679 struct drm_display_mode *saved_mode;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030011680 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011681 struct intel_crtc_state *crtc_state_copy = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020011682 struct intel_crtc *intel_crtc;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011683 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020011684
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011685 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011686 if (!saved_mode)
11687 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020011688
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011689 crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
11690 if (!crtc_state_copy) {
11691 ret = -ENOMEM;
11692 goto done;
11693 }
11694
Tim Gardner3ac18232012-12-07 07:54:26 -070011695 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020011696
Jesse Barnes30a970c2013-11-04 13:48:12 -080011697 /*
11698 * See if the config requires any additional preparation, e.g.
11699 * to adjust global state with pipes off. We need to do this
11700 * here so we can get the modeset_pipe updated config for the new
11701 * mode set on this crtc. For other crtcs we need to use the
11702 * adjusted_mode bits in the crtc directly.
11703 */
Ville Syrjäläc164f832013-11-05 22:34:12 +020011704 if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030011705 ret = valleyview_modeset_global_pipes(state, &prepare_pipes);
11706 if (ret)
11707 goto done;
Jesse Barnes30a970c2013-11-04 13:48:12 -080011708
Ville Syrjäläc164f832013-11-05 22:34:12 +020011709 /* may have added more to prepare_pipes than we should */
11710 prepare_pipes &= ~disable_pipes;
11711 }
11712
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030011713 ret = __intel_set_mode_setup_plls(state, modeset_pipes, disable_pipes);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011714 if (ret)
11715 goto done;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +020011716
Daniel Vetter460da9162013-03-27 00:44:51 +010011717 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
11718 intel_crtc_disable(&intel_crtc->base);
11719
Daniel Vetterea9d7582012-07-10 10:42:52 +020011720 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011721 if (intel_crtc->base.state->enable)
Daniel Vetterea9d7582012-07-10 10:42:52 +020011722 dev_priv->display.crtc_disable(&intel_crtc->base);
11723 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011724
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020011725 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
11726 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011727 *
11728 * Note we'll need to fix this up when we start tracking multiple
11729 * pipes; here we assume a single modeset_pipe and only track the
11730 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020011731 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011732 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020011733 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011734 /* mode_set/enable/disable functions rely on a correct pipe
11735 * config. */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020011736 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020011737
11738 /*
11739 * Calculate and store various constants which
11740 * are later needed by vblank and swap-completion
11741 * timestamping. They are derived from true hwmode.
11742 */
11743 drm_calc_timestamping_constants(crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011744 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011745 }
Daniel Vetter7758a112012-07-08 19:40:39 +020011746
Daniel Vetterea9d7582012-07-10 10:42:52 +020011747 /* Only after disabling all output pipelines that will be changed can we
11748 * update the the output configuration. */
11749 intel_modeset_update_state(dev, prepare_pipes);
11750
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030011751 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020011752
Daniel Vetter25c5b262012-07-08 22:08:04 +020011753 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Gustavo Padovan455a6802014-12-01 15:40:11 -080011754 struct drm_plane *primary = intel_crtc->base.primary;
11755 int vdisplay, hdisplay;
Daniel Vetter4c107942014-04-24 23:55:05 +020011756
Gustavo Padovan455a6802014-12-01 15:40:11 -080011757 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070011758 ret = drm_plane_helper_update(primary, &intel_crtc->base,
11759 fb, 0, 0,
11760 hdisplay, vdisplay,
11761 x << 16, y << 16,
11762 hdisplay << 16, vdisplay << 16);
Daniel Vettera6778b32012-07-02 09:56:42 +020011763 }
11764
11765 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030011766 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11767 update_scanline_offset(intel_crtc);
11768
Daniel Vetter25c5b262012-07-08 22:08:04 +020011769 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030011770 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011771
Daniel Vettera6778b32012-07-02 09:56:42 +020011772 /* FIXME: add subpixel order */
11773done:
Matt Roper83d65732015-02-25 13:12:16 -080011774 if (ret && crtc->state->enable)
Tim Gardner3ac18232012-12-07 07:54:26 -070011775 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020011776
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011777 if (ret == 0 && pipe_config) {
11778 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11779
11780 /* The pipe_config will be freed with the atomic state, so
11781 * make a copy. */
11782 memcpy(crtc_state_copy, intel_crtc->config,
11783 sizeof *crtc_state_copy);
11784 intel_crtc->config = crtc_state_copy;
11785 intel_crtc->base.state = &crtc_state_copy->base;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011786 } else {
11787 kfree(crtc_state_copy);
11788 }
11789
Tim Gardner3ac18232012-12-07 07:54:26 -070011790 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020011791 return ret;
11792}
11793
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011794static int intel_set_mode_pipes(struct drm_crtc *crtc,
11795 struct drm_display_mode *mode,
11796 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011797 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011798 unsigned modeset_pipes,
11799 unsigned prepare_pipes,
11800 unsigned disable_pipes)
11801{
11802 int ret;
11803
11804 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
11805 prepare_pipes, disable_pipes);
11806
11807 if (ret == 0)
11808 intel_modeset_check_state(crtc->dev);
11809
11810 return ret;
11811}
11812
Damien Lespiaue7457a92013-08-08 22:28:59 +010011813static int intel_set_mode(struct drm_crtc *crtc,
11814 struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011815 int x, int y, struct drm_framebuffer *fb,
11816 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020011817{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011818 struct intel_crtc_state *pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011819 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011820 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020011821
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011822 pipe_config = intel_modeset_compute_config(crtc, mode, fb, state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011823 &modeset_pipes,
11824 &prepare_pipes,
11825 &disable_pipes);
Daniel Vetterf30da182013-04-11 20:22:50 +020011826
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011827 if (IS_ERR(pipe_config)) {
11828 ret = PTR_ERR(pipe_config);
11829 goto out;
11830 }
Daniel Vetterf30da182013-04-11 20:22:50 +020011831
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011832 ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
11833 modeset_pipes, prepare_pipes,
11834 disable_pipes);
11835 if (ret)
11836 goto out;
11837
11838out:
11839 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020011840}
11841
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011842void intel_crtc_restore_mode(struct drm_crtc *crtc)
11843{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011844 struct drm_device *dev = crtc->dev;
11845 struct drm_atomic_state *state;
11846 struct intel_encoder *encoder;
11847 struct intel_connector *connector;
11848 struct drm_connector_state *connector_state;
11849
11850 state = drm_atomic_state_alloc(dev);
11851 if (!state) {
11852 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
11853 crtc->base.id);
11854 return;
11855 }
11856
11857 state->acquire_ctx = dev->mode_config.acquire_ctx;
11858
11859 /* The force restore path in the HW readout code relies on the staged
11860 * config still keeping the user requested config while the actual
11861 * state has been overwritten by the configuration read from HW. We
11862 * need to copy the staged config to the atomic state, otherwise the
11863 * mode set will just reapply the state the HW is already in. */
11864 for_each_intel_encoder(dev, encoder) {
11865 if (&encoder->new_crtc->base != crtc)
11866 continue;
11867
11868 for_each_intel_connector(dev, connector) {
11869 if (connector->new_encoder != encoder)
11870 continue;
11871
11872 connector_state = drm_atomic_get_connector_state(state, &connector->base);
11873 if (IS_ERR(connector_state)) {
11874 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
11875 connector->base.base.id,
11876 connector->base.name,
11877 PTR_ERR(connector_state));
11878 continue;
11879 }
11880
11881 connector_state->crtc = crtc;
11882 connector_state->best_encoder = &encoder->base;
11883 }
11884 }
11885
11886 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
11887 state);
11888
11889 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011890}
11891
Daniel Vetter25c5b262012-07-08 22:08:04 +020011892#undef for_each_intel_crtc_masked
11893
Daniel Vetterd9e55602012-07-04 22:16:09 +020011894static void intel_set_config_free(struct intel_set_config *config)
11895{
11896 if (!config)
11897 return;
11898
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011899 kfree(config->save_connector_encoders);
11900 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020011901 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020011902 kfree(config);
11903}
11904
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011905static int intel_set_config_save_state(struct drm_device *dev,
11906 struct intel_set_config *config)
11907{
Ville Syrjälä76688512014-01-10 11:28:06 +020011908 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011909 struct drm_encoder *encoder;
11910 struct drm_connector *connector;
11911 int count;
11912
Ville Syrjälä76688512014-01-10 11:28:06 +020011913 config->save_crtc_enabled =
11914 kcalloc(dev->mode_config.num_crtc,
11915 sizeof(bool), GFP_KERNEL);
11916 if (!config->save_crtc_enabled)
11917 return -ENOMEM;
11918
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011919 config->save_encoder_crtcs =
11920 kcalloc(dev->mode_config.num_encoder,
11921 sizeof(struct drm_crtc *), GFP_KERNEL);
11922 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011923 return -ENOMEM;
11924
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011925 config->save_connector_encoders =
11926 kcalloc(dev->mode_config.num_connector,
11927 sizeof(struct drm_encoder *), GFP_KERNEL);
11928 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011929 return -ENOMEM;
11930
11931 /* Copy data. Note that driver private data is not affected.
11932 * Should anything bad happen only the expected state is
11933 * restored, not the drivers personal bookkeeping.
11934 */
11935 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010011936 for_each_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011937 config->save_crtc_enabled[count++] = crtc->state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011938 }
11939
11940 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011941 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011942 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011943 }
11944
11945 count = 0;
11946 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011947 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011948 }
11949
11950 return 0;
11951}
11952
11953static void intel_set_config_restore_state(struct drm_device *dev,
11954 struct intel_set_config *config)
11955{
Ville Syrjälä76688512014-01-10 11:28:06 +020011956 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011957 struct intel_encoder *encoder;
11958 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011959 int count;
11960
11961 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011962 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011963 crtc->new_enabled = config->save_crtc_enabled[count++];
11964 }
11965
11966 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010011967 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011968 encoder->new_crtc =
11969 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011970 }
11971
11972 count = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011973 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011974 connector->new_encoder =
11975 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011976 }
11977}
11978
Imre Deake3de42b2013-05-03 19:44:07 +020011979static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010011980is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020011981{
11982 int i;
11983
Chris Wilson2e57f472013-07-17 12:14:40 +010011984 if (set->num_connectors == 0)
11985 return false;
11986
11987 if (WARN_ON(set->connectors == NULL))
11988 return false;
11989
11990 for (i = 0; i < set->num_connectors; i++)
11991 if (set->connectors[i]->encoder &&
11992 set->connectors[i]->encoder->crtc == set->crtc &&
11993 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020011994 return true;
11995
11996 return false;
11997}
11998
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011999static void
12000intel_set_config_compute_mode_changes(struct drm_mode_set *set,
12001 struct intel_set_config *config)
12002{
12003
12004 /* We should be able to check here if the fb has the same properties
12005 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010012006 if (is_crtc_connector_off(set)) {
12007 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070012008 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070012009 /*
12010 * If we have no fb, we can only flip as long as the crtc is
12011 * active, otherwise we need a full mode set. The crtc may
12012 * be active if we've only disabled the primary plane, or
12013 * in fastboot situations.
12014 */
Matt Roperf4510a22014-04-01 15:22:40 -070012015 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012016 struct intel_crtc *intel_crtc =
12017 to_intel_crtc(set->crtc);
12018
Matt Roper3b150f02014-05-29 08:06:53 -070012019 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012020 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
12021 config->fb_changed = true;
12022 } else {
12023 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
12024 config->mode_changed = true;
12025 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012026 } else if (set->fb == NULL) {
12027 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010012028 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070012029 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012030 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012031 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012032 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012033 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012034 }
12035
Daniel Vetter835c5872012-07-10 18:11:08 +020012036 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012037 config->fb_changed = true;
12038
12039 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
12040 DRM_DEBUG_KMS("modes are different, full mode set\n");
12041 drm_mode_debug_printmodeline(&set->crtc->mode);
12042 drm_mode_debug_printmodeline(set->mode);
12043 config->mode_changed = true;
12044 }
Chris Wilsona1d95702013-08-13 18:48:47 +010012045
12046 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
12047 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012048}
12049
Daniel Vetter2e431052012-07-04 22:42:15 +020012050static int
Daniel Vetter9a935852012-07-05 22:34:27 +020012051intel_modeset_stage_output_state(struct drm_device *dev,
12052 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012053 struct intel_set_config *config,
12054 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020012055{
Daniel Vetter9a935852012-07-05 22:34:27 +020012056 struct intel_connector *connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012057 struct drm_connector_state *connector_state;
Daniel Vetter9a935852012-07-05 22:34:27 +020012058 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020012059 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030012060 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020012061
Damien Lespiau9abdda72013-02-13 13:29:23 +000012062 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020012063 * of connectors. For paranoia, double-check this. */
12064 WARN_ON(!set->fb && (set->num_connectors != 0));
12065 WARN_ON(set->fb && (set->num_connectors == 0));
12066
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012067 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012068 /* Otherwise traverse passed in connector list and get encoders
12069 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020012070 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012071 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012072 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020012073 break;
12074 }
12075 }
12076
Daniel Vetter9a935852012-07-05 22:34:27 +020012077 /* If we disable the crtc, disable all its connectors. Also, if
12078 * the connector is on the changing crtc but not on the new
12079 * connector list, disable it. */
12080 if ((!set->fb || ro == set->num_connectors) &&
12081 connector->base.encoder &&
12082 connector->base.encoder->crtc == set->crtc) {
12083 connector->new_encoder = NULL;
12084
12085 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12086 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012087 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020012088 }
12089
12090
12091 if (&connector->new_encoder->base != connector->base.encoder) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012092 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
12093 connector->base.base.id,
12094 connector->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012095 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020012096 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012097 }
12098 /* connector->new_encoder is now updated for all connectors. */
12099
12100 /* Update crtc of enabled connectors. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012101 for_each_intel_connector(dev, connector) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012102 struct drm_crtc *new_crtc;
12103
Daniel Vetter9a935852012-07-05 22:34:27 +020012104 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020012105 continue;
12106
Daniel Vetter9a935852012-07-05 22:34:27 +020012107 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020012108
12109 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012110 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020012111 new_crtc = set->crtc;
12112 }
12113
12114 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010012115 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
12116 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012117 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012118 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012119 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020012120
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012121 connector_state =
12122 drm_atomic_get_connector_state(state, &connector->base);
12123 if (IS_ERR(connector_state))
12124 return PTR_ERR(connector_state);
12125
12126 connector_state->crtc = new_crtc;
12127 connector_state->best_encoder = &connector->new_encoder->base;
12128
Daniel Vetter9a935852012-07-05 22:34:27 +020012129 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12130 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012131 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020012132 new_crtc->base.id);
12133 }
12134
12135 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010012136 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012137 int num_connectors = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012138 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012139 if (connector->new_encoder == encoder) {
12140 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012141 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020012142 }
12143 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012144
12145 if (num_connectors == 0)
12146 encoder->new_crtc = NULL;
12147 else if (num_connectors > 1)
12148 return -EINVAL;
12149
Daniel Vetter9a935852012-07-05 22:34:27 +020012150 /* Only now check for crtc changes so we don't miss encoders
12151 * that will be disabled. */
12152 if (&encoder->new_crtc->base != encoder->base.crtc) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012153 DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
12154 encoder->base.base.id,
12155 encoder->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012156 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020012157 }
12158 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012159 /* Now we've also updated encoder->new_crtc for all encoders. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012160 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012161 connector_state =
12162 drm_atomic_get_connector_state(state, &connector->base);
Ander Conselvan de Oliveira9d918c12015-03-27 15:33:51 +020012163 if (IS_ERR(connector_state))
12164 return PTR_ERR(connector_state);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012165
12166 if (connector->new_encoder) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012167 if (connector->new_encoder != connector->encoder)
12168 connector->encoder = connector->new_encoder;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012169 } else {
12170 connector_state->crtc = NULL;
Ander Conselvan de Oliveiraf61cccf2015-03-31 11:35:00 +030012171 connector_state->best_encoder = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012172 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012173 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012174 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012175 crtc->new_enabled = false;
12176
Damien Lespiaub2784e12014-08-05 11:29:37 +010012177 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012178 if (encoder->new_crtc == crtc) {
12179 crtc->new_enabled = true;
12180 break;
12181 }
12182 }
12183
Matt Roper83d65732015-02-25 13:12:16 -080012184 if (crtc->new_enabled != crtc->base.state->enable) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012185 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
12186 crtc->base.base.id,
Ville Syrjälä76688512014-01-10 11:28:06 +020012187 crtc->new_enabled ? "en" : "dis");
12188 config->mode_changed = true;
12189 }
12190 }
12191
Daniel Vetter2e431052012-07-04 22:42:15 +020012192 return 0;
12193}
12194
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012195static void disable_crtc_nofb(struct intel_crtc *crtc)
12196{
12197 struct drm_device *dev = crtc->base.dev;
12198 struct intel_encoder *encoder;
12199 struct intel_connector *connector;
12200
12201 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
12202 pipe_name(crtc->pipe));
12203
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012204 for_each_intel_connector(dev, connector) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012205 if (connector->new_encoder &&
12206 connector->new_encoder->new_crtc == crtc)
12207 connector->new_encoder = NULL;
12208 }
12209
Damien Lespiaub2784e12014-08-05 11:29:37 +010012210 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012211 if (encoder->new_crtc == crtc)
12212 encoder->new_crtc = NULL;
12213 }
12214
12215 crtc->new_enabled = false;
12216}
12217
Daniel Vetter2e431052012-07-04 22:42:15 +020012218static int intel_crtc_set_config(struct drm_mode_set *set)
12219{
12220 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020012221 struct drm_mode_set save_set;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012222 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020012223 struct intel_set_config *config;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012224 struct intel_crtc_state *pipe_config;
Jesse Barnes50f52752014-11-07 13:11:00 -080012225 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Daniel Vetter2e431052012-07-04 22:42:15 +020012226 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020012227
Daniel Vetter8d3e3752012-07-05 16:09:09 +020012228 BUG_ON(!set);
12229 BUG_ON(!set->crtc);
12230 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020012231
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010012232 /* Enforce sane interface api - has been abused by the fb helper. */
12233 BUG_ON(!set->mode && set->fb);
12234 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020012235
Daniel Vetter2e431052012-07-04 22:42:15 +020012236 if (set->fb) {
12237 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12238 set->crtc->base.id, set->fb->base.id,
12239 (int)set->num_connectors, set->x, set->y);
12240 } else {
12241 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020012242 }
12243
12244 dev = set->crtc->dev;
12245
12246 ret = -ENOMEM;
12247 config = kzalloc(sizeof(*config), GFP_KERNEL);
12248 if (!config)
12249 goto out_config;
12250
12251 ret = intel_set_config_save_state(dev, config);
12252 if (ret)
12253 goto out_config;
12254
12255 save_set.crtc = set->crtc;
12256 save_set.mode = &set->crtc->mode;
12257 save_set.x = set->crtc->x;
12258 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070012259 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020012260
12261 /* Compute whether we need a full modeset, only an fb base update or no
12262 * change at all. In the future we might also check whether only the
12263 * mode changed, e.g. for LVDS where we only change the panel fitter in
12264 * such cases. */
12265 intel_set_config_compute_mode_changes(set, config);
12266
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012267 state = drm_atomic_state_alloc(dev);
12268 if (!state) {
12269 ret = -ENOMEM;
12270 goto out_config;
12271 }
12272
12273 state->acquire_ctx = dev->mode_config.acquire_ctx;
12274
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012275 ret = intel_modeset_stage_output_state(dev, set, config, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020012276 if (ret)
12277 goto fail;
12278
Jesse Barnes50f52752014-11-07 13:11:00 -080012279 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012280 set->fb, state,
Jesse Barnes50f52752014-11-07 13:11:00 -080012281 &modeset_pipes,
12282 &prepare_pipes,
12283 &disable_pipes);
Jesse Barnes20664592014-11-05 14:26:09 -080012284 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080012285 ret = PTR_ERR(pipe_config);
Jesse Barnes50f52752014-11-07 13:11:00 -080012286 goto fail;
Jesse Barnes20664592014-11-05 14:26:09 -080012287 } else if (pipe_config) {
Ville Syrjäläb9950a12014-11-21 21:00:36 +020012288 if (pipe_config->has_audio !=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012289 to_intel_crtc(set->crtc)->config->has_audio)
Jesse Barnes20664592014-11-05 14:26:09 -080012290 config->mode_changed = true;
12291
Jesse Barnesaf15d2c2014-12-01 09:54:28 -080012292 /*
12293 * Note we have an issue here with infoframes: current code
12294 * only updates them on the full mode set path per hw
12295 * requirements. So here we should be checking for any
12296 * required changes and forcing a mode set.
12297 */
Jesse Barnes20664592014-11-05 14:26:09 -080012298 }
Jesse Barnes50f52752014-11-07 13:11:00 -080012299
Jesse Barnes1f9954d2014-11-05 14:26:10 -080012300 intel_update_pipe_size(to_intel_crtc(set->crtc));
12301
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012302 if (config->mode_changed) {
Jesse Barnes50f52752014-11-07 13:11:00 -080012303 ret = intel_set_mode_pipes(set->crtc, set->mode,
12304 set->x, set->y, set->fb, pipe_config,
12305 modeset_pipes, prepare_pipes,
12306 disable_pipes);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012307 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070012308 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080012309 struct drm_plane *primary = set->crtc->primary;
12310 int vdisplay, hdisplay;
Matt Roper3b150f02014-05-29 08:06:53 -070012311
Gustavo Padovan455a6802014-12-01 15:40:11 -080012312 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070012313 ret = drm_plane_helper_update(primary, set->crtc, set->fb,
12314 0, 0, hdisplay, vdisplay,
12315 set->x << 16, set->y << 16,
12316 hdisplay << 16, vdisplay << 16);
Matt Roper3b150f02014-05-29 08:06:53 -070012317
12318 /*
12319 * We need to make sure the primary plane is re-enabled if it
12320 * has previously been turned off.
12321 */
12322 if (!intel_crtc->primary_enabled && ret == 0) {
12323 WARN_ON(!intel_crtc->active);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +030012324 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012325 }
12326
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012327 /*
12328 * In the fastboot case this may be our only check of the
12329 * state after boot. It would be better to only do it on
12330 * the first update, but we don't have a nice way of doing that
12331 * (and really, set_config isn't used much for high freq page
12332 * flipping, so increasing its cost here shouldn't be a big
12333 * deal).
12334 */
Jani Nikulad330a952014-01-21 11:24:25 +020012335 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012336 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020012337 }
12338
Chris Wilson2d05eae2013-05-03 17:36:25 +010012339 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020012340 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12341 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020012342fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010012343 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012344
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012345 drm_atomic_state_clear(state);
12346
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012347 /*
12348 * HACK: if the pipe was on, but we didn't have a framebuffer,
12349 * force the pipe off to avoid oopsing in the modeset code
12350 * due to fb==NULL. This should only happen during boot since
12351 * we don't yet reconstruct the FB from the hardware state.
12352 */
12353 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
12354 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
12355
Chris Wilson2d05eae2013-05-03 17:36:25 +010012356 /* Try to restore the config */
12357 if (config->mode_changed &&
12358 intel_set_mode(save_set.crtc, save_set.mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012359 save_set.x, save_set.y, save_set.fb,
12360 state))
Chris Wilson2d05eae2013-05-03 17:36:25 +010012361 DRM_ERROR("failed to restore config after modeset failure\n");
12362 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012363
Daniel Vetterd9e55602012-07-04 22:16:09 +020012364out_config:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012365 if (state)
12366 drm_atomic_state_free(state);
12367
Daniel Vetterd9e55602012-07-04 22:16:09 +020012368 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012369 return ret;
12370}
12371
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012372static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012373 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020012374 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012375 .destroy = intel_crtc_destroy,
12376 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080012377 .atomic_duplicate_state = intel_crtc_duplicate_state,
12378 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012379};
12380
Daniel Vetter53589012013-06-05 13:34:16 +020012381static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
12382 struct intel_shared_dpll *pll,
12383 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012384{
Daniel Vetter53589012013-06-05 13:34:16 +020012385 uint32_t val;
12386
Daniel Vetterf458ebb2014-09-30 10:56:39 +020012387 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030012388 return false;
12389
Daniel Vetter53589012013-06-05 13:34:16 +020012390 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020012391 hw_state->dpll = val;
12392 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
12393 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020012394
12395 return val & DPLL_VCO_ENABLE;
12396}
12397
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012398static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
12399 struct intel_shared_dpll *pll)
12400{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012401 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
12402 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012403}
12404
Daniel Vettere7b903d2013-06-05 13:34:14 +020012405static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
12406 struct intel_shared_dpll *pll)
12407{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012408 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020012409 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020012410
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012411 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012412
12413 /* Wait for the clocks to stabilize. */
12414 POSTING_READ(PCH_DPLL(pll->id));
12415 udelay(150);
12416
12417 /* The pixel multiplier can only be updated once the
12418 * DPLL is enabled and the clocks are stable.
12419 *
12420 * So write it again.
12421 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012422 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012423 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012424 udelay(200);
12425}
12426
12427static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
12428 struct intel_shared_dpll *pll)
12429{
12430 struct drm_device *dev = dev_priv->dev;
12431 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020012432
12433 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012434 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020012435 if (intel_crtc_to_shared_dpll(crtc) == pll)
12436 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
12437 }
12438
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012439 I915_WRITE(PCH_DPLL(pll->id), 0);
12440 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012441 udelay(200);
12442}
12443
Daniel Vetter46edb022013-06-05 13:34:12 +020012444static char *ibx_pch_dpll_names[] = {
12445 "PCH DPLL A",
12446 "PCH DPLL B",
12447};
12448
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012449static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012450{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012451 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012452 int i;
12453
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012454 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012455
Daniel Vettere72f9fb2013-06-05 13:34:06 +020012456 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020012457 dev_priv->shared_dplls[i].id = i;
12458 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012459 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020012460 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
12461 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020012462 dev_priv->shared_dplls[i].get_hw_state =
12463 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012464 }
12465}
12466
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012467static void intel_shared_dpll_init(struct drm_device *dev)
12468{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012469 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012470
Daniel Vetter9cd86932014-06-25 22:01:57 +030012471 if (HAS_DDI(dev))
12472 intel_ddi_pll_init(dev);
12473 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012474 ibx_pch_dpll_init(dev);
12475 else
12476 dev_priv->num_shared_dpll = 0;
12477
12478 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012479}
12480
Matt Roper6beb8c232014-12-01 15:40:14 -080012481/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000012482 * intel_wm_need_update - Check whether watermarks need updating
12483 * @plane: drm plane
12484 * @state: new plane state
12485 *
12486 * Check current plane state versus the new one to determine whether
12487 * watermarks need to be recalculated.
12488 *
12489 * Returns true or false.
12490 */
12491bool intel_wm_need_update(struct drm_plane *plane,
12492 struct drm_plane_state *state)
12493{
12494 /* Update watermarks on tiling changes. */
12495 if (!plane->state->fb || !state->fb ||
12496 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
12497 plane->state->rotation != state->rotation)
12498 return true;
12499
12500 return false;
12501}
12502
12503/**
Matt Roper6beb8c232014-12-01 15:40:14 -080012504 * intel_prepare_plane_fb - Prepare fb for usage on plane
12505 * @plane: drm plane to prepare for
12506 * @fb: framebuffer to prepare for presentation
12507 *
12508 * Prepares a framebuffer for usage on a display plane. Generally this
12509 * involves pinning the underlying object and updating the frontbuffer tracking
12510 * bits. Some older platforms need special physical address handling for
12511 * cursor planes.
12512 *
12513 * Returns 0 on success, negative error code on failure.
12514 */
12515int
12516intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000012517 struct drm_framebuffer *fb,
12518 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070012519{
12520 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080012521 struct intel_plane *intel_plane = to_intel_plane(plane);
12522 enum pipe pipe = intel_plane->pipe;
12523 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12524 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
12525 unsigned frontbuffer_bits = 0;
12526 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070012527
Matt Roperea2c67b2014-12-23 10:41:52 -080012528 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070012529 return 0;
12530
Matt Roper6beb8c232014-12-01 15:40:14 -080012531 switch (plane->type) {
12532 case DRM_PLANE_TYPE_PRIMARY:
12533 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
12534 break;
12535 case DRM_PLANE_TYPE_CURSOR:
12536 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
12537 break;
12538 case DRM_PLANE_TYPE_OVERLAY:
12539 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
12540 break;
12541 }
Matt Roper465c1202014-05-29 08:06:54 -070012542
Matt Roper4c345742014-07-09 16:22:10 -070012543 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070012544
Matt Roper6beb8c232014-12-01 15:40:14 -080012545 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
12546 INTEL_INFO(dev)->cursor_needs_physical) {
12547 int align = IS_I830(dev) ? 16 * 1024 : 256;
12548 ret = i915_gem_object_attach_phys(obj, align);
12549 if (ret)
12550 DRM_DEBUG_KMS("failed to attach phys object\n");
12551 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000012552 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080012553 }
12554
12555 if (ret == 0)
12556 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
12557
12558 mutex_unlock(&dev->struct_mutex);
12559
12560 return ret;
12561}
12562
Matt Roper38f3ce32014-12-02 07:45:25 -080012563/**
12564 * intel_cleanup_plane_fb - Cleans up an fb after plane use
12565 * @plane: drm plane to clean up for
12566 * @fb: old framebuffer that was on plane
12567 *
12568 * Cleans up a framebuffer that has just been removed from a plane.
12569 */
12570void
12571intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000012572 struct drm_framebuffer *fb,
12573 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080012574{
12575 struct drm_device *dev = plane->dev;
12576 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12577
12578 if (WARN_ON(!obj))
12579 return;
12580
12581 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
12582 !INTEL_INFO(dev)->cursor_needs_physical) {
12583 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000012584 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080012585 mutex_unlock(&dev->struct_mutex);
12586 }
Matt Roper465c1202014-05-29 08:06:54 -070012587}
12588
12589static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012590intel_check_primary_plane(struct drm_plane *plane,
12591 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070012592{
Matt Roper32b7eee2014-12-24 07:59:06 -080012593 struct drm_device *dev = plane->dev;
12594 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080012595 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080012596 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080012597 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012598 struct drm_rect *dest = &state->dst;
12599 struct drm_rect *src = &state->src;
12600 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053012601 bool can_position = false;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012602 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012603
Matt Roperea2c67b2014-12-23 10:41:52 -080012604 crtc = crtc ? crtc : plane->crtc;
12605 intel_crtc = to_intel_crtc(crtc);
12606
Sonika Jindald8106362015-04-10 14:37:28 +053012607 if (INTEL_INFO(dev)->gen >= 9)
12608 can_position = true;
12609
Matt Roperc59cb172014-12-01 15:40:16 -080012610 ret = drm_plane_helper_check_update(plane, crtc, fb,
12611 src, dest, clip,
12612 DRM_PLANE_HELPER_NO_SCALING,
12613 DRM_PLANE_HELPER_NO_SCALING,
Sonika Jindald8106362015-04-10 14:37:28 +053012614 can_position, true,
12615 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080012616 if (ret)
12617 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012618
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012619 if (intel_crtc->active) {
Matt Roper32b7eee2014-12-24 07:59:06 -080012620 intel_crtc->atomic.wait_for_flips = true;
12621
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012622 /*
12623 * FBC does not work on some platforms for rotated
12624 * planes, so disable it when rotation is not 0 and
12625 * update it when rotation is set back to 0.
12626 *
12627 * FIXME: This is redundant with the fbc update done in
12628 * the primary plane enable function except that that
12629 * one is done too late. We eventually need to unify
12630 * this.
12631 */
12632 if (intel_crtc->primary_enabled &&
12633 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020012634 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080012635 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080012636 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012637 }
12638
12639 if (state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080012640 /*
12641 * BDW signals flip done immediately if the plane
12642 * is disabled, even if the plane enable is already
12643 * armed to occur at the next vblank :(
12644 */
12645 if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
12646 intel_crtc->atomic.wait_vblank = true;
12647 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012648
Matt Roper32b7eee2014-12-24 07:59:06 -080012649 intel_crtc->atomic.fb_bits |=
12650 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
12651
12652 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000012653
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000012654 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000012655 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080012656 }
12657
12658 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070012659}
12660
Sonika Jindal48404c12014-08-22 14:06:04 +053012661static void
12662intel_commit_primary_plane(struct drm_plane *plane,
12663 struct intel_plane_state *state)
12664{
Matt Roper2b875c22014-12-01 15:40:13 -080012665 struct drm_crtc *crtc = state->base.crtc;
12666 struct drm_framebuffer *fb = state->base.fb;
12667 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053012668 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080012669 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053012670 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080012671
Matt Roperea2c67b2014-12-23 10:41:52 -080012672 crtc = crtc ? crtc : plane->crtc;
12673 intel_crtc = to_intel_crtc(crtc);
12674
Matt Ropercf4c7c12014-12-04 10:27:42 -080012675 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053012676 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070012677 crtc->y = src->y1 >> 16;
12678
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012679 if (intel_crtc->active) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012680 if (state->visible) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012681 /* FIXME: kill this fastboot hack */
12682 intel_update_pipe_size(intel_crtc);
12683
12684 intel_crtc->primary_enabled = true;
12685
12686 dev_priv->display.update_primary_plane(crtc, plane->fb,
12687 crtc->x, crtc->y);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012688 } else {
12689 /*
12690 * If clipping results in a non-visible primary plane,
12691 * we'll disable the primary plane. Note that this is
12692 * a bit different than what happens if userspace
12693 * explicitly disables the plane by passing fb=0
12694 * because plane->fb still gets set and pinned.
12695 */
12696 intel_disable_primary_hw_plane(plane, crtc);
12697 }
Matt Roper32b7eee2014-12-24 07:59:06 -080012698 }
12699}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012700
Matt Roper32b7eee2014-12-24 07:59:06 -080012701static void intel_begin_crtc_commit(struct drm_crtc *crtc)
12702{
12703 struct drm_device *dev = crtc->dev;
12704 struct drm_i915_private *dev_priv = dev->dev_private;
12705 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080012706 struct intel_plane *intel_plane;
12707 struct drm_plane *p;
12708 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012709
Matt Roperea2c67b2014-12-23 10:41:52 -080012710 /* Track fb's for any planes being disabled */
12711 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
12712 intel_plane = to_intel_plane(p);
12713
12714 if (intel_crtc->atomic.disabled_planes &
12715 (1 << drm_plane_index(p))) {
12716 switch (p->type) {
12717 case DRM_PLANE_TYPE_PRIMARY:
12718 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
12719 break;
12720 case DRM_PLANE_TYPE_CURSOR:
12721 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
12722 break;
12723 case DRM_PLANE_TYPE_OVERLAY:
12724 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
12725 break;
12726 }
12727
12728 mutex_lock(&dev->struct_mutex);
12729 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
12730 mutex_unlock(&dev->struct_mutex);
12731 }
12732 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012733
Matt Roper32b7eee2014-12-24 07:59:06 -080012734 if (intel_crtc->atomic.wait_for_flips)
12735 intel_crtc_wait_for_pending_flips(crtc);
12736
12737 if (intel_crtc->atomic.disable_fbc)
12738 intel_fbc_disable(dev);
12739
12740 if (intel_crtc->atomic.pre_disable_primary)
12741 intel_pre_disable_primary(crtc);
12742
12743 if (intel_crtc->atomic.update_wm)
12744 intel_update_watermarks(crtc);
12745
12746 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080012747
12748 /* Perform vblank evasion around commit operation */
12749 if (intel_crtc->active)
12750 intel_crtc->atomic.evade =
12751 intel_pipe_update_start(intel_crtc,
12752 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080012753}
12754
12755static void intel_finish_crtc_commit(struct drm_crtc *crtc)
12756{
12757 struct drm_device *dev = crtc->dev;
12758 struct drm_i915_private *dev_priv = dev->dev_private;
12759 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12760 struct drm_plane *p;
12761
Matt Roperc34c9ee2014-12-23 10:41:50 -080012762 if (intel_crtc->atomic.evade)
12763 intel_pipe_update_end(intel_crtc,
12764 intel_crtc->atomic.start_vbl_count);
12765
Matt Roper32b7eee2014-12-24 07:59:06 -080012766 intel_runtime_pm_put(dev_priv);
12767
12768 if (intel_crtc->atomic.wait_vblank)
12769 intel_wait_for_vblank(dev, intel_crtc->pipe);
12770
12771 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
12772
12773 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012774 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020012775 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012776 mutex_unlock(&dev->struct_mutex);
12777 }
Matt Roper465c1202014-05-29 08:06:54 -070012778
Matt Roper32b7eee2014-12-24 07:59:06 -080012779 if (intel_crtc->atomic.post_enable_primary)
12780 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012781
Matt Roper32b7eee2014-12-24 07:59:06 -080012782 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
12783 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
12784 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
12785 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012786
Matt Roper32b7eee2014-12-24 07:59:06 -080012787 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012788}
12789
Matt Ropercf4c7c12014-12-04 10:27:42 -080012790/**
Matt Roper4a3b8762014-12-23 10:41:51 -080012791 * intel_plane_destroy - destroy a plane
12792 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080012793 *
Matt Roper4a3b8762014-12-23 10:41:51 -080012794 * Common destruction function for all types of planes (primary, cursor,
12795 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080012796 */
Matt Roper4a3b8762014-12-23 10:41:51 -080012797void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070012798{
12799 struct intel_plane *intel_plane = to_intel_plane(plane);
12800 drm_plane_cleanup(plane);
12801 kfree(intel_plane);
12802}
12803
Matt Roper65a3fea2015-01-21 16:35:42 -080012804const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070012805 .update_plane = drm_atomic_helper_update_plane,
12806 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070012807 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080012808 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080012809 .atomic_get_property = intel_plane_atomic_get_property,
12810 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080012811 .atomic_duplicate_state = intel_plane_duplicate_state,
12812 .atomic_destroy_state = intel_plane_destroy_state,
12813
Matt Roper465c1202014-05-29 08:06:54 -070012814};
12815
12816static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
12817 int pipe)
12818{
12819 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080012820 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070012821 const uint32_t *intel_primary_formats;
12822 int num_formats;
12823
12824 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
12825 if (primary == NULL)
12826 return NULL;
12827
Matt Roper8e7d6882015-01-21 16:35:41 -080012828 state = intel_create_plane_state(&primary->base);
12829 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080012830 kfree(primary);
12831 return NULL;
12832 }
Matt Roper8e7d6882015-01-21 16:35:41 -080012833 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080012834
Matt Roper465c1202014-05-29 08:06:54 -070012835 primary->can_scale = false;
12836 primary->max_downscale = 1;
12837 primary->pipe = pipe;
12838 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080012839 primary->check_plane = intel_check_primary_plane;
12840 primary->commit_plane = intel_commit_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070012841 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
12842 primary->plane = !pipe;
12843
12844 if (INTEL_INFO(dev)->gen <= 3) {
12845 intel_primary_formats = intel_primary_formats_gen2;
12846 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
12847 } else {
12848 intel_primary_formats = intel_primary_formats_gen4;
12849 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
12850 }
12851
12852 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080012853 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070012854 intel_primary_formats, num_formats,
12855 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053012856
Sonika Jindal3b7a5112015-04-10 14:37:29 +053012857 if (INTEL_INFO(dev)->gen >= 4)
12858 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053012859
Matt Roperea2c67b2014-12-23 10:41:52 -080012860 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
12861
Matt Roper465c1202014-05-29 08:06:54 -070012862 return &primary->base;
12863}
12864
Sonika Jindal3b7a5112015-04-10 14:37:29 +053012865void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
12866{
12867 if (!dev->mode_config.rotation_property) {
12868 unsigned long flags = BIT(DRM_ROTATE_0) |
12869 BIT(DRM_ROTATE_180);
12870
12871 if (INTEL_INFO(dev)->gen >= 9)
12872 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
12873
12874 dev->mode_config.rotation_property =
12875 drm_mode_create_rotation_property(dev, flags);
12876 }
12877 if (dev->mode_config.rotation_property)
12878 drm_object_attach_property(&plane->base.base,
12879 dev->mode_config.rotation_property,
12880 plane->base.state->rotation);
12881}
12882
Matt Roper3d7d6512014-06-10 08:28:13 -070012883static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030012884intel_check_cursor_plane(struct drm_plane *plane,
12885 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070012886{
Matt Roper2b875c22014-12-01 15:40:13 -080012887 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080012888 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080012889 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012890 struct drm_rect *dest = &state->dst;
12891 struct drm_rect *src = &state->src;
12892 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012893 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080012894 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012895 unsigned stride;
12896 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012897
Matt Roperea2c67b2014-12-23 10:41:52 -080012898 crtc = crtc ? crtc : plane->crtc;
12899 intel_crtc = to_intel_crtc(crtc);
12900
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012901 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030012902 src, dest, clip,
12903 DRM_PLANE_HELPER_NO_SCALING,
12904 DRM_PLANE_HELPER_NO_SCALING,
12905 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012906 if (ret)
12907 return ret;
12908
12909
12910 /* if we want to turn off the cursor ignore width and height */
12911 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080012912 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012913
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012914 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080012915 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
12916 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
12917 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012918 return -EINVAL;
12919 }
12920
Matt Roperea2c67b2014-12-23 10:41:52 -080012921 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
12922 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012923 DRM_DEBUG_KMS("buffer is too small\n");
12924 return -ENOMEM;
12925 }
12926
Ville Syrjälä3a656b52015-03-09 21:08:37 +020012927 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012928 DRM_DEBUG_KMS("cursor cannot be tiled\n");
12929 ret = -EINVAL;
12930 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012931
Matt Roper32b7eee2014-12-24 07:59:06 -080012932finish:
12933 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020012934 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080012935 intel_crtc->atomic.update_wm = true;
12936
12937 intel_crtc->atomic.fb_bits |=
12938 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
12939 }
12940
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012941 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012942}
12943
Matt Roperf4a2cf22014-12-01 15:40:12 -080012944static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030012945intel_commit_cursor_plane(struct drm_plane *plane,
12946 struct intel_plane_state *state)
12947{
Matt Roper2b875c22014-12-01 15:40:13 -080012948 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080012949 struct drm_device *dev = plane->dev;
12950 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080012951 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080012952 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070012953
Matt Roperea2c67b2014-12-23 10:41:52 -080012954 crtc = crtc ? crtc : plane->crtc;
12955 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070012956
Matt Roperea2c67b2014-12-23 10:41:52 -080012957 plane->fb = state->base.fb;
12958 crtc->cursor_x = state->base.crtc_x;
12959 crtc->cursor_y = state->base.crtc_y;
12960
Gustavo Padovana912f122014-12-01 15:40:10 -080012961 if (intel_crtc->cursor_bo == obj)
12962 goto update;
12963
Matt Roperf4a2cf22014-12-01 15:40:12 -080012964 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080012965 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080012966 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080012967 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080012968 else
Gustavo Padovana912f122014-12-01 15:40:10 -080012969 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080012970
Gustavo Padovana912f122014-12-01 15:40:10 -080012971 intel_crtc->cursor_addr = addr;
12972 intel_crtc->cursor_bo = obj;
12973update:
Gustavo Padovana912f122014-12-01 15:40:10 -080012974
Matt Roper32b7eee2014-12-24 07:59:06 -080012975 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030012976 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070012977}
Gustavo Padovan852e7872014-09-05 17:22:31 -030012978
Matt Roper3d7d6512014-06-10 08:28:13 -070012979static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
12980 int pipe)
12981{
12982 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080012983 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070012984
12985 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
12986 if (cursor == NULL)
12987 return NULL;
12988
Matt Roper8e7d6882015-01-21 16:35:41 -080012989 state = intel_create_plane_state(&cursor->base);
12990 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080012991 kfree(cursor);
12992 return NULL;
12993 }
Matt Roper8e7d6882015-01-21 16:35:41 -080012994 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080012995
Matt Roper3d7d6512014-06-10 08:28:13 -070012996 cursor->can_scale = false;
12997 cursor->max_downscale = 1;
12998 cursor->pipe = pipe;
12999 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013000 cursor->check_plane = intel_check_cursor_plane;
13001 cursor->commit_plane = intel_commit_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013002
13003 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013004 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013005 intel_cursor_formats,
13006 ARRAY_SIZE(intel_cursor_formats),
13007 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013008
13009 if (INTEL_INFO(dev)->gen >= 4) {
13010 if (!dev->mode_config.rotation_property)
13011 dev->mode_config.rotation_property =
13012 drm_mode_create_rotation_property(dev,
13013 BIT(DRM_ROTATE_0) |
13014 BIT(DRM_ROTATE_180));
13015 if (dev->mode_config.rotation_property)
13016 drm_object_attach_property(&cursor->base.base,
13017 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013018 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013019 }
13020
Matt Roperea2c67b2014-12-23 10:41:52 -080013021 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13022
Matt Roper3d7d6512014-06-10 08:28:13 -070013023 return &cursor->base;
13024}
13025
Hannes Ederb358d0a2008-12-18 21:18:47 +010013026static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013027{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013028 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013029 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013030 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013031 struct drm_plane *primary = NULL;
13032 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013033 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013034
Daniel Vetter955382f2013-09-19 14:05:45 +020013035 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013036 if (intel_crtc == NULL)
13037 return;
13038
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013039 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13040 if (!crtc_state)
13041 goto fail;
13042 intel_crtc_set_state(intel_crtc, crtc_state);
Matt Roper07878242015-02-25 11:43:26 -080013043 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013044
Matt Roper465c1202014-05-29 08:06:54 -070013045 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013046 if (!primary)
13047 goto fail;
13048
13049 cursor = intel_cursor_plane_create(dev, pipe);
13050 if (!cursor)
13051 goto fail;
13052
Matt Roper465c1202014-05-29 08:06:54 -070013053 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013054 cursor, &intel_crtc_funcs);
13055 if (ret)
13056 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013057
13058 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013059 for (i = 0; i < 256; i++) {
13060 intel_crtc->lut_r[i] = i;
13061 intel_crtc->lut_g[i] = i;
13062 intel_crtc->lut_b[i] = i;
13063 }
13064
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013065 /*
13066 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013067 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013068 */
Jesse Barnes80824002009-09-10 15:28:06 -070013069 intel_crtc->pipe = pipe;
13070 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013071 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013072 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013073 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013074 }
13075
Chris Wilson4b0e3332014-05-30 16:35:26 +030013076 intel_crtc->cursor_base = ~0;
13077 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013078 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013079
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013080 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13081 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13082 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13083 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13084
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020013085 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
13086
Jesse Barnes79e53942008-11-07 14:24:08 -080013087 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013088
13089 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013090 return;
13091
13092fail:
13093 if (primary)
13094 drm_plane_cleanup(primary);
13095 if (cursor)
13096 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013097 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013098 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013099}
13100
Jesse Barnes752aa882013-10-31 18:55:49 +020013101enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13102{
13103 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013104 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013105
Rob Clark51fd3712013-11-19 12:10:12 -050013106 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013107
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013108 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013109 return INVALID_PIPE;
13110
13111 return to_intel_crtc(encoder->crtc)->pipe;
13112}
13113
Carl Worth08d7b3d2009-04-29 14:43:54 -070013114int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013115 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013116{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013117 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013118 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013119 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013120
Rob Clark7707e652014-07-17 23:30:04 -040013121 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013122
Rob Clark7707e652014-07-17 23:30:04 -040013123 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013124 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013125 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013126 }
13127
Rob Clark7707e652014-07-17 23:30:04 -040013128 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013129 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013130
Daniel Vetterc05422d2009-08-11 16:05:30 +020013131 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013132}
13133
Daniel Vetter66a92782012-07-12 20:08:18 +020013134static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013135{
Daniel Vetter66a92782012-07-12 20:08:18 +020013136 struct drm_device *dev = encoder->base.dev;
13137 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013138 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013139 int entry = 0;
13140
Damien Lespiaub2784e12014-08-05 11:29:37 +010013141 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013142 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013143 index_mask |= (1 << entry);
13144
Jesse Barnes79e53942008-11-07 14:24:08 -080013145 entry++;
13146 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013147
Jesse Barnes79e53942008-11-07 14:24:08 -080013148 return index_mask;
13149}
13150
Chris Wilson4d302442010-12-14 19:21:29 +000013151static bool has_edp_a(struct drm_device *dev)
13152{
13153 struct drm_i915_private *dev_priv = dev->dev_private;
13154
13155 if (!IS_MOBILE(dev))
13156 return false;
13157
13158 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13159 return false;
13160
Damien Lespiaue3589902014-02-07 19:12:50 +000013161 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013162 return false;
13163
13164 return true;
13165}
13166
Jesse Barnes84b4e042014-06-25 08:24:29 -070013167static bool intel_crt_present(struct drm_device *dev)
13168{
13169 struct drm_i915_private *dev_priv = dev->dev_private;
13170
Damien Lespiau884497e2013-12-03 13:56:23 +000013171 if (INTEL_INFO(dev)->gen >= 9)
13172 return false;
13173
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013174 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013175 return false;
13176
13177 if (IS_CHERRYVIEW(dev))
13178 return false;
13179
13180 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13181 return false;
13182
13183 return true;
13184}
13185
Jesse Barnes79e53942008-11-07 14:24:08 -080013186static void intel_setup_outputs(struct drm_device *dev)
13187{
Eric Anholt725e30a2009-01-22 13:01:02 -080013188 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013189 struct intel_encoder *encoder;
Matt Roperc6f95f22015-01-22 16:50:32 -080013190 struct drm_connector *connector;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013191 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013192
Daniel Vetterc9093352013-06-06 22:22:47 +020013193 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013194
Jesse Barnes84b4e042014-06-25 08:24:29 -070013195 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013196 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013197
Paulo Zanoniaffa9352012-11-23 15:30:39 -020013198 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013199 int found;
13200
Jesse Barnesde31fac2015-03-06 15:53:32 -080013201 /*
13202 * Haswell uses DDI functions to detect digital outputs.
13203 * On SKL pre-D0 the strap isn't connected, so we assume
13204 * it's there.
13205 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013206 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013207 /* WaIgnoreDDIAStrap: skl */
13208 if (found ||
13209 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013210 intel_ddi_init(dev, PORT_A);
13211
13212 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13213 * register */
13214 found = I915_READ(SFUSE_STRAP);
13215
13216 if (found & SFUSE_STRAP_DDIB_DETECTED)
13217 intel_ddi_init(dev, PORT_B);
13218 if (found & SFUSE_STRAP_DDIC_DETECTED)
13219 intel_ddi_init(dev, PORT_C);
13220 if (found & SFUSE_STRAP_DDID_DETECTED)
13221 intel_ddi_init(dev, PORT_D);
13222 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013223 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013224 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013225
13226 if (has_edp_a(dev))
13227 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013228
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013229 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013230 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013231 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013232 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013233 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013234 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013235 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013236 }
13237
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013238 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013239 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013240
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013241 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013242 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013243
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013244 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013245 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013246
Daniel Vetter270b3042012-10-27 15:52:05 +020013247 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013248 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013249 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013250 /*
13251 * The DP_DETECTED bit is the latched state of the DDC
13252 * SDA pin at boot. However since eDP doesn't require DDC
13253 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13254 * eDP ports may have been muxed to an alternate function.
13255 * Thus we can't rely on the DP_DETECTED bit alone to detect
13256 * eDP ports. Consult the VBT as well as DP_DETECTED to
13257 * detect eDP ports.
13258 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013259 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13260 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013261 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13262 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013263 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13264 intel_dp_is_edp(dev, PORT_B))
13265 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013266
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013267 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13268 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070013269 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13270 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013271 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13272 intel_dp_is_edp(dev, PORT_C))
13273 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053013274
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013275 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013276 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013277 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
13278 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013279 /* eDP not supported on port D, so don't check VBT */
13280 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
13281 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013282 }
13283
Jani Nikula3cfca972013-08-27 15:12:26 +030013284 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080013285 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013286 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080013287
Paulo Zanonie2debe92013-02-18 19:00:27 -030013288 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013289 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013290 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013291 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
13292 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013293 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013294 }
Ma Ling27185ae2009-08-24 13:50:23 +080013295
Imre Deake7281ea2013-05-08 13:14:08 +030013296 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013297 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080013298 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013299
13300 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013301
Paulo Zanonie2debe92013-02-18 19:00:27 -030013302 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013303 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013304 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013305 }
Ma Ling27185ae2009-08-24 13:50:23 +080013306
Paulo Zanonie2debe92013-02-18 19:00:27 -030013307 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013308
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013309 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
13310 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013311 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013312 }
Imre Deake7281ea2013-05-08 13:14:08 +030013313 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013314 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080013315 }
Ma Ling27185ae2009-08-24 13:50:23 +080013316
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013317 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030013318 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013319 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070013320 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013321 intel_dvo_init(dev);
13322
Zhenyu Wang103a1962009-11-27 11:44:36 +080013323 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013324 intel_tv_init(dev);
13325
Matt Roperc6f95f22015-01-22 16:50:32 -080013326 /*
13327 * FIXME: We don't have full atomic support yet, but we want to be
13328 * able to enable/test plane updates via the atomic interface in the
13329 * meantime. However as soon as we flip DRIVER_ATOMIC on, the DRM core
13330 * will take some atomic codepaths to lookup properties during
13331 * drmModeGetConnector() that unconditionally dereference
13332 * connector->state.
13333 *
13334 * We create a dummy connector state here for each connector to ensure
13335 * the DRM core doesn't try to dereference a NULL connector->state.
13336 * The actual connector properties will never be updated or contain
13337 * useful information, but since we're doing this specifically for
13338 * testing/debug of the plane operations (and only when a specific
13339 * kernel module option is given), that shouldn't really matter.
13340 *
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020013341 * We are also relying on these states to convert the legacy mode set
13342 * to use a drm_atomic_state struct. The states are kept consistent
13343 * with actual state, so that it is safe to rely on that instead of
13344 * the staged config.
13345 *
Matt Roperc6f95f22015-01-22 16:50:32 -080013346 * Once atomic support for crtc's + connectors lands, this loop should
13347 * be removed since we'll be setting up real connector state, which
13348 * will contain Intel-specific properties.
13349 */
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020013350 list_for_each_entry(connector,
13351 &dev->mode_config.connector_list,
13352 head) {
13353 if (!WARN_ON(connector->state)) {
13354 connector->state = kzalloc(sizeof(*connector->state),
13355 GFP_KERNEL);
Matt Roperc6f95f22015-01-22 16:50:32 -080013356 }
13357 }
13358
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080013359 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070013360
Damien Lespiaub2784e12014-08-05 11:29:37 +010013361 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010013362 encoder->base.possible_crtcs = encoder->crtc_mask;
13363 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020013364 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080013365 }
Chris Wilson47356eb2011-01-11 17:06:04 +000013366
Paulo Zanonidde86e22012-12-01 12:04:25 -020013367 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020013368
13369 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013370}
13371
13372static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
13373{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013374 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080013375 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080013376
Daniel Vetteref2d6332014-02-10 18:00:38 +010013377 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013378 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010013379 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013380 drm_gem_object_unreference(&intel_fb->obj->base);
13381 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013382 kfree(intel_fb);
13383}
13384
13385static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000013386 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080013387 unsigned int *handle)
13388{
13389 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000013390 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080013391
Chris Wilson05394f32010-11-08 19:18:58 +000013392 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080013393}
13394
13395static const struct drm_framebuffer_funcs intel_fb_funcs = {
13396 .destroy = intel_user_framebuffer_destroy,
13397 .create_handle = intel_user_framebuffer_create_handle,
13398};
13399
Damien Lespiaub3218032015-02-27 11:15:18 +000013400static
13401u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
13402 uint32_t pixel_format)
13403{
13404 u32 gen = INTEL_INFO(dev)->gen;
13405
13406 if (gen >= 9) {
13407 /* "The stride in bytes must not exceed the of the size of 8K
13408 * pixels and 32K bytes."
13409 */
13410 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
13411 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
13412 return 32*1024;
13413 } else if (gen >= 4) {
13414 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13415 return 16*1024;
13416 else
13417 return 32*1024;
13418 } else if (gen >= 3) {
13419 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13420 return 8*1024;
13421 else
13422 return 16*1024;
13423 } else {
13424 /* XXX DSPC is limited to 4k tiled */
13425 return 8*1024;
13426 }
13427}
13428
Daniel Vetterb5ea6422014-03-02 21:18:00 +010013429static int intel_framebuffer_init(struct drm_device *dev,
13430 struct intel_framebuffer *intel_fb,
13431 struct drm_mode_fb_cmd2 *mode_cmd,
13432 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080013433{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000013434 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080013435 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000013436 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080013437
Daniel Vetterdd4916c2013-10-09 21:23:51 +020013438 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
13439
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013440 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
13441 /* Enforce that fb modifier and tiling mode match, but only for
13442 * X-tiled. This is needed for FBC. */
13443 if (!!(obj->tiling_mode == I915_TILING_X) !=
13444 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
13445 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
13446 return -EINVAL;
13447 }
13448 } else {
13449 if (obj->tiling_mode == I915_TILING_X)
13450 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
13451 else if (obj->tiling_mode == I915_TILING_Y) {
13452 DRM_DEBUG("No Y tiling for legacy addfb\n");
13453 return -EINVAL;
13454 }
13455 }
13456
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000013457 /* Passed in modifier sanity checking. */
13458 switch (mode_cmd->modifier[0]) {
13459 case I915_FORMAT_MOD_Y_TILED:
13460 case I915_FORMAT_MOD_Yf_TILED:
13461 if (INTEL_INFO(dev)->gen < 9) {
13462 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
13463 mode_cmd->modifier[0]);
13464 return -EINVAL;
13465 }
13466 case DRM_FORMAT_MOD_NONE:
13467 case I915_FORMAT_MOD_X_TILED:
13468 break;
13469 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070013470 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
13471 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010013472 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013473 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013474
Damien Lespiaub3218032015-02-27 11:15:18 +000013475 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
13476 mode_cmd->pixel_format);
13477 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
13478 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
13479 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010013480 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013481 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013482
Damien Lespiaub3218032015-02-27 11:15:18 +000013483 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
13484 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010013485 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000013486 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
13487 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013488 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010013489 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013490 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013491 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013492
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013493 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013494 mode_cmd->pitches[0] != obj->stride) {
13495 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
13496 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013497 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013498 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013499
Ville Syrjälä57779d02012-10-31 17:50:14 +020013500 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013501 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020013502 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020013503 case DRM_FORMAT_RGB565:
13504 case DRM_FORMAT_XRGB8888:
13505 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020013506 break;
13507 case DRM_FORMAT_XRGB1555:
13508 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013509 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013510 DRM_DEBUG("unsupported pixel format: %s\n",
13511 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013512 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013513 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020013514 break;
13515 case DRM_FORMAT_XBGR8888:
13516 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020013517 case DRM_FORMAT_XRGB2101010:
13518 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020013519 case DRM_FORMAT_XBGR2101010:
13520 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013521 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013522 DRM_DEBUG("unsupported pixel format: %s\n",
13523 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013524 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013525 }
Jesse Barnesb5626742011-06-24 12:19:27 -070013526 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020013527 case DRM_FORMAT_YUYV:
13528 case DRM_FORMAT_UYVY:
13529 case DRM_FORMAT_YVYU:
13530 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013531 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013532 DRM_DEBUG("unsupported pixel format: %s\n",
13533 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013534 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013535 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013536 break;
13537 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013538 DRM_DEBUG("unsupported pixel format: %s\n",
13539 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010013540 return -EINVAL;
13541 }
13542
Ville Syrjälä90f9a332012-10-31 17:50:19 +020013543 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
13544 if (mode_cmd->offsets[0] != 0)
13545 return -EINVAL;
13546
Damien Lespiauec2c9812015-01-20 12:51:45 +000013547 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000013548 mode_cmd->pixel_format,
13549 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020013550 /* FIXME drm helper for size checks (especially planar formats)? */
13551 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
13552 return -EINVAL;
13553
Daniel Vetterc7d73f62012-12-13 23:38:38 +010013554 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
13555 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020013556 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010013557
Jesse Barnes79e53942008-11-07 14:24:08 -080013558 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
13559 if (ret) {
13560 DRM_ERROR("framebuffer init failed %d\n", ret);
13561 return ret;
13562 }
13563
Jesse Barnes79e53942008-11-07 14:24:08 -080013564 return 0;
13565}
13566
Jesse Barnes79e53942008-11-07 14:24:08 -080013567static struct drm_framebuffer *
13568intel_user_framebuffer_create(struct drm_device *dev,
13569 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013570 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080013571{
Chris Wilson05394f32010-11-08 19:18:58 +000013572 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080013573
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013574 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
13575 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000013576 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010013577 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080013578
Chris Wilsond2dff872011-04-19 08:36:26 +010013579 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080013580}
13581
Daniel Vetter4520f532013-10-09 09:18:51 +020013582#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020013583static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020013584{
13585}
13586#endif
13587
Jesse Barnes79e53942008-11-07 14:24:08 -080013588static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080013589 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020013590 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080013591 .atomic_check = intel_atomic_check,
13592 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080013593};
13594
Jesse Barnese70236a2009-09-21 10:42:27 -070013595/* Set up chip specific display functions */
13596static void intel_init_display(struct drm_device *dev)
13597{
13598 struct drm_i915_private *dev_priv = dev->dev_private;
13599
Daniel Vetteree9300b2013-06-03 22:40:22 +020013600 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
13601 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030013602 else if (IS_CHERRYVIEW(dev))
13603 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020013604 else if (IS_VALLEYVIEW(dev))
13605 dev_priv->display.find_dpll = vlv_find_best_dpll;
13606 else if (IS_PINEVIEW(dev))
13607 dev_priv->display.find_dpll = pnv_find_best_dpll;
13608 else
13609 dev_priv->display.find_dpll = i9xx_find_best_dpll;
13610
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000013611 if (INTEL_INFO(dev)->gen >= 9) {
13612 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013613 dev_priv->display.get_initial_plane_config =
13614 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000013615 dev_priv->display.crtc_compute_clock =
13616 haswell_crtc_compute_clock;
13617 dev_priv->display.crtc_enable = haswell_crtc_enable;
13618 dev_priv->display.crtc_disable = haswell_crtc_disable;
13619 dev_priv->display.off = ironlake_crtc_off;
13620 dev_priv->display.update_primary_plane =
13621 skylake_update_primary_plane;
13622 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013623 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013624 dev_priv->display.get_initial_plane_config =
13625 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020013626 dev_priv->display.crtc_compute_clock =
13627 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020013628 dev_priv->display.crtc_enable = haswell_crtc_enable;
13629 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030013630 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000013631 dev_priv->display.update_primary_plane =
13632 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030013633 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013634 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013635 dev_priv->display.get_initial_plane_config =
13636 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020013637 dev_priv->display.crtc_compute_clock =
13638 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020013639 dev_priv->display.crtc_enable = ironlake_crtc_enable;
13640 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013641 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070013642 dev_priv->display.update_primary_plane =
13643 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070013644 } else if (IS_VALLEYVIEW(dev)) {
13645 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013646 dev_priv->display.get_initial_plane_config =
13647 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020013648 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070013649 dev_priv->display.crtc_enable = valleyview_crtc_enable;
13650 dev_priv->display.crtc_disable = i9xx_crtc_disable;
13651 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070013652 dev_priv->display.update_primary_plane =
13653 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070013654 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013655 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013656 dev_priv->display.get_initial_plane_config =
13657 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020013658 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020013659 dev_priv->display.crtc_enable = i9xx_crtc_enable;
13660 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013661 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070013662 dev_priv->display.update_primary_plane =
13663 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070013664 }
Jesse Barnese70236a2009-09-21 10:42:27 -070013665
Jesse Barnese70236a2009-09-21 10:42:27 -070013666 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030013667 if (IS_SKYLAKE(dev))
13668 dev_priv->display.get_display_clock_speed =
13669 skylake_get_display_clock_speed;
13670 else if (IS_BROADWELL(dev))
13671 dev_priv->display.get_display_clock_speed =
13672 broadwell_get_display_clock_speed;
13673 else if (IS_HASWELL(dev))
13674 dev_priv->display.get_display_clock_speed =
13675 haswell_get_display_clock_speed;
13676 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070013677 dev_priv->display.get_display_clock_speed =
13678 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030013679 else if (IS_GEN5(dev))
13680 dev_priv->display.get_display_clock_speed =
13681 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030013682 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
13683 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070013684 dev_priv->display.get_display_clock_speed =
13685 i945_get_display_clock_speed;
13686 else if (IS_I915G(dev))
13687 dev_priv->display.get_display_clock_speed =
13688 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020013689 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070013690 dev_priv->display.get_display_clock_speed =
13691 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020013692 else if (IS_PINEVIEW(dev))
13693 dev_priv->display.get_display_clock_speed =
13694 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070013695 else if (IS_I915GM(dev))
13696 dev_priv->display.get_display_clock_speed =
13697 i915gm_get_display_clock_speed;
13698 else if (IS_I865G(dev))
13699 dev_priv->display.get_display_clock_speed =
13700 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020013701 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070013702 dev_priv->display.get_display_clock_speed =
13703 i855_get_display_clock_speed;
13704 else /* 852, 830 */
13705 dev_priv->display.get_display_clock_speed =
13706 i830_get_display_clock_speed;
13707
Jani Nikula7c10a2b2014-10-27 16:26:43 +020013708 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013709 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013710 } else if (IS_GEN6(dev)) {
13711 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013712 } else if (IS_IVYBRIDGE(dev)) {
13713 /* FIXME: detect B0+ stepping and use auto training */
13714 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030013715 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013716 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080013717 } else if (IS_VALLEYVIEW(dev)) {
13718 dev_priv->display.modeset_global_resources =
13719 valleyview_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070013720 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070013721
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070013722 switch (INTEL_INFO(dev)->gen) {
13723 case 2:
13724 dev_priv->display.queue_flip = intel_gen2_queue_flip;
13725 break;
13726
13727 case 3:
13728 dev_priv->display.queue_flip = intel_gen3_queue_flip;
13729 break;
13730
13731 case 4:
13732 case 5:
13733 dev_priv->display.queue_flip = intel_gen4_queue_flip;
13734 break;
13735
13736 case 6:
13737 dev_priv->display.queue_flip = intel_gen6_queue_flip;
13738 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070013739 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070013740 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070013741 dev_priv->display.queue_flip = intel_gen7_queue_flip;
13742 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000013743 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000013744 /* Drop through - unsupported since execlist only. */
13745 default:
13746 /* Default just returns -ENODEV to indicate unsupported */
13747 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070013748 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020013749
13750 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030013751
13752 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070013753}
13754
Jesse Barnesb690e962010-07-19 13:53:12 -070013755/*
13756 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
13757 * resume, or other times. This quirk makes sure that's the case for
13758 * affected systems.
13759 */
Akshay Joshi0206e352011-08-16 15:34:10 -040013760static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070013761{
13762 struct drm_i915_private *dev_priv = dev->dev_private;
13763
13764 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013765 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070013766}
13767
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030013768static void quirk_pipeb_force(struct drm_device *dev)
13769{
13770 struct drm_i915_private *dev_priv = dev->dev_private;
13771
13772 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
13773 DRM_INFO("applying pipe b force quirk\n");
13774}
13775
Keith Packard435793d2011-07-12 14:56:22 -070013776/*
13777 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
13778 */
13779static void quirk_ssc_force_disable(struct drm_device *dev)
13780{
13781 struct drm_i915_private *dev_priv = dev->dev_private;
13782 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013783 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070013784}
13785
Carsten Emde4dca20e2012-03-15 15:56:26 +010013786/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010013787 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
13788 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010013789 */
13790static void quirk_invert_brightness(struct drm_device *dev)
13791{
13792 struct drm_i915_private *dev_priv = dev->dev_private;
13793 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013794 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070013795}
13796
Scot Doyle9c72cc62014-07-03 23:27:50 +000013797/* Some VBT's incorrectly indicate no backlight is present */
13798static void quirk_backlight_present(struct drm_device *dev)
13799{
13800 struct drm_i915_private *dev_priv = dev->dev_private;
13801 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
13802 DRM_INFO("applying backlight present quirk\n");
13803}
13804
Jesse Barnesb690e962010-07-19 13:53:12 -070013805struct intel_quirk {
13806 int device;
13807 int subsystem_vendor;
13808 int subsystem_device;
13809 void (*hook)(struct drm_device *dev);
13810};
13811
Egbert Eich5f85f172012-10-14 15:46:38 +020013812/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
13813struct intel_dmi_quirk {
13814 void (*hook)(struct drm_device *dev);
13815 const struct dmi_system_id (*dmi_id_list)[];
13816};
13817
13818static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
13819{
13820 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
13821 return 1;
13822}
13823
13824static const struct intel_dmi_quirk intel_dmi_quirks[] = {
13825 {
13826 .dmi_id_list = &(const struct dmi_system_id[]) {
13827 {
13828 .callback = intel_dmi_reverse_brightness,
13829 .ident = "NCR Corporation",
13830 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
13831 DMI_MATCH(DMI_PRODUCT_NAME, ""),
13832 },
13833 },
13834 { } /* terminating entry */
13835 },
13836 .hook = quirk_invert_brightness,
13837 },
13838};
13839
Ben Widawskyc43b5632012-04-16 14:07:40 -070013840static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070013841 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040013842 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070013843
Jesse Barnesb690e962010-07-19 13:53:12 -070013844 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
13845 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
13846
Jesse Barnesb690e962010-07-19 13:53:12 -070013847 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
13848 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
13849
Ville Syrjälä5f080c02014-08-15 01:22:06 +030013850 /* 830 needs to leave pipe A & dpll A up */
13851 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
13852
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030013853 /* 830 needs to leave pipe B & dpll B up */
13854 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
13855
Keith Packard435793d2011-07-12 14:56:22 -070013856 /* Lenovo U160 cannot use SSC on LVDS */
13857 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020013858
13859 /* Sony Vaio Y cannot use SSC on LVDS */
13860 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010013861
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010013862 /* Acer Aspire 5734Z must invert backlight brightness */
13863 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
13864
13865 /* Acer/eMachines G725 */
13866 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
13867
13868 /* Acer/eMachines e725 */
13869 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
13870
13871 /* Acer/Packard Bell NCL20 */
13872 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
13873
13874 /* Acer Aspire 4736Z */
13875 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020013876
13877 /* Acer Aspire 5336 */
13878 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000013879
13880 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
13881 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000013882
Scot Doyledfb3d47b2014-08-21 16:08:02 +000013883 /* Acer C720 Chromebook (Core i3 4005U) */
13884 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
13885
jens steinb2a96012014-10-28 20:25:53 +010013886 /* Apple Macbook 2,1 (Core 2 T7400) */
13887 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
13888
Scot Doyled4967d82014-07-03 23:27:52 +000013889 /* Toshiba CB35 Chromebook (Celeron 2955U) */
13890 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000013891
13892 /* HP Chromebook 14 (Celeron 2955U) */
13893 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020013894
13895 /* Dell Chromebook 11 */
13896 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070013897};
13898
13899static void intel_init_quirks(struct drm_device *dev)
13900{
13901 struct pci_dev *d = dev->pdev;
13902 int i;
13903
13904 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
13905 struct intel_quirk *q = &intel_quirks[i];
13906
13907 if (d->device == q->device &&
13908 (d->subsystem_vendor == q->subsystem_vendor ||
13909 q->subsystem_vendor == PCI_ANY_ID) &&
13910 (d->subsystem_device == q->subsystem_device ||
13911 q->subsystem_device == PCI_ANY_ID))
13912 q->hook(dev);
13913 }
Egbert Eich5f85f172012-10-14 15:46:38 +020013914 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
13915 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
13916 intel_dmi_quirks[i].hook(dev);
13917 }
Jesse Barnesb690e962010-07-19 13:53:12 -070013918}
13919
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013920/* Disable the VGA plane that we never use */
13921static void i915_disable_vga(struct drm_device *dev)
13922{
13923 struct drm_i915_private *dev_priv = dev->dev_private;
13924 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020013925 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013926
Ville Syrjälä2b37c612014-01-22 21:32:38 +020013927 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013928 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070013929 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013930 sr1 = inb(VGA_SR_DATA);
13931 outb(sr1 | 1<<5, VGA_SR_DATA);
13932 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
13933 udelay(300);
13934
Ville Syrjälä01f5a622014-12-16 18:38:37 +020013935 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013936 POSTING_READ(vga_reg);
13937}
13938
Daniel Vetterf8175862012-04-10 15:50:11 +020013939void intel_modeset_init_hw(struct drm_device *dev)
13940{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030013941 intel_prepare_ddi(dev);
13942
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030013943 if (IS_VALLEYVIEW(dev))
13944 vlv_update_cdclk(dev);
13945
Daniel Vetterf8175862012-04-10 15:50:11 +020013946 intel_init_clock_gating(dev);
13947
Daniel Vetter8090c6b2012-06-24 16:42:32 +020013948 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020013949}
13950
Jesse Barnes79e53942008-11-07 14:24:08 -080013951void intel_modeset_init(struct drm_device *dev)
13952{
Jesse Barnes652c3932009-08-17 13:31:43 -070013953 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000013954 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000013955 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080013956 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080013957
13958 drm_mode_config_init(dev);
13959
13960 dev->mode_config.min_width = 0;
13961 dev->mode_config.min_height = 0;
13962
Dave Airlie019d96c2011-09-29 16:20:42 +010013963 dev->mode_config.preferred_depth = 24;
13964 dev->mode_config.prefer_shadow = 1;
13965
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000013966 dev->mode_config.allow_fb_modifiers = true;
13967
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020013968 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080013969
Jesse Barnesb690e962010-07-19 13:53:12 -070013970 intel_init_quirks(dev);
13971
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030013972 intel_init_pm(dev);
13973
Ben Widawskye3c74752013-04-05 13:12:39 -070013974 if (INTEL_INFO(dev)->num_pipes == 0)
13975 return;
13976
Jesse Barnese70236a2009-09-21 10:42:27 -070013977 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020013978 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070013979
Chris Wilsona6c45cf2010-09-17 00:32:17 +010013980 if (IS_GEN2(dev)) {
13981 dev->mode_config.max_width = 2048;
13982 dev->mode_config.max_height = 2048;
13983 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070013984 dev->mode_config.max_width = 4096;
13985 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080013986 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010013987 dev->mode_config.max_width = 8192;
13988 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080013989 }
Damien Lespiau068be562014-03-28 14:17:49 +000013990
Ville Syrjälädc41c152014-08-13 11:57:05 +030013991 if (IS_845G(dev) || IS_I865G(dev)) {
13992 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
13993 dev->mode_config.cursor_height = 1023;
13994 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000013995 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
13996 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
13997 } else {
13998 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
13999 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14000 }
14001
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014002 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014003
Zhao Yakui28c97732009-10-09 11:39:41 +080014004 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014005 INTEL_INFO(dev)->num_pipes,
14006 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014007
Damien Lespiau055e3932014-08-18 13:49:10 +010014008 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014009 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014010 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014011 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014012 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014013 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014014 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014015 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014016 }
14017
Jesse Barnesf42bb702013-12-16 16:34:23 -080014018 intel_init_dpio(dev);
14019
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014020 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014021
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014022 /* Just disable it once at startup */
14023 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014024 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014025
14026 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014027 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014028
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014029 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014030 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014031 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014032
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014033 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080014034 if (!crtc->active)
14035 continue;
14036
Jesse Barnes46f297f2014-03-07 08:57:48 -080014037 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014038 * Note that reserving the BIOS fb up front prevents us
14039 * from stuffing other stolen allocations like the ring
14040 * on top. This prevents some ugliness at boot time, and
14041 * can even allow for smooth boot transitions if the BIOS
14042 * fb is large enough for the active pipe configuration.
14043 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014044 if (dev_priv->display.get_initial_plane_config) {
14045 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080014046 &crtc->plane_config);
14047 /*
14048 * If the fb is shared between multiple heads, we'll
14049 * just get the first one.
14050 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010014051 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014052 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080014053 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014054}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014055
Daniel Vetter7fad7982012-07-04 17:51:47 +020014056static void intel_enable_pipe_a(struct drm_device *dev)
14057{
14058 struct intel_connector *connector;
14059 struct drm_connector *crt = NULL;
14060 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014061 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014062
14063 /* We can't just switch on the pipe A, we need to set things up with a
14064 * proper mode and output configuration. As a gross hack, enable pipe A
14065 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014066 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014067 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14068 crt = &connector->base;
14069 break;
14070 }
14071 }
14072
14073 if (!crt)
14074 return;
14075
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014076 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014077 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014078}
14079
Daniel Vetterfa555832012-10-10 23:14:00 +020014080static bool
14081intel_check_plane_mapping(struct intel_crtc *crtc)
14082{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014083 struct drm_device *dev = crtc->base.dev;
14084 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014085 u32 reg, val;
14086
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014087 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014088 return true;
14089
14090 reg = DSPCNTR(!crtc->plane);
14091 val = I915_READ(reg);
14092
14093 if ((val & DISPLAY_PLANE_ENABLE) &&
14094 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14095 return false;
14096
14097 return true;
14098}
14099
Daniel Vetter24929352012-07-02 20:28:59 +020014100static void intel_sanitize_crtc(struct intel_crtc *crtc)
14101{
14102 struct drm_device *dev = crtc->base.dev;
14103 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014104 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014105
Daniel Vetter24929352012-07-02 20:28:59 +020014106 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014107 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014108 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14109
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014110 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014111 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014112 if (crtc->active) {
14113 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014114 drm_crtc_vblank_on(&crtc->base);
14115 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014116
Daniel Vetter24929352012-07-02 20:28:59 +020014117 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014118 * disable the crtc (and hence change the state) if it is wrong. Note
14119 * that gen4+ has a fixed plane -> pipe mapping. */
14120 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014121 struct intel_connector *connector;
14122 bool plane;
14123
Daniel Vetter24929352012-07-02 20:28:59 +020014124 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14125 crtc->base.base.id);
14126
14127 /* Pipe has the wrong plane attached and the plane is active.
14128 * Temporarily change the plane mapping and disable everything
14129 * ... */
14130 plane = crtc->plane;
14131 crtc->plane = !plane;
Daniel Vetter9c8958b2014-07-14 19:35:31 +020014132 crtc->primary_enabled = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014133 dev_priv->display.crtc_disable(&crtc->base);
14134 crtc->plane = plane;
14135
14136 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014137 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014138 if (connector->encoder->base.crtc != &crtc->base)
14139 continue;
14140
Egbert Eich7f1950f2014-04-25 10:56:22 +020014141 connector->base.dpms = DRM_MODE_DPMS_OFF;
14142 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014143 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014144 /* multiple connectors may have the same encoder:
14145 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014146 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020014147 if (connector->encoder->base.crtc == &crtc->base) {
14148 connector->encoder->base.crtc = NULL;
14149 connector->encoder->connectors_active = false;
14150 }
Daniel Vetter24929352012-07-02 20:28:59 +020014151
14152 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080014153 crtc->base.state->enable = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014154 crtc->base.enabled = false;
14155 }
Daniel Vetter24929352012-07-02 20:28:59 +020014156
Daniel Vetter7fad7982012-07-04 17:51:47 +020014157 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14158 crtc->pipe == PIPE_A && !crtc->active) {
14159 /* BIOS forgot to enable pipe A, this mostly happens after
14160 * resume. Force-enable the pipe to fix this, the update_dpms
14161 * call below we restore the pipe to the right state, but leave
14162 * the required bits on. */
14163 intel_enable_pipe_a(dev);
14164 }
14165
Daniel Vetter24929352012-07-02 20:28:59 +020014166 /* Adjust the state of the output pipe according to whether we
14167 * have active connectors/encoders. */
14168 intel_crtc_update_dpms(&crtc->base);
14169
Matt Roper83d65732015-02-25 13:12:16 -080014170 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020014171 struct intel_encoder *encoder;
14172
14173 /* This can happen either due to bugs in the get_hw_state
14174 * functions or because the pipe is force-enabled due to the
14175 * pipe A quirk. */
14176 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14177 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014178 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014179 crtc->active ? "enabled" : "disabled");
14180
Matt Roper83d65732015-02-25 13:12:16 -080014181 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014182 crtc->base.enabled = crtc->active;
14183
14184 /* Because we only establish the connector -> encoder ->
14185 * crtc links if something is active, this means the
14186 * crtc is now deactivated. Break the links. connector
14187 * -> encoder links are only establish when things are
14188 * actually up, hence no need to break them. */
14189 WARN_ON(crtc->active);
14190
14191 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14192 WARN_ON(encoder->connectors_active);
14193 encoder->base.crtc = NULL;
14194 }
14195 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014196
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014197 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014198 /*
14199 * We start out with underrun reporting disabled to avoid races.
14200 * For correct bookkeeping mark this on active crtcs.
14201 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014202 * Also on gmch platforms we dont have any hardware bits to
14203 * disable the underrun reporting. Which means we need to start
14204 * out with underrun reporting disabled also on inactive pipes,
14205 * since otherwise we'll complain about the garbage we read when
14206 * e.g. coming up after runtime pm.
14207 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014208 * No protection against concurrent access is required - at
14209 * worst a fifo underrun happens which also sets this to false.
14210 */
14211 crtc->cpu_fifo_underrun_disabled = true;
14212 crtc->pch_fifo_underrun_disabled = true;
14213 }
Daniel Vetter24929352012-07-02 20:28:59 +020014214}
14215
14216static void intel_sanitize_encoder(struct intel_encoder *encoder)
14217{
14218 struct intel_connector *connector;
14219 struct drm_device *dev = encoder->base.dev;
14220
14221 /* We need to check both for a crtc link (meaning that the
14222 * encoder is active and trying to read from a pipe) and the
14223 * pipe itself being active. */
14224 bool has_active_crtc = encoder->base.crtc &&
14225 to_intel_crtc(encoder->base.crtc)->active;
14226
14227 if (encoder->connectors_active && !has_active_crtc) {
14228 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14229 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014230 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014231
14232 /* Connector is active, but has no active pipe. This is
14233 * fallout from our resume register restoring. Disable
14234 * the encoder manually again. */
14235 if (encoder->base.crtc) {
14236 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14237 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014238 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014239 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014240 if (encoder->post_disable)
14241 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014242 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014243 encoder->base.crtc = NULL;
14244 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014245
14246 /* Inconsistent output/port/pipe state happens presumably due to
14247 * a bug in one of the get_hw_state functions. Or someplace else
14248 * in our code, like the register restore mess on resume. Clamp
14249 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014250 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014251 if (connector->encoder != encoder)
14252 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014253 connector->base.dpms = DRM_MODE_DPMS_OFF;
14254 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014255 }
14256 }
14257 /* Enabled encoders without active connectors will be fixed in
14258 * the crtc fixup. */
14259}
14260
Imre Deak04098752014-02-18 00:02:16 +020014261void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014262{
14263 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014264 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014265
Imre Deak04098752014-02-18 00:02:16 +020014266 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14267 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14268 i915_disable_vga(dev);
14269 }
14270}
14271
14272void i915_redisable_vga(struct drm_device *dev)
14273{
14274 struct drm_i915_private *dev_priv = dev->dev_private;
14275
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014276 /* This function can be called both from intel_modeset_setup_hw_state or
14277 * at a very early point in our resume sequence, where the power well
14278 * structures are not yet restored. Since this function is at a very
14279 * paranoid "someone might have enabled VGA while we were not looking"
14280 * level, just check if the power well is enabled instead of trying to
14281 * follow the "don't touch the power well if we don't need it" policy
14282 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014283 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014284 return;
14285
Imre Deak04098752014-02-18 00:02:16 +020014286 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014287}
14288
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014289static bool primary_get_hw_state(struct intel_crtc *crtc)
14290{
14291 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14292
14293 if (!crtc->active)
14294 return false;
14295
14296 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
14297}
14298
Daniel Vetter30e984d2013-06-05 13:34:17 +020014299static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020014300{
14301 struct drm_i915_private *dev_priv = dev->dev_private;
14302 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020014303 struct intel_crtc *crtc;
14304 struct intel_encoder *encoder;
14305 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020014306 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020014307
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014308 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014309 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020014310
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014311 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020014312
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014313 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014314 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014315
Matt Roper83d65732015-02-25 13:12:16 -080014316 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014317 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014318 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020014319
14320 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
14321 crtc->base.base.id,
14322 crtc->active ? "enabled" : "disabled");
14323 }
14324
Daniel Vetter53589012013-06-05 13:34:16 +020014325 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14326 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14327
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014328 pll->on = pll->get_hw_state(dev_priv, pll,
14329 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020014330 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014331 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014332 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014333 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020014334 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014335 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014336 }
Daniel Vetter53589012013-06-05 13:34:16 +020014337 }
Daniel Vetter53589012013-06-05 13:34:16 +020014338
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014339 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014340 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014341
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014342 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014343 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020014344 }
14345
Damien Lespiaub2784e12014-08-05 11:29:37 +010014346 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014347 pipe = 0;
14348
14349 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070014350 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14351 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014352 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014353 } else {
14354 encoder->base.crtc = NULL;
14355 }
14356
14357 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014358 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020014359 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014360 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014361 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014362 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020014363 }
14364
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014365 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014366 if (connector->get_hw_state(connector)) {
14367 connector->base.dpms = DRM_MODE_DPMS_ON;
14368 connector->encoder->connectors_active = true;
14369 connector->base.encoder = &connector->encoder->base;
14370 } else {
14371 connector->base.dpms = DRM_MODE_DPMS_OFF;
14372 connector->base.encoder = NULL;
14373 }
14374 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
14375 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030014376 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014377 connector->base.encoder ? "enabled" : "disabled");
14378 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020014379}
14380
14381/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
14382 * and i915 state tracking structures. */
14383void intel_modeset_setup_hw_state(struct drm_device *dev,
14384 bool force_restore)
14385{
14386 struct drm_i915_private *dev_priv = dev->dev_private;
14387 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014388 struct intel_crtc *crtc;
14389 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020014390 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014391
14392 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020014393
Jesse Barnesbabea612013-06-26 18:57:38 +030014394 /*
14395 * Now that we have the config, copy it to each CRTC struct
14396 * Note that this could go away if we move to using crtc_config
14397 * checking everywhere.
14398 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014399 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020014400 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014401 intel_mode_from_pipe_config(&crtc->base.mode,
14402 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030014403 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
14404 crtc->base.base.id);
14405 drm_mode_debug_printmodeline(&crtc->base.mode);
14406 }
14407 }
14408
Daniel Vetter24929352012-07-02 20:28:59 +020014409 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010014410 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014411 intel_sanitize_encoder(encoder);
14412 }
14413
Damien Lespiau055e3932014-08-18 13:49:10 +010014414 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020014415 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14416 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014417 intel_dump_pipe_config(crtc, crtc->config,
14418 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020014419 }
Daniel Vetter9a935852012-07-05 22:34:27 +020014420
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020014421 intel_modeset_update_connector_atomic_state(dev);
14422
Daniel Vetter35c95372013-07-17 06:55:04 +020014423 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14424 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14425
14426 if (!pll->on || pll->active)
14427 continue;
14428
14429 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
14430
14431 pll->disable(dev_priv, pll);
14432 pll->on = false;
14433 }
14434
Pradeep Bhat30789992014-11-04 17:06:45 +000014435 if (IS_GEN9(dev))
14436 skl_wm_get_hw_state(dev);
14437 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030014438 ilk_wm_get_hw_state(dev);
14439
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010014440 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030014441 i915_redisable_vga(dev);
14442
Daniel Vetterf30da182013-04-11 20:22:50 +020014443 /*
14444 * We need to use raw interfaces for restoring state to avoid
14445 * checking (bogus) intermediate states.
14446 */
Damien Lespiau055e3932014-08-18 13:49:10 +010014447 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070014448 struct drm_crtc *crtc =
14449 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020014450
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014451 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010014452 }
14453 } else {
14454 intel_modeset_update_staged_output_state(dev);
14455 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020014456
14457 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010014458}
14459
14460void intel_modeset_gem_init(struct drm_device *dev)
14461{
Jesse Barnes92122782014-10-09 12:57:42 -070014462 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080014463 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070014464 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -080014465
Imre Deakae484342014-03-31 15:10:44 +030014466 mutex_lock(&dev->struct_mutex);
14467 intel_init_gt_powersave(dev);
14468 mutex_unlock(&dev->struct_mutex);
14469
Jesse Barnes92122782014-10-09 12:57:42 -070014470 /*
14471 * There may be no VBT; and if the BIOS enabled SSC we can
14472 * just keep using it to avoid unnecessary flicker. Whereas if the
14473 * BIOS isn't using it, don't assume it will work even if the VBT
14474 * indicates as much.
14475 */
14476 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
14477 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14478 DREF_SSC1_ENABLE);
14479
Chris Wilson1833b132012-05-09 11:56:28 +010014480 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020014481
14482 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080014483
14484 /*
14485 * Make sure any fbs we allocated at startup are properly
14486 * pinned & fenced. When we do the allocation it's too early
14487 * for this.
14488 */
14489 mutex_lock(&dev->struct_mutex);
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010014490 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070014491 obj = intel_fb_obj(c->primary->fb);
14492 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080014493 continue;
14494
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +000014495 if (intel_pin_and_fence_fb_obj(c->primary,
14496 c->primary->fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000014497 c->primary->state,
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +000014498 NULL)) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080014499 DRM_ERROR("failed to pin boot fb on pipe %d\n",
14500 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100014501 drm_framebuffer_unreference(c->primary->fb);
14502 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080014503 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080014504 }
14505 }
14506 mutex_unlock(&dev->struct_mutex);
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020014507
14508 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014509}
14510
Imre Deak4932e2c2014-02-11 17:12:48 +020014511void intel_connector_unregister(struct intel_connector *intel_connector)
14512{
14513 struct drm_connector *connector = &intel_connector->base;
14514
14515 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010014516 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020014517}
14518
Jesse Barnes79e53942008-11-07 14:24:08 -080014519void intel_modeset_cleanup(struct drm_device *dev)
14520{
Jesse Barnes652c3932009-08-17 13:31:43 -070014521 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030014522 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070014523
Imre Deak2eb52522014-11-19 15:30:05 +020014524 intel_disable_gt_powersave(dev);
14525
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020014526 intel_backlight_unregister(dev);
14527
Daniel Vetterfd0c0642013-04-24 11:13:35 +020014528 /*
14529 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020014530 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020014531 * experience fancy races otherwise.
14532 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020014533 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070014534
Daniel Vetterfd0c0642013-04-24 11:13:35 +020014535 /*
14536 * Due to the hpd irq storm handling the hotplug work can re-arm the
14537 * poll handlers. Hence disable polling after hpd handling is shut down.
14538 */
Keith Packardf87ea762010-10-03 19:36:26 -070014539 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020014540
Jesse Barnes652c3932009-08-17 13:31:43 -070014541 mutex_lock(&dev->struct_mutex);
14542
Jesse Barnes723bfd72010-10-07 16:01:13 -070014543 intel_unregister_dsm_handler();
14544
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014545 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014546
Kristian Høgsberg69341a52009-11-11 12:19:17 -050014547 mutex_unlock(&dev->struct_mutex);
14548
Chris Wilson1630fe72011-07-08 12:22:42 +010014549 /* flush any delayed tasks or pending work */
14550 flush_scheduled_work();
14551
Jani Nikuladb31af1d2013-11-08 16:48:53 +020014552 /* destroy the backlight and sysfs files before encoders/connectors */
14553 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020014554 struct intel_connector *intel_connector;
14555
14556 intel_connector = to_intel_connector(connector);
14557 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020014558 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030014559
Jesse Barnes79e53942008-11-07 14:24:08 -080014560 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010014561
14562 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030014563
14564 mutex_lock(&dev->struct_mutex);
14565 intel_cleanup_gt_powersave(dev);
14566 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014567}
14568
Dave Airlie28d52042009-09-21 14:33:58 +100014569/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080014570 * Return which encoder is currently attached for connector.
14571 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010014572struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080014573{
Chris Wilsondf0e9242010-09-09 16:20:55 +010014574 return &intel_attached_encoder(connector)->base;
14575}
Jesse Barnes79e53942008-11-07 14:24:08 -080014576
Chris Wilsondf0e9242010-09-09 16:20:55 +010014577void intel_connector_attach_encoder(struct intel_connector *connector,
14578 struct intel_encoder *encoder)
14579{
14580 connector->encoder = encoder;
14581 drm_mode_connector_attach_encoder(&connector->base,
14582 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080014583}
Dave Airlie28d52042009-09-21 14:33:58 +100014584
14585/*
14586 * set vga decode state - true == enable VGA decode
14587 */
14588int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
14589{
14590 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000014591 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100014592 u16 gmch_ctrl;
14593
Chris Wilson75fa0412014-02-07 18:37:02 -020014594 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
14595 DRM_ERROR("failed to read control word\n");
14596 return -EIO;
14597 }
14598
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020014599 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
14600 return 0;
14601
Dave Airlie28d52042009-09-21 14:33:58 +100014602 if (state)
14603 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
14604 else
14605 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020014606
14607 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
14608 DRM_ERROR("failed to write control word\n");
14609 return -EIO;
14610 }
14611
Dave Airlie28d52042009-09-21 14:33:58 +100014612 return 0;
14613}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014614
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014615struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030014616
14617 u32 power_well_driver;
14618
Chris Wilson63b66e52013-08-08 15:12:06 +020014619 int num_transcoders;
14620
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014621 struct intel_cursor_error_state {
14622 u32 control;
14623 u32 position;
14624 u32 base;
14625 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010014626 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014627
14628 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020014629 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014630 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030014631 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010014632 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014633
14634 struct intel_plane_error_state {
14635 u32 control;
14636 u32 stride;
14637 u32 size;
14638 u32 pos;
14639 u32 addr;
14640 u32 surface;
14641 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010014642 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020014643
14644 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020014645 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020014646 enum transcoder cpu_transcoder;
14647
14648 u32 conf;
14649
14650 u32 htotal;
14651 u32 hblank;
14652 u32 hsync;
14653 u32 vtotal;
14654 u32 vblank;
14655 u32 vsync;
14656 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014657};
14658
14659struct intel_display_error_state *
14660intel_display_capture_error_state(struct drm_device *dev)
14661{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014662 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014663 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020014664 int transcoders[] = {
14665 TRANSCODER_A,
14666 TRANSCODER_B,
14667 TRANSCODER_C,
14668 TRANSCODER_EDP,
14669 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014670 int i;
14671
Chris Wilson63b66e52013-08-08 15:12:06 +020014672 if (INTEL_INFO(dev)->num_pipes == 0)
14673 return NULL;
14674
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020014675 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014676 if (error == NULL)
14677 return NULL;
14678
Imre Deak190be112013-11-25 17:15:31 +020014679 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030014680 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
14681
Damien Lespiau055e3932014-08-18 13:49:10 +010014682 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020014683 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014684 __intel_display_power_is_enabled(dev_priv,
14685 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020014686 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020014687 continue;
14688
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030014689 error->cursor[i].control = I915_READ(CURCNTR(i));
14690 error->cursor[i].position = I915_READ(CURPOS(i));
14691 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014692
14693 error->plane[i].control = I915_READ(DSPCNTR(i));
14694 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014695 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030014696 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014697 error->plane[i].pos = I915_READ(DSPPOS(i));
14698 }
Paulo Zanonica291362013-03-06 20:03:14 -030014699 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
14700 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014701 if (INTEL_INFO(dev)->gen >= 4) {
14702 error->plane[i].surface = I915_READ(DSPSURF(i));
14703 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
14704 }
14705
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014706 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030014707
Sonika Jindal3abfce72014-07-21 15:23:43 +053014708 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030014709 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020014710 }
14711
14712 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
14713 if (HAS_DDI(dev_priv->dev))
14714 error->num_transcoders++; /* Account for eDP. */
14715
14716 for (i = 0; i < error->num_transcoders; i++) {
14717 enum transcoder cpu_transcoder = transcoders[i];
14718
Imre Deakddf9c532013-11-27 22:02:02 +020014719 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014720 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020014721 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020014722 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020014723 continue;
14724
Chris Wilson63b66e52013-08-08 15:12:06 +020014725 error->transcoder[i].cpu_transcoder = cpu_transcoder;
14726
14727 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
14728 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
14729 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
14730 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
14731 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
14732 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
14733 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014734 }
14735
14736 return error;
14737}
14738
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014739#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
14740
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014741void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014742intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014743 struct drm_device *dev,
14744 struct intel_display_error_state *error)
14745{
Damien Lespiau055e3932014-08-18 13:49:10 +010014746 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014747 int i;
14748
Chris Wilson63b66e52013-08-08 15:12:06 +020014749 if (!error)
14750 return;
14751
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014752 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020014753 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014754 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030014755 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010014756 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014757 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020014758 err_printf(m, " Power: %s\n",
14759 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014760 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030014761 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014762
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014763 err_printf(m, "Plane [%d]:\n", i);
14764 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
14765 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014766 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014767 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
14768 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014769 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030014770 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014771 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014772 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014773 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
14774 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014775 }
14776
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014777 err_printf(m, "Cursor [%d]:\n", i);
14778 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
14779 err_printf(m, " POS: %08x\n", error->cursor[i].position);
14780 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014781 }
Chris Wilson63b66e52013-08-08 15:12:06 +020014782
14783 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010014784 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020014785 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020014786 err_printf(m, " Power: %s\n",
14787 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020014788 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
14789 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
14790 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
14791 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
14792 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
14793 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
14794 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
14795 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014796}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014797
14798void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
14799{
14800 struct intel_crtc *crtc;
14801
14802 for_each_intel_crtc(dev, crtc) {
14803 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014804
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020014805 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014806
14807 work = crtc->unpin_work;
14808
14809 if (work && work->event &&
14810 work->event->base.file_priv == file) {
14811 kfree(work->event);
14812 work->event = NULL;
14813 }
14814
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020014815 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014816 }
14817}