blob: 73f5e5b01125a73a23495274fee9d6193a4de18a [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 for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010049static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010050 DRM_FORMAT_C8,
51 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070052 DRM_FORMAT_XRGB1555,
53 DRM_FORMAT_ARGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010054 DRM_FORMAT_XRGB8888,
55 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070056};
57
58/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010059static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010060 DRM_FORMAT_C8,
61 DRM_FORMAT_RGB565,
62 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070063 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010064 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070065 DRM_FORMAT_ABGR8888,
66 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070067 DRM_FORMAT_XBGR2101010,
Matt Roper465c1202014-05-29 08:06:54 -070068};
69
Matt Roper3d7d6512014-06-10 08:28:13 -070070/* Cursor formats */
71static const uint32_t intel_cursor_formats[] = {
72 DRM_FORMAT_ARGB8888,
73};
74
Chris Wilson6b383a72010-09-13 13:54:26 +010075static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080076
Jesse Barnesf1f644d2013-06-27 00:39:25 +030077static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020078 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030079static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020080 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030081
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030082static int intel_set_mode(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020083 struct drm_atomic_state *state);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080084static int intel_framebuffer_init(struct drm_device *dev,
85 struct intel_framebuffer *ifb,
86 struct drm_mode_fb_cmd2 *mode_cmd,
87 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020088static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
89static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020090static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070091 struct intel_link_m_n *m_n,
92 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +020093static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +020094static void haswell_set_pipeconf(struct drm_crtc *crtc);
95static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +020096static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020097 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +020098static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020099 const struct intel_crtc_state *pipe_config);
Matt Roperea2c67b2014-12-23 10:41:52 -0800100static void intel_begin_crtc_commit(struct drm_crtc *crtc);
101static void intel_finish_crtc_commit(struct drm_crtc *crtc);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700102static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
103 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200104static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
105 int num_connectors);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +0300106static void intel_crtc_enable_planes(struct drm_crtc *crtc);
107static void intel_crtc_disable_planes(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100108
Dave Airlie0e32b392014-05-02 14:02:48 +1000109static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
110{
111 if (!connector->mst_port)
112 return connector->encoder;
113 else
114 return &connector->mst_port->mst_encoders[pipe]->base;
115}
116
Jesse Barnes79e53942008-11-07 14:24:08 -0800117typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400118 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800119} intel_range_t;
120
121typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400122 int dot_limit;
123 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800124} intel_p2_t;
125
Ma Lingd4906092009-03-18 20:13:27 +0800126typedef struct intel_limit intel_limit_t;
127struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400128 intel_range_t dot, vco, n, m, m1, m2, p, p1;
129 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800130};
Jesse Barnes79e53942008-11-07 14:24:08 -0800131
Daniel Vetterd2acd212012-10-20 20:57:43 +0200132int
133intel_pch_rawclk(struct drm_device *dev)
134{
135 struct drm_i915_private *dev_priv = dev->dev_private;
136
137 WARN_ON(!HAS_PCH_SPLIT(dev));
138
139 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
140}
141
Chris Wilson021357a2010-09-07 20:54:59 +0100142static inline u32 /* units of 100MHz */
143intel_fdi_link_freq(struct drm_device *dev)
144{
Chris Wilson8b99e682010-10-13 09:59:17 +0100145 if (IS_GEN5(dev)) {
146 struct drm_i915_private *dev_priv = dev->dev_private;
147 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
148 } else
149 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100150}
151
Daniel Vetter5d536e22013-07-06 12:52:06 +0200152static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400153 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200154 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200155 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400156 .m = { .min = 96, .max = 140 },
157 .m1 = { .min = 18, .max = 26 },
158 .m2 = { .min = 6, .max = 16 },
159 .p = { .min = 4, .max = 128 },
160 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700161 .p2 = { .dot_limit = 165000,
162 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700163};
164
Daniel Vetter5d536e22013-07-06 12:52:06 +0200165static const intel_limit_t intel_limits_i8xx_dvo = {
166 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200167 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200168 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200169 .m = { .min = 96, .max = 140 },
170 .m1 = { .min = 18, .max = 26 },
171 .m2 = { .min = 6, .max = 16 },
172 .p = { .min = 4, .max = 128 },
173 .p1 = { .min = 2, .max = 33 },
174 .p2 = { .dot_limit = 165000,
175 .p2_slow = 4, .p2_fast = 4 },
176};
177
Keith Packarde4b36692009-06-05 19:22:17 -0700178static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400179 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200180 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200181 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400182 .m = { .min = 96, .max = 140 },
183 .m1 = { .min = 18, .max = 26 },
184 .m2 = { .min = 6, .max = 16 },
185 .p = { .min = 4, .max = 128 },
186 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700187 .p2 = { .dot_limit = 165000,
188 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700189};
Eric Anholt273e27c2011-03-30 13:01:10 -0700190
Keith Packarde4b36692009-06-05 19:22:17 -0700191static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400192 .dot = { .min = 20000, .max = 400000 },
193 .vco = { .min = 1400000, .max = 2800000 },
194 .n = { .min = 1, .max = 6 },
195 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100196 .m1 = { .min = 8, .max = 18 },
197 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400198 .p = { .min = 5, .max = 80 },
199 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700200 .p2 = { .dot_limit = 200000,
201 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700202};
203
204static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400205 .dot = { .min = 20000, .max = 400000 },
206 .vco = { .min = 1400000, .max = 2800000 },
207 .n = { .min = 1, .max = 6 },
208 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100209 .m1 = { .min = 8, .max = 18 },
210 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400211 .p = { .min = 7, .max = 98 },
212 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700213 .p2 = { .dot_limit = 112000,
214 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700215};
216
Eric Anholt273e27c2011-03-30 13:01:10 -0700217
Keith Packarde4b36692009-06-05 19:22:17 -0700218static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700219 .dot = { .min = 25000, .max = 270000 },
220 .vco = { .min = 1750000, .max = 3500000},
221 .n = { .min = 1, .max = 4 },
222 .m = { .min = 104, .max = 138 },
223 .m1 = { .min = 17, .max = 23 },
224 .m2 = { .min = 5, .max = 11 },
225 .p = { .min = 10, .max = 30 },
226 .p1 = { .min = 1, .max = 3},
227 .p2 = { .dot_limit = 270000,
228 .p2_slow = 10,
229 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800230 },
Keith Packarde4b36692009-06-05 19:22:17 -0700231};
232
233static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700234 .dot = { .min = 22000, .max = 400000 },
235 .vco = { .min = 1750000, .max = 3500000},
236 .n = { .min = 1, .max = 4 },
237 .m = { .min = 104, .max = 138 },
238 .m1 = { .min = 16, .max = 23 },
239 .m2 = { .min = 5, .max = 11 },
240 .p = { .min = 5, .max = 80 },
241 .p1 = { .min = 1, .max = 8},
242 .p2 = { .dot_limit = 165000,
243 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700244};
245
246static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700247 .dot = { .min = 20000, .max = 115000 },
248 .vco = { .min = 1750000, .max = 3500000 },
249 .n = { .min = 1, .max = 3 },
250 .m = { .min = 104, .max = 138 },
251 .m1 = { .min = 17, .max = 23 },
252 .m2 = { .min = 5, .max = 11 },
253 .p = { .min = 28, .max = 112 },
254 .p1 = { .min = 2, .max = 8 },
255 .p2 = { .dot_limit = 0,
256 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800257 },
Keith Packarde4b36692009-06-05 19:22:17 -0700258};
259
260static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700261 .dot = { .min = 80000, .max = 224000 },
262 .vco = { .min = 1750000, .max = 3500000 },
263 .n = { .min = 1, .max = 3 },
264 .m = { .min = 104, .max = 138 },
265 .m1 = { .min = 17, .max = 23 },
266 .m2 = { .min = 5, .max = 11 },
267 .p = { .min = 14, .max = 42 },
268 .p1 = { .min = 2, .max = 6 },
269 .p2 = { .dot_limit = 0,
270 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800271 },
Keith Packarde4b36692009-06-05 19:22:17 -0700272};
273
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500274static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400275 .dot = { .min = 20000, .max = 400000},
276 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700277 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400278 .n = { .min = 3, .max = 6 },
279 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700280 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400281 .m1 = { .min = 0, .max = 0 },
282 .m2 = { .min = 0, .max = 254 },
283 .p = { .min = 5, .max = 80 },
284 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700285 .p2 = { .dot_limit = 200000,
286 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700287};
288
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500289static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400290 .dot = { .min = 20000, .max = 400000 },
291 .vco = { .min = 1700000, .max = 3500000 },
292 .n = { .min = 3, .max = 6 },
293 .m = { .min = 2, .max = 256 },
294 .m1 = { .min = 0, .max = 0 },
295 .m2 = { .min = 0, .max = 254 },
296 .p = { .min = 7, .max = 112 },
297 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700298 .p2 = { .dot_limit = 112000,
299 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700300};
301
Eric Anholt273e27c2011-03-30 13:01:10 -0700302/* Ironlake / Sandybridge
303 *
304 * We calculate clock using (register_value + 2) for N/M1/M2, so here
305 * the range value for them is (actual_value - 2).
306 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800307static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700308 .dot = { .min = 25000, .max = 350000 },
309 .vco = { .min = 1760000, .max = 3510000 },
310 .n = { .min = 1, .max = 5 },
311 .m = { .min = 79, .max = 127 },
312 .m1 = { .min = 12, .max = 22 },
313 .m2 = { .min = 5, .max = 9 },
314 .p = { .min = 5, .max = 80 },
315 .p1 = { .min = 1, .max = 8 },
316 .p2 = { .dot_limit = 225000,
317 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700318};
319
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800320static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700321 .dot = { .min = 25000, .max = 350000 },
322 .vco = { .min = 1760000, .max = 3510000 },
323 .n = { .min = 1, .max = 3 },
324 .m = { .min = 79, .max = 118 },
325 .m1 = { .min = 12, .max = 22 },
326 .m2 = { .min = 5, .max = 9 },
327 .p = { .min = 28, .max = 112 },
328 .p1 = { .min = 2, .max = 8 },
329 .p2 = { .dot_limit = 225000,
330 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800331};
332
333static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700334 .dot = { .min = 25000, .max = 350000 },
335 .vco = { .min = 1760000, .max = 3510000 },
336 .n = { .min = 1, .max = 3 },
337 .m = { .min = 79, .max = 127 },
338 .m1 = { .min = 12, .max = 22 },
339 .m2 = { .min = 5, .max = 9 },
340 .p = { .min = 14, .max = 56 },
341 .p1 = { .min = 2, .max = 8 },
342 .p2 = { .dot_limit = 225000,
343 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800344};
345
Eric Anholt273e27c2011-03-30 13:01:10 -0700346/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800347static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700348 .dot = { .min = 25000, .max = 350000 },
349 .vco = { .min = 1760000, .max = 3510000 },
350 .n = { .min = 1, .max = 2 },
351 .m = { .min = 79, .max = 126 },
352 .m1 = { .min = 12, .max = 22 },
353 .m2 = { .min = 5, .max = 9 },
354 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400355 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700356 .p2 = { .dot_limit = 225000,
357 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800358};
359
360static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700361 .dot = { .min = 25000, .max = 350000 },
362 .vco = { .min = 1760000, .max = 3510000 },
363 .n = { .min = 1, .max = 3 },
364 .m = { .min = 79, .max = 126 },
365 .m1 = { .min = 12, .max = 22 },
366 .m2 = { .min = 5, .max = 9 },
367 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400368 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700369 .p2 = { .dot_limit = 225000,
370 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800371};
372
Ville Syrjälädc730512013-09-24 21:26:30 +0300373static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300374 /*
375 * These are the data rate limits (measured in fast clocks)
376 * since those are the strictest limits we have. The fast
377 * clock and actual rate limits are more relaxed, so checking
378 * them would make no difference.
379 */
380 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200381 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700382 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700383 .m1 = { .min = 2, .max = 3 },
384 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300385 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300386 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700387};
388
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300389static const intel_limit_t intel_limits_chv = {
390 /*
391 * These are the data rate limits (measured in fast clocks)
392 * since those are the strictest limits we have. The fast
393 * clock and actual rate limits are more relaxed, so checking
394 * them would make no difference.
395 */
396 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200397 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300398 .n = { .min = 1, .max = 1 },
399 .m1 = { .min = 2, .max = 2 },
400 .m2 = { .min = 24 << 22, .max = 175 << 22 },
401 .p1 = { .min = 2, .max = 4 },
402 .p2 = { .p2_slow = 1, .p2_fast = 14 },
403};
404
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200405static const intel_limit_t intel_limits_bxt = {
406 /* FIXME: find real dot limits */
407 .dot = { .min = 0, .max = INT_MAX },
408 .vco = { .min = 4800000, .max = 6480000 },
409 .n = { .min = 1, .max = 1 },
410 .m1 = { .min = 2, .max = 2 },
411 /* FIXME: find real m2 limits */
412 .m2 = { .min = 2 << 22, .max = 255 << 22 },
413 .p1 = { .min = 2, .max = 4 },
414 .p2 = { .p2_slow = 1, .p2_fast = 20 },
415};
416
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300417static void vlv_clock(int refclk, intel_clock_t *clock)
418{
419 clock->m = clock->m1 * clock->m2;
420 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200421 if (WARN_ON(clock->n == 0 || clock->p == 0))
422 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300423 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
424 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300425}
426
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300427/**
428 * Returns whether any output on the specified pipe is of the specified type
429 */
Damien Lespiau40935612014-10-29 11:16:59 +0000430bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300431{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300432 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300433 struct intel_encoder *encoder;
434
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300435 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300436 if (encoder->type == type)
437 return true;
438
439 return false;
440}
441
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200442/**
443 * Returns whether any output on the specified pipe will have the specified
444 * type after a staged modeset is complete, i.e., the same as
445 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
446 * encoder->crtc.
447 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200448static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
449 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200450{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200451 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300452 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200453 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200454 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200455 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200456
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300457 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200458 if (connector_state->crtc != crtc_state->base.crtc)
459 continue;
460
461 num_connectors++;
462
463 encoder = to_intel_encoder(connector_state->best_encoder);
464 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200465 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200466 }
467
468 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200469
470 return false;
471}
472
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200473static const intel_limit_t *
474intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800475{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200476 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800477 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800478
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200479 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100480 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000481 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800482 limit = &intel_limits_ironlake_dual_lvds_100m;
483 else
484 limit = &intel_limits_ironlake_dual_lvds;
485 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000486 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800487 limit = &intel_limits_ironlake_single_lvds_100m;
488 else
489 limit = &intel_limits_ironlake_single_lvds;
490 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200491 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800492 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800493
494 return limit;
495}
496
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200497static const intel_limit_t *
498intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800499{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200500 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800501 const intel_limit_t *limit;
502
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200503 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100504 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700505 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800506 else
Keith Packarde4b36692009-06-05 19:22:17 -0700507 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200508 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
509 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700510 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200511 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700512 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800513 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700514 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800515
516 return limit;
517}
518
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200519static const intel_limit_t *
520intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800521{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200522 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800523 const intel_limit_t *limit;
524
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200525 if (IS_BROXTON(dev))
526 limit = &intel_limits_bxt;
527 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200528 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800529 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200530 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500531 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200532 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500533 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800534 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500535 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300536 } else if (IS_CHERRYVIEW(dev)) {
537 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700538 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300539 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100540 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200541 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100542 limit = &intel_limits_i9xx_lvds;
543 else
544 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800545 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200546 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700547 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200548 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700549 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200550 else
551 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800552 }
553 return limit;
554}
555
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500556/* m1 is reserved as 0 in Pineview, n is a ring counter */
557static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800558{
Shaohua Li21778322009-02-23 15:19:16 +0800559 clock->m = clock->m2 + 2;
560 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200561 if (WARN_ON(clock->n == 0 || clock->p == 0))
562 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300563 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
564 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800565}
566
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200567static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
568{
569 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
570}
571
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200572static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800573{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200574 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800575 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200576 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
577 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300578 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
579 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800580}
581
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300582static void chv_clock(int refclk, intel_clock_t *clock)
583{
584 clock->m = clock->m1 * clock->m2;
585 clock->p = clock->p1 * clock->p2;
586 if (WARN_ON(clock->n == 0 || clock->p == 0))
587 return;
588 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
589 clock->n << 22);
590 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
591}
592
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800593#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800594/**
595 * Returns whether the given set of divisors are valid for a given refclk with
596 * the given connectors.
597 */
598
Chris Wilson1b894b52010-12-14 20:04:54 +0000599static bool intel_PLL_is_valid(struct drm_device *dev,
600 const intel_limit_t *limit,
601 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800602{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300603 if (clock->n < limit->n.min || limit->n.max < clock->n)
604 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800605 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400606 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800607 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400608 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800609 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400610 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300611
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200612 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300613 if (clock->m1 <= clock->m2)
614 INTELPllInvalid("m1 <= m2\n");
615
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200616 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300617 if (clock->p < limit->p.min || limit->p.max < clock->p)
618 INTELPllInvalid("p out of range\n");
619 if (clock->m < limit->m.min || limit->m.max < clock->m)
620 INTELPllInvalid("m out of range\n");
621 }
622
Jesse Barnes79e53942008-11-07 14:24:08 -0800623 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400624 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800625 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
626 * connector, etc., rather than just a single range.
627 */
628 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400629 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800630
631 return true;
632}
633
Ma Lingd4906092009-03-18 20:13:27 +0800634static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200635i9xx_find_best_dpll(const intel_limit_t *limit,
636 struct intel_crtc_state *crtc_state,
Sean Paulcec2f352012-01-10 15:09:36 -0800637 int target, int refclk, intel_clock_t *match_clock,
638 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800639{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200640 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300641 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800642 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800643 int err = target;
644
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200645 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800646 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100647 * For LVDS just rely on its current settings for dual-channel.
648 * We haven't figured out how to reliably set up different
649 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800650 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100651 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800652 clock.p2 = limit->p2.p2_fast;
653 else
654 clock.p2 = limit->p2.p2_slow;
655 } else {
656 if (target < limit->p2.dot_limit)
657 clock.p2 = limit->p2.p2_slow;
658 else
659 clock.p2 = limit->p2.p2_fast;
660 }
661
Akshay Joshi0206e352011-08-16 15:34:10 -0400662 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800663
Zhao Yakui42158662009-11-20 11:24:18 +0800664 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
665 clock.m1++) {
666 for (clock.m2 = limit->m2.min;
667 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200668 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800669 break;
670 for (clock.n = limit->n.min;
671 clock.n <= limit->n.max; clock.n++) {
672 for (clock.p1 = limit->p1.min;
673 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800674 int this_err;
675
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200676 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000677 if (!intel_PLL_is_valid(dev, limit,
678 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800679 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800680 if (match_clock &&
681 clock.p != match_clock->p)
682 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800683
684 this_err = abs(clock.dot - target);
685 if (this_err < err) {
686 *best_clock = clock;
687 err = this_err;
688 }
689 }
690 }
691 }
692 }
693
694 return (err != target);
695}
696
Ma Lingd4906092009-03-18 20:13:27 +0800697static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200698pnv_find_best_dpll(const intel_limit_t *limit,
699 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200700 int target, int refclk, intel_clock_t *match_clock,
701 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200702{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200703 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300704 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200705 intel_clock_t clock;
706 int err = target;
707
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200708 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200709 /*
710 * For LVDS just rely on its current settings for dual-channel.
711 * We haven't figured out how to reliably set up different
712 * single/dual channel state, if we even can.
713 */
714 if (intel_is_dual_link_lvds(dev))
715 clock.p2 = limit->p2.p2_fast;
716 else
717 clock.p2 = limit->p2.p2_slow;
718 } else {
719 if (target < limit->p2.dot_limit)
720 clock.p2 = limit->p2.p2_slow;
721 else
722 clock.p2 = limit->p2.p2_fast;
723 }
724
725 memset(best_clock, 0, sizeof(*best_clock));
726
727 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
728 clock.m1++) {
729 for (clock.m2 = limit->m2.min;
730 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200731 for (clock.n = limit->n.min;
732 clock.n <= limit->n.max; clock.n++) {
733 for (clock.p1 = limit->p1.min;
734 clock.p1 <= limit->p1.max; clock.p1++) {
735 int this_err;
736
737 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800738 if (!intel_PLL_is_valid(dev, limit,
739 &clock))
740 continue;
741 if (match_clock &&
742 clock.p != match_clock->p)
743 continue;
744
745 this_err = abs(clock.dot - target);
746 if (this_err < err) {
747 *best_clock = clock;
748 err = this_err;
749 }
750 }
751 }
752 }
753 }
754
755 return (err != target);
756}
757
Ma Lingd4906092009-03-18 20:13:27 +0800758static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200759g4x_find_best_dpll(const intel_limit_t *limit,
760 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200761 int target, int refclk, intel_clock_t *match_clock,
762 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800763{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200764 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300765 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800766 intel_clock_t clock;
767 int max_n;
768 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400769 /* approximately equals target * 0.00585 */
770 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800771 found = false;
772
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200773 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100774 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800775 clock.p2 = limit->p2.p2_fast;
776 else
777 clock.p2 = limit->p2.p2_slow;
778 } else {
779 if (target < limit->p2.dot_limit)
780 clock.p2 = limit->p2.p2_slow;
781 else
782 clock.p2 = limit->p2.p2_fast;
783 }
784
785 memset(best_clock, 0, sizeof(*best_clock));
786 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200787 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800788 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200789 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800790 for (clock.m1 = limit->m1.max;
791 clock.m1 >= limit->m1.min; clock.m1--) {
792 for (clock.m2 = limit->m2.max;
793 clock.m2 >= limit->m2.min; clock.m2--) {
794 for (clock.p1 = limit->p1.max;
795 clock.p1 >= limit->p1.min; clock.p1--) {
796 int this_err;
797
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200798 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000799 if (!intel_PLL_is_valid(dev, limit,
800 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800801 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000802
803 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800804 if (this_err < err_most) {
805 *best_clock = clock;
806 err_most = this_err;
807 max_n = clock.n;
808 found = true;
809 }
810 }
811 }
812 }
813 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800814 return found;
815}
Ma Lingd4906092009-03-18 20:13:27 +0800816
Imre Deakd5dd62b2015-03-17 11:40:03 +0200817/*
818 * Check if the calculated PLL configuration is more optimal compared to the
819 * best configuration and error found so far. Return the calculated error.
820 */
821static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
822 const intel_clock_t *calculated_clock,
823 const intel_clock_t *best_clock,
824 unsigned int best_error_ppm,
825 unsigned int *error_ppm)
826{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200827 /*
828 * For CHV ignore the error and consider only the P value.
829 * Prefer a bigger P value based on HW requirements.
830 */
831 if (IS_CHERRYVIEW(dev)) {
832 *error_ppm = 0;
833
834 return calculated_clock->p > best_clock->p;
835 }
836
Imre Deak24be4e42015-03-17 11:40:04 +0200837 if (WARN_ON_ONCE(!target_freq))
838 return false;
839
Imre Deakd5dd62b2015-03-17 11:40:03 +0200840 *error_ppm = div_u64(1000000ULL *
841 abs(target_freq - calculated_clock->dot),
842 target_freq);
843 /*
844 * Prefer a better P value over a better (smaller) error if the error
845 * is small. Ensure this preference for future configurations too by
846 * setting the error to 0.
847 */
848 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
849 *error_ppm = 0;
850
851 return true;
852 }
853
854 return *error_ppm + 10 < best_error_ppm;
855}
856
Zhenyu Wang2c072452009-06-05 15:38:42 +0800857static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200858vlv_find_best_dpll(const intel_limit_t *limit,
859 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200860 int target, int refclk, intel_clock_t *match_clock,
861 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700862{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200863 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300864 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300865 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300866 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300867 /* min update 19.2 MHz */
868 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300869 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700870
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300871 target *= 5; /* fast clock */
872
873 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700874
875 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300876 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300877 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300878 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300879 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300880 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700881 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300882 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200883 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300884
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300885 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
886 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300887
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300888 vlv_clock(refclk, &clock);
889
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300890 if (!intel_PLL_is_valid(dev, limit,
891 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300892 continue;
893
Imre Deakd5dd62b2015-03-17 11:40:03 +0200894 if (!vlv_PLL_is_optimal(dev, target,
895 &clock,
896 best_clock,
897 bestppm, &ppm))
898 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300899
Imre Deakd5dd62b2015-03-17 11:40:03 +0200900 *best_clock = clock;
901 bestppm = ppm;
902 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700903 }
904 }
905 }
906 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700907
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300908 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700909}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700910
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300911static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200912chv_find_best_dpll(const intel_limit_t *limit,
913 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300914 int target, int refclk, intel_clock_t *match_clock,
915 intel_clock_t *best_clock)
916{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200917 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300918 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200919 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300920 intel_clock_t clock;
921 uint64_t m2;
922 int found = false;
923
924 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200925 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300926
927 /*
928 * Based on hardware doc, the n always set to 1, and m1 always
929 * set to 2. If requires to support 200Mhz refclk, we need to
930 * revisit this because n may not 1 anymore.
931 */
932 clock.n = 1, clock.m1 = 2;
933 target *= 5; /* fast clock */
934
935 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
936 for (clock.p2 = limit->p2.p2_fast;
937 clock.p2 >= limit->p2.p2_slow;
938 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200939 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300940
941 clock.p = clock.p1 * clock.p2;
942
943 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
944 clock.n) << 22, refclk * clock.m1);
945
946 if (m2 > INT_MAX/clock.m1)
947 continue;
948
949 clock.m2 = m2;
950
951 chv_clock(refclk, &clock);
952
953 if (!intel_PLL_is_valid(dev, limit, &clock))
954 continue;
955
Imre Deak9ca3ba02015-03-17 11:40:05 +0200956 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
957 best_error_ppm, &error_ppm))
958 continue;
959
960 *best_clock = clock;
961 best_error_ppm = error_ppm;
962 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300963 }
964 }
965
966 return found;
967}
968
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200969bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
970 intel_clock_t *best_clock)
971{
972 int refclk = i9xx_get_refclk(crtc_state, 0);
973
974 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
975 target_clock, refclk, NULL, best_clock);
976}
977
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300978bool intel_crtc_active(struct drm_crtc *crtc)
979{
980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
981
982 /* Be paranoid as we can arrive here with only partial
983 * state retrieved from the hardware during setup.
984 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100985 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300986 * as Haswell has gained clock readout/fastboot support.
987 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000988 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300989 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700990 *
991 * FIXME: The intel_crtc->active here should be switched to
992 * crtc->state->active once we have proper CRTC states wired up
993 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300994 */
Matt Roperc3d1f432015-03-09 10:19:23 -0700995 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200996 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300997}
998
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200999enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1000 enum pipe pipe)
1001{
1002 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1004
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001005 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001006}
1007
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001008static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1009{
1010 struct drm_i915_private *dev_priv = dev->dev_private;
1011 u32 reg = PIPEDSL(pipe);
1012 u32 line1, line2;
1013 u32 line_mask;
1014
1015 if (IS_GEN2(dev))
1016 line_mask = DSL_LINEMASK_GEN2;
1017 else
1018 line_mask = DSL_LINEMASK_GEN3;
1019
1020 line1 = I915_READ(reg) & line_mask;
1021 mdelay(5);
1022 line2 = I915_READ(reg) & line_mask;
1023
1024 return line1 == line2;
1025}
1026
Keith Packardab7ad7f2010-10-03 00:33:06 -07001027/*
1028 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001029 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001030 *
1031 * After disabling a pipe, we can't wait for vblank in the usual way,
1032 * spinning on the vblank interrupt status bit, since we won't actually
1033 * see an interrupt when the pipe is disabled.
1034 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001035 * On Gen4 and above:
1036 * wait for the pipe register state bit to turn off
1037 *
1038 * Otherwise:
1039 * wait for the display line value to settle (it usually
1040 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001041 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001042 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001043static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001044{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001045 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001046 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001047 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001048 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001049
Keith Packardab7ad7f2010-10-03 00:33:06 -07001050 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001051 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001052
Keith Packardab7ad7f2010-10-03 00:33:06 -07001053 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001054 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1055 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001056 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001057 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001058 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001059 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001060 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001061 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001062}
1063
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001064/*
1065 * ibx_digital_port_connected - is the specified port connected?
1066 * @dev_priv: i915 private structure
1067 * @port: the port to test
1068 *
1069 * Returns true if @port is connected, false otherwise.
1070 */
1071bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1072 struct intel_digital_port *port)
1073{
1074 u32 bit;
1075
Damien Lespiauc36346e2012-12-13 16:09:03 +00001076 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001077 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001078 case PORT_B:
1079 bit = SDE_PORTB_HOTPLUG;
1080 break;
1081 case PORT_C:
1082 bit = SDE_PORTC_HOTPLUG;
1083 break;
1084 case PORT_D:
1085 bit = SDE_PORTD_HOTPLUG;
1086 break;
1087 default:
1088 return true;
1089 }
1090 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001091 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001092 case PORT_B:
1093 bit = SDE_PORTB_HOTPLUG_CPT;
1094 break;
1095 case PORT_C:
1096 bit = SDE_PORTC_HOTPLUG_CPT;
1097 break;
1098 case PORT_D:
1099 bit = SDE_PORTD_HOTPLUG_CPT;
1100 break;
1101 default:
1102 return true;
1103 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001104 }
1105
1106 return I915_READ(SDEISR) & bit;
1107}
1108
Jesse Barnesb24e7172011-01-04 15:09:30 -08001109static const char *state_string(bool enabled)
1110{
1111 return enabled ? "on" : "off";
1112}
1113
1114/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001115void assert_pll(struct drm_i915_private *dev_priv,
1116 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001117{
1118 int reg;
1119 u32 val;
1120 bool cur_state;
1121
1122 reg = DPLL(pipe);
1123 val = I915_READ(reg);
1124 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001125 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001126 "PLL state assertion failure (expected %s, current %s)\n",
1127 state_string(state), state_string(cur_state));
1128}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001129
Jani Nikula23538ef2013-08-27 15:12:22 +03001130/* XXX: the dsi pll is shared between MIPI DSI ports */
1131static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1132{
1133 u32 val;
1134 bool cur_state;
1135
1136 mutex_lock(&dev_priv->dpio_lock);
1137 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1138 mutex_unlock(&dev_priv->dpio_lock);
1139
1140 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001141 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001142 "DSI PLL state assertion failure (expected %s, current %s)\n",
1143 state_string(state), state_string(cur_state));
1144}
1145#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1146#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1147
Daniel Vetter55607e82013-06-16 21:42:39 +02001148struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001149intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001150{
Daniel Vettere2b78262013-06-07 23:10:03 +02001151 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1152
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001153 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001154 return NULL;
1155
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001156 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001157}
1158
Jesse Barnesb24e7172011-01-04 15:09:30 -08001159/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001160void assert_shared_dpll(struct drm_i915_private *dev_priv,
1161 struct intel_shared_dpll *pll,
1162 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001163{
Jesse Barnes040484a2011-01-03 12:14:26 -08001164 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001165 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001166
Chris Wilson92b27b02012-05-20 18:10:50 +01001167 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001168 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001169 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001170
Daniel Vetter53589012013-06-05 13:34:16 +02001171 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001172 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001173 "%s assertion failure (expected %s, current %s)\n",
1174 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001175}
Jesse Barnes040484a2011-01-03 12:14:26 -08001176
1177static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1178 enum pipe pipe, bool state)
1179{
1180 int reg;
1181 u32 val;
1182 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001183 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1184 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001185
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001186 if (HAS_DDI(dev_priv->dev)) {
1187 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001188 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001189 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001190 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001191 } else {
1192 reg = FDI_TX_CTL(pipe);
1193 val = I915_READ(reg);
1194 cur_state = !!(val & FDI_TX_ENABLE);
1195 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001196 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001197 "FDI TX state assertion failure (expected %s, current %s)\n",
1198 state_string(state), state_string(cur_state));
1199}
1200#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1201#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1202
1203static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1204 enum pipe pipe, bool state)
1205{
1206 int reg;
1207 u32 val;
1208 bool cur_state;
1209
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001210 reg = FDI_RX_CTL(pipe);
1211 val = I915_READ(reg);
1212 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001213 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001214 "FDI RX state assertion failure (expected %s, current %s)\n",
1215 state_string(state), state_string(cur_state));
1216}
1217#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1218#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1219
1220static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1221 enum pipe pipe)
1222{
1223 int reg;
1224 u32 val;
1225
1226 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001227 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001228 return;
1229
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001230 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001231 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001232 return;
1233
Jesse Barnes040484a2011-01-03 12:14:26 -08001234 reg = FDI_TX_CTL(pipe);
1235 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001236 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 -08001237}
1238
Daniel Vetter55607e82013-06-16 21:42:39 +02001239void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1240 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001241{
1242 int reg;
1243 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001244 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001245
1246 reg = FDI_RX_CTL(pipe);
1247 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001248 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001249 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001250 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1251 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001252}
1253
Daniel Vetterb680c372014-09-19 18:27:27 +02001254void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1255 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001256{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001257 struct drm_device *dev = dev_priv->dev;
1258 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001259 u32 val;
1260 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001261 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001262
Jani Nikulabedd4db2014-08-22 15:04:13 +03001263 if (WARN_ON(HAS_DDI(dev)))
1264 return;
1265
1266 if (HAS_PCH_SPLIT(dev)) {
1267 u32 port_sel;
1268
Jesse Barnesea0760c2011-01-04 15:09:32 -08001269 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001270 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1271
1272 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1273 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1274 panel_pipe = PIPE_B;
1275 /* XXX: else fix for eDP */
1276 } else if (IS_VALLEYVIEW(dev)) {
1277 /* presumably write lock depends on pipe, not port select */
1278 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1279 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001280 } else {
1281 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001282 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1283 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001284 }
1285
1286 val = I915_READ(pp_reg);
1287 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001288 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001289 locked = false;
1290
Rob Clarke2c719b2014-12-15 13:56:32 -05001291 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001292 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001293 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001294}
1295
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001296static void assert_cursor(struct drm_i915_private *dev_priv,
1297 enum pipe pipe, bool state)
1298{
1299 struct drm_device *dev = dev_priv->dev;
1300 bool cur_state;
1301
Paulo Zanonid9d82082014-02-27 16:30:56 -03001302 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001303 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001304 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001305 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001306
Rob Clarke2c719b2014-12-15 13:56:32 -05001307 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001308 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1309 pipe_name(pipe), state_string(state), state_string(cur_state));
1310}
1311#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1312#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1313
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001314void assert_pipe(struct drm_i915_private *dev_priv,
1315 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001316{
1317 int reg;
1318 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001319 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001320 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1321 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001322
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001323 /* if we need the pipe quirk it must be always on */
1324 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1325 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001326 state = true;
1327
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001328 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001329 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001330 cur_state = false;
1331 } else {
1332 reg = PIPECONF(cpu_transcoder);
1333 val = I915_READ(reg);
1334 cur_state = !!(val & PIPECONF_ENABLE);
1335 }
1336
Rob Clarke2c719b2014-12-15 13:56:32 -05001337 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001338 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001339 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001340}
1341
Chris Wilson931872f2012-01-16 23:01:13 +00001342static void assert_plane(struct drm_i915_private *dev_priv,
1343 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001344{
1345 int reg;
1346 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001347 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001348
1349 reg = DSPCNTR(plane);
1350 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001351 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001352 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001353 "plane %c assertion failure (expected %s, current %s)\n",
1354 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001355}
1356
Chris Wilson931872f2012-01-16 23:01:13 +00001357#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1358#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1359
Jesse Barnesb24e7172011-01-04 15:09:30 -08001360static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1361 enum pipe pipe)
1362{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001363 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001364 int reg, i;
1365 u32 val;
1366 int cur_pipe;
1367
Ville Syrjälä653e1022013-06-04 13:49:05 +03001368 /* Primary planes are fixed to pipes on gen4+ */
1369 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001370 reg = DSPCNTR(pipe);
1371 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001372 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001373 "plane %c assertion failure, should be disabled but not\n",
1374 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001375 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001376 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001377
Jesse Barnesb24e7172011-01-04 15:09:30 -08001378 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001379 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001380 reg = DSPCNTR(i);
1381 val = I915_READ(reg);
1382 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1383 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001384 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001385 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1386 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001387 }
1388}
1389
Jesse Barnes19332d72013-03-28 09:55:38 -07001390static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1391 enum pipe pipe)
1392{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001393 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001394 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001395 u32 val;
1396
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001397 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001398 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001399 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001400 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001401 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1402 sprite, pipe_name(pipe));
1403 }
1404 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001405 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001406 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001407 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001408 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001409 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001410 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001411 }
1412 } else if (INTEL_INFO(dev)->gen >= 7) {
1413 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001414 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001415 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001416 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001417 plane_name(pipe), pipe_name(pipe));
1418 } else if (INTEL_INFO(dev)->gen >= 5) {
1419 reg = DVSCNTR(pipe);
1420 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001421 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001422 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1423 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001424 }
1425}
1426
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001427static void assert_vblank_disabled(struct drm_crtc *crtc)
1428{
Rob Clarke2c719b2014-12-15 13:56:32 -05001429 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001430 drm_crtc_vblank_put(crtc);
1431}
1432
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001433static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001434{
1435 u32 val;
1436 bool enabled;
1437
Rob Clarke2c719b2014-12-15 13:56:32 -05001438 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001439
Jesse Barnes92f25842011-01-04 15:09:34 -08001440 val = I915_READ(PCH_DREF_CONTROL);
1441 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1442 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001443 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001444}
1445
Daniel Vetterab9412b2013-05-03 11:49:46 +02001446static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1447 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001448{
1449 int reg;
1450 u32 val;
1451 bool enabled;
1452
Daniel Vetterab9412b2013-05-03 11:49:46 +02001453 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001454 val = I915_READ(reg);
1455 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001456 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001457 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1458 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001459}
1460
Keith Packard4e634382011-08-06 10:39:45 -07001461static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1462 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001463{
1464 if ((val & DP_PORT_EN) == 0)
1465 return false;
1466
1467 if (HAS_PCH_CPT(dev_priv->dev)) {
1468 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1469 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1470 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1471 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001472 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1473 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1474 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001475 } else {
1476 if ((val & DP_PIPE_MASK) != (pipe << 30))
1477 return false;
1478 }
1479 return true;
1480}
1481
Keith Packard1519b992011-08-06 10:35:34 -07001482static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1483 enum pipe pipe, u32 val)
1484{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001485 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001486 return false;
1487
1488 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001489 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001490 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001491 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1492 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1493 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001494 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001495 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001496 return false;
1497 }
1498 return true;
1499}
1500
1501static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1502 enum pipe pipe, u32 val)
1503{
1504 if ((val & LVDS_PORT_EN) == 0)
1505 return false;
1506
1507 if (HAS_PCH_CPT(dev_priv->dev)) {
1508 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1509 return false;
1510 } else {
1511 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1512 return false;
1513 }
1514 return true;
1515}
1516
1517static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1518 enum pipe pipe, u32 val)
1519{
1520 if ((val & ADPA_DAC_ENABLE) == 0)
1521 return false;
1522 if (HAS_PCH_CPT(dev_priv->dev)) {
1523 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1524 return false;
1525 } else {
1526 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1527 return false;
1528 }
1529 return true;
1530}
1531
Jesse Barnes291906f2011-02-02 12:28:03 -08001532static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001533 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001534{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001535 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001536 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001537 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001538 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001539
Rob Clarke2c719b2014-12-15 13:56:32 -05001540 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001541 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001542 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001543}
1544
1545static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1546 enum pipe pipe, int reg)
1547{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001548 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001549 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001550 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001551 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001552
Rob Clarke2c719b2014-12-15 13:56:32 -05001553 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001554 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001555 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001556}
1557
1558static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1559 enum pipe pipe)
1560{
1561 int reg;
1562 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001563
Keith Packardf0575e92011-07-25 22:12:43 -07001564 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1565 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1566 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001567
1568 reg = PCH_ADPA;
1569 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001570 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001571 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001572 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001573
1574 reg = PCH_LVDS;
1575 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001576 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001577 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001578 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001579
Paulo Zanonie2debe92013-02-18 19:00:27 -03001580 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1581 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1582 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001583}
1584
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001585static void intel_init_dpio(struct drm_device *dev)
1586{
1587 struct drm_i915_private *dev_priv = dev->dev_private;
1588
1589 if (!IS_VALLEYVIEW(dev))
1590 return;
1591
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001592 /*
1593 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1594 * CHV x1 PHY (DP/HDMI D)
1595 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1596 */
1597 if (IS_CHERRYVIEW(dev)) {
1598 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1599 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1600 } else {
1601 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1602 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001603}
1604
Ville Syrjäläd288f652014-10-28 13:20:22 +02001605static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001606 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001607{
Daniel Vetter426115c2013-07-11 22:13:42 +02001608 struct drm_device *dev = crtc->base.dev;
1609 struct drm_i915_private *dev_priv = dev->dev_private;
1610 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001611 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001612
Daniel Vetter426115c2013-07-11 22:13:42 +02001613 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001614
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001615 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001616 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1617
1618 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001619 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001620 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001621
Daniel Vetter426115c2013-07-11 22:13:42 +02001622 I915_WRITE(reg, dpll);
1623 POSTING_READ(reg);
1624 udelay(150);
1625
1626 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1627 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1628
Ville Syrjäläd288f652014-10-28 13:20:22 +02001629 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001630 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001631
1632 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001633 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001634 POSTING_READ(reg);
1635 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001636 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001637 POSTING_READ(reg);
1638 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001639 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001640 POSTING_READ(reg);
1641 udelay(150); /* wait for warmup */
1642}
1643
Ville Syrjäläd288f652014-10-28 13:20:22 +02001644static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001645 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001646{
1647 struct drm_device *dev = crtc->base.dev;
1648 struct drm_i915_private *dev_priv = dev->dev_private;
1649 int pipe = crtc->pipe;
1650 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001651 u32 tmp;
1652
1653 assert_pipe_disabled(dev_priv, crtc->pipe);
1654
1655 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1656
1657 mutex_lock(&dev_priv->dpio_lock);
1658
1659 /* Enable back the 10bit clock to display controller */
1660 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1661 tmp |= DPIO_DCLKP_EN;
1662 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1663
1664 /*
1665 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1666 */
1667 udelay(1);
1668
1669 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001670 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001671
1672 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001673 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001674 DRM_ERROR("PLL %d failed to lock\n", pipe);
1675
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001676 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001677 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001678 POSTING_READ(DPLL_MD(pipe));
1679
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001680 mutex_unlock(&dev_priv->dpio_lock);
1681}
1682
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001683static int intel_num_dvo_pipes(struct drm_device *dev)
1684{
1685 struct intel_crtc *crtc;
1686 int count = 0;
1687
1688 for_each_intel_crtc(dev, crtc)
1689 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001690 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001691
1692 return count;
1693}
1694
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001695static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001696{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001697 struct drm_device *dev = crtc->base.dev;
1698 struct drm_i915_private *dev_priv = dev->dev_private;
1699 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001700 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001701
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001702 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001703
1704 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001705 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001706
1707 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001708 if (IS_MOBILE(dev) && !IS_I830(dev))
1709 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001710
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001711 /* Enable DVO 2x clock on both PLLs if necessary */
1712 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1713 /*
1714 * It appears to be important that we don't enable this
1715 * for the current pipe before otherwise configuring the
1716 * PLL. No idea how this should be handled if multiple
1717 * DVO outputs are enabled simultaneosly.
1718 */
1719 dpll |= DPLL_DVO_2X_MODE;
1720 I915_WRITE(DPLL(!crtc->pipe),
1721 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1722 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001723
1724 /* Wait for the clocks to stabilize. */
1725 POSTING_READ(reg);
1726 udelay(150);
1727
1728 if (INTEL_INFO(dev)->gen >= 4) {
1729 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001730 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001731 } else {
1732 /* The pixel multiplier can only be updated once the
1733 * DPLL is enabled and the clocks are stable.
1734 *
1735 * So write it again.
1736 */
1737 I915_WRITE(reg, dpll);
1738 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001739
1740 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001741 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001742 POSTING_READ(reg);
1743 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001744 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001745 POSTING_READ(reg);
1746 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001747 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001748 POSTING_READ(reg);
1749 udelay(150); /* wait for warmup */
1750}
1751
1752/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001753 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001754 * @dev_priv: i915 private structure
1755 * @pipe: pipe PLL to disable
1756 *
1757 * Disable the PLL for @pipe, making sure the pipe is off first.
1758 *
1759 * Note! This is for pre-ILK only.
1760 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001761static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001762{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001763 struct drm_device *dev = crtc->base.dev;
1764 struct drm_i915_private *dev_priv = dev->dev_private;
1765 enum pipe pipe = crtc->pipe;
1766
1767 /* Disable DVO 2x clock on both PLLs if necessary */
1768 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001769 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001770 intel_num_dvo_pipes(dev) == 1) {
1771 I915_WRITE(DPLL(PIPE_B),
1772 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1773 I915_WRITE(DPLL(PIPE_A),
1774 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1775 }
1776
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001777 /* Don't disable pipe or pipe PLLs if needed */
1778 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1779 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001780 return;
1781
1782 /* Make sure the pipe isn't still relying on us */
1783 assert_pipe_disabled(dev_priv, pipe);
1784
Daniel Vetter50b44a42013-06-05 13:34:33 +02001785 I915_WRITE(DPLL(pipe), 0);
1786 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001787}
1788
Jesse Barnesf6071162013-10-01 10:41:38 -07001789static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1790{
1791 u32 val = 0;
1792
1793 /* Make sure the pipe isn't still relying on us */
1794 assert_pipe_disabled(dev_priv, pipe);
1795
Imre Deake5cbfbf2014-01-09 17:08:16 +02001796 /*
1797 * Leave integrated clock source and reference clock enabled for pipe B.
1798 * The latter is needed for VGA hotplug / manual detection.
1799 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001800 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001801 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001802 I915_WRITE(DPLL(pipe), val);
1803 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001804
1805}
1806
1807static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1808{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001809 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001810 u32 val;
1811
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001812 /* Make sure the pipe isn't still relying on us */
1813 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001814
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001815 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001816 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001817 if (pipe != PIPE_A)
1818 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1819 I915_WRITE(DPLL(pipe), val);
1820 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001821
1822 mutex_lock(&dev_priv->dpio_lock);
1823
1824 /* Disable 10bit clock to display controller */
1825 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1826 val &= ~DPIO_DCLKP_EN;
1827 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1828
Ville Syrjälä61407f62014-05-27 16:32:55 +03001829 /* disable left/right clock distribution */
1830 if (pipe != PIPE_B) {
1831 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1832 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1833 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1834 } else {
1835 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1836 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1837 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1838 }
1839
Ville Syrjäläd7520482014-04-09 13:28:59 +03001840 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001841}
1842
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001843void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001844 struct intel_digital_port *dport,
1845 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001846{
1847 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001848 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001849
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001850 switch (dport->port) {
1851 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001852 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001853 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001854 break;
1855 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001856 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001857 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001858 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001859 break;
1860 case PORT_D:
1861 port_mask = DPLL_PORTD_READY_MASK;
1862 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001863 break;
1864 default:
1865 BUG();
1866 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001867
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001868 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1869 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1870 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001871}
1872
Daniel Vetterb14b1052014-04-24 23:55:13 +02001873static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1874{
1875 struct drm_device *dev = crtc->base.dev;
1876 struct drm_i915_private *dev_priv = dev->dev_private;
1877 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1878
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001879 if (WARN_ON(pll == NULL))
1880 return;
1881
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001882 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001883 if (pll->active == 0) {
1884 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1885 WARN_ON(pll->on);
1886 assert_shared_dpll_disabled(dev_priv, pll);
1887
1888 pll->mode_set(dev_priv, pll);
1889 }
1890}
1891
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001892/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001893 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001894 * @dev_priv: i915 private structure
1895 * @pipe: pipe PLL to enable
1896 *
1897 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1898 * drives the transcoder clock.
1899 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001900static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001901{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001902 struct drm_device *dev = crtc->base.dev;
1903 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001904 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001905
Daniel Vetter87a875b2013-06-05 13:34:19 +02001906 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001907 return;
1908
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001909 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001910 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001911
Damien Lespiau74dd6922014-07-29 18:06:17 +01001912 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001913 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001914 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001915
Daniel Vettercdbd2312013-06-05 13:34:03 +02001916 if (pll->active++) {
1917 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001918 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001919 return;
1920 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001921 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001922
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001923 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1924
Daniel Vetter46edb022013-06-05 13:34:12 +02001925 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001926 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001927 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001928}
1929
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001930static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001931{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001932 struct drm_device *dev = crtc->base.dev;
1933 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001934 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001935
Jesse Barnes92f25842011-01-04 15:09:34 -08001936 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001937 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001938 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001939 return;
1940
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001941 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001942 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001943
Daniel Vetter46edb022013-06-05 13:34:12 +02001944 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1945 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001946 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001947
Chris Wilson48da64a2012-05-13 20:16:12 +01001948 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001949 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001950 return;
1951 }
1952
Daniel Vettere9d69442013-06-05 13:34:15 +02001953 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001954 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001955 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001956 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001957
Daniel Vetter46edb022013-06-05 13:34:12 +02001958 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001959 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001960 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001961
1962 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001963}
1964
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001965static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1966 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001967{
Daniel Vetter23670b322012-11-01 09:15:30 +01001968 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001969 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001970 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001971 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001972
1973 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001974 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001975
1976 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001977 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001978 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001979
1980 /* FDI must be feeding us bits for PCH ports */
1981 assert_fdi_tx_enabled(dev_priv, pipe);
1982 assert_fdi_rx_enabled(dev_priv, pipe);
1983
Daniel Vetter23670b322012-11-01 09:15:30 +01001984 if (HAS_PCH_CPT(dev)) {
1985 /* Workaround: Set the timing override bit before enabling the
1986 * pch transcoder. */
1987 reg = TRANS_CHICKEN2(pipe);
1988 val = I915_READ(reg);
1989 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1990 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001991 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001992
Daniel Vetterab9412b2013-05-03 11:49:46 +02001993 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001994 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001995 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001996
1997 if (HAS_PCH_IBX(dev_priv->dev)) {
1998 /*
1999 * make the BPC in transcoder be consistent with
2000 * that in pipeconf reg.
2001 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002002 val &= ~PIPECONF_BPC_MASK;
2003 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002004 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002005
2006 val &= ~TRANS_INTERLACE_MASK;
2007 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002008 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002009 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002010 val |= TRANS_LEGACY_INTERLACED_ILK;
2011 else
2012 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002013 else
2014 val |= TRANS_PROGRESSIVE;
2015
Jesse Barnes040484a2011-01-03 12:14:26 -08002016 I915_WRITE(reg, val | TRANS_ENABLE);
2017 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002018 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002019}
2020
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002021static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002022 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002023{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002024 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002025
2026 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002027 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002028
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002029 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002030 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002031 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002032
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002033 /* Workaround: set timing override bit. */
2034 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002035 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002036 I915_WRITE(_TRANSA_CHICKEN2, val);
2037
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002038 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002039 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002040
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002041 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2042 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002043 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002044 else
2045 val |= TRANS_PROGRESSIVE;
2046
Daniel Vetterab9412b2013-05-03 11:49:46 +02002047 I915_WRITE(LPT_TRANSCONF, val);
2048 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002049 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002050}
2051
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002052static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2053 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002054{
Daniel Vetter23670b322012-11-01 09:15:30 +01002055 struct drm_device *dev = dev_priv->dev;
2056 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002057
2058 /* FDI relies on the transcoder */
2059 assert_fdi_tx_disabled(dev_priv, pipe);
2060 assert_fdi_rx_disabled(dev_priv, pipe);
2061
Jesse Barnes291906f2011-02-02 12:28:03 -08002062 /* Ports must be off as well */
2063 assert_pch_ports_disabled(dev_priv, pipe);
2064
Daniel Vetterab9412b2013-05-03 11:49:46 +02002065 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002066 val = I915_READ(reg);
2067 val &= ~TRANS_ENABLE;
2068 I915_WRITE(reg, val);
2069 /* wait for PCH transcoder off, transcoder state */
2070 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002071 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002072
2073 if (!HAS_PCH_IBX(dev)) {
2074 /* Workaround: Clear the timing override chicken bit again. */
2075 reg = TRANS_CHICKEN2(pipe);
2076 val = I915_READ(reg);
2077 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2078 I915_WRITE(reg, val);
2079 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002080}
2081
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002082static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002083{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002084 u32 val;
2085
Daniel Vetterab9412b2013-05-03 11:49:46 +02002086 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002087 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002088 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002089 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002090 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002091 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002092
2093 /* Workaround: clear timing override bit. */
2094 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002095 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002096 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002097}
2098
2099/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002100 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002101 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002102 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002103 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002104 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002105 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002106static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002107{
Paulo Zanoni03722642014-01-17 13:51:09 -02002108 struct drm_device *dev = crtc->base.dev;
2109 struct drm_i915_private *dev_priv = dev->dev_private;
2110 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002111 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2112 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002113 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002114 int reg;
2115 u32 val;
2116
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002117 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002118 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002119 assert_sprites_disabled(dev_priv, pipe);
2120
Paulo Zanoni681e5812012-12-06 11:12:38 -02002121 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002122 pch_transcoder = TRANSCODER_A;
2123 else
2124 pch_transcoder = pipe;
2125
Jesse Barnesb24e7172011-01-04 15:09:30 -08002126 /*
2127 * A pipe without a PLL won't actually be able to drive bits from
2128 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2129 * need the check.
2130 */
Imre Deak50360402015-01-16 00:55:16 -08002131 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002132 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002133 assert_dsi_pll_enabled(dev_priv);
2134 else
2135 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002136 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002137 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002138 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002139 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002140 assert_fdi_tx_pll_enabled(dev_priv,
2141 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002142 }
2143 /* FIXME: assert CPU port conditions for SNB+ */
2144 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002145
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002146 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002147 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002148 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002149 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2150 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002151 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002152 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002153
2154 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002155 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002156}
2157
2158/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002159 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002160 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002161 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002162 * Disable the pipe of @crtc, making sure that various hardware
2163 * specific requirements are met, if applicable, e.g. plane
2164 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002165 *
2166 * Will wait until the pipe has shut down before returning.
2167 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002168static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002169{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002170 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002171 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002172 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002173 int reg;
2174 u32 val;
2175
2176 /*
2177 * Make sure planes won't keep trying to pump pixels to us,
2178 * or we might hang the display.
2179 */
2180 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002181 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002182 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002183
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002184 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002185 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002186 if ((val & PIPECONF_ENABLE) == 0)
2187 return;
2188
Ville Syrjälä67adc642014-08-15 01:21:57 +03002189 /*
2190 * Double wide has implications for planes
2191 * so best keep it disabled when not needed.
2192 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002193 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002194 val &= ~PIPECONF_DOUBLE_WIDE;
2195
2196 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002197 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2198 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002199 val &= ~PIPECONF_ENABLE;
2200
2201 I915_WRITE(reg, val);
2202 if ((val & PIPECONF_ENABLE) == 0)
2203 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002204}
2205
Keith Packardd74362c2011-07-28 14:47:14 -07002206/*
2207 * Plane regs are double buffered, going from enabled->disabled needs a
2208 * trigger in order to latch. The display address reg provides this.
2209 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002210void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2211 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002212{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002213 struct drm_device *dev = dev_priv->dev;
2214 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002215
2216 I915_WRITE(reg, I915_READ(reg));
2217 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002218}
2219
Jesse Barnesb24e7172011-01-04 15:09:30 -08002220/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002221 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002222 * @plane: plane to be enabled
2223 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002224 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002225 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002226 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002227static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2228 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002229{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002230 struct drm_device *dev = plane->dev;
2231 struct drm_i915_private *dev_priv = dev->dev_private;
2232 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002233
2234 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002235 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002236 to_intel_plane_state(plane->state)->visible = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002237
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002238 dev_priv->display.update_primary_plane(crtc, plane->fb,
2239 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002240}
2241
Chris Wilson693db182013-03-05 14:52:39 +00002242static bool need_vtd_wa(struct drm_device *dev)
2243{
2244#ifdef CONFIG_INTEL_IOMMU
2245 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2246 return true;
2247#endif
2248 return false;
2249}
2250
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002251unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002252intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2253 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002254{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002255 unsigned int tile_height;
2256 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002257
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002258 switch (fb_format_modifier) {
2259 case DRM_FORMAT_MOD_NONE:
2260 tile_height = 1;
2261 break;
2262 case I915_FORMAT_MOD_X_TILED:
2263 tile_height = IS_GEN2(dev) ? 16 : 8;
2264 break;
2265 case I915_FORMAT_MOD_Y_TILED:
2266 tile_height = 32;
2267 break;
2268 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002269 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2270 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002271 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002272 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002273 tile_height = 64;
2274 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002275 case 2:
2276 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002277 tile_height = 32;
2278 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002279 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002280 tile_height = 16;
2281 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002282 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002283 WARN_ONCE(1,
2284 "128-bit pixels are not supported for display!");
2285 tile_height = 16;
2286 break;
2287 }
2288 break;
2289 default:
2290 MISSING_CASE(fb_format_modifier);
2291 tile_height = 1;
2292 break;
2293 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002294
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002295 return tile_height;
2296}
2297
2298unsigned int
2299intel_fb_align_height(struct drm_device *dev, unsigned int height,
2300 uint32_t pixel_format, uint64_t fb_format_modifier)
2301{
2302 return ALIGN(height, intel_tile_height(dev, pixel_format,
2303 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002304}
2305
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002306static int
2307intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2308 const struct drm_plane_state *plane_state)
2309{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002310 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002311
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002312 *view = i915_ggtt_view_normal;
2313
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002314 if (!plane_state)
2315 return 0;
2316
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002317 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002318 return 0;
2319
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002320 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002321
2322 info->height = fb->height;
2323 info->pixel_format = fb->pixel_format;
2324 info->pitch = fb->pitches[0];
2325 info->fb_modifier = fb->modifier[0];
2326
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002327 return 0;
2328}
2329
Chris Wilson127bd2a2010-07-23 23:32:05 +01002330int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002331intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2332 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002333 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002334 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002335{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002336 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002337 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002338 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002339 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002340 u32 alignment;
2341 int ret;
2342
Matt Roperebcdd392014-07-09 16:22:11 -07002343 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2344
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002345 switch (fb->modifier[0]) {
2346 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002347 if (INTEL_INFO(dev)->gen >= 9)
2348 alignment = 256 * 1024;
2349 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002350 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002351 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002352 alignment = 4 * 1024;
2353 else
2354 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002355 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002356 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002357 if (INTEL_INFO(dev)->gen >= 9)
2358 alignment = 256 * 1024;
2359 else {
2360 /* pin() will align the object as required by fence */
2361 alignment = 0;
2362 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002363 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002364 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002365 case I915_FORMAT_MOD_Yf_TILED:
2366 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2367 "Y tiling bo slipped through, driver bug!\n"))
2368 return -EINVAL;
2369 alignment = 1 * 1024 * 1024;
2370 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002371 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002372 MISSING_CASE(fb->modifier[0]);
2373 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002374 }
2375
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002376 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2377 if (ret)
2378 return ret;
2379
Chris Wilson693db182013-03-05 14:52:39 +00002380 /* Note that the w/a also requires 64 PTE of padding following the
2381 * bo. We currently fill all unused PTE with the shadow page and so
2382 * we should always have valid PTE following the scanout preventing
2383 * the VT-d warning.
2384 */
2385 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2386 alignment = 256 * 1024;
2387
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002388 /*
2389 * Global gtt pte registers are special registers which actually forward
2390 * writes to a chunk of system memory. Which means that there is no risk
2391 * that the register values disappear as soon as we call
2392 * intel_runtime_pm_put(), so it is correct to wrap only the
2393 * pin/unpin/fence and not more.
2394 */
2395 intel_runtime_pm_get(dev_priv);
2396
Chris Wilsonce453d82011-02-21 14:43:56 +00002397 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002398 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002399 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002400 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002401 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002402
2403 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2404 * fence, whereas 965+ only requires a fence if using
2405 * framebuffer compression. For simplicity, we always install
2406 * a fence as the cost is not that onerous.
2407 */
Chris Wilson06d98132012-04-17 15:31:24 +01002408 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002409 if (ret)
2410 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002411
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002412 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002413
Chris Wilsonce453d82011-02-21 14:43:56 +00002414 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002415 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002416 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002417
2418err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002419 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002420err_interruptible:
2421 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002422 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002423 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002424}
2425
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002426static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2427 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002428{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002429 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002430 struct i915_ggtt_view view;
2431 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002432
Matt Roperebcdd392014-07-09 16:22:11 -07002433 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2434
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002435 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2436 WARN_ONCE(ret, "Couldn't get view from plane state!");
2437
Chris Wilson1690e1e2011-12-14 13:57:08 +01002438 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002439 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002440}
2441
Daniel Vetterc2c75132012-07-05 12:17:30 +02002442/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2443 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002444unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2445 unsigned int tiling_mode,
2446 unsigned int cpp,
2447 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002448{
Chris Wilsonbc752862013-02-21 20:04:31 +00002449 if (tiling_mode != I915_TILING_NONE) {
2450 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002451
Chris Wilsonbc752862013-02-21 20:04:31 +00002452 tile_rows = *y / 8;
2453 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002454
Chris Wilsonbc752862013-02-21 20:04:31 +00002455 tiles = *x / (512/cpp);
2456 *x %= 512/cpp;
2457
2458 return tile_rows * pitch * 8 + tiles * 4096;
2459 } else {
2460 unsigned int offset;
2461
2462 offset = *y * pitch + *x * cpp;
2463 *y = 0;
2464 *x = (offset & 4095) / cpp;
2465 return offset & -4096;
2466 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002467}
2468
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002469static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002470{
2471 switch (format) {
2472 case DISPPLANE_8BPP:
2473 return DRM_FORMAT_C8;
2474 case DISPPLANE_BGRX555:
2475 return DRM_FORMAT_XRGB1555;
2476 case DISPPLANE_BGRX565:
2477 return DRM_FORMAT_RGB565;
2478 default:
2479 case DISPPLANE_BGRX888:
2480 return DRM_FORMAT_XRGB8888;
2481 case DISPPLANE_RGBX888:
2482 return DRM_FORMAT_XBGR8888;
2483 case DISPPLANE_BGRX101010:
2484 return DRM_FORMAT_XRGB2101010;
2485 case DISPPLANE_RGBX101010:
2486 return DRM_FORMAT_XBGR2101010;
2487 }
2488}
2489
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002490static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2491{
2492 switch (format) {
2493 case PLANE_CTL_FORMAT_RGB_565:
2494 return DRM_FORMAT_RGB565;
2495 default:
2496 case PLANE_CTL_FORMAT_XRGB_8888:
2497 if (rgb_order) {
2498 if (alpha)
2499 return DRM_FORMAT_ABGR8888;
2500 else
2501 return DRM_FORMAT_XBGR8888;
2502 } else {
2503 if (alpha)
2504 return DRM_FORMAT_ARGB8888;
2505 else
2506 return DRM_FORMAT_XRGB8888;
2507 }
2508 case PLANE_CTL_FORMAT_XRGB_2101010:
2509 if (rgb_order)
2510 return DRM_FORMAT_XBGR2101010;
2511 else
2512 return DRM_FORMAT_XRGB2101010;
2513 }
2514}
2515
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002516static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002517intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2518 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002519{
2520 struct drm_device *dev = crtc->base.dev;
2521 struct drm_i915_gem_object *obj = NULL;
2522 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002523 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002524 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2525 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2526 PAGE_SIZE);
2527
2528 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002529
Chris Wilsonff2652e2014-03-10 08:07:02 +00002530 if (plane_config->size == 0)
2531 return false;
2532
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002533 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2534 base_aligned,
2535 base_aligned,
2536 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002537 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002538 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002539
Damien Lespiau49af4492015-01-20 12:51:44 +00002540 obj->tiling_mode = plane_config->tiling;
2541 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002542 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002543
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002544 mode_cmd.pixel_format = fb->pixel_format;
2545 mode_cmd.width = fb->width;
2546 mode_cmd.height = fb->height;
2547 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002548 mode_cmd.modifier[0] = fb->modifier[0];
2549 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002550
2551 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002552 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002553 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002554 DRM_DEBUG_KMS("intel fb init failed\n");
2555 goto out_unref_obj;
2556 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002557 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002558
Daniel Vetterf6936e22015-03-26 12:17:05 +01002559 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002560 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002561
2562out_unref_obj:
2563 drm_gem_object_unreference(&obj->base);
2564 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002565 return false;
2566}
2567
Matt Roperafd65eb2015-02-03 13:10:04 -08002568/* Update plane->state->fb to match plane->fb after driver-internal updates */
2569static void
2570update_state_fb(struct drm_plane *plane)
2571{
2572 if (plane->fb == plane->state->fb)
2573 return;
2574
2575 if (plane->state->fb)
2576 drm_framebuffer_unreference(plane->state->fb);
2577 plane->state->fb = plane->fb;
2578 if (plane->state->fb)
2579 drm_framebuffer_reference(plane->state->fb);
2580}
2581
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002582static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002583intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2584 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002585{
2586 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002587 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002588 struct drm_crtc *c;
2589 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002590 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002591 struct drm_plane *primary = intel_crtc->base.primary;
2592 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002593
Damien Lespiau2d140302015-02-05 17:22:18 +00002594 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002595 return;
2596
Daniel Vetterf6936e22015-03-26 12:17:05 +01002597 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002598 fb = &plane_config->fb->base;
2599 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002600 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002601
Damien Lespiau2d140302015-02-05 17:22:18 +00002602 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002603
2604 /*
2605 * Failed to alloc the obj, check to see if we should share
2606 * an fb with another CRTC instead
2607 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002608 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002609 i = to_intel_crtc(c);
2610
2611 if (c == &intel_crtc->base)
2612 continue;
2613
Matt Roper2ff8fde2014-07-08 07:50:07 -07002614 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002615 continue;
2616
Daniel Vetter88595ac2015-03-26 12:42:24 +01002617 fb = c->primary->fb;
2618 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002619 continue;
2620
Daniel Vetter88595ac2015-03-26 12:42:24 +01002621 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002622 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002623 drm_framebuffer_reference(fb);
2624 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002625 }
2626 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002627
2628 return;
2629
2630valid_fb:
2631 obj = intel_fb_obj(fb);
2632 if (obj->tiling_mode != I915_TILING_NONE)
2633 dev_priv->preserve_bios_swizzle = true;
2634
2635 primary->fb = fb;
2636 primary->state->crtc = &intel_crtc->base;
2637 primary->crtc = &intel_crtc->base;
2638 update_state_fb(primary);
2639 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002640}
2641
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002642static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2643 struct drm_framebuffer *fb,
2644 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002645{
2646 struct drm_device *dev = crtc->dev;
2647 struct drm_i915_private *dev_priv = dev->dev_private;
2648 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002649 struct drm_plane *primary = crtc->primary;
2650 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002651 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002652 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002653 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002654 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002655 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302656 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002657
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002658 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002659 I915_WRITE(reg, 0);
2660 if (INTEL_INFO(dev)->gen >= 4)
2661 I915_WRITE(DSPSURF(plane), 0);
2662 else
2663 I915_WRITE(DSPADDR(plane), 0);
2664 POSTING_READ(reg);
2665 return;
2666 }
2667
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002668 obj = intel_fb_obj(fb);
2669 if (WARN_ON(obj == NULL))
2670 return;
2671
2672 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2673
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002674 dspcntr = DISPPLANE_GAMMA_ENABLE;
2675
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002676 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002677
2678 if (INTEL_INFO(dev)->gen < 4) {
2679 if (intel_crtc->pipe == PIPE_B)
2680 dspcntr |= DISPPLANE_SEL_PIPE_B;
2681
2682 /* pipesrc and dspsize control the size that is scaled from,
2683 * which should always be the user's requested size.
2684 */
2685 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002686 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2687 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002688 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002689 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2690 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002691 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2692 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002693 I915_WRITE(PRIMPOS(plane), 0);
2694 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002695 }
2696
Ville Syrjälä57779d02012-10-31 17:50:14 +02002697 switch (fb->pixel_format) {
2698 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002699 dspcntr |= DISPPLANE_8BPP;
2700 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002701 case DRM_FORMAT_XRGB1555:
2702 case DRM_FORMAT_ARGB1555:
2703 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002704 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002705 case DRM_FORMAT_RGB565:
2706 dspcntr |= DISPPLANE_BGRX565;
2707 break;
2708 case DRM_FORMAT_XRGB8888:
2709 case DRM_FORMAT_ARGB8888:
2710 dspcntr |= DISPPLANE_BGRX888;
2711 break;
2712 case DRM_FORMAT_XBGR8888:
2713 case DRM_FORMAT_ABGR8888:
2714 dspcntr |= DISPPLANE_RGBX888;
2715 break;
2716 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002717 dspcntr |= DISPPLANE_BGRX101010;
2718 break;
2719 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002720 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002721 break;
2722 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002723 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002724 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002725
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002726 if (INTEL_INFO(dev)->gen >= 4 &&
2727 obj->tiling_mode != I915_TILING_NONE)
2728 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002729
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002730 if (IS_G4X(dev))
2731 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2732
Ville Syrjäläb98971272014-08-27 16:51:22 +03002733 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002734
Daniel Vetterc2c75132012-07-05 12:17:30 +02002735 if (INTEL_INFO(dev)->gen >= 4) {
2736 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002737 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002738 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002739 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002740 linear_offset -= intel_crtc->dspaddr_offset;
2741 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002742 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002743 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002744
Matt Roper8e7d6882015-01-21 16:35:41 -08002745 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302746 dspcntr |= DISPPLANE_ROTATE_180;
2747
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002748 x += (intel_crtc->config->pipe_src_w - 1);
2749 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302750
2751 /* Finding the last pixel of the last line of the display
2752 data and adding to linear_offset*/
2753 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002754 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2755 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302756 }
2757
2758 I915_WRITE(reg, dspcntr);
2759
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002760 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002761 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002762 I915_WRITE(DSPSURF(plane),
2763 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002764 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002765 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002766 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002767 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002768 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002769}
2770
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002771static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2772 struct drm_framebuffer *fb,
2773 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002774{
2775 struct drm_device *dev = crtc->dev;
2776 struct drm_i915_private *dev_priv = dev->dev_private;
2777 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002778 struct drm_plane *primary = crtc->primary;
2779 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002780 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002781 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002782 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002783 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002784 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302785 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002786
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002787 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002788 I915_WRITE(reg, 0);
2789 I915_WRITE(DSPSURF(plane), 0);
2790 POSTING_READ(reg);
2791 return;
2792 }
2793
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002794 obj = intel_fb_obj(fb);
2795 if (WARN_ON(obj == NULL))
2796 return;
2797
2798 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2799
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002800 dspcntr = DISPPLANE_GAMMA_ENABLE;
2801
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002802 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002803
2804 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2805 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2806
Ville Syrjälä57779d02012-10-31 17:50:14 +02002807 switch (fb->pixel_format) {
2808 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002809 dspcntr |= DISPPLANE_8BPP;
2810 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002811 case DRM_FORMAT_RGB565:
2812 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002813 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002814 case DRM_FORMAT_XRGB8888:
2815 case DRM_FORMAT_ARGB8888:
2816 dspcntr |= DISPPLANE_BGRX888;
2817 break;
2818 case DRM_FORMAT_XBGR8888:
2819 case DRM_FORMAT_ABGR8888:
2820 dspcntr |= DISPPLANE_RGBX888;
2821 break;
2822 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002823 dspcntr |= DISPPLANE_BGRX101010;
2824 break;
2825 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002826 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002827 break;
2828 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002829 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002830 }
2831
2832 if (obj->tiling_mode != I915_TILING_NONE)
2833 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002834
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002835 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002836 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002837
Ville Syrjäläb98971272014-08-27 16:51:22 +03002838 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002839 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002840 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002841 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002842 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002843 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002844 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302845 dspcntr |= DISPPLANE_ROTATE_180;
2846
2847 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002848 x += (intel_crtc->config->pipe_src_w - 1);
2849 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302850
2851 /* Finding the last pixel of the last line of the display
2852 data and adding to linear_offset*/
2853 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002854 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2855 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302856 }
2857 }
2858
2859 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002860
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002861 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002862 I915_WRITE(DSPSURF(plane),
2863 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002864 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002865 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2866 } else {
2867 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2868 I915_WRITE(DSPLINOFF(plane), linear_offset);
2869 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002870 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002871}
2872
Damien Lespiaub3218032015-02-27 11:15:18 +00002873u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2874 uint32_t pixel_format)
2875{
2876 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2877
2878 /*
2879 * The stride is either expressed as a multiple of 64 bytes
2880 * chunks for linear buffers or in number of tiles for tiled
2881 * buffers.
2882 */
2883 switch (fb_modifier) {
2884 case DRM_FORMAT_MOD_NONE:
2885 return 64;
2886 case I915_FORMAT_MOD_X_TILED:
2887 if (INTEL_INFO(dev)->gen == 2)
2888 return 128;
2889 return 512;
2890 case I915_FORMAT_MOD_Y_TILED:
2891 /* No need to check for old gens and Y tiling since this is
2892 * about the display engine and those will be blocked before
2893 * we get here.
2894 */
2895 return 128;
2896 case I915_FORMAT_MOD_Yf_TILED:
2897 if (bits_per_pixel == 8)
2898 return 64;
2899 else
2900 return 128;
2901 default:
2902 MISSING_CASE(fb_modifier);
2903 return 64;
2904 }
2905}
2906
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002907unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2908 struct drm_i915_gem_object *obj)
2909{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002910 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002911
2912 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002913 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002914
2915 return i915_gem_obj_ggtt_offset_view(obj, view);
2916}
2917
Chandra Kondurua1b22782015-04-07 15:28:45 -07002918/*
2919 * This function detaches (aka. unbinds) unused scalers in hardware
2920 */
2921void skl_detach_scalers(struct intel_crtc *intel_crtc)
2922{
2923 struct drm_device *dev;
2924 struct drm_i915_private *dev_priv;
2925 struct intel_crtc_scaler_state *scaler_state;
2926 int i;
2927
2928 if (!intel_crtc || !intel_crtc->config)
2929 return;
2930
2931 dev = intel_crtc->base.dev;
2932 dev_priv = dev->dev_private;
2933 scaler_state = &intel_crtc->config->scaler_state;
2934
2935 /* loop through and disable scalers that aren't in use */
2936 for (i = 0; i < intel_crtc->num_scalers; i++) {
2937 if (!scaler_state->scalers[i].in_use) {
2938 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2939 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2940 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2941 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2942 intel_crtc->base.base.id, intel_crtc->pipe, i);
2943 }
2944 }
2945}
2946
Chandra Konduru6156a452015-04-27 13:48:39 -07002947u32 skl_plane_ctl_format(uint32_t pixel_format)
2948{
Chandra Konduru6156a452015-04-27 13:48:39 -07002949 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002950 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002951 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002952 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002953 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002955 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002956 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002957 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 /*
2959 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2960 * to be already pre-multiplied. We need to add a knob (or a different
2961 * DRM_FORMAT) for user-space to configure that.
2962 */
2963 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002964 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002965 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002966 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002967 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002968 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002969 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002970 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002971 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002972 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002973 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002974 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002975 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002976 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002977 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002978 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002979 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002980 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002981 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002982 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002983 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002984
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002985 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002986}
2987
2988u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2989{
Chandra Konduru6156a452015-04-27 13:48:39 -07002990 switch (fb_modifier) {
2991 case DRM_FORMAT_MOD_NONE:
2992 break;
2993 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002994 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002995 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002996 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002997 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002998 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002999 default:
3000 MISSING_CASE(fb_modifier);
3001 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003002
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003003 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003004}
3005
3006u32 skl_plane_ctl_rotation(unsigned int rotation)
3007{
Chandra Konduru6156a452015-04-27 13:48:39 -07003008 switch (rotation) {
3009 case BIT(DRM_ROTATE_0):
3010 break;
3011 case BIT(DRM_ROTATE_90):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003012 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003013 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003014 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003015 case BIT(DRM_ROTATE_270):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003016 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003017 default:
3018 MISSING_CASE(rotation);
3019 }
3020
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003021 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003022}
3023
Damien Lespiau70d21f02013-07-03 21:06:04 +01003024static void skylake_update_primary_plane(struct drm_crtc *crtc,
3025 struct drm_framebuffer *fb,
3026 int x, int y)
3027{
3028 struct drm_device *dev = crtc->dev;
3029 struct drm_i915_private *dev_priv = dev->dev_private;
3030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003031 struct drm_plane *plane = crtc->primary;
3032 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003033 struct drm_i915_gem_object *obj;
3034 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303035 u32 plane_ctl, stride_div, stride;
3036 u32 tile_height, plane_offset, plane_size;
3037 unsigned int rotation;
3038 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003039 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003040 struct intel_crtc_state *crtc_state = intel_crtc->config;
3041 struct intel_plane_state *plane_state;
3042 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3043 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3044 int scaler_id = -1;
3045
Chandra Konduru6156a452015-04-27 13:48:39 -07003046 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003047
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003048 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003049 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3050 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3051 POSTING_READ(PLANE_CTL(pipe, 0));
3052 return;
3053 }
3054
3055 plane_ctl = PLANE_CTL_ENABLE |
3056 PLANE_CTL_PIPE_GAMMA_ENABLE |
3057 PLANE_CTL_PIPE_CSC_ENABLE;
3058
Chandra Konduru6156a452015-04-27 13:48:39 -07003059 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3060 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003061 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303062
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303063 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003064 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003065
Damien Lespiaub3218032015-02-27 11:15:18 +00003066 obj = intel_fb_obj(fb);
3067 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3068 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303069 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3070
Chandra Konduru6156a452015-04-27 13:48:39 -07003071 /*
3072 * FIXME: intel_plane_state->src, dst aren't set when transitional
3073 * update_plane helpers are called from legacy paths.
3074 * Once full atomic crtc is available, below check can be avoided.
3075 */
3076 if (drm_rect_width(&plane_state->src)) {
3077 scaler_id = plane_state->scaler_id;
3078 src_x = plane_state->src.x1 >> 16;
3079 src_y = plane_state->src.y1 >> 16;
3080 src_w = drm_rect_width(&plane_state->src) >> 16;
3081 src_h = drm_rect_height(&plane_state->src) >> 16;
3082 dst_x = plane_state->dst.x1;
3083 dst_y = plane_state->dst.y1;
3084 dst_w = drm_rect_width(&plane_state->dst);
3085 dst_h = drm_rect_height(&plane_state->dst);
3086
3087 WARN_ON(x != src_x || y != src_y);
3088 } else {
3089 src_w = intel_crtc->config->pipe_src_w;
3090 src_h = intel_crtc->config->pipe_src_h;
3091 }
3092
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303093 if (intel_rotation_90_or_270(rotation)) {
3094 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003095 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303096 fb->modifier[0]);
3097 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003098 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303099 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003100 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303101 } else {
3102 stride = fb->pitches[0] / stride_div;
3103 x_offset = x;
3104 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003105 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303106 }
3107 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003108
Damien Lespiau70d21f02013-07-03 21:06:04 +01003109 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303110 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3111 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3112 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003113
3114 if (scaler_id >= 0) {
3115 uint32_t ps_ctrl = 0;
3116
3117 WARN_ON(!dst_w || !dst_h);
3118 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3119 crtc_state->scaler_state.scalers[scaler_id].mode;
3120 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3121 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3122 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3123 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3124 I915_WRITE(PLANE_POS(pipe, 0), 0);
3125 } else {
3126 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3127 }
3128
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003129 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003130
3131 POSTING_READ(PLANE_SURF(pipe, 0));
3132}
3133
Jesse Barnes17638cd2011-06-24 12:19:23 -07003134/* Assume fb object is pinned & idle & fenced and just update base pointers */
3135static int
3136intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3137 int x, int y, enum mode_set_atomic state)
3138{
3139 struct drm_device *dev = crtc->dev;
3140 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003141
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003142 if (dev_priv->display.disable_fbc)
3143 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003144
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003145 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3146
3147 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003148}
3149
Ville Syrjälä75147472014-11-24 18:28:11 +02003150static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003151{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003152 struct drm_crtc *crtc;
3153
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003154 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003155 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3156 enum plane plane = intel_crtc->plane;
3157
3158 intel_prepare_page_flip(dev, plane);
3159 intel_finish_page_flip_plane(dev, plane);
3160 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003161}
3162
3163static void intel_update_primary_planes(struct drm_device *dev)
3164{
3165 struct drm_i915_private *dev_priv = dev->dev_private;
3166 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003167
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003168 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003169 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3170
Rob Clark51fd3712013-11-19 12:10:12 -05003171 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003172 /*
3173 * FIXME: Once we have proper support for primary planes (and
3174 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003175 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003176 */
Matt Roperf4510a22014-04-01 15:22:40 -07003177 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003178 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003179 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003180 crtc->x,
3181 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003182 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003183 }
3184}
3185
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003186void intel_crtc_reset(struct intel_crtc *crtc)
3187{
3188 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3189
3190 if (!crtc->active)
3191 return;
3192
3193 intel_crtc_disable_planes(&crtc->base);
3194 dev_priv->display.crtc_disable(&crtc->base);
3195 dev_priv->display.crtc_enable(&crtc->base);
3196 intel_crtc_enable_planes(&crtc->base);
3197}
3198
Ville Syrjälä75147472014-11-24 18:28:11 +02003199void intel_prepare_reset(struct drm_device *dev)
3200{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003201 struct drm_i915_private *dev_priv = to_i915(dev);
3202 struct intel_crtc *crtc;
3203
Ville Syrjälä75147472014-11-24 18:28:11 +02003204 /* no reset support for gen2 */
3205 if (IS_GEN2(dev))
3206 return;
3207
3208 /* reset doesn't touch the display */
3209 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3210 return;
3211
3212 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003213
3214 /*
3215 * Disabling the crtcs gracefully seems nicer. Also the
3216 * g33 docs say we should at least disable all the planes.
3217 */
3218 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003219 if (!crtc->active)
3220 continue;
3221
3222 intel_crtc_disable_planes(&crtc->base);
3223 dev_priv->display.crtc_disable(&crtc->base);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003224 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003225}
3226
3227void intel_finish_reset(struct drm_device *dev)
3228{
3229 struct drm_i915_private *dev_priv = to_i915(dev);
3230
3231 /*
3232 * Flips in the rings will be nuked by the reset,
3233 * so complete all pending flips so that user space
3234 * will get its events and not get stuck.
3235 */
3236 intel_complete_page_flips(dev);
3237
3238 /* no reset support for gen2 */
3239 if (IS_GEN2(dev))
3240 return;
3241
3242 /* reset doesn't touch the display */
3243 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3244 /*
3245 * Flips in the rings have been nuked by the reset,
3246 * so update the base address of all primary
3247 * planes to the the last fb to make sure we're
3248 * showing the correct fb after a reset.
3249 */
3250 intel_update_primary_planes(dev);
3251 return;
3252 }
3253
3254 /*
3255 * The display has been reset as well,
3256 * so need a full re-initialization.
3257 */
3258 intel_runtime_pm_disable_interrupts(dev_priv);
3259 intel_runtime_pm_enable_interrupts(dev_priv);
3260
3261 intel_modeset_init_hw(dev);
3262
3263 spin_lock_irq(&dev_priv->irq_lock);
3264 if (dev_priv->display.hpd_irq_setup)
3265 dev_priv->display.hpd_irq_setup(dev);
3266 spin_unlock_irq(&dev_priv->irq_lock);
3267
3268 intel_modeset_setup_hw_state(dev, true);
3269
3270 intel_hpd_init(dev_priv);
3271
3272 drm_modeset_unlock_all(dev);
3273}
3274
Chris Wilson2e2f3512015-04-27 13:41:14 +01003275static void
Chris Wilson14667a42012-04-03 17:58:35 +01003276intel_finish_fb(struct drm_framebuffer *old_fb)
3277{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003278 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003279 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003280 bool was_interruptible = dev_priv->mm.interruptible;
3281 int ret;
3282
Chris Wilson14667a42012-04-03 17:58:35 +01003283 /* Big Hammer, we also need to ensure that any pending
3284 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3285 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003286 * framebuffer. Note that we rely on userspace rendering
3287 * into the buffer attached to the pipe they are waiting
3288 * on. If not, userspace generates a GPU hang with IPEHR
3289 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003290 *
3291 * This should only fail upon a hung GPU, in which case we
3292 * can safely continue.
3293 */
3294 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003295 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003296 dev_priv->mm.interruptible = was_interruptible;
3297
Chris Wilson2e2f3512015-04-27 13:41:14 +01003298 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003299}
3300
Chris Wilson7d5e3792014-03-04 13:15:08 +00003301static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3302{
3303 struct drm_device *dev = crtc->dev;
3304 struct drm_i915_private *dev_priv = dev->dev_private;
3305 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003306 bool pending;
3307
3308 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3309 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3310 return false;
3311
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003312 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003313 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003314 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003315
3316 return pending;
3317}
3318
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003319static void intel_update_pipe_size(struct intel_crtc *crtc)
3320{
3321 struct drm_device *dev = crtc->base.dev;
3322 struct drm_i915_private *dev_priv = dev->dev_private;
3323 const struct drm_display_mode *adjusted_mode;
3324
3325 if (!i915.fastboot)
3326 return;
3327
3328 /*
3329 * Update pipe size and adjust fitter if needed: the reason for this is
3330 * that in compute_mode_changes we check the native mode (not the pfit
3331 * mode) to see if we can flip rather than do a full mode set. In the
3332 * fastboot case, we'll flip, but if we don't update the pipesrc and
3333 * pfit state, we'll end up with a big fb scanned out into the wrong
3334 * sized surface.
3335 *
3336 * To fix this properly, we need to hoist the checks up into
3337 * compute_mode_changes (or above), check the actual pfit state and
3338 * whether the platform allows pfit disable with pipe active, and only
3339 * then update the pipesrc and pfit state, even on the flip path.
3340 */
3341
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003342 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003343
3344 I915_WRITE(PIPESRC(crtc->pipe),
3345 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3346 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003347 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003348 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3349 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003350 I915_WRITE(PF_CTL(crtc->pipe), 0);
3351 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3352 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3353 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003354 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3355 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003356}
3357
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003358static void intel_fdi_normal_train(struct drm_crtc *crtc)
3359{
3360 struct drm_device *dev = crtc->dev;
3361 struct drm_i915_private *dev_priv = dev->dev_private;
3362 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3363 int pipe = intel_crtc->pipe;
3364 u32 reg, temp;
3365
3366 /* enable normal train */
3367 reg = FDI_TX_CTL(pipe);
3368 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003369 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003370 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3371 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003372 } else {
3373 temp &= ~FDI_LINK_TRAIN_NONE;
3374 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003375 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003376 I915_WRITE(reg, temp);
3377
3378 reg = FDI_RX_CTL(pipe);
3379 temp = I915_READ(reg);
3380 if (HAS_PCH_CPT(dev)) {
3381 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3382 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3383 } else {
3384 temp &= ~FDI_LINK_TRAIN_NONE;
3385 temp |= FDI_LINK_TRAIN_NONE;
3386 }
3387 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3388
3389 /* wait one idle pattern time */
3390 POSTING_READ(reg);
3391 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003392
3393 /* IVB wants error correction enabled */
3394 if (IS_IVYBRIDGE(dev))
3395 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3396 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003397}
3398
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003399/* The FDI link training functions for ILK/Ibexpeak. */
3400static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3401{
3402 struct drm_device *dev = crtc->dev;
3403 struct drm_i915_private *dev_priv = dev->dev_private;
3404 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3405 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003406 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003407
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003408 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003409 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003410
Adam Jacksone1a44742010-06-25 15:32:14 -04003411 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3412 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003413 reg = FDI_RX_IMR(pipe);
3414 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003415 temp &= ~FDI_RX_SYMBOL_LOCK;
3416 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003417 I915_WRITE(reg, temp);
3418 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003419 udelay(150);
3420
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003421 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003422 reg = FDI_TX_CTL(pipe);
3423 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003424 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003425 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003426 temp &= ~FDI_LINK_TRAIN_NONE;
3427 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003428 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003429
Chris Wilson5eddb702010-09-11 13:48:45 +01003430 reg = FDI_RX_CTL(pipe);
3431 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003432 temp &= ~FDI_LINK_TRAIN_NONE;
3433 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003434 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3435
3436 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003437 udelay(150);
3438
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003439 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003440 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3441 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3442 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003443
Chris Wilson5eddb702010-09-11 13:48:45 +01003444 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003445 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003446 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003447 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3448
3449 if ((temp & FDI_RX_BIT_LOCK)) {
3450 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003451 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003452 break;
3453 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003454 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003455 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003456 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003457
3458 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003459 reg = FDI_TX_CTL(pipe);
3460 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003461 temp &= ~FDI_LINK_TRAIN_NONE;
3462 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003463 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003464
Chris Wilson5eddb702010-09-11 13:48:45 +01003465 reg = FDI_RX_CTL(pipe);
3466 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003467 temp &= ~FDI_LINK_TRAIN_NONE;
3468 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003469 I915_WRITE(reg, temp);
3470
3471 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003472 udelay(150);
3473
Chris Wilson5eddb702010-09-11 13:48:45 +01003474 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003475 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003476 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003477 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3478
3479 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003480 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003481 DRM_DEBUG_KMS("FDI train 2 done.\n");
3482 break;
3483 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003484 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003485 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003486 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003487
3488 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003489
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003490}
3491
Akshay Joshi0206e352011-08-16 15:34:10 -04003492static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003493 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3494 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3495 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3496 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3497};
3498
3499/* The FDI link training functions for SNB/Cougarpoint. */
3500static void gen6_fdi_link_train(struct drm_crtc *crtc)
3501{
3502 struct drm_device *dev = crtc->dev;
3503 struct drm_i915_private *dev_priv = dev->dev_private;
3504 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3505 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003506 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003507
Adam Jacksone1a44742010-06-25 15:32:14 -04003508 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3509 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003510 reg = FDI_RX_IMR(pipe);
3511 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003512 temp &= ~FDI_RX_SYMBOL_LOCK;
3513 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003514 I915_WRITE(reg, temp);
3515
3516 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003517 udelay(150);
3518
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003519 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003520 reg = FDI_TX_CTL(pipe);
3521 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003522 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003523 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003524 temp &= ~FDI_LINK_TRAIN_NONE;
3525 temp |= FDI_LINK_TRAIN_PATTERN_1;
3526 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3527 /* SNB-B */
3528 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003529 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003530
Daniel Vetterd74cf322012-10-26 10:58:13 +02003531 I915_WRITE(FDI_RX_MISC(pipe),
3532 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3533
Chris Wilson5eddb702010-09-11 13:48:45 +01003534 reg = FDI_RX_CTL(pipe);
3535 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003536 if (HAS_PCH_CPT(dev)) {
3537 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3538 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3539 } else {
3540 temp &= ~FDI_LINK_TRAIN_NONE;
3541 temp |= FDI_LINK_TRAIN_PATTERN_1;
3542 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003543 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3544
3545 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003546 udelay(150);
3547
Akshay Joshi0206e352011-08-16 15:34:10 -04003548 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003549 reg = FDI_TX_CTL(pipe);
3550 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003551 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3552 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003553 I915_WRITE(reg, temp);
3554
3555 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003556 udelay(500);
3557
Sean Paulfa37d392012-03-02 12:53:39 -05003558 for (retry = 0; retry < 5; retry++) {
3559 reg = FDI_RX_IIR(pipe);
3560 temp = I915_READ(reg);
3561 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3562 if (temp & FDI_RX_BIT_LOCK) {
3563 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3564 DRM_DEBUG_KMS("FDI train 1 done.\n");
3565 break;
3566 }
3567 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003568 }
Sean Paulfa37d392012-03-02 12:53:39 -05003569 if (retry < 5)
3570 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003571 }
3572 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003573 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003574
3575 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003576 reg = FDI_TX_CTL(pipe);
3577 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003578 temp &= ~FDI_LINK_TRAIN_NONE;
3579 temp |= FDI_LINK_TRAIN_PATTERN_2;
3580 if (IS_GEN6(dev)) {
3581 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3582 /* SNB-B */
3583 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3584 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003585 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003586
Chris Wilson5eddb702010-09-11 13:48:45 +01003587 reg = FDI_RX_CTL(pipe);
3588 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003589 if (HAS_PCH_CPT(dev)) {
3590 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3591 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3592 } else {
3593 temp &= ~FDI_LINK_TRAIN_NONE;
3594 temp |= FDI_LINK_TRAIN_PATTERN_2;
3595 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003596 I915_WRITE(reg, temp);
3597
3598 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003599 udelay(150);
3600
Akshay Joshi0206e352011-08-16 15:34:10 -04003601 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003602 reg = FDI_TX_CTL(pipe);
3603 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003604 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3605 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003606 I915_WRITE(reg, temp);
3607
3608 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003609 udelay(500);
3610
Sean Paulfa37d392012-03-02 12:53:39 -05003611 for (retry = 0; retry < 5; retry++) {
3612 reg = FDI_RX_IIR(pipe);
3613 temp = I915_READ(reg);
3614 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3615 if (temp & FDI_RX_SYMBOL_LOCK) {
3616 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3617 DRM_DEBUG_KMS("FDI train 2 done.\n");
3618 break;
3619 }
3620 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003621 }
Sean Paulfa37d392012-03-02 12:53:39 -05003622 if (retry < 5)
3623 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003624 }
3625 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003626 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003627
3628 DRM_DEBUG_KMS("FDI train done.\n");
3629}
3630
Jesse Barnes357555c2011-04-28 15:09:55 -07003631/* Manual link training for Ivy Bridge A0 parts */
3632static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3633{
3634 struct drm_device *dev = crtc->dev;
3635 struct drm_i915_private *dev_priv = dev->dev_private;
3636 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3637 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003638 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003639
3640 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3641 for train result */
3642 reg = FDI_RX_IMR(pipe);
3643 temp = I915_READ(reg);
3644 temp &= ~FDI_RX_SYMBOL_LOCK;
3645 temp &= ~FDI_RX_BIT_LOCK;
3646 I915_WRITE(reg, temp);
3647
3648 POSTING_READ(reg);
3649 udelay(150);
3650
Daniel Vetter01a415f2012-10-27 15:58:40 +02003651 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3652 I915_READ(FDI_RX_IIR(pipe)));
3653
Jesse Barnes139ccd32013-08-19 11:04:55 -07003654 /* Try each vswing and preemphasis setting twice before moving on */
3655 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3656 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003657 reg = FDI_TX_CTL(pipe);
3658 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003659 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3660 temp &= ~FDI_TX_ENABLE;
3661 I915_WRITE(reg, temp);
3662
3663 reg = FDI_RX_CTL(pipe);
3664 temp = I915_READ(reg);
3665 temp &= ~FDI_LINK_TRAIN_AUTO;
3666 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3667 temp &= ~FDI_RX_ENABLE;
3668 I915_WRITE(reg, temp);
3669
3670 /* enable CPU FDI TX and PCH FDI RX */
3671 reg = FDI_TX_CTL(pipe);
3672 temp = I915_READ(reg);
3673 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003674 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003675 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003676 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003677 temp |= snb_b_fdi_train_param[j/2];
3678 temp |= FDI_COMPOSITE_SYNC;
3679 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3680
3681 I915_WRITE(FDI_RX_MISC(pipe),
3682 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3683
3684 reg = FDI_RX_CTL(pipe);
3685 temp = I915_READ(reg);
3686 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3687 temp |= FDI_COMPOSITE_SYNC;
3688 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3689
3690 POSTING_READ(reg);
3691 udelay(1); /* should be 0.5us */
3692
3693 for (i = 0; i < 4; i++) {
3694 reg = FDI_RX_IIR(pipe);
3695 temp = I915_READ(reg);
3696 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3697
3698 if (temp & FDI_RX_BIT_LOCK ||
3699 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3700 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3701 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3702 i);
3703 break;
3704 }
3705 udelay(1); /* should be 0.5us */
3706 }
3707 if (i == 4) {
3708 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3709 continue;
3710 }
3711
3712 /* Train 2 */
3713 reg = FDI_TX_CTL(pipe);
3714 temp = I915_READ(reg);
3715 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3716 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3717 I915_WRITE(reg, temp);
3718
3719 reg = FDI_RX_CTL(pipe);
3720 temp = I915_READ(reg);
3721 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3722 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003723 I915_WRITE(reg, temp);
3724
3725 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003726 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003727
Jesse Barnes139ccd32013-08-19 11:04:55 -07003728 for (i = 0; i < 4; i++) {
3729 reg = FDI_RX_IIR(pipe);
3730 temp = I915_READ(reg);
3731 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003732
Jesse Barnes139ccd32013-08-19 11:04:55 -07003733 if (temp & FDI_RX_SYMBOL_LOCK ||
3734 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3735 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3736 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3737 i);
3738 goto train_done;
3739 }
3740 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003741 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003742 if (i == 4)
3743 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003744 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003745
Jesse Barnes139ccd32013-08-19 11:04:55 -07003746train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003747 DRM_DEBUG_KMS("FDI train done.\n");
3748}
3749
Daniel Vetter88cefb62012-08-12 19:27:14 +02003750static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003751{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003752 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003753 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003754 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003755 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003756
Jesse Barnesc64e3112010-09-10 11:27:03 -07003757
Jesse Barnes0e23b992010-09-10 11:10:00 -07003758 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003759 reg = FDI_RX_CTL(pipe);
3760 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003761 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003762 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003763 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003764 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3765
3766 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003767 udelay(200);
3768
3769 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003770 temp = I915_READ(reg);
3771 I915_WRITE(reg, temp | FDI_PCDCLK);
3772
3773 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003774 udelay(200);
3775
Paulo Zanoni20749732012-11-23 15:30:38 -02003776 /* Enable CPU FDI TX PLL, always on for Ironlake */
3777 reg = FDI_TX_CTL(pipe);
3778 temp = I915_READ(reg);
3779 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3780 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003781
Paulo Zanoni20749732012-11-23 15:30:38 -02003782 POSTING_READ(reg);
3783 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003784 }
3785}
3786
Daniel Vetter88cefb62012-08-12 19:27:14 +02003787static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3788{
3789 struct drm_device *dev = intel_crtc->base.dev;
3790 struct drm_i915_private *dev_priv = dev->dev_private;
3791 int pipe = intel_crtc->pipe;
3792 u32 reg, temp;
3793
3794 /* Switch from PCDclk to Rawclk */
3795 reg = FDI_RX_CTL(pipe);
3796 temp = I915_READ(reg);
3797 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3798
3799 /* Disable CPU FDI TX PLL */
3800 reg = FDI_TX_CTL(pipe);
3801 temp = I915_READ(reg);
3802 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3803
3804 POSTING_READ(reg);
3805 udelay(100);
3806
3807 reg = FDI_RX_CTL(pipe);
3808 temp = I915_READ(reg);
3809 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3810
3811 /* Wait for the clocks to turn off. */
3812 POSTING_READ(reg);
3813 udelay(100);
3814}
3815
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003816static void ironlake_fdi_disable(struct drm_crtc *crtc)
3817{
3818 struct drm_device *dev = crtc->dev;
3819 struct drm_i915_private *dev_priv = dev->dev_private;
3820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3821 int pipe = intel_crtc->pipe;
3822 u32 reg, temp;
3823
3824 /* disable CPU FDI tx and PCH FDI rx */
3825 reg = FDI_TX_CTL(pipe);
3826 temp = I915_READ(reg);
3827 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3828 POSTING_READ(reg);
3829
3830 reg = FDI_RX_CTL(pipe);
3831 temp = I915_READ(reg);
3832 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003833 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003834 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3835
3836 POSTING_READ(reg);
3837 udelay(100);
3838
3839 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003840 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003841 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003842
3843 /* still set train pattern 1 */
3844 reg = FDI_TX_CTL(pipe);
3845 temp = I915_READ(reg);
3846 temp &= ~FDI_LINK_TRAIN_NONE;
3847 temp |= FDI_LINK_TRAIN_PATTERN_1;
3848 I915_WRITE(reg, temp);
3849
3850 reg = FDI_RX_CTL(pipe);
3851 temp = I915_READ(reg);
3852 if (HAS_PCH_CPT(dev)) {
3853 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3854 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3855 } else {
3856 temp &= ~FDI_LINK_TRAIN_NONE;
3857 temp |= FDI_LINK_TRAIN_PATTERN_1;
3858 }
3859 /* BPC in FDI rx is consistent with that in PIPECONF */
3860 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003861 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003862 I915_WRITE(reg, temp);
3863
3864 POSTING_READ(reg);
3865 udelay(100);
3866}
3867
Chris Wilson5dce5b932014-01-20 10:17:36 +00003868bool intel_has_pending_fb_unpin(struct drm_device *dev)
3869{
3870 struct intel_crtc *crtc;
3871
3872 /* Note that we don't need to be called with mode_config.lock here
3873 * as our list of CRTC objects is static for the lifetime of the
3874 * device and so cannot disappear as we iterate. Similarly, we can
3875 * happily treat the predicates as racy, atomic checks as userspace
3876 * cannot claim and pin a new fb without at least acquring the
3877 * struct_mutex and so serialising with us.
3878 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003879 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003880 if (atomic_read(&crtc->unpin_work_count) == 0)
3881 continue;
3882
3883 if (crtc->unpin_work)
3884 intel_wait_for_vblank(dev, crtc->pipe);
3885
3886 return true;
3887 }
3888
3889 return false;
3890}
3891
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003892static void page_flip_completed(struct intel_crtc *intel_crtc)
3893{
3894 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3895 struct intel_unpin_work *work = intel_crtc->unpin_work;
3896
3897 /* ensure that the unpin work is consistent wrt ->pending. */
3898 smp_rmb();
3899 intel_crtc->unpin_work = NULL;
3900
3901 if (work->event)
3902 drm_send_vblank_event(intel_crtc->base.dev,
3903 intel_crtc->pipe,
3904 work->event);
3905
3906 drm_crtc_vblank_put(&intel_crtc->base);
3907
3908 wake_up_all(&dev_priv->pending_flip_queue);
3909 queue_work(dev_priv->wq, &work->work);
3910
3911 trace_i915_flip_complete(intel_crtc->plane,
3912 work->pending_flip_obj);
3913}
3914
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003915void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003916{
Chris Wilson0f911282012-04-17 10:05:38 +01003917 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003918 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003919
Daniel Vetter2c10d572012-12-20 21:24:07 +01003920 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003921 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3922 !intel_crtc_has_pending_flip(crtc),
3923 60*HZ) == 0)) {
3924 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003925
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003926 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003927 if (intel_crtc->unpin_work) {
3928 WARN_ONCE(1, "Removing stuck page flip\n");
3929 page_flip_completed(intel_crtc);
3930 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003931 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003932 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003933
Chris Wilson975d5682014-08-20 13:13:34 +01003934 if (crtc->primary->fb) {
3935 mutex_lock(&dev->struct_mutex);
3936 intel_finish_fb(crtc->primary->fb);
3937 mutex_unlock(&dev->struct_mutex);
3938 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003939}
3940
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003941/* Program iCLKIP clock to the desired frequency */
3942static void lpt_program_iclkip(struct drm_crtc *crtc)
3943{
3944 struct drm_device *dev = crtc->dev;
3945 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003946 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003947 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3948 u32 temp;
3949
Daniel Vetter09153002012-12-12 14:06:44 +01003950 mutex_lock(&dev_priv->dpio_lock);
3951
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003952 /* It is necessary to ungate the pixclk gate prior to programming
3953 * the divisors, and gate it back when it is done.
3954 */
3955 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3956
3957 /* Disable SSCCTL */
3958 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003959 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3960 SBI_SSCCTL_DISABLE,
3961 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003962
3963 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003964 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003965 auxdiv = 1;
3966 divsel = 0x41;
3967 phaseinc = 0x20;
3968 } else {
3969 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003970 * but the adjusted_mode->crtc_clock in in KHz. To get the
3971 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003972 * convert the virtual clock precision to KHz here for higher
3973 * precision.
3974 */
3975 u32 iclk_virtual_root_freq = 172800 * 1000;
3976 u32 iclk_pi_range = 64;
3977 u32 desired_divisor, msb_divisor_value, pi_value;
3978
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003979 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003980 msb_divisor_value = desired_divisor / iclk_pi_range;
3981 pi_value = desired_divisor % iclk_pi_range;
3982
3983 auxdiv = 0;
3984 divsel = msb_divisor_value - 2;
3985 phaseinc = pi_value;
3986 }
3987
3988 /* This should not happen with any sane values */
3989 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3990 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3991 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3992 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3993
3994 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 +03003995 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003996 auxdiv,
3997 divsel,
3998 phasedir,
3999 phaseinc);
4000
4001 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004002 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004003 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4004 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4005 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4006 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4007 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4008 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004009 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004010
4011 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004012 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004013 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4014 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004015 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004016
4017 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004018 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004019 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004020 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004021
4022 /* Wait for initialization time */
4023 udelay(24);
4024
4025 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004026
4027 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004028}
4029
Daniel Vetter275f01b22013-05-03 11:49:47 +02004030static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4031 enum pipe pch_transcoder)
4032{
4033 struct drm_device *dev = crtc->base.dev;
4034 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004035 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004036
4037 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4038 I915_READ(HTOTAL(cpu_transcoder)));
4039 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4040 I915_READ(HBLANK(cpu_transcoder)));
4041 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4042 I915_READ(HSYNC(cpu_transcoder)));
4043
4044 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4045 I915_READ(VTOTAL(cpu_transcoder)));
4046 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4047 I915_READ(VBLANK(cpu_transcoder)));
4048 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4049 I915_READ(VSYNC(cpu_transcoder)));
4050 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4051 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4052}
4053
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004054static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004055{
4056 struct drm_i915_private *dev_priv = dev->dev_private;
4057 uint32_t temp;
4058
4059 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004060 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004061 return;
4062
4063 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4064 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4065
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004066 temp &= ~FDI_BC_BIFURCATION_SELECT;
4067 if (enable)
4068 temp |= FDI_BC_BIFURCATION_SELECT;
4069
4070 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004071 I915_WRITE(SOUTH_CHICKEN1, temp);
4072 POSTING_READ(SOUTH_CHICKEN1);
4073}
4074
4075static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4076{
4077 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004078
4079 switch (intel_crtc->pipe) {
4080 case PIPE_A:
4081 break;
4082 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004083 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004084 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004085 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004086 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004087
4088 break;
4089 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004090 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004091
4092 break;
4093 default:
4094 BUG();
4095 }
4096}
4097
Jesse Barnesf67a5592011-01-05 10:31:48 -08004098/*
4099 * Enable PCH resources required for PCH ports:
4100 * - PCH PLLs
4101 * - FDI training & RX/TX
4102 * - update transcoder timings
4103 * - DP transcoding bits
4104 * - transcoder
4105 */
4106static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004107{
4108 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004109 struct drm_i915_private *dev_priv = dev->dev_private;
4110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4111 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004112 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004113
Daniel Vetterab9412b2013-05-03 11:49:46 +02004114 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004115
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004116 if (IS_IVYBRIDGE(dev))
4117 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4118
Daniel Vettercd986ab2012-10-26 10:58:12 +02004119 /* Write the TU size bits before fdi link training, so that error
4120 * detection works. */
4121 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4122 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4123
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004124 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004125 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004126
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004127 /* We need to program the right clock selection before writing the pixel
4128 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004129 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004130 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004131
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004132 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004133 temp |= TRANS_DPLL_ENABLE(pipe);
4134 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004135 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004136 temp |= sel;
4137 else
4138 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004139 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004140 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004141
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004142 /* XXX: pch pll's can be enabled any time before we enable the PCH
4143 * transcoder, and we actually should do this to not upset any PCH
4144 * transcoder that already use the clock when we share it.
4145 *
4146 * Note that enable_shared_dpll tries to do the right thing, but
4147 * get_shared_dpll unconditionally resets the pll - we need that to have
4148 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004149 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004150
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004151 /* set transcoder timing, panel must allow it */
4152 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004153 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004154
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004155 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004156
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004157 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004158 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004159 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004160 reg = TRANS_DP_CTL(pipe);
4161 temp = I915_READ(reg);
4162 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004163 TRANS_DP_SYNC_MASK |
4164 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01004165 temp |= (TRANS_DP_OUTPUT_ENABLE |
4166 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004167 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004168
4169 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004170 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004171 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004172 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004173
4174 switch (intel_trans_dp_port_sel(crtc)) {
4175 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004176 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004177 break;
4178 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004179 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004180 break;
4181 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004182 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004183 break;
4184 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004185 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004186 }
4187
Chris Wilson5eddb702010-09-11 13:48:45 +01004188 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004189 }
4190
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004191 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004192}
4193
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004194static void lpt_pch_enable(struct drm_crtc *crtc)
4195{
4196 struct drm_device *dev = crtc->dev;
4197 struct drm_i915_private *dev_priv = dev->dev_private;
4198 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004199 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004200
Daniel Vetterab9412b2013-05-03 11:49:46 +02004201 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004202
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004203 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004204
Paulo Zanoni0540e482012-10-31 18:12:40 -02004205 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004206 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004207
Paulo Zanoni937bb612012-10-31 18:12:47 -02004208 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004209}
4210
Daniel Vetter716c2e52014-06-25 22:02:02 +03004211void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004212{
Daniel Vettere2b78262013-06-07 23:10:03 +02004213 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004214
4215 if (pll == NULL)
4216 return;
4217
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004218 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004219 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004220 return;
4221 }
4222
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004223 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4224 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004225 WARN_ON(pll->on);
4226 WARN_ON(pll->active);
4227 }
4228
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004229 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004230}
4231
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004232struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4233 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004234{
Daniel Vettere2b78262013-06-07 23:10:03 +02004235 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004236 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004237 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004238
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004239 if (HAS_PCH_IBX(dev_priv->dev)) {
4240 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004241 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004242 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004243
Daniel Vetter46edb022013-06-05 13:34:12 +02004244 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4245 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004246
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004247 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004248
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004249 goto found;
4250 }
4251
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304252 if (IS_BROXTON(dev_priv->dev)) {
4253 /* PLL is attached to port in bxt */
4254 struct intel_encoder *encoder;
4255 struct intel_digital_port *intel_dig_port;
4256
4257 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4258 if (WARN_ON(!encoder))
4259 return NULL;
4260
4261 intel_dig_port = enc_to_dig_port(&encoder->base);
4262 /* 1:1 mapping between ports and PLLs */
4263 i = (enum intel_dpll_id)intel_dig_port->port;
4264 pll = &dev_priv->shared_dplls[i];
4265 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4266 crtc->base.base.id, pll->name);
4267 WARN_ON(pll->new_config->crtc_mask);
4268
4269 goto found;
4270 }
4271
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004272 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4273 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004274
4275 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004276 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004277 continue;
4278
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004279 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004280 &pll->new_config->hw_state,
4281 sizeof(pll->new_config->hw_state)) == 0) {
4282 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004283 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004284 pll->new_config->crtc_mask,
4285 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004286 goto found;
4287 }
4288 }
4289
4290 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004291 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4292 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004293 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004294 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4295 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004296 goto found;
4297 }
4298 }
4299
4300 return NULL;
4301
4302found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004303 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004304 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004305
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004306 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004307 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4308 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004309
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004310 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004311
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004312 return pll;
4313}
4314
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004315/**
4316 * intel_shared_dpll_start_config - start a new PLL staged config
4317 * @dev_priv: DRM device
4318 * @clear_pipes: mask of pipes that will have their PLLs freed
4319 *
4320 * Starts a new PLL staged config, copying the current config but
4321 * releasing the references of pipes specified in clear_pipes.
4322 */
4323static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4324 unsigned clear_pipes)
4325{
4326 struct intel_shared_dpll *pll;
4327 enum intel_dpll_id i;
4328
4329 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4330 pll = &dev_priv->shared_dplls[i];
4331
4332 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4333 GFP_KERNEL);
4334 if (!pll->new_config)
4335 goto cleanup;
4336
4337 pll->new_config->crtc_mask &= ~clear_pipes;
4338 }
4339
4340 return 0;
4341
4342cleanup:
4343 while (--i >= 0) {
4344 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004345 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004346 pll->new_config = NULL;
4347 }
4348
4349 return -ENOMEM;
4350}
4351
4352static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4353{
4354 struct intel_shared_dpll *pll;
4355 enum intel_dpll_id i;
4356
4357 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4358 pll = &dev_priv->shared_dplls[i];
4359
4360 WARN_ON(pll->new_config == &pll->config);
4361
4362 pll->config = *pll->new_config;
4363 kfree(pll->new_config);
4364 pll->new_config = NULL;
4365 }
4366}
4367
4368static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4369{
4370 struct intel_shared_dpll *pll;
4371 enum intel_dpll_id i;
4372
4373 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4374 pll = &dev_priv->shared_dplls[i];
4375
4376 WARN_ON(pll->new_config == &pll->config);
4377
4378 kfree(pll->new_config);
4379 pll->new_config = NULL;
4380 }
4381}
4382
Daniel Vettera1520312013-05-03 11:49:50 +02004383static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004384{
4385 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004386 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004387 u32 temp;
4388
4389 temp = I915_READ(dslreg);
4390 udelay(500);
4391 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004392 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004393 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004394 }
4395}
4396
Chandra Kondurua1b22782015-04-07 15:28:45 -07004397/**
4398 * skl_update_scaler_users - Stages update to crtc's scaler state
4399 * @intel_crtc: crtc
4400 * @crtc_state: crtc_state
4401 * @plane: plane (NULL indicates crtc is requesting update)
4402 * @plane_state: plane's state
4403 * @force_detach: request unconditional detachment of scaler
4404 *
4405 * This function updates scaler state for requested plane or crtc.
4406 * To request scaler usage update for a plane, caller shall pass plane pointer.
4407 * To request scaler usage update for crtc, caller shall pass plane pointer
4408 * as NULL.
4409 *
4410 * Return
4411 * 0 - scaler_usage updated successfully
4412 * error - requested scaling cannot be supported or other error condition
4413 */
4414int
4415skl_update_scaler_users(
4416 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4417 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4418 int force_detach)
4419{
4420 int need_scaling;
4421 int idx;
4422 int src_w, src_h, dst_w, dst_h;
4423 int *scaler_id;
4424 struct drm_framebuffer *fb;
4425 struct intel_crtc_scaler_state *scaler_state;
Chandra Konduru6156a452015-04-27 13:48:39 -07004426 unsigned int rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004427
4428 if (!intel_crtc || !crtc_state)
4429 return 0;
4430
4431 scaler_state = &crtc_state->scaler_state;
4432
4433 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4434 fb = intel_plane ? plane_state->base.fb : NULL;
4435
4436 if (intel_plane) {
4437 src_w = drm_rect_width(&plane_state->src) >> 16;
4438 src_h = drm_rect_height(&plane_state->src) >> 16;
4439 dst_w = drm_rect_width(&plane_state->dst);
4440 dst_h = drm_rect_height(&plane_state->dst);
4441 scaler_id = &plane_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004442 rotation = plane_state->base.rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004443 } else {
4444 struct drm_display_mode *adjusted_mode =
4445 &crtc_state->base.adjusted_mode;
4446 src_w = crtc_state->pipe_src_w;
4447 src_h = crtc_state->pipe_src_h;
4448 dst_w = adjusted_mode->hdisplay;
4449 dst_h = adjusted_mode->vdisplay;
4450 scaler_id = &scaler_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004451 rotation = DRM_ROTATE_0;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004452 }
Chandra Konduru6156a452015-04-27 13:48:39 -07004453
4454 need_scaling = intel_rotation_90_or_270(rotation) ?
4455 (src_h != dst_w || src_w != dst_h):
4456 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004457
4458 /*
4459 * if plane is being disabled or scaler is no more required or force detach
4460 * - free scaler binded to this plane/crtc
4461 * - in order to do this, update crtc->scaler_usage
4462 *
4463 * Here scaler state in crtc_state is set free so that
4464 * scaler can be assigned to other user. Actual register
4465 * update to free the scaler is done in plane/panel-fit programming.
4466 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4467 */
4468 if (force_detach || !need_scaling || (intel_plane &&
4469 (!fb || !plane_state->visible))) {
4470 if (*scaler_id >= 0) {
4471 scaler_state->scaler_users &= ~(1 << idx);
4472 scaler_state->scalers[*scaler_id].in_use = 0;
4473
4474 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4475 "crtc_state = %p scaler_users = 0x%x\n",
4476 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4477 intel_plane ? intel_plane->base.base.id :
4478 intel_crtc->base.base.id, crtc_state,
4479 scaler_state->scaler_users);
4480 *scaler_id = -1;
4481 }
4482 return 0;
4483 }
4484
4485 /* range checks */
4486 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4487 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4488
4489 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4490 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4491 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4492 "size is out of scaler range\n",
4493 intel_plane ? "PLANE" : "CRTC",
4494 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4495 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4496 return -EINVAL;
4497 }
4498
4499 /* check colorkey */
4500 if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
4501 DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
4502 intel_plane->base.base.id);
4503 return -EINVAL;
4504 }
4505
4506 /* Check src format */
4507 if (intel_plane) {
4508 switch (fb->pixel_format) {
4509 case DRM_FORMAT_RGB565:
4510 case DRM_FORMAT_XBGR8888:
4511 case DRM_FORMAT_XRGB8888:
4512 case DRM_FORMAT_ABGR8888:
4513 case DRM_FORMAT_ARGB8888:
4514 case DRM_FORMAT_XRGB2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004515 case DRM_FORMAT_XBGR2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004516 case DRM_FORMAT_YUYV:
4517 case DRM_FORMAT_YVYU:
4518 case DRM_FORMAT_UYVY:
4519 case DRM_FORMAT_VYUY:
4520 break;
4521 default:
4522 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4523 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4524 return -EINVAL;
4525 }
4526 }
4527
4528 /* mark this plane as a scaler user in crtc_state */
4529 scaler_state->scaler_users |= (1 << idx);
4530 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4531 "crtc_state = %p scaler_users = 0x%x\n",
4532 intel_plane ? "PLANE" : "CRTC",
4533 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4534 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4535 return 0;
4536}
4537
4538static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004539{
4540 struct drm_device *dev = crtc->base.dev;
4541 struct drm_i915_private *dev_priv = dev->dev_private;
4542 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004543 struct intel_crtc_scaler_state *scaler_state =
4544 &crtc->config->scaler_state;
4545
4546 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4547
4548 /* To update pfit, first update scaler state */
4549 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4550 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4551 skl_detach_scalers(crtc);
4552 if (!enable)
4553 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004554
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004555 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004556 int id;
4557
4558 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4559 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4560 return;
4561 }
4562
4563 id = scaler_state->scaler_id;
4564 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4565 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4566 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4567 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4568
4569 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004570 }
4571}
4572
Jesse Barnesb074cec2013-04-25 12:55:02 -07004573static void ironlake_pfit_enable(struct intel_crtc *crtc)
4574{
4575 struct drm_device *dev = crtc->base.dev;
4576 struct drm_i915_private *dev_priv = dev->dev_private;
4577 int pipe = crtc->pipe;
4578
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004579 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004580 /* Force use of hard-coded filter coefficients
4581 * as some pre-programmed values are broken,
4582 * e.g. x201.
4583 */
4584 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4585 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4586 PF_PIPE_SEL_IVB(pipe));
4587 else
4588 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004589 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4590 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004591 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004592}
4593
Matt Roper4a3b8762014-12-23 10:41:51 -08004594static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004595{
4596 struct drm_device *dev = crtc->dev;
4597 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004598 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004599 struct intel_plane *intel_plane;
4600
Matt Roperaf2b6532014-04-01 15:22:32 -07004601 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4602 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004603 if (intel_plane->pipe == pipe)
4604 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004605 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004606}
4607
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004608void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004609{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004610 struct drm_device *dev = crtc->base.dev;
4611 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004612
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004613 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004614 return;
4615
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004616 /* We can only enable IPS after we enable a plane and wait for a vblank */
4617 intel_wait_for_vblank(dev, crtc->pipe);
4618
Paulo Zanonid77e4532013-09-24 13:52:55 -03004619 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004620 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004621 mutex_lock(&dev_priv->rps.hw_lock);
4622 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4623 mutex_unlock(&dev_priv->rps.hw_lock);
4624 /* Quoting Art Runyan: "its not safe to expect any particular
4625 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004626 * mailbox." Moreover, the mailbox may return a bogus state,
4627 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004628 */
4629 } else {
4630 I915_WRITE(IPS_CTL, IPS_ENABLE);
4631 /* The bit only becomes 1 in the next vblank, so this wait here
4632 * is essentially intel_wait_for_vblank. If we don't have this
4633 * and don't wait for vblanks until the end of crtc_enable, then
4634 * the HW state readout code will complain that the expected
4635 * IPS_CTL value is not the one we read. */
4636 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4637 DRM_ERROR("Timed out waiting for IPS enable\n");
4638 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004639}
4640
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004641void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004642{
4643 struct drm_device *dev = crtc->base.dev;
4644 struct drm_i915_private *dev_priv = dev->dev_private;
4645
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004646 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004647 return;
4648
4649 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004650 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004651 mutex_lock(&dev_priv->rps.hw_lock);
4652 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4653 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004654 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4655 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4656 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004657 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004658 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004659 POSTING_READ(IPS_CTL);
4660 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004661
4662 /* We need to wait for a vblank before we can disable the plane. */
4663 intel_wait_for_vblank(dev, crtc->pipe);
4664}
4665
4666/** Loads the palette/gamma unit for the CRTC with the prepared values */
4667static void intel_crtc_load_lut(struct drm_crtc *crtc)
4668{
4669 struct drm_device *dev = crtc->dev;
4670 struct drm_i915_private *dev_priv = dev->dev_private;
4671 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4672 enum pipe pipe = intel_crtc->pipe;
4673 int palreg = PALETTE(pipe);
4674 int i;
4675 bool reenable_ips = false;
4676
4677 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004678 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004679 return;
4680
Imre Deak50360402015-01-16 00:55:16 -08004681 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004682 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004683 assert_dsi_pll_enabled(dev_priv);
4684 else
4685 assert_pll_enabled(dev_priv, pipe);
4686 }
4687
4688 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304689 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004690 palreg = LGC_PALETTE(pipe);
4691
4692 /* Workaround : Do not read or write the pipe palette/gamma data while
4693 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4694 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004695 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004696 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4697 GAMMA_MODE_MODE_SPLIT)) {
4698 hsw_disable_ips(intel_crtc);
4699 reenable_ips = true;
4700 }
4701
4702 for (i = 0; i < 256; i++) {
4703 I915_WRITE(palreg + 4 * i,
4704 (intel_crtc->lut_r[i] << 16) |
4705 (intel_crtc->lut_g[i] << 8) |
4706 intel_crtc->lut_b[i]);
4707 }
4708
4709 if (reenable_ips)
4710 hsw_enable_ips(intel_crtc);
4711}
4712
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004713static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004714{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004715 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004716 struct drm_device *dev = intel_crtc->base.dev;
4717 struct drm_i915_private *dev_priv = dev->dev_private;
4718
4719 mutex_lock(&dev->struct_mutex);
4720 dev_priv->mm.interruptible = false;
4721 (void) intel_overlay_switch_off(intel_crtc->overlay);
4722 dev_priv->mm.interruptible = true;
4723 mutex_unlock(&dev->struct_mutex);
4724 }
4725
4726 /* Let userspace switch the overlay on again. In most cases userspace
4727 * has to recompute where to put it anyway.
4728 */
4729}
4730
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004731/**
4732 * intel_post_enable_primary - Perform operations after enabling primary plane
4733 * @crtc: the CRTC whose primary plane was just enabled
4734 *
4735 * Performs potentially sleeping operations that must be done after the primary
4736 * plane is enabled, such as updating FBC and IPS. Note that this may be
4737 * called due to an explicit primary plane update, or due to an implicit
4738 * re-enable that is caused when a sprite plane is updated to no longer
4739 * completely hide the primary plane.
4740 */
4741static void
4742intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004743{
4744 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004745 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004746 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4747 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004748
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004749 /*
4750 * BDW signals flip done immediately if the plane
4751 * is disabled, even if the plane enable is already
4752 * armed to occur at the next vblank :(
4753 */
4754 if (IS_BROADWELL(dev))
4755 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004756
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004757 /*
4758 * FIXME IPS should be fine as long as one plane is
4759 * enabled, but in practice it seems to have problems
4760 * when going from primary only to sprite only and vice
4761 * versa.
4762 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004763 hsw_enable_ips(intel_crtc);
4764
4765 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004766 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004767 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004768
4769 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004770 * Gen2 reports pipe underruns whenever all planes are disabled.
4771 * So don't enable underrun reporting before at least some planes
4772 * are enabled.
4773 * FIXME: Need to fix the logic to work when we turn off all planes
4774 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004775 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004776 if (IS_GEN2(dev))
4777 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4778
4779 /* Underruns don't raise interrupts, so check manually. */
4780 if (HAS_GMCH_DISPLAY(dev))
4781 i9xx_check_fifo_underruns(dev_priv);
4782}
4783
4784/**
4785 * intel_pre_disable_primary - Perform operations before disabling primary plane
4786 * @crtc: the CRTC whose primary plane is to be disabled
4787 *
4788 * Performs potentially sleeping operations that must be done before the
4789 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4790 * be called due to an explicit primary plane update, or due to an implicit
4791 * disable that is caused when a sprite plane completely hides the primary
4792 * plane.
4793 */
4794static void
4795intel_pre_disable_primary(struct drm_crtc *crtc)
4796{
4797 struct drm_device *dev = crtc->dev;
4798 struct drm_i915_private *dev_priv = dev->dev_private;
4799 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4800 int pipe = intel_crtc->pipe;
4801
4802 /*
4803 * Gen2 reports pipe underruns whenever all planes are disabled.
4804 * So diasble underrun reporting before all the planes get disabled.
4805 * FIXME: Need to fix the logic to work when we turn off all planes
4806 * but leave the pipe running.
4807 */
4808 if (IS_GEN2(dev))
4809 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4810
4811 /*
4812 * Vblank time updates from the shadow to live plane control register
4813 * are blocked if the memory self-refresh mode is active at that
4814 * moment. So to make sure the plane gets truly disabled, disable
4815 * first the self-refresh mode. The self-refresh enable bit in turn
4816 * will be checked/applied by the HW only at the next frame start
4817 * event which is after the vblank start event, so we need to have a
4818 * wait-for-vblank between disabling the plane and the pipe.
4819 */
4820 if (HAS_GMCH_DISPLAY(dev))
4821 intel_set_memory_cxsr(dev_priv, false);
4822
4823 mutex_lock(&dev->struct_mutex);
4824 if (dev_priv->fbc.crtc == intel_crtc)
4825 intel_fbc_disable(dev);
4826 mutex_unlock(&dev->struct_mutex);
4827
4828 /*
4829 * FIXME IPS should be fine as long as one plane is
4830 * enabled, but in practice it seems to have problems
4831 * when going from primary only to sprite only and vice
4832 * versa.
4833 */
4834 hsw_disable_ips(intel_crtc);
4835}
4836
4837static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4838{
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004839 intel_enable_primary_hw_plane(crtc->primary, crtc);
4840 intel_enable_sprite_planes(crtc);
4841 intel_crtc_update_cursor(crtc, true);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004842
4843 intel_post_enable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004844}
4845
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004846static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004847{
4848 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004849 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004850 struct intel_plane *intel_plane;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004851 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004852
4853 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004854
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004855 intel_pre_disable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004856
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004857 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004858 for_each_intel_plane(dev, intel_plane) {
4859 if (intel_plane->pipe == pipe) {
4860 struct drm_crtc *from = intel_plane->base.crtc;
4861
4862 intel_plane->disable_plane(&intel_plane->base,
4863 from ?: crtc, true);
4864 }
4865 }
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004866
Daniel Vetterf99d7062014-06-19 16:01:59 +02004867 /*
4868 * FIXME: Once we grow proper nuclear flip support out of this we need
4869 * to compute the mask of flip planes precisely. For the time being
4870 * consider this a flip to a NULL plane.
4871 */
4872 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004873}
4874
Jesse Barnesf67a5592011-01-05 10:31:48 -08004875static void ironlake_crtc_enable(struct drm_crtc *crtc)
4876{
4877 struct drm_device *dev = crtc->dev;
4878 struct drm_i915_private *dev_priv = dev->dev_private;
4879 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004880 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004881 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004882
Matt Roper83d65732015-02-25 13:12:16 -08004883 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004884
Jesse Barnesf67a5592011-01-05 10:31:48 -08004885 if (intel_crtc->active)
4886 return;
4887
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004888 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004889 intel_prepare_shared_dpll(intel_crtc);
4890
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004891 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304892 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004893
4894 intel_set_pipe_timings(intel_crtc);
4895
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004896 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004897 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004898 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004899 }
4900
4901 ironlake_set_pipeconf(crtc);
4902
Jesse Barnesf67a5592011-01-05 10:31:48 -08004903 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004904
Daniel Vettera72e4c92014-09-30 10:56:47 +02004905 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4906 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004907
Daniel Vetterf6736a12013-06-05 13:34:30 +02004908 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004909 if (encoder->pre_enable)
4910 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004911
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004912 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004913 /* Note: FDI PLL enabling _must_ be done before we enable the
4914 * cpu pipes, hence this is separate from all the other fdi/pch
4915 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004916 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004917 } else {
4918 assert_fdi_tx_disabled(dev_priv, pipe);
4919 assert_fdi_rx_disabled(dev_priv, pipe);
4920 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004921
Jesse Barnesb074cec2013-04-25 12:55:02 -07004922 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004923
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004924 /*
4925 * On ILK+ LUT must be loaded before the pipe is running but with
4926 * clocks enabled
4927 */
4928 intel_crtc_load_lut(crtc);
4929
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004930 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004931 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004932
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004933 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004934 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004935
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004936 assert_vblank_disabled(crtc);
4937 drm_crtc_vblank_on(crtc);
4938
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004939 for_each_encoder_on_crtc(dev, crtc, encoder)
4940 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004941
4942 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004943 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004944}
4945
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004946/* IPS only exists on ULT machines and is tied to pipe A. */
4947static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4948{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004949 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004950}
4951
Paulo Zanonie4916942013-09-20 16:21:19 -03004952/*
4953 * This implements the workaround described in the "notes" section of the mode
4954 * set sequence documentation. When going from no pipes or single pipe to
4955 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4956 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4957 */
4958static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4959{
4960 struct drm_device *dev = crtc->base.dev;
4961 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4962
4963 /* We want to get the other_active_crtc only if there's only 1 other
4964 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004965 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004966 if (!crtc_it->active || crtc_it == crtc)
4967 continue;
4968
4969 if (other_active_crtc)
4970 return;
4971
4972 other_active_crtc = crtc_it;
4973 }
4974 if (!other_active_crtc)
4975 return;
4976
4977 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4978 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4979}
4980
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004981static void haswell_crtc_enable(struct drm_crtc *crtc)
4982{
4983 struct drm_device *dev = crtc->dev;
4984 struct drm_i915_private *dev_priv = dev->dev_private;
4985 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4986 struct intel_encoder *encoder;
4987 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004988
Matt Roper83d65732015-02-25 13:12:16 -08004989 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004990
4991 if (intel_crtc->active)
4992 return;
4993
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004994 if (intel_crtc_to_shared_dpll(intel_crtc))
4995 intel_enable_shared_dpll(intel_crtc);
4996
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004997 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304998 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004999
5000 intel_set_pipe_timings(intel_crtc);
5001
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005002 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
5003 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
5004 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005005 }
5006
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005007 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005008 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005009 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005010 }
5011
5012 haswell_set_pipeconf(crtc);
5013
5014 intel_set_pipe_csc(crtc);
5015
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005016 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005017
Daniel Vettera72e4c92014-09-30 10:56:47 +02005018 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005019 for_each_encoder_on_crtc(dev, crtc, encoder)
5020 if (encoder->pre_enable)
5021 encoder->pre_enable(encoder);
5022
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005023 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02005024 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5025 true);
Imre Deak4fe94672014-06-25 22:01:49 +03005026 dev_priv->display.fdi_link_train(crtc);
5027 }
5028
Paulo Zanoni1f544382012-10-24 11:32:00 -02005029 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005030
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005031 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005032 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005033 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005034 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005035 else
5036 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005037
5038 /*
5039 * On ILK+ LUT must be loaded before the pipe is running but with
5040 * clocks enabled
5041 */
5042 intel_crtc_load_lut(crtc);
5043
Paulo Zanoni1f544382012-10-24 11:32:00 -02005044 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00005045 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005046
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005047 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005048 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005049
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005050 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005051 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005052
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005053 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005054 intel_ddi_set_vc_payload_alloc(crtc, true);
5055
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005056 assert_vblank_disabled(crtc);
5057 drm_crtc_vblank_on(crtc);
5058
Jani Nikula8807e552013-08-30 19:40:32 +03005059 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005060 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005061 intel_opregion_notify_encoder(encoder, true);
5062 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005063
Paulo Zanonie4916942013-09-20 16:21:19 -03005064 /* If we change the relative order between pipe/planes enabling, we need
5065 * to change the workaround. */
5066 haswell_mode_set_planes_workaround(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005067}
5068
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005069static void ironlake_pfit_disable(struct intel_crtc *crtc)
5070{
5071 struct drm_device *dev = crtc->base.dev;
5072 struct drm_i915_private *dev_priv = dev->dev_private;
5073 int pipe = crtc->pipe;
5074
5075 /* To avoid upsetting the power well on haswell only disable the pfit if
5076 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005077 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005078 I915_WRITE(PF_CTL(pipe), 0);
5079 I915_WRITE(PF_WIN_POS(pipe), 0);
5080 I915_WRITE(PF_WIN_SZ(pipe), 0);
5081 }
5082}
5083
Jesse Barnes6be4a602010-09-10 10:26:01 -07005084static void ironlake_crtc_disable(struct drm_crtc *crtc)
5085{
5086 struct drm_device *dev = crtc->dev;
5087 struct drm_i915_private *dev_priv = dev->dev_private;
5088 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005089 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005090 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005091 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005092
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005093 if (!intel_crtc->active)
5094 return;
5095
Daniel Vetterea9d7582012-07-10 10:42:52 +02005096 for_each_encoder_on_crtc(dev, crtc, encoder)
5097 encoder->disable(encoder);
5098
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005099 drm_crtc_vblank_off(crtc);
5100 assert_vblank_disabled(crtc);
5101
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005102 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005103 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005104
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005105 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005106
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005107 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005108
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005109 for_each_encoder_on_crtc(dev, crtc, encoder)
5110 if (encoder->post_disable)
5111 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005112
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005113 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005114 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005115
Daniel Vetterd925c592013-06-05 13:34:04 +02005116 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005117
Daniel Vetterd925c592013-06-05 13:34:04 +02005118 if (HAS_PCH_CPT(dev)) {
5119 /* disable TRANS_DP_CTL */
5120 reg = TRANS_DP_CTL(pipe);
5121 temp = I915_READ(reg);
5122 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5123 TRANS_DP_PORT_SEL_MASK);
5124 temp |= TRANS_DP_PORT_SEL_NONE;
5125 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005126
Daniel Vetterd925c592013-06-05 13:34:04 +02005127 /* disable DPLL_SEL */
5128 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005129 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005130 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005131 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005132
5133 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005134 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005135
5136 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005137 }
5138
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005139 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005140 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005141
5142 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005143 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005144 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005145}
5146
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005147static void haswell_crtc_disable(struct drm_crtc *crtc)
5148{
5149 struct drm_device *dev = crtc->dev;
5150 struct drm_i915_private *dev_priv = dev->dev_private;
5151 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5152 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005153 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005154
5155 if (!intel_crtc->active)
5156 return;
5157
Jani Nikula8807e552013-08-30 19:40:32 +03005158 for_each_encoder_on_crtc(dev, crtc, encoder) {
5159 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005160 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005161 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005162
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005163 drm_crtc_vblank_off(crtc);
5164 assert_vblank_disabled(crtc);
5165
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005166 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005167 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5168 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005169 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005170
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005171 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005172 intel_ddi_set_vc_payload_alloc(crtc, false);
5173
Paulo Zanoniad80a812012-10-24 16:06:19 -02005174 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005175
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005176 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005177 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005178 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005179 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005180 else
5181 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005182
Paulo Zanoni1f544382012-10-24 11:32:00 -02005183 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005184
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005185 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005186 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005187 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005188 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005189
Imre Deak97b040a2014-06-25 22:01:50 +03005190 for_each_encoder_on_crtc(dev, crtc, encoder)
5191 if (encoder->post_disable)
5192 encoder->post_disable(encoder);
5193
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005194 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005195 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005196
5197 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005198 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005199 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005200
5201 if (intel_crtc_to_shared_dpll(intel_crtc))
5202 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005203}
5204
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005205static void ironlake_crtc_off(struct drm_crtc *crtc)
5206{
5207 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005208 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005209}
5210
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005211
Jesse Barnes2dd24552013-04-25 12:55:01 -07005212static void i9xx_pfit_enable(struct intel_crtc *crtc)
5213{
5214 struct drm_device *dev = crtc->base.dev;
5215 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005216 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005217
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005218 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005219 return;
5220
Daniel Vetterc0b03412013-05-28 12:05:54 +02005221 /*
5222 * The panel fitter should only be adjusted whilst the pipe is disabled,
5223 * according to register description and PRM.
5224 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005225 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5226 assert_pipe_disabled(dev_priv, crtc->pipe);
5227
Jesse Barnesb074cec2013-04-25 12:55:02 -07005228 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5229 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005230
5231 /* Border color in case we don't scale up to the full screen. Black by
5232 * default, change to something else for debugging. */
5233 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005234}
5235
Dave Airlied05410f2014-06-05 13:22:59 +10005236static enum intel_display_power_domain port_to_power_domain(enum port port)
5237{
5238 switch (port) {
5239 case PORT_A:
5240 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5241 case PORT_B:
5242 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5243 case PORT_C:
5244 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5245 case PORT_D:
5246 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5247 default:
5248 WARN_ON_ONCE(1);
5249 return POWER_DOMAIN_PORT_OTHER;
5250 }
5251}
5252
Imre Deak77d22dc2014-03-05 16:20:52 +02005253#define for_each_power_domain(domain, mask) \
5254 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5255 if ((1 << (domain)) & (mask))
5256
Imre Deak319be8a2014-03-04 19:22:57 +02005257enum intel_display_power_domain
5258intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005259{
Imre Deak319be8a2014-03-04 19:22:57 +02005260 struct drm_device *dev = intel_encoder->base.dev;
5261 struct intel_digital_port *intel_dig_port;
5262
5263 switch (intel_encoder->type) {
5264 case INTEL_OUTPUT_UNKNOWN:
5265 /* Only DDI platforms should ever use this output type */
5266 WARN_ON_ONCE(!HAS_DDI(dev));
5267 case INTEL_OUTPUT_DISPLAYPORT:
5268 case INTEL_OUTPUT_HDMI:
5269 case INTEL_OUTPUT_EDP:
5270 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005271 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005272 case INTEL_OUTPUT_DP_MST:
5273 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5274 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005275 case INTEL_OUTPUT_ANALOG:
5276 return POWER_DOMAIN_PORT_CRT;
5277 case INTEL_OUTPUT_DSI:
5278 return POWER_DOMAIN_PORT_DSI;
5279 default:
5280 return POWER_DOMAIN_PORT_OTHER;
5281 }
5282}
5283
5284static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5285{
5286 struct drm_device *dev = crtc->dev;
5287 struct intel_encoder *intel_encoder;
5288 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5289 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005290 unsigned long mask;
5291 enum transcoder transcoder;
5292
5293 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5294
5295 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5296 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005297 if (intel_crtc->config->pch_pfit.enabled ||
5298 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005299 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5300
Imre Deak319be8a2014-03-04 19:22:57 +02005301 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5302 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5303
Imre Deak77d22dc2014-03-05 16:20:52 +02005304 return mask;
5305}
5306
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005307static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005308{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005309 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005310 struct drm_i915_private *dev_priv = dev->dev_private;
5311 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5312 struct intel_crtc *crtc;
5313
5314 /*
5315 * First get all needed power domains, then put all unneeded, to avoid
5316 * any unnecessary toggling of the power wells.
5317 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005318 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005319 enum intel_display_power_domain domain;
5320
Matt Roper83d65732015-02-25 13:12:16 -08005321 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005322 continue;
5323
Imre Deak319be8a2014-03-04 19:22:57 +02005324 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005325
5326 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5327 intel_display_power_get(dev_priv, domain);
5328 }
5329
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005330 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005331 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005332
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005333 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005334 enum intel_display_power_domain domain;
5335
5336 for_each_power_domain(domain, crtc->enabled_power_domains)
5337 intel_display_power_put(dev_priv, domain);
5338
5339 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5340 }
5341
5342 intel_display_set_init_power(dev_priv, false);
5343}
5344
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305345void broxton_set_cdclk(struct drm_device *dev, int frequency)
5346{
5347 struct drm_i915_private *dev_priv = dev->dev_private;
5348 uint32_t divider;
5349 uint32_t ratio;
5350 uint32_t current_freq;
5351 int ret;
5352
5353 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5354 switch (frequency) {
5355 case 144000:
5356 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5357 ratio = BXT_DE_PLL_RATIO(60);
5358 break;
5359 case 288000:
5360 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5361 ratio = BXT_DE_PLL_RATIO(60);
5362 break;
5363 case 384000:
5364 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5365 ratio = BXT_DE_PLL_RATIO(60);
5366 break;
5367 case 576000:
5368 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5369 ratio = BXT_DE_PLL_RATIO(60);
5370 break;
5371 case 624000:
5372 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5373 ratio = BXT_DE_PLL_RATIO(65);
5374 break;
5375 case 19200:
5376 /*
5377 * Bypass frequency with DE PLL disabled. Init ratio, divider
5378 * to suppress GCC warning.
5379 */
5380 ratio = 0;
5381 divider = 0;
5382 break;
5383 default:
5384 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5385
5386 return;
5387 }
5388
5389 mutex_lock(&dev_priv->rps.hw_lock);
5390 /* Inform power controller of upcoming frequency change */
5391 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5392 0x80000000);
5393 mutex_unlock(&dev_priv->rps.hw_lock);
5394
5395 if (ret) {
5396 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5397 ret, frequency);
5398 return;
5399 }
5400
5401 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5402 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5403 current_freq = current_freq * 500 + 1000;
5404
5405 /*
5406 * DE PLL has to be disabled when
5407 * - setting to 19.2MHz (bypass, PLL isn't used)
5408 * - before setting to 624MHz (PLL needs toggling)
5409 * - before setting to any frequency from 624MHz (PLL needs toggling)
5410 */
5411 if (frequency == 19200 || frequency == 624000 ||
5412 current_freq == 624000) {
5413 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5414 /* Timeout 200us */
5415 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5416 1))
5417 DRM_ERROR("timout waiting for DE PLL unlock\n");
5418 }
5419
5420 if (frequency != 19200) {
5421 uint32_t val;
5422
5423 val = I915_READ(BXT_DE_PLL_CTL);
5424 val &= ~BXT_DE_PLL_RATIO_MASK;
5425 val |= ratio;
5426 I915_WRITE(BXT_DE_PLL_CTL, val);
5427
5428 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5429 /* Timeout 200us */
5430 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5431 DRM_ERROR("timeout waiting for DE PLL lock\n");
5432
5433 val = I915_READ(CDCLK_CTL);
5434 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5435 val |= divider;
5436 /*
5437 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5438 * enable otherwise.
5439 */
5440 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5441 if (frequency >= 500000)
5442 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5443
5444 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5445 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5446 val |= (frequency - 1000) / 500;
5447 I915_WRITE(CDCLK_CTL, val);
5448 }
5449
5450 mutex_lock(&dev_priv->rps.hw_lock);
5451 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5452 DIV_ROUND_UP(frequency, 25000));
5453 mutex_unlock(&dev_priv->rps.hw_lock);
5454
5455 if (ret) {
5456 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5457 ret, frequency);
5458 return;
5459 }
5460
5461 dev_priv->cdclk_freq = frequency;
5462}
5463
5464void broxton_init_cdclk(struct drm_device *dev)
5465{
5466 struct drm_i915_private *dev_priv = dev->dev_private;
5467 uint32_t val;
5468
5469 /*
5470 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5471 * or else the reset will hang because there is no PCH to respond.
5472 * Move the handshake programming to initialization sequence.
5473 * Previously was left up to BIOS.
5474 */
5475 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5476 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5477 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5478
5479 /* Enable PG1 for cdclk */
5480 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5481
5482 /* check if cd clock is enabled */
5483 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5484 DRM_DEBUG_KMS("Display already initialized\n");
5485 return;
5486 }
5487
5488 /*
5489 * FIXME:
5490 * - The initial CDCLK needs to be read from VBT.
5491 * Need to make this change after VBT has changes for BXT.
5492 * - check if setting the max (or any) cdclk freq is really necessary
5493 * here, it belongs to modeset time
5494 */
5495 broxton_set_cdclk(dev, 624000);
5496
5497 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005498 POSTING_READ(DBUF_CTL);
5499
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305500 udelay(10);
5501
5502 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5503 DRM_ERROR("DBuf power enable timeout!\n");
5504}
5505
5506void broxton_uninit_cdclk(struct drm_device *dev)
5507{
5508 struct drm_i915_private *dev_priv = dev->dev_private;
5509
5510 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005511 POSTING_READ(DBUF_CTL);
5512
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305513 udelay(10);
5514
5515 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5516 DRM_ERROR("DBuf power disable timeout!\n");
5517
5518 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5519 broxton_set_cdclk(dev, 19200);
5520
5521 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5522}
5523
Ville Syrjälädfcab172014-06-13 13:37:47 +03005524/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005525static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005526{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005527 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005528
Jesse Barnes586f49d2013-11-04 16:06:59 -08005529 /* Obtain SKU information */
5530 mutex_lock(&dev_priv->dpio_lock);
5531 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5532 CCK_FUSE_HPLL_FREQ_MASK;
5533 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005534
Ville Syrjälädfcab172014-06-13 13:37:47 +03005535 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005536}
5537
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005538static void vlv_update_cdclk(struct drm_device *dev)
5539{
5540 struct drm_i915_private *dev_priv = dev->dev_private;
5541
Vandana Kannan164dfd22014-11-24 13:37:41 +05305542 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03005543 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Vandana Kannan164dfd22014-11-24 13:37:41 +05305544 dev_priv->cdclk_freq);
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005545
5546 /*
5547 * Program the gmbus_freq based on the cdclk frequency.
5548 * BSpec erroneously claims we should aim for 4MHz, but
5549 * in fact 1MHz is the correct frequency.
5550 */
Vandana Kannan164dfd22014-11-24 13:37:41 +05305551 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005552}
5553
Jesse Barnes30a970c2013-11-04 13:48:12 -08005554/* Adjust CDclk dividers to allow high res or save power if possible */
5555static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5556{
5557 struct drm_i915_private *dev_priv = dev->dev_private;
5558 u32 val, cmd;
5559
Vandana Kannan164dfd22014-11-24 13:37:41 +05305560 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5561 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005562
Ville Syrjälädfcab172014-06-13 13:37:47 +03005563 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005564 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005565 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005566 cmd = 1;
5567 else
5568 cmd = 0;
5569
5570 mutex_lock(&dev_priv->rps.hw_lock);
5571 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5572 val &= ~DSPFREQGUAR_MASK;
5573 val |= (cmd << DSPFREQGUAR_SHIFT);
5574 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5575 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5576 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5577 50)) {
5578 DRM_ERROR("timed out waiting for CDclk change\n");
5579 }
5580 mutex_unlock(&dev_priv->rps.hw_lock);
5581
Ville Syrjälädfcab172014-06-13 13:37:47 +03005582 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005583 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005584
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005585 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005586
5587 mutex_lock(&dev_priv->dpio_lock);
5588 /* adjust cdclk divider */
5589 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005590 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005591 val |= divider;
5592 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005593
5594 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5595 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5596 50))
5597 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005598 mutex_unlock(&dev_priv->dpio_lock);
5599 }
5600
5601 mutex_lock(&dev_priv->dpio_lock);
5602 /* adjust self-refresh exit latency value */
5603 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5604 val &= ~0x7f;
5605
5606 /*
5607 * For high bandwidth configs, we set a higher latency in the bunit
5608 * so that the core display fetch happens in time to avoid underruns.
5609 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005610 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005611 val |= 4500 / 250; /* 4.5 usec */
5612 else
5613 val |= 3000 / 250; /* 3.0 usec */
5614 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5615 mutex_unlock(&dev_priv->dpio_lock);
5616
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005617 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005618}
5619
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005620static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5621{
5622 struct drm_i915_private *dev_priv = dev->dev_private;
5623 u32 val, cmd;
5624
Vandana Kannan164dfd22014-11-24 13:37:41 +05305625 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5626 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005627
5628 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005629 case 333333:
5630 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005631 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005632 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005633 break;
5634 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005635 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005636 return;
5637 }
5638
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005639 /*
5640 * Specs are full of misinformation, but testing on actual
5641 * hardware has shown that we just need to write the desired
5642 * CCK divider into the Punit register.
5643 */
5644 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5645
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005646 mutex_lock(&dev_priv->rps.hw_lock);
5647 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5648 val &= ~DSPFREQGUAR_MASK_CHV;
5649 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5650 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5651 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5652 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5653 50)) {
5654 DRM_ERROR("timed out waiting for CDclk change\n");
5655 }
5656 mutex_unlock(&dev_priv->rps.hw_lock);
5657
5658 vlv_update_cdclk(dev);
5659}
5660
Jesse Barnes30a970c2013-11-04 13:48:12 -08005661static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5662 int max_pixclk)
5663{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005664 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005665 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005666
Jesse Barnes30a970c2013-11-04 13:48:12 -08005667 /*
5668 * Really only a few cases to deal with, as only 4 CDclks are supported:
5669 * 200MHz
5670 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005671 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005672 * 400MHz (VLV only)
5673 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5674 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005675 *
5676 * We seem to get an unstable or solid color picture at 200MHz.
5677 * Not sure what's wrong. For now use 200MHz only when all pipes
5678 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005679 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005680 if (!IS_CHERRYVIEW(dev_priv) &&
5681 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005682 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005683 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005684 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005685 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005686 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005687 else
5688 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005689}
5690
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305691static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5692 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005693{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305694 /*
5695 * FIXME:
5696 * - remove the guardband, it's not needed on BXT
5697 * - set 19.2MHz bypass frequency if there are no active pipes
5698 */
5699 if (max_pixclk > 576000*9/10)
5700 return 624000;
5701 else if (max_pixclk > 384000*9/10)
5702 return 576000;
5703 else if (max_pixclk > 288000*9/10)
5704 return 384000;
5705 else if (max_pixclk > 144000*9/10)
5706 return 288000;
5707 else
5708 return 144000;
5709}
5710
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005711/* Compute the max pixel clock for new configuration. Uses atomic state if
5712 * that's non-NULL, look at current state otherwise. */
5713static int intel_mode_max_pixclk(struct drm_device *dev,
5714 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005715{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005716 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005717 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005718 int max_pixclk = 0;
5719
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005720 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005721 if (state)
5722 crtc_state =
5723 intel_atomic_get_crtc_state(state, intel_crtc);
5724 else
5725 crtc_state = intel_crtc->config;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005726 if (IS_ERR(crtc_state))
5727 return PTR_ERR(crtc_state);
5728
5729 if (!crtc_state->base.enable)
5730 continue;
5731
5732 max_pixclk = max(max_pixclk,
5733 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005734 }
5735
5736 return max_pixclk;
5737}
5738
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005739static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005740{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005741 struct drm_i915_private *dev_priv = to_i915(state->dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005742 struct drm_crtc *crtc;
5743 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005744 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005745 int cdclk, i;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005746
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005747 if (max_pixclk < 0)
5748 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005749
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305750 if (IS_VALLEYVIEW(dev_priv))
5751 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5752 else
5753 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5754
5755 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005756 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005757
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005758 /* add all active pipes to the state */
5759 for_each_crtc(state->dev, crtc) {
5760 if (!crtc->state->enable)
5761 continue;
5762
5763 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5764 if (IS_ERR(crtc_state))
5765 return PTR_ERR(crtc_state);
5766 }
5767
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005768 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005769 for_each_crtc_in_state(state, crtc, crtc_state, i)
5770 if (crtc_state->enable)
5771 crtc_state->mode_changed = true;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005772
5773 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005774}
5775
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005776static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5777{
5778 unsigned int credits, default_credits;
5779
5780 if (IS_CHERRYVIEW(dev_priv))
5781 default_credits = PFI_CREDIT(12);
5782 else
5783 default_credits = PFI_CREDIT(8);
5784
Vandana Kannan164dfd22014-11-24 13:37:41 +05305785 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005786 /* CHV suggested value is 31 or 63 */
5787 if (IS_CHERRYVIEW(dev_priv))
5788 credits = PFI_CREDIT_31;
5789 else
5790 credits = PFI_CREDIT(15);
5791 } else {
5792 credits = default_credits;
5793 }
5794
5795 /*
5796 * WA - write default credits before re-programming
5797 * FIXME: should we also set the resend bit here?
5798 */
5799 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5800 default_credits);
5801
5802 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5803 credits | PFI_CREDIT_RESEND);
5804
5805 /*
5806 * FIXME is this guaranteed to clear
5807 * immediately or should we poll for it?
5808 */
5809 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5810}
5811
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005812static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005813{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005814 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005815 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005816 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005817 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005818
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005819 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
5820 * never fail. */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005821 if (WARN_ON(max_pixclk < 0))
5822 return;
5823
5824 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005825
Vandana Kannan164dfd22014-11-24 13:37:41 +05305826 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005827 /*
5828 * FIXME: We can end up here with all power domains off, yet
5829 * with a CDCLK frequency other than the minimum. To account
5830 * for this take the PIPE-A power domain, which covers the HW
5831 * blocks needed for the following programming. This can be
5832 * removed once it's guaranteed that we get here either with
5833 * the minimum CDCLK set, or the required power domains
5834 * enabled.
5835 */
5836 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5837
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005838 if (IS_CHERRYVIEW(dev))
5839 cherryview_set_cdclk(dev, req_cdclk);
5840 else
5841 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005842
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005843 vlv_program_pfi_credits(dev_priv);
5844
Imre Deak738c05c2014-11-19 16:25:37 +02005845 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005846 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005847}
5848
Jesse Barnes89b667f2013-04-18 14:51:36 -07005849static void valleyview_crtc_enable(struct drm_crtc *crtc)
5850{
5851 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005852 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5854 struct intel_encoder *encoder;
5855 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005856 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005857
Matt Roper83d65732015-02-25 13:12:16 -08005858 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005859
5860 if (intel_crtc->active)
5861 return;
5862
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005863 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305864
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005865 if (!is_dsi) {
5866 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005867 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005868 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005869 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005870 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005871
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005872 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305873 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005874
5875 intel_set_pipe_timings(intel_crtc);
5876
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005877 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5878 struct drm_i915_private *dev_priv = dev->dev_private;
5879
5880 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5881 I915_WRITE(CHV_CANVAS(pipe), 0);
5882 }
5883
Daniel Vetter5b18e572014-04-24 23:55:06 +02005884 i9xx_set_pipeconf(intel_crtc);
5885
Jesse Barnes89b667f2013-04-18 14:51:36 -07005886 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005887
Daniel Vettera72e4c92014-09-30 10:56:47 +02005888 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005889
Jesse Barnes89b667f2013-04-18 14:51:36 -07005890 for_each_encoder_on_crtc(dev, crtc, encoder)
5891 if (encoder->pre_pll_enable)
5892 encoder->pre_pll_enable(encoder);
5893
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005894 if (!is_dsi) {
5895 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005896 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005897 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005898 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005899 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005900
5901 for_each_encoder_on_crtc(dev, crtc, encoder)
5902 if (encoder->pre_enable)
5903 encoder->pre_enable(encoder);
5904
Jesse Barnes2dd24552013-04-25 12:55:01 -07005905 i9xx_pfit_enable(intel_crtc);
5906
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005907 intel_crtc_load_lut(crtc);
5908
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005909 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005910 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005911
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005912 assert_vblank_disabled(crtc);
5913 drm_crtc_vblank_on(crtc);
5914
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005915 for_each_encoder_on_crtc(dev, crtc, encoder)
5916 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005917}
5918
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005919static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5920{
5921 struct drm_device *dev = crtc->base.dev;
5922 struct drm_i915_private *dev_priv = dev->dev_private;
5923
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005924 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5925 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005926}
5927
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005928static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005929{
5930 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005931 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005932 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005933 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005934 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005935
Matt Roper83d65732015-02-25 13:12:16 -08005936 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02005937
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005938 if (intel_crtc->active)
5939 return;
5940
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005941 i9xx_set_pll_dividers(intel_crtc);
5942
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005943 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305944 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005945
5946 intel_set_pipe_timings(intel_crtc);
5947
Daniel Vetter5b18e572014-04-24 23:55:06 +02005948 i9xx_set_pipeconf(intel_crtc);
5949
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005950 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005951
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005952 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005953 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005954
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005955 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005956 if (encoder->pre_enable)
5957 encoder->pre_enable(encoder);
5958
Daniel Vetterf6736a12013-06-05 13:34:30 +02005959 i9xx_enable_pll(intel_crtc);
5960
Jesse Barnes2dd24552013-04-25 12:55:01 -07005961 i9xx_pfit_enable(intel_crtc);
5962
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005963 intel_crtc_load_lut(crtc);
5964
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005965 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005966 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005967
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005968 assert_vblank_disabled(crtc);
5969 drm_crtc_vblank_on(crtc);
5970
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005971 for_each_encoder_on_crtc(dev, crtc, encoder)
5972 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005973}
5974
Daniel Vetter87476d62013-04-11 16:29:06 +02005975static void i9xx_pfit_disable(struct intel_crtc *crtc)
5976{
5977 struct drm_device *dev = crtc->base.dev;
5978 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02005979
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005980 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005981 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02005982
5983 assert_pipe_disabled(dev_priv, crtc->pipe);
5984
Daniel Vetter328d8e82013-05-08 10:36:31 +02005985 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5986 I915_READ(PFIT_CONTROL));
5987 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02005988}
5989
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005990static void i9xx_crtc_disable(struct drm_crtc *crtc)
5991{
5992 struct drm_device *dev = crtc->dev;
5993 struct drm_i915_private *dev_priv = dev->dev_private;
5994 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005995 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005996 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005997
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005998 if (!intel_crtc->active)
5999 return;
6000
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006001 /*
6002 * On gen2 planes are double buffered but the pipe isn't, so we must
6003 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006004 * We also need to wait on all gmch platforms because of the
6005 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006006 */
Imre Deak564ed192014-06-13 14:54:21 +03006007 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006008
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006009 for_each_encoder_on_crtc(dev, crtc, encoder)
6010 encoder->disable(encoder);
6011
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006012 drm_crtc_vblank_off(crtc);
6013 assert_vblank_disabled(crtc);
6014
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006015 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006016
Daniel Vetter87476d62013-04-11 16:29:06 +02006017 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006018
Jesse Barnes89b667f2013-04-18 14:51:36 -07006019 for_each_encoder_on_crtc(dev, crtc, encoder)
6020 if (encoder->post_disable)
6021 encoder->post_disable(encoder);
6022
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006023 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006024 if (IS_CHERRYVIEW(dev))
6025 chv_disable_pll(dev_priv, pipe);
6026 else if (IS_VALLEYVIEW(dev))
6027 vlv_disable_pll(dev_priv, pipe);
6028 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006029 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006030 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006031
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006032 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006033 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006034
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006035 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006036 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006037
Daniel Vetterefa96242014-04-24 23:55:02 +02006038 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006039 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02006040 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006041}
6042
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006043static void i9xx_crtc_off(struct drm_crtc *crtc)
6044{
6045}
6046
Borun Fub04c5bd2014-07-12 10:02:27 +05306047/* Master function to enable/disable CRTC and corresponding power wells */
6048void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01006049{
Chris Wilsoncdd59982010-09-08 16:30:16 +01006050 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006051 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006052 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006053 enum intel_display_power_domain domain;
6054 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006055
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006056 if (enable) {
6057 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006058 domains = get_crtc_power_domains(crtc);
6059 for_each_power_domain(domain, domains)
6060 intel_display_power_get(dev_priv, domain);
6061 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006062
6063 dev_priv->display.crtc_enable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006064 intel_crtc_enable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006065 }
6066 } else {
6067 if (intel_crtc->active) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006068 intel_crtc_disable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006069 dev_priv->display.crtc_disable(crtc);
6070
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006071 domains = intel_crtc->enabled_power_domains;
6072 for_each_power_domain(domain, domains)
6073 intel_display_power_put(dev_priv, domain);
6074 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006075 }
6076 }
Borun Fub04c5bd2014-07-12 10:02:27 +05306077}
6078
6079/**
6080 * Sets the power management mode of the pipe and plane.
6081 */
6082void intel_crtc_update_dpms(struct drm_crtc *crtc)
6083{
6084 struct drm_device *dev = crtc->dev;
6085 struct intel_encoder *intel_encoder;
6086 bool enable = false;
6087
6088 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6089 enable |= intel_encoder->connectors_active;
6090
6091 intel_crtc_control(crtc, enable);
Ander Conselvan de Oliveira0f63cca2015-04-21 17:13:17 +03006092
6093 crtc->state->active = enable;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006094}
6095
Daniel Vetter976f8a22012-07-08 22:34:21 +02006096static void intel_crtc_disable(struct drm_crtc *crtc)
6097{
6098 struct drm_device *dev = crtc->dev;
6099 struct drm_connector *connector;
6100 struct drm_i915_private *dev_priv = dev->dev_private;
6101
6102 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08006103 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006104
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006105 intel_crtc_disable_planes(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006106 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006107 dev_priv->display.off(crtc);
6108
Matt Roper70a101f2015-04-08 18:56:53 -07006109 drm_plane_helper_disable(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006110
6111 /* Update computed state. */
6112 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6113 if (!connector->encoder || !connector->encoder->crtc)
6114 continue;
6115
6116 if (connector->encoder->crtc != crtc)
6117 continue;
6118
6119 connector->dpms = DRM_MODE_DPMS_OFF;
6120 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01006121 }
6122}
6123
Chris Wilsonea5b2132010-08-04 13:50:23 +01006124void intel_encoder_destroy(struct drm_encoder *encoder)
6125{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006126 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006127
Chris Wilsonea5b2132010-08-04 13:50:23 +01006128 drm_encoder_cleanup(encoder);
6129 kfree(intel_encoder);
6130}
6131
Damien Lespiau92373292013-08-08 22:28:57 +01006132/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006133 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6134 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006135static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006136{
6137 if (mode == DRM_MODE_DPMS_ON) {
6138 encoder->connectors_active = true;
6139
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006140 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006141 } else {
6142 encoder->connectors_active = false;
6143
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006144 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006145 }
6146}
6147
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006148/* Cross check the actual hw state with our own modeset state tracking (and it's
6149 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006150static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006151{
6152 if (connector->get_hw_state(connector)) {
6153 struct intel_encoder *encoder = connector->encoder;
6154 struct drm_crtc *crtc;
6155 bool encoder_enabled;
6156 enum pipe pipe;
6157
6158 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6159 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006160 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006161
Dave Airlie0e32b392014-05-02 14:02:48 +10006162 /* there is no real hw state for MST connectors */
6163 if (connector->mst_port)
6164 return;
6165
Rob Clarke2c719b2014-12-15 13:56:32 -05006166 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006167 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006168 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006169 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006170
Dave Airlie36cd7442014-05-02 13:44:18 +10006171 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006172 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006173 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006174
Dave Airlie36cd7442014-05-02 13:44:18 +10006175 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006176 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6177 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006178 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006179
Dave Airlie36cd7442014-05-02 13:44:18 +10006180 crtc = encoder->base.crtc;
6181
Matt Roper83d65732015-02-25 13:12:16 -08006182 I915_STATE_WARN(!crtc->state->enable,
6183 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006184 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6185 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006186 "encoder active on the wrong pipe\n");
6187 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006188 }
6189}
6190
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006191int intel_connector_init(struct intel_connector *connector)
6192{
6193 struct drm_connector_state *connector_state;
6194
6195 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6196 if (!connector_state)
6197 return -ENOMEM;
6198
6199 connector->base.state = connector_state;
6200 return 0;
6201}
6202
6203struct intel_connector *intel_connector_alloc(void)
6204{
6205 struct intel_connector *connector;
6206
6207 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6208 if (!connector)
6209 return NULL;
6210
6211 if (intel_connector_init(connector) < 0) {
6212 kfree(connector);
6213 return NULL;
6214 }
6215
6216 return connector;
6217}
6218
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006219/* Even simpler default implementation, if there's really no special case to
6220 * consider. */
6221void intel_connector_dpms(struct drm_connector *connector, int mode)
6222{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006223 /* All the simple cases only support two dpms states. */
6224 if (mode != DRM_MODE_DPMS_ON)
6225 mode = DRM_MODE_DPMS_OFF;
6226
6227 if (mode == connector->dpms)
6228 return;
6229
6230 connector->dpms = mode;
6231
6232 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01006233 if (connector->encoder)
6234 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006235
Daniel Vetterb9805142012-08-31 17:37:33 +02006236 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006237}
6238
Daniel Vetterf0947c32012-07-02 13:10:34 +02006239/* Simple connector->get_hw_state implementation for encoders that support only
6240 * one connector and no cloning and hence the encoder state determines the state
6241 * of the connector. */
6242bool intel_connector_get_hw_state(struct intel_connector *connector)
6243{
Daniel Vetter24929352012-07-02 20:28:59 +02006244 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006245 struct intel_encoder *encoder = connector->encoder;
6246
6247 return encoder->get_hw_state(encoder, &pipe);
6248}
6249
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006250static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006251{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006252 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6253 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006254
6255 return 0;
6256}
6257
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006258static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006259 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006260{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006261 struct drm_atomic_state *state = pipe_config->base.state;
6262 struct intel_crtc *other_crtc;
6263 struct intel_crtc_state *other_crtc_state;
6264
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006265 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6266 pipe_name(pipe), pipe_config->fdi_lanes);
6267 if (pipe_config->fdi_lanes > 4) {
6268 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6269 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006270 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006271 }
6272
Paulo Zanonibafb6552013-11-02 21:07:44 -07006273 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006274 if (pipe_config->fdi_lanes > 2) {
6275 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6276 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006277 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006278 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006279 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006280 }
6281 }
6282
6283 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006284 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006285
6286 /* Ivybridge 3 pipe is really complicated */
6287 switch (pipe) {
6288 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006289 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006290 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006291 if (pipe_config->fdi_lanes <= 2)
6292 return 0;
6293
6294 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6295 other_crtc_state =
6296 intel_atomic_get_crtc_state(state, other_crtc);
6297 if (IS_ERR(other_crtc_state))
6298 return PTR_ERR(other_crtc_state);
6299
6300 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006301 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6302 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006303 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006304 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006305 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006306 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006307 if (pipe_config->fdi_lanes > 2) {
6308 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6309 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006310 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006311 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006312
6313 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6314 other_crtc_state =
6315 intel_atomic_get_crtc_state(state, other_crtc);
6316 if (IS_ERR(other_crtc_state))
6317 return PTR_ERR(other_crtc_state);
6318
6319 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006320 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006321 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006322 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006323 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006324 default:
6325 BUG();
6326 }
6327}
6328
Daniel Vettere29c22c2013-02-21 00:00:16 +01006329#define RETRY 1
6330static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006331 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006332{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006333 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006334 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006335 int lane, link_bw, fdi_dotclock, ret;
6336 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006337
Daniel Vettere29c22c2013-02-21 00:00:16 +01006338retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006339 /* FDI is a binary signal running at ~2.7GHz, encoding
6340 * each output octet as 10 bits. The actual frequency
6341 * is stored as a divider into a 100MHz clock, and the
6342 * mode pixel clock is stored in units of 1KHz.
6343 * Hence the bw of each lane in terms of the mode signal
6344 * is:
6345 */
6346 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6347
Damien Lespiau241bfc32013-09-25 16:45:37 +01006348 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006349
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006350 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006351 pipe_config->pipe_bpp);
6352
6353 pipe_config->fdi_lanes = lane;
6354
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006355 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006356 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006357
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006358 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6359 intel_crtc->pipe, pipe_config);
6360 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006361 pipe_config->pipe_bpp -= 2*3;
6362 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6363 pipe_config->pipe_bpp);
6364 needs_recompute = true;
6365 pipe_config->bw_constrained = true;
6366
6367 goto retry;
6368 }
6369
6370 if (needs_recompute)
6371 return RETRY;
6372
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006373 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006374}
6375
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006376static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006377 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006378{
Jani Nikulad330a952014-01-21 11:24:25 +02006379 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03006380 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07006381 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006382}
6383
Daniel Vettera43f6e02013-06-07 23:10:32 +02006384static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006385 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006386{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006387 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006388 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006389 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006390 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006391
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006392 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006393 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006394 int clock_limit =
6395 dev_priv->display.get_display_clock_speed(dev);
6396
6397 /*
6398 * Enable pixel doubling when the dot clock
6399 * is > 90% of the (display) core speed.
6400 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006401 * GDG double wide on either pipe,
6402 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006403 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006404 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006405 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006406 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006407 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006408 }
6409
Damien Lespiau241bfc32013-09-25 16:45:37 +01006410 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006411 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006412 }
Chris Wilson89749352010-09-12 18:25:19 +01006413
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006414 /*
6415 * Pipe horizontal size must be even in:
6416 * - DVO ganged mode
6417 * - LVDS dual channel mode
6418 * - Double wide pipe
6419 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006420 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006421 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6422 pipe_config->pipe_src_w &= ~1;
6423
Damien Lespiau8693a822013-05-03 18:48:11 +01006424 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6425 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006426 */
6427 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6428 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006429 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006430
Damien Lespiauf5adf942013-06-24 18:29:34 +01006431 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006432 hsw_compute_ips_config(crtc, pipe_config);
6433
Daniel Vetter877d48d2013-04-19 11:24:43 +02006434 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006435 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006436
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006437 /* FIXME: remove below call once atomic mode set is place and all crtc
6438 * related checks called from atomic_crtc_check function */
6439 ret = 0;
6440 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6441 crtc, pipe_config->base.state);
6442 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6443
6444 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006445}
6446
Ville Syrjälä1652d192015-03-31 14:12:01 +03006447static int skylake_get_display_clock_speed(struct drm_device *dev)
6448{
6449 struct drm_i915_private *dev_priv = to_i915(dev);
6450 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6451 uint32_t cdctl = I915_READ(CDCLK_CTL);
6452 uint32_t linkrate;
6453
6454 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6455 WARN(1, "LCPLL1 not enabled\n");
6456 return 24000; /* 24MHz is the cd freq with NSSC ref */
6457 }
6458
6459 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6460 return 540000;
6461
6462 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006463 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006464
Damien Lespiau71cd8422015-04-30 16:39:17 +01006465 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6466 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006467 /* vco 8640 */
6468 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6469 case CDCLK_FREQ_450_432:
6470 return 432000;
6471 case CDCLK_FREQ_337_308:
6472 return 308570;
6473 case CDCLK_FREQ_675_617:
6474 return 617140;
6475 default:
6476 WARN(1, "Unknown cd freq selection\n");
6477 }
6478 } else {
6479 /* vco 8100 */
6480 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6481 case CDCLK_FREQ_450_432:
6482 return 450000;
6483 case CDCLK_FREQ_337_308:
6484 return 337500;
6485 case CDCLK_FREQ_675_617:
6486 return 675000;
6487 default:
6488 WARN(1, "Unknown cd freq selection\n");
6489 }
6490 }
6491
6492 /* error case, do as if DPLL0 isn't enabled */
6493 return 24000;
6494}
6495
6496static int broadwell_get_display_clock_speed(struct drm_device *dev)
6497{
6498 struct drm_i915_private *dev_priv = dev->dev_private;
6499 uint32_t lcpll = I915_READ(LCPLL_CTL);
6500 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6501
6502 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6503 return 800000;
6504 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6505 return 450000;
6506 else if (freq == LCPLL_CLK_FREQ_450)
6507 return 450000;
6508 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6509 return 540000;
6510 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6511 return 337500;
6512 else
6513 return 675000;
6514}
6515
6516static int haswell_get_display_clock_speed(struct drm_device *dev)
6517{
6518 struct drm_i915_private *dev_priv = dev->dev_private;
6519 uint32_t lcpll = I915_READ(LCPLL_CTL);
6520 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6521
6522 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6523 return 800000;
6524 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6525 return 450000;
6526 else if (freq == LCPLL_CLK_FREQ_450)
6527 return 450000;
6528 else if (IS_HSW_ULT(dev))
6529 return 337500;
6530 else
6531 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006532}
6533
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006534static int valleyview_get_display_clock_speed(struct drm_device *dev)
6535{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006536 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006537 u32 val;
6538 int divider;
6539
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006540 if (dev_priv->hpll_freq == 0)
6541 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6542
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006543 mutex_lock(&dev_priv->dpio_lock);
6544 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6545 mutex_unlock(&dev_priv->dpio_lock);
6546
6547 divider = val & DISPLAY_FREQUENCY_VALUES;
6548
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006549 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6550 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6551 "cdclk change in progress\n");
6552
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006553 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006554}
6555
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006556static int ilk_get_display_clock_speed(struct drm_device *dev)
6557{
6558 return 450000;
6559}
6560
Jesse Barnese70236a2009-09-21 10:42:27 -07006561static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006562{
Jesse Barnese70236a2009-09-21 10:42:27 -07006563 return 400000;
6564}
Jesse Barnes79e53942008-11-07 14:24:08 -08006565
Jesse Barnese70236a2009-09-21 10:42:27 -07006566static int i915_get_display_clock_speed(struct drm_device *dev)
6567{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006568 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006569}
Jesse Barnes79e53942008-11-07 14:24:08 -08006570
Jesse Barnese70236a2009-09-21 10:42:27 -07006571static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6572{
6573 return 200000;
6574}
Jesse Barnes79e53942008-11-07 14:24:08 -08006575
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006576static int pnv_get_display_clock_speed(struct drm_device *dev)
6577{
6578 u16 gcfgc = 0;
6579
6580 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6581
6582 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6583 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006584 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006585 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006586 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006587 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006588 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006589 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6590 return 200000;
6591 default:
6592 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6593 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006594 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006595 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006596 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006597 }
6598}
6599
Jesse Barnese70236a2009-09-21 10:42:27 -07006600static int i915gm_get_display_clock_speed(struct drm_device *dev)
6601{
6602 u16 gcfgc = 0;
6603
6604 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6605
6606 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006607 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006608 else {
6609 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6610 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006611 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006612 default:
6613 case GC_DISPLAY_CLOCK_190_200_MHZ:
6614 return 190000;
6615 }
6616 }
6617}
Jesse Barnes79e53942008-11-07 14:24:08 -08006618
Jesse Barnese70236a2009-09-21 10:42:27 -07006619static int i865_get_display_clock_speed(struct drm_device *dev)
6620{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006621 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006622}
6623
6624static int i855_get_display_clock_speed(struct drm_device *dev)
6625{
6626 u16 hpllcc = 0;
6627 /* Assume that the hardware is in the high speed state. This
6628 * should be the default.
6629 */
6630 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6631 case GC_CLOCK_133_200:
6632 case GC_CLOCK_100_200:
6633 return 200000;
6634 case GC_CLOCK_166_250:
6635 return 250000;
6636 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006637 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006638 }
6639
6640 /* Shouldn't happen */
6641 return 0;
6642}
6643
6644static int i830_get_display_clock_speed(struct drm_device *dev)
6645{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006646 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006647}
6648
Zhenyu Wang2c072452009-06-05 15:38:42 +08006649static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006650intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006651{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006652 while (*num > DATA_LINK_M_N_MASK ||
6653 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006654 *num >>= 1;
6655 *den >>= 1;
6656 }
6657}
6658
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006659static void compute_m_n(unsigned int m, unsigned int n,
6660 uint32_t *ret_m, uint32_t *ret_n)
6661{
6662 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6663 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6664 intel_reduce_m_n_ratio(ret_m, ret_n);
6665}
6666
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006667void
6668intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6669 int pixel_clock, int link_clock,
6670 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006671{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006672 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006673
6674 compute_m_n(bits_per_pixel * pixel_clock,
6675 link_clock * nlanes * 8,
6676 &m_n->gmch_m, &m_n->gmch_n);
6677
6678 compute_m_n(pixel_clock, link_clock,
6679 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006680}
6681
Chris Wilsona7615032011-01-12 17:04:08 +00006682static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6683{
Jani Nikulad330a952014-01-21 11:24:25 +02006684 if (i915.panel_use_ssc >= 0)
6685 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006686 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006687 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006688}
6689
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006690static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6691 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006692{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006693 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006694 struct drm_i915_private *dev_priv = dev->dev_private;
6695 int refclk;
6696
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006697 WARN_ON(!crtc_state->base.state);
6698
Imre Deak5ab7b0b2015-03-06 03:29:25 +02006699 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006700 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006701 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006702 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006703 refclk = dev_priv->vbt.lvds_ssc_freq;
6704 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006705 } else if (!IS_GEN2(dev)) {
6706 refclk = 96000;
6707 } else {
6708 refclk = 48000;
6709 }
6710
6711 return refclk;
6712}
6713
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006714static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006715{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006716 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006717}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006718
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006719static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6720{
6721 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006722}
6723
Daniel Vetterf47709a2013-03-28 10:42:02 +01006724static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006725 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08006726 intel_clock_t *reduced_clock)
6727{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006728 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006729 u32 fp, fp2 = 0;
6730
6731 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006732 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006733 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006734 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006735 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006736 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006737 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006738 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006739 }
6740
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006741 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006742
Daniel Vetterf47709a2013-03-28 10:42:02 +01006743 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006744 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006745 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006746 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006747 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006748 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006749 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006750 }
6751}
6752
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006753static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6754 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006755{
6756 u32 reg_val;
6757
6758 /*
6759 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6760 * and set it to a reasonable value instead.
6761 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006762 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006763 reg_val &= 0xffffff00;
6764 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006765 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006766
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006767 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006768 reg_val &= 0x8cffffff;
6769 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006770 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006771
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006772 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006773 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006774 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006775
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006776 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006777 reg_val &= 0x00ffffff;
6778 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006779 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006780}
6781
Daniel Vetterb5518422013-05-03 11:49:48 +02006782static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6783 struct intel_link_m_n *m_n)
6784{
6785 struct drm_device *dev = crtc->base.dev;
6786 struct drm_i915_private *dev_priv = dev->dev_private;
6787 int pipe = crtc->pipe;
6788
Daniel Vettere3b95f12013-05-03 11:49:49 +02006789 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6790 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6791 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6792 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006793}
6794
6795static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006796 struct intel_link_m_n *m_n,
6797 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006798{
6799 struct drm_device *dev = crtc->base.dev;
6800 struct drm_i915_private *dev_priv = dev->dev_private;
6801 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006802 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006803
6804 if (INTEL_INFO(dev)->gen >= 5) {
6805 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6806 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6807 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6808 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006809 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6810 * for gen < 8) and if DRRS is supported (to make sure the
6811 * registers are not unnecessarily accessed).
6812 */
Durgadoss R44395bf2015-02-13 15:33:02 +05306813 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006814 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006815 I915_WRITE(PIPE_DATA_M2(transcoder),
6816 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6817 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6818 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6819 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6820 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006821 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006822 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6823 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6824 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6825 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006826 }
6827}
6828
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306829void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006830{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306831 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6832
6833 if (m_n == M1_N1) {
6834 dp_m_n = &crtc->config->dp_m_n;
6835 dp_m2_n2 = &crtc->config->dp_m2_n2;
6836 } else if (m_n == M2_N2) {
6837
6838 /*
6839 * M2_N2 registers are not supported. Hence m2_n2 divider value
6840 * needs to be programmed into M1_N1.
6841 */
6842 dp_m_n = &crtc->config->dp_m2_n2;
6843 } else {
6844 DRM_ERROR("Unsupported divider value\n");
6845 return;
6846 }
6847
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006848 if (crtc->config->has_pch_encoder)
6849 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006850 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306851 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006852}
6853
Ville Syrjäläd288f652014-10-28 13:20:22 +02006854static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006855 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006856{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006857 u32 dpll, dpll_md;
6858
6859 /*
6860 * Enable DPIO clock input. We should never disable the reference
6861 * clock for pipe B, since VGA hotplug / manual detection depends
6862 * on it.
6863 */
6864 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6865 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6866 /* We should never disable this, set it here for state tracking */
6867 if (crtc->pipe == PIPE_B)
6868 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6869 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006870 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006871
Ville Syrjäläd288f652014-10-28 13:20:22 +02006872 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006873 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006874 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006875}
6876
Ville Syrjäläd288f652014-10-28 13:20:22 +02006877static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006878 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006879{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006880 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006881 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006882 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006883 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006884 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006885 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006886
Daniel Vetter09153002012-12-12 14:06:44 +01006887 mutex_lock(&dev_priv->dpio_lock);
6888
Ville Syrjäläd288f652014-10-28 13:20:22 +02006889 bestn = pipe_config->dpll.n;
6890 bestm1 = pipe_config->dpll.m1;
6891 bestm2 = pipe_config->dpll.m2;
6892 bestp1 = pipe_config->dpll.p1;
6893 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006894
Jesse Barnes89b667f2013-04-18 14:51:36 -07006895 /* See eDP HDMI DPIO driver vbios notes doc */
6896
6897 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006898 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006899 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006900
6901 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006902 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006903
6904 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006905 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006906 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006907 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006908
6909 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006910 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006911
6912 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006913 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6914 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6915 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006916 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006917
6918 /*
6919 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6920 * but we don't support that).
6921 * Note: don't use the DAC post divider as it seems unstable.
6922 */
6923 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006924 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006925
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006926 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006927 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006928
Jesse Barnes89b667f2013-04-18 14:51:36 -07006929 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006930 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006931 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6932 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006933 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03006934 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006935 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006936 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006937 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006938
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02006939 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006940 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006941 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006942 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006943 0x0df40000);
6944 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006945 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006946 0x0df70000);
6947 } else { /* HDMI or VGA */
6948 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006949 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006950 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006951 0x0df70000);
6952 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006953 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006954 0x0df40000);
6955 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006956
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006957 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006958 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006959 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6960 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006961 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006962 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006963
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006964 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01006965 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006966}
6967
Ville Syrjäläd288f652014-10-28 13:20:22 +02006968static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006969 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006970{
Ville Syrjäläd288f652014-10-28 13:20:22 +02006971 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006972 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6973 DPLL_VCO_ENABLE;
6974 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006975 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006976
Ville Syrjäläd288f652014-10-28 13:20:22 +02006977 pipe_config->dpll_hw_state.dpll_md =
6978 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006979}
6980
Ville Syrjäläd288f652014-10-28 13:20:22 +02006981static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006982 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006983{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006984 struct drm_device *dev = crtc->base.dev;
6985 struct drm_i915_private *dev_priv = dev->dev_private;
6986 int pipe = crtc->pipe;
6987 int dpll_reg = DPLL(crtc->pipe);
6988 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306989 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006990 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306991 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306992 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006993
Ville Syrjäläd288f652014-10-28 13:20:22 +02006994 bestn = pipe_config->dpll.n;
6995 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6996 bestm1 = pipe_config->dpll.m1;
6997 bestm2 = pipe_config->dpll.m2 >> 22;
6998 bestp1 = pipe_config->dpll.p1;
6999 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307000 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307001 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307002 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007003
7004 /*
7005 * Enable Refclk and SSC
7006 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007007 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007008 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007009
7010 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007011
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007012 /* p1 and p2 divider */
7013 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7014 5 << DPIO_CHV_S1_DIV_SHIFT |
7015 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7016 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7017 1 << DPIO_CHV_K_DIV_SHIFT);
7018
7019 /* Feedback post-divider - m2 */
7020 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7021
7022 /* Feedback refclk divider - n and m1 */
7023 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7024 DPIO_CHV_M1_DIV_BY_2 |
7025 1 << DPIO_CHV_N_DIV_SHIFT);
7026
7027 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307028 if (bestm2_frac)
7029 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007030
7031 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307032 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7033 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7034 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7035 if (bestm2_frac)
7036 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7037 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007038
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307039 /* Program digital lock detect threshold */
7040 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7041 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7042 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7043 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7044 if (!bestm2_frac)
7045 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7046 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7047
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007048 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307049 if (vco == 5400000) {
7050 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7051 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7052 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7053 tribuf_calcntr = 0x9;
7054 } else if (vco <= 6200000) {
7055 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7056 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7057 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7058 tribuf_calcntr = 0x9;
7059 } else if (vco <= 6480000) {
7060 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7061 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7062 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7063 tribuf_calcntr = 0x8;
7064 } else {
7065 /* Not supported. Apply the same limits as in the max case */
7066 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7067 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7068 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7069 tribuf_calcntr = 0;
7070 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007071 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7072
Ville Syrjälä968040b2015-03-11 22:52:08 +02007073 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307074 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7075 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7076 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7077
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007078 /* AFC Recal */
7079 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7080 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7081 DPIO_AFC_RECAL);
7082
7083 mutex_unlock(&dev_priv->dpio_lock);
7084}
7085
Ville Syrjäläd288f652014-10-28 13:20:22 +02007086/**
7087 * vlv_force_pll_on - forcibly enable just the PLL
7088 * @dev_priv: i915 private structure
7089 * @pipe: pipe PLL to enable
7090 * @dpll: PLL configuration
7091 *
7092 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7093 * in cases where we need the PLL enabled even when @pipe is not going to
7094 * be enabled.
7095 */
7096void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7097 const struct dpll *dpll)
7098{
7099 struct intel_crtc *crtc =
7100 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007101 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007102 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007103 .pixel_multiplier = 1,
7104 .dpll = *dpll,
7105 };
7106
7107 if (IS_CHERRYVIEW(dev)) {
7108 chv_update_pll(crtc, &pipe_config);
7109 chv_prepare_pll(crtc, &pipe_config);
7110 chv_enable_pll(crtc, &pipe_config);
7111 } else {
7112 vlv_update_pll(crtc, &pipe_config);
7113 vlv_prepare_pll(crtc, &pipe_config);
7114 vlv_enable_pll(crtc, &pipe_config);
7115 }
7116}
7117
7118/**
7119 * vlv_force_pll_off - forcibly disable just the PLL
7120 * @dev_priv: i915 private structure
7121 * @pipe: pipe PLL to disable
7122 *
7123 * Disable the PLL for @pipe. To be used in cases where we need
7124 * the PLL enabled even when @pipe is not going to be enabled.
7125 */
7126void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7127{
7128 if (IS_CHERRYVIEW(dev))
7129 chv_disable_pll(to_i915(dev), pipe);
7130 else
7131 vlv_disable_pll(to_i915(dev), pipe);
7132}
7133
Daniel Vetterf47709a2013-03-28 10:42:02 +01007134static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007135 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007136 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007137 int num_connectors)
7138{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007139 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007140 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007141 u32 dpll;
7142 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007143 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007144
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007145 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307146
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007147 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7148 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007149
7150 dpll = DPLL_VGA_MODE_DIS;
7151
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007152 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007153 dpll |= DPLLB_MODE_LVDS;
7154 else
7155 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007156
Daniel Vetteref1b4602013-06-01 17:17:04 +02007157 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007158 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007159 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007160 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007161
7162 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007163 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007164
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007165 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007166 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007167
7168 /* compute bitmask from p1 value */
7169 if (IS_PINEVIEW(dev))
7170 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7171 else {
7172 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7173 if (IS_G4X(dev) && reduced_clock)
7174 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7175 }
7176 switch (clock->p2) {
7177 case 5:
7178 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7179 break;
7180 case 7:
7181 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7182 break;
7183 case 10:
7184 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7185 break;
7186 case 14:
7187 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7188 break;
7189 }
7190 if (INTEL_INFO(dev)->gen >= 4)
7191 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7192
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007193 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007194 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007195 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007196 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7197 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7198 else
7199 dpll |= PLL_REF_INPUT_DREFCLK;
7200
7201 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007202 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007203
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007204 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007205 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007206 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007207 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007208 }
7209}
7210
Daniel Vetterf47709a2013-03-28 10:42:02 +01007211static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007212 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007213 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007214 int num_connectors)
7215{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007216 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007217 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007218 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007219 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007220
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007221 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307222
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007223 dpll = DPLL_VGA_MODE_DIS;
7224
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007225 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007226 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7227 } else {
7228 if (clock->p1 == 2)
7229 dpll |= PLL_P1_DIVIDE_BY_TWO;
7230 else
7231 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7232 if (clock->p2 == 4)
7233 dpll |= PLL_P2_DIVIDE_BY_4;
7234 }
7235
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007236 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007237 dpll |= DPLL_DVO_2X_MODE;
7238
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007239 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007240 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7241 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7242 else
7243 dpll |= PLL_REF_INPUT_DREFCLK;
7244
7245 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007246 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007247}
7248
Daniel Vetter8a654f32013-06-01 17:16:22 +02007249static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007250{
7251 struct drm_device *dev = intel_crtc->base.dev;
7252 struct drm_i915_private *dev_priv = dev->dev_private;
7253 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007254 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007255 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007256 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007257 uint32_t crtc_vtotal, crtc_vblank_end;
7258 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007259
7260 /* We need to be careful not to changed the adjusted mode, for otherwise
7261 * the hw state checker will get angry at the mismatch. */
7262 crtc_vtotal = adjusted_mode->crtc_vtotal;
7263 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007264
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007265 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007266 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007267 crtc_vtotal -= 1;
7268 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007269
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007270 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007271 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7272 else
7273 vsyncshift = adjusted_mode->crtc_hsync_start -
7274 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007275 if (vsyncshift < 0)
7276 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007277 }
7278
7279 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007280 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007281
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007282 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007283 (adjusted_mode->crtc_hdisplay - 1) |
7284 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007285 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007286 (adjusted_mode->crtc_hblank_start - 1) |
7287 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007288 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007289 (adjusted_mode->crtc_hsync_start - 1) |
7290 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7291
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007292 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007293 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007294 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007295 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007296 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007297 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007298 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007299 (adjusted_mode->crtc_vsync_start - 1) |
7300 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7301
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007302 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7303 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7304 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7305 * bits. */
7306 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7307 (pipe == PIPE_B || pipe == PIPE_C))
7308 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7309
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007310 /* pipesrc controls the size that is scaled from, which should
7311 * always be the user's requested size.
7312 */
7313 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007314 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7315 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007316}
7317
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007318static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007319 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007320{
7321 struct drm_device *dev = crtc->base.dev;
7322 struct drm_i915_private *dev_priv = dev->dev_private;
7323 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7324 uint32_t tmp;
7325
7326 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007327 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7328 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007329 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007330 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7331 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007332 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007333 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7334 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007335
7336 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007337 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7338 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007339 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007340 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7341 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007342 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007343 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7344 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007345
7346 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007347 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7348 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7349 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007350 }
7351
7352 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007353 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7354 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7355
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007356 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7357 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007358}
7359
Daniel Vetterf6a83282014-02-11 15:28:57 -08007360void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007361 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007362{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007363 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7364 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7365 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7366 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007367
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007368 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7369 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7370 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7371 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007372
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007373 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007374
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007375 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7376 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007377}
7378
Daniel Vetter84b046f2013-02-19 18:48:54 +01007379static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7380{
7381 struct drm_device *dev = intel_crtc->base.dev;
7382 struct drm_i915_private *dev_priv = dev->dev_private;
7383 uint32_t pipeconf;
7384
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007385 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007386
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007387 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7388 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7389 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007390
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007391 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007392 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007393
Daniel Vetterff9ce462013-04-24 14:57:17 +02007394 /* only g4x and later have fancy bpc/dither controls */
7395 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007396 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007397 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007398 pipeconf |= PIPECONF_DITHER_EN |
7399 PIPECONF_DITHER_TYPE_SP;
7400
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007401 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007402 case 18:
7403 pipeconf |= PIPECONF_6BPC;
7404 break;
7405 case 24:
7406 pipeconf |= PIPECONF_8BPC;
7407 break;
7408 case 30:
7409 pipeconf |= PIPECONF_10BPC;
7410 break;
7411 default:
7412 /* Case prevented by intel_choose_pipe_bpp_dither. */
7413 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007414 }
7415 }
7416
7417 if (HAS_PIPE_CXSR(dev)) {
7418 if (intel_crtc->lowfreq_avail) {
7419 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7420 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7421 } else {
7422 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007423 }
7424 }
7425
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007426 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007427 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007428 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007429 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7430 else
7431 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7432 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007433 pipeconf |= PIPECONF_PROGRESSIVE;
7434
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007435 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007436 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007437
Daniel Vetter84b046f2013-02-19 18:48:54 +01007438 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7439 POSTING_READ(PIPECONF(intel_crtc->pipe));
7440}
7441
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007442static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7443 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007444{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007445 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007446 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007447 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007448 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007449 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007450 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007451 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007452 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007453 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007454 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007455 struct drm_connector_state *connector_state;
7456 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007457
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007458 memset(&crtc_state->dpll_hw_state, 0,
7459 sizeof(crtc_state->dpll_hw_state));
7460
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007461 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007462 if (connector_state->crtc != &crtc->base)
7463 continue;
7464
7465 encoder = to_intel_encoder(connector_state->best_encoder);
7466
Chris Wilson5eddb702010-09-11 13:48:45 +01007467 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007468 case INTEL_OUTPUT_LVDS:
7469 is_lvds = true;
7470 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007471 case INTEL_OUTPUT_DSI:
7472 is_dsi = true;
7473 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007474 default:
7475 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007476 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007477
Eric Anholtc751ce42010-03-25 11:48:48 -07007478 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007479 }
7480
Jani Nikulaf2335332013-09-13 11:03:09 +03007481 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007482 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007483
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007484 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007485 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007486
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007487 /*
7488 * Returns a set of divisors for the desired target clock with
7489 * the given refclk, or FALSE. The returned values represent
7490 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7491 * 2) / p1 / p2.
7492 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007493 limit = intel_limit(crtc_state, refclk);
7494 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007495 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007496 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007497 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007498 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7499 return -EINVAL;
7500 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007501
Jani Nikulaf2335332013-09-13 11:03:09 +03007502 if (is_lvds && dev_priv->lvds_downclock_avail) {
7503 /*
7504 * Ensure we match the reduced clock's P to the target
7505 * clock. If the clocks don't match, we can't switch
7506 * the display clock by using the FP0/FP1. In such case
7507 * we will disable the LVDS downclock feature.
7508 */
7509 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007510 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007511 dev_priv->lvds_downclock,
7512 refclk, &clock,
7513 &reduced_clock);
7514 }
7515 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007516 crtc_state->dpll.n = clock.n;
7517 crtc_state->dpll.m1 = clock.m1;
7518 crtc_state->dpll.m2 = clock.m2;
7519 crtc_state->dpll.p1 = clock.p1;
7520 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007521 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007522
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007523 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007524 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307525 has_reduced_clock ? &reduced_clock : NULL,
7526 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007527 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007528 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007529 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007530 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007531 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007532 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007533 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007534 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007535 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007536
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007537 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007538}
7539
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007540static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007541 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007542{
7543 struct drm_device *dev = crtc->base.dev;
7544 struct drm_i915_private *dev_priv = dev->dev_private;
7545 uint32_t tmp;
7546
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007547 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7548 return;
7549
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007550 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007551 if (!(tmp & PFIT_ENABLE))
7552 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007553
Daniel Vetter06922822013-07-11 13:35:40 +02007554 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007555 if (INTEL_INFO(dev)->gen < 4) {
7556 if (crtc->pipe != PIPE_B)
7557 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007558 } else {
7559 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7560 return;
7561 }
7562
Daniel Vetter06922822013-07-11 13:35:40 +02007563 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007564 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7565 if (INTEL_INFO(dev)->gen < 5)
7566 pipe_config->gmch_pfit.lvds_border_bits =
7567 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7568}
7569
Jesse Barnesacbec812013-09-20 11:29:32 -07007570static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007571 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007572{
7573 struct drm_device *dev = crtc->base.dev;
7574 struct drm_i915_private *dev_priv = dev->dev_private;
7575 int pipe = pipe_config->cpu_transcoder;
7576 intel_clock_t clock;
7577 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007578 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007579
Shobhit Kumarf573de52014-07-30 20:32:37 +05307580 /* In case of MIPI DPLL will not even be used */
7581 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7582 return;
7583
Jesse Barnesacbec812013-09-20 11:29:32 -07007584 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007585 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07007586 mutex_unlock(&dev_priv->dpio_lock);
7587
7588 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7589 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7590 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7591 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7592 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7593
Ville Syrjäläf6466282013-10-14 14:50:31 +03007594 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007595
Ville Syrjäläf6466282013-10-14 14:50:31 +03007596 /* clock.dot is the fast clock */
7597 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007598}
7599
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007600static void
7601i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7602 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007603{
7604 struct drm_device *dev = crtc->base.dev;
7605 struct drm_i915_private *dev_priv = dev->dev_private;
7606 u32 val, base, offset;
7607 int pipe = crtc->pipe, plane = crtc->plane;
7608 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007609 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007610 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007611 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007612
Damien Lespiau42a7b082015-02-05 19:35:13 +00007613 val = I915_READ(DSPCNTR(plane));
7614 if (!(val & DISPLAY_PLANE_ENABLE))
7615 return;
7616
Damien Lespiaud9806c92015-01-21 14:07:19 +00007617 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007618 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007619 DRM_DEBUG_KMS("failed to alloc fb\n");
7620 return;
7621 }
7622
Damien Lespiau1b842c82015-01-21 13:50:54 +00007623 fb = &intel_fb->base;
7624
Daniel Vetter18c52472015-02-10 17:16:09 +00007625 if (INTEL_INFO(dev)->gen >= 4) {
7626 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007627 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007628 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7629 }
7630 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007631
7632 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007633 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007634 fb->pixel_format = fourcc;
7635 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007636
7637 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007638 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007639 offset = I915_READ(DSPTILEOFF(plane));
7640 else
7641 offset = I915_READ(DSPLINOFF(plane));
7642 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7643 } else {
7644 base = I915_READ(DSPADDR(plane));
7645 }
7646 plane_config->base = base;
7647
7648 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007649 fb->width = ((val >> 16) & 0xfff) + 1;
7650 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007651
7652 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007653 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007654
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007655 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007656 fb->pixel_format,
7657 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007658
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007659 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007660
Damien Lespiau2844a922015-01-20 12:51:48 +00007661 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7662 pipe_name(pipe), plane, fb->width, fb->height,
7663 fb->bits_per_pixel, base, fb->pitches[0],
7664 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007665
Damien Lespiau2d140302015-02-05 17:22:18 +00007666 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007667}
7668
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007669static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007670 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007671{
7672 struct drm_device *dev = crtc->base.dev;
7673 struct drm_i915_private *dev_priv = dev->dev_private;
7674 int pipe = pipe_config->cpu_transcoder;
7675 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7676 intel_clock_t clock;
7677 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7678 int refclk = 100000;
7679
7680 mutex_lock(&dev_priv->dpio_lock);
7681 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7682 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7683 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7684 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7685 mutex_unlock(&dev_priv->dpio_lock);
7686
7687 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7688 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7689 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7690 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7691 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7692
7693 chv_clock(refclk, &clock);
7694
7695 /* clock.dot is the fast clock */
7696 pipe_config->port_clock = clock.dot / 5;
7697}
7698
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007699static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007700 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007701{
7702 struct drm_device *dev = crtc->base.dev;
7703 struct drm_i915_private *dev_priv = dev->dev_private;
7704 uint32_t tmp;
7705
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007706 if (!intel_display_power_is_enabled(dev_priv,
7707 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007708 return false;
7709
Daniel Vettere143a212013-07-04 12:01:15 +02007710 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007711 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007712
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007713 tmp = I915_READ(PIPECONF(crtc->pipe));
7714 if (!(tmp & PIPECONF_ENABLE))
7715 return false;
7716
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007717 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7718 switch (tmp & PIPECONF_BPC_MASK) {
7719 case PIPECONF_6BPC:
7720 pipe_config->pipe_bpp = 18;
7721 break;
7722 case PIPECONF_8BPC:
7723 pipe_config->pipe_bpp = 24;
7724 break;
7725 case PIPECONF_10BPC:
7726 pipe_config->pipe_bpp = 30;
7727 break;
7728 default:
7729 break;
7730 }
7731 }
7732
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007733 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7734 pipe_config->limited_color_range = true;
7735
Ville Syrjälä282740f2013-09-04 18:30:03 +03007736 if (INTEL_INFO(dev)->gen < 4)
7737 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7738
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007739 intel_get_pipe_timings(crtc, pipe_config);
7740
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007741 i9xx_get_pfit_config(crtc, pipe_config);
7742
Daniel Vetter6c49f242013-06-06 12:45:25 +02007743 if (INTEL_INFO(dev)->gen >= 4) {
7744 tmp = I915_READ(DPLL_MD(crtc->pipe));
7745 pipe_config->pixel_multiplier =
7746 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7747 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007748 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02007749 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7750 tmp = I915_READ(DPLL(crtc->pipe));
7751 pipe_config->pixel_multiplier =
7752 ((tmp & SDVO_MULTIPLIER_MASK)
7753 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7754 } else {
7755 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7756 * port and will be fixed up in the encoder->get_config
7757 * function. */
7758 pipe_config->pixel_multiplier = 1;
7759 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007760 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7761 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007762 /*
7763 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7764 * on 830. Filter it out here so that we don't
7765 * report errors due to that.
7766 */
7767 if (IS_I830(dev))
7768 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7769
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007770 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7771 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007772 } else {
7773 /* Mask out read-only status bits. */
7774 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7775 DPLL_PORTC_READY_MASK |
7776 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007777 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007778
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007779 if (IS_CHERRYVIEW(dev))
7780 chv_crtc_clock_get(crtc, pipe_config);
7781 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07007782 vlv_crtc_clock_get(crtc, pipe_config);
7783 else
7784 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007785
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007786 return true;
7787}
7788
Paulo Zanonidde86e22012-12-01 12:04:25 -02007789static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007790{
7791 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007792 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007793 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007794 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007795 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007796 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007797 bool has_ck505 = false;
7798 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007799
7800 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01007801 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007802 switch (encoder->type) {
7803 case INTEL_OUTPUT_LVDS:
7804 has_panel = true;
7805 has_lvds = true;
7806 break;
7807 case INTEL_OUTPUT_EDP:
7808 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03007809 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007810 has_cpu_edp = true;
7811 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007812 default:
7813 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007814 }
7815 }
7816
Keith Packard99eb6a02011-09-26 14:29:12 -07007817 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007818 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007819 can_ssc = has_ck505;
7820 } else {
7821 has_ck505 = false;
7822 can_ssc = true;
7823 }
7824
Imre Deak2de69052013-05-08 13:14:04 +03007825 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7826 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007827
7828 /* Ironlake: try to setup display ref clock before DPLL
7829 * enabling. This is only under driver's control after
7830 * PCH B stepping, previous chipset stepping should be
7831 * ignoring this setting.
7832 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007833 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007834
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007835 /* As we must carefully and slowly disable/enable each source in turn,
7836 * compute the final state we want first and check if we need to
7837 * make any changes at all.
7838 */
7839 final = val;
7840 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007841 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007842 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007843 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007844 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7845
7846 final &= ~DREF_SSC_SOURCE_MASK;
7847 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7848 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007849
Keith Packard199e5d72011-09-22 12:01:57 -07007850 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007851 final |= DREF_SSC_SOURCE_ENABLE;
7852
7853 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7854 final |= DREF_SSC1_ENABLE;
7855
7856 if (has_cpu_edp) {
7857 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7858 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7859 else
7860 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7861 } else
7862 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7863 } else {
7864 final |= DREF_SSC_SOURCE_DISABLE;
7865 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7866 }
7867
7868 if (final == val)
7869 return;
7870
7871 /* Always enable nonspread source */
7872 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7873
7874 if (has_ck505)
7875 val |= DREF_NONSPREAD_CK505_ENABLE;
7876 else
7877 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7878
7879 if (has_panel) {
7880 val &= ~DREF_SSC_SOURCE_MASK;
7881 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007882
Keith Packard199e5d72011-09-22 12:01:57 -07007883 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007884 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007885 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007886 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007887 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007888 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007889
7890 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007891 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007892 POSTING_READ(PCH_DREF_CONTROL);
7893 udelay(200);
7894
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007895 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007896
7897 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007898 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007899 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007900 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007901 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007902 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007903 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007904 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007905 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007906
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007907 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007908 POSTING_READ(PCH_DREF_CONTROL);
7909 udelay(200);
7910 } else {
7911 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7912
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007913 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007914
7915 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007916 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007917
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007918 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007919 POSTING_READ(PCH_DREF_CONTROL);
7920 udelay(200);
7921
7922 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007923 val &= ~DREF_SSC_SOURCE_MASK;
7924 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007925
7926 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007927 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007928
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007929 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007930 POSTING_READ(PCH_DREF_CONTROL);
7931 udelay(200);
7932 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007933
7934 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007935}
7936
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007937static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007938{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007939 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007940
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007941 tmp = I915_READ(SOUTH_CHICKEN2);
7942 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7943 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007944
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007945 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7946 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7947 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007948
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007949 tmp = I915_READ(SOUTH_CHICKEN2);
7950 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7951 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007952
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007953 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7954 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7955 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007956}
7957
7958/* WaMPhyProgramming:hsw */
7959static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7960{
7961 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007962
7963 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7964 tmp &= ~(0xFF << 24);
7965 tmp |= (0x12 << 24);
7966 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7967
Paulo Zanonidde86e22012-12-01 12:04:25 -02007968 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7969 tmp |= (1 << 11);
7970 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7971
7972 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7973 tmp |= (1 << 11);
7974 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7975
Paulo Zanonidde86e22012-12-01 12:04:25 -02007976 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7977 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7978 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7979
7980 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7981 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7982 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7983
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007984 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7985 tmp &= ~(7 << 13);
7986 tmp |= (5 << 13);
7987 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007988
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007989 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7990 tmp &= ~(7 << 13);
7991 tmp |= (5 << 13);
7992 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007993
7994 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7995 tmp &= ~0xFF;
7996 tmp |= 0x1C;
7997 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7998
7999 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8000 tmp &= ~0xFF;
8001 tmp |= 0x1C;
8002 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8003
8004 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8005 tmp &= ~(0xFF << 16);
8006 tmp |= (0x1C << 16);
8007 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8008
8009 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8010 tmp &= ~(0xFF << 16);
8011 tmp |= (0x1C << 16);
8012 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8013
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008014 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8015 tmp |= (1 << 27);
8016 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008017
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008018 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8019 tmp |= (1 << 27);
8020 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008021
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008022 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8023 tmp &= ~(0xF << 28);
8024 tmp |= (4 << 28);
8025 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008026
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008027 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8028 tmp &= ~(0xF << 28);
8029 tmp |= (4 << 28);
8030 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008031}
8032
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008033/* Implements 3 different sequences from BSpec chapter "Display iCLK
8034 * Programming" based on the parameters passed:
8035 * - Sequence to enable CLKOUT_DP
8036 * - Sequence to enable CLKOUT_DP without spread
8037 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8038 */
8039static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8040 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008041{
8042 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008043 uint32_t reg, tmp;
8044
8045 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8046 with_spread = true;
8047 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8048 with_fdi, "LP PCH doesn't have FDI\n"))
8049 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008050
8051 mutex_lock(&dev_priv->dpio_lock);
8052
8053 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8054 tmp &= ~SBI_SSCCTL_DISABLE;
8055 tmp |= SBI_SSCCTL_PATHALT;
8056 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8057
8058 udelay(24);
8059
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008060 if (with_spread) {
8061 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8062 tmp &= ~SBI_SSCCTL_PATHALT;
8063 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008064
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008065 if (with_fdi) {
8066 lpt_reset_fdi_mphy(dev_priv);
8067 lpt_program_fdi_mphy(dev_priv);
8068 }
8069 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008070
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008071 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8072 SBI_GEN0 : SBI_DBUFF0;
8073 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8074 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8075 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008076
8077 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008078}
8079
Paulo Zanoni47701c32013-07-23 11:19:25 -03008080/* Sequence to disable CLKOUT_DP */
8081static void lpt_disable_clkout_dp(struct drm_device *dev)
8082{
8083 struct drm_i915_private *dev_priv = dev->dev_private;
8084 uint32_t reg, tmp;
8085
8086 mutex_lock(&dev_priv->dpio_lock);
8087
8088 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8089 SBI_GEN0 : SBI_DBUFF0;
8090 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8091 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8092 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8093
8094 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8095 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8096 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8097 tmp |= SBI_SSCCTL_PATHALT;
8098 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8099 udelay(32);
8100 }
8101 tmp |= SBI_SSCCTL_DISABLE;
8102 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8103 }
8104
8105 mutex_unlock(&dev_priv->dpio_lock);
8106}
8107
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008108static void lpt_init_pch_refclk(struct drm_device *dev)
8109{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008110 struct intel_encoder *encoder;
8111 bool has_vga = false;
8112
Damien Lespiaub2784e12014-08-05 11:29:37 +01008113 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008114 switch (encoder->type) {
8115 case INTEL_OUTPUT_ANALOG:
8116 has_vga = true;
8117 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008118 default:
8119 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008120 }
8121 }
8122
Paulo Zanoni47701c32013-07-23 11:19:25 -03008123 if (has_vga)
8124 lpt_enable_clkout_dp(dev, true, true);
8125 else
8126 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008127}
8128
Paulo Zanonidde86e22012-12-01 12:04:25 -02008129/*
8130 * Initialize reference clocks when the driver loads
8131 */
8132void intel_init_pch_refclk(struct drm_device *dev)
8133{
8134 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8135 ironlake_init_pch_refclk(dev);
8136 else if (HAS_PCH_LPT(dev))
8137 lpt_init_pch_refclk(dev);
8138}
8139
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008140static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008141{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008142 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008143 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008144 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008145 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008146 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008147 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008148 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008149 bool is_lvds = false;
8150
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008151 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008152 if (connector_state->crtc != crtc_state->base.crtc)
8153 continue;
8154
8155 encoder = to_intel_encoder(connector_state->best_encoder);
8156
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008157 switch (encoder->type) {
8158 case INTEL_OUTPUT_LVDS:
8159 is_lvds = true;
8160 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008161 default:
8162 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008163 }
8164 num_connectors++;
8165 }
8166
8167 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008168 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008169 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008170 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008171 }
8172
8173 return 120000;
8174}
8175
Daniel Vetter6ff93602013-04-19 11:24:36 +02008176static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008177{
8178 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8180 int pipe = intel_crtc->pipe;
8181 uint32_t val;
8182
Daniel Vetter78114072013-06-13 00:54:57 +02008183 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008184
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008185 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008186 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008187 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008188 break;
8189 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008190 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008191 break;
8192 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008193 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008194 break;
8195 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008196 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008197 break;
8198 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008199 /* Case prevented by intel_choose_pipe_bpp_dither. */
8200 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008201 }
8202
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008203 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008204 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8205
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008206 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008207 val |= PIPECONF_INTERLACED_ILK;
8208 else
8209 val |= PIPECONF_PROGRESSIVE;
8210
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008211 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008212 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008213
Paulo Zanonic8203562012-09-12 10:06:29 -03008214 I915_WRITE(PIPECONF(pipe), val);
8215 POSTING_READ(PIPECONF(pipe));
8216}
8217
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008218/*
8219 * Set up the pipe CSC unit.
8220 *
8221 * Currently only full range RGB to limited range RGB conversion
8222 * is supported, but eventually this should handle various
8223 * RGB<->YCbCr scenarios as well.
8224 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008225static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008226{
8227 struct drm_device *dev = crtc->dev;
8228 struct drm_i915_private *dev_priv = dev->dev_private;
8229 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8230 int pipe = intel_crtc->pipe;
8231 uint16_t coeff = 0x7800; /* 1.0 */
8232
8233 /*
8234 * TODO: Check what kind of values actually come out of the pipe
8235 * with these coeff/postoff values and adjust to get the best
8236 * accuracy. Perhaps we even need to take the bpc value into
8237 * consideration.
8238 */
8239
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008240 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008241 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8242
8243 /*
8244 * GY/GU and RY/RU should be the other way around according
8245 * to BSpec, but reality doesn't agree. Just set them up in
8246 * a way that results in the correct picture.
8247 */
8248 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8249 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8250
8251 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8252 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8253
8254 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8255 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8256
8257 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8258 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8259 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8260
8261 if (INTEL_INFO(dev)->gen > 6) {
8262 uint16_t postoff = 0;
8263
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008264 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008265 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008266
8267 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8268 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8269 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8270
8271 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8272 } else {
8273 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8274
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008275 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008276 mode |= CSC_BLACK_SCREEN_OFFSET;
8277
8278 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8279 }
8280}
8281
Daniel Vetter6ff93602013-04-19 11:24:36 +02008282static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008283{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008284 struct drm_device *dev = crtc->dev;
8285 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008286 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008287 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008288 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008289 uint32_t val;
8290
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008291 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008292
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008293 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008294 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8295
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008296 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008297 val |= PIPECONF_INTERLACED_ILK;
8298 else
8299 val |= PIPECONF_PROGRESSIVE;
8300
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008301 I915_WRITE(PIPECONF(cpu_transcoder), val);
8302 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008303
8304 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8305 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008306
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05308307 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008308 val = 0;
8309
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008310 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008311 case 18:
8312 val |= PIPEMISC_DITHER_6_BPC;
8313 break;
8314 case 24:
8315 val |= PIPEMISC_DITHER_8_BPC;
8316 break;
8317 case 30:
8318 val |= PIPEMISC_DITHER_10_BPC;
8319 break;
8320 case 36:
8321 val |= PIPEMISC_DITHER_12_BPC;
8322 break;
8323 default:
8324 /* Case prevented by pipe_config_set_bpp. */
8325 BUG();
8326 }
8327
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008328 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008329 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8330
8331 I915_WRITE(PIPEMISC(pipe), val);
8332 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008333}
8334
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008335static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008336 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008337 intel_clock_t *clock,
8338 bool *has_reduced_clock,
8339 intel_clock_t *reduced_clock)
8340{
8341 struct drm_device *dev = crtc->dev;
8342 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008343 int refclk;
8344 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008345 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008346
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008347 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008348
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008349 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008350
8351 /*
8352 * Returns a set of divisors for the desired target clock with the given
8353 * refclk, or FALSE. The returned values represent the clock equation:
8354 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8355 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008356 limit = intel_limit(crtc_state, refclk);
8357 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008358 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008359 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008360 if (!ret)
8361 return false;
8362
8363 if (is_lvds && dev_priv->lvds_downclock_avail) {
8364 /*
8365 * Ensure we match the reduced clock's P to the target clock.
8366 * If the clocks don't match, we can't switch the display clock
8367 * by using the FP0/FP1. In such case we will disable the LVDS
8368 * downclock feature.
8369 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008370 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008371 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008372 dev_priv->lvds_downclock,
8373 refclk, clock,
8374 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008375 }
8376
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008377 return true;
8378}
8379
Paulo Zanonid4b19312012-11-29 11:29:32 -02008380int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8381{
8382 /*
8383 * Account for spread spectrum to avoid
8384 * oversubscribing the link. Max center spread
8385 * is 2.5%; use 5% for safety's sake.
8386 */
8387 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008388 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008389}
8390
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008391static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008392{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008393 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008394}
8395
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008396static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008397 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008398 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008399 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008400{
8401 struct drm_crtc *crtc = &intel_crtc->base;
8402 struct drm_device *dev = crtc->dev;
8403 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008404 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008405 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008406 struct drm_connector_state *connector_state;
8407 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008408 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008409 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008410 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008411
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008412 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008413 if (connector_state->crtc != crtc_state->base.crtc)
8414 continue;
8415
8416 encoder = to_intel_encoder(connector_state->best_encoder);
8417
8418 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008419 case INTEL_OUTPUT_LVDS:
8420 is_lvds = true;
8421 break;
8422 case INTEL_OUTPUT_SDVO:
8423 case INTEL_OUTPUT_HDMI:
8424 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008425 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008426 default:
8427 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008428 }
8429
8430 num_connectors++;
8431 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008432
Chris Wilsonc1858122010-12-03 21:35:48 +00008433 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008434 factor = 21;
8435 if (is_lvds) {
8436 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008437 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008438 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008439 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008440 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008441 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008442
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008443 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008444 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008445
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008446 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8447 *fp2 |= FP_CB_TUNE;
8448
Chris Wilson5eddb702010-09-11 13:48:45 +01008449 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008450
Eric Anholta07d6782011-03-30 13:01:08 -07008451 if (is_lvds)
8452 dpll |= DPLLB_MODE_LVDS;
8453 else
8454 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008455
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008456 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008457 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008458
8459 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008460 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008461 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008462 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008463
Eric Anholta07d6782011-03-30 13:01:08 -07008464 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008465 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008466 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008467 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008468
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008469 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008470 case 5:
8471 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8472 break;
8473 case 7:
8474 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8475 break;
8476 case 10:
8477 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8478 break;
8479 case 14:
8480 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8481 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008482 }
8483
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008484 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008485 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008486 else
8487 dpll |= PLL_REF_INPUT_DREFCLK;
8488
Daniel Vetter959e16d2013-06-05 13:34:21 +02008489 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008490}
8491
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008492static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8493 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008494{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008495 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008496 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008497 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008498 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008499 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008500 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008501
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008502 memset(&crtc_state->dpll_hw_state, 0,
8503 sizeof(crtc_state->dpll_hw_state));
8504
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008505 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008506
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008507 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8508 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8509
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008510 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008511 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008512 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008513 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8514 return -EINVAL;
8515 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008516 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008517 if (!crtc_state->clock_set) {
8518 crtc_state->dpll.n = clock.n;
8519 crtc_state->dpll.m1 = clock.m1;
8520 crtc_state->dpll.m2 = clock.m2;
8521 crtc_state->dpll.p1 = clock.p1;
8522 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008523 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008524
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008525 /* 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 +02008526 if (crtc_state->has_pch_encoder) {
8527 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008528 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008529 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008530
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008531 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008532 &fp, &reduced_clock,
8533 has_reduced_clock ? &fp2 : NULL);
8534
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008535 crtc_state->dpll_hw_state.dpll = dpll;
8536 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008537 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008538 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008539 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008540 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008541
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008542 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008543 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008544 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008545 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008546 return -EINVAL;
8547 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008548 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008549
Rodrigo Viviab585de2015-03-24 12:40:09 -07008550 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008551 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008552 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008553 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008554
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008555 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008556}
8557
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008558static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8559 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008560{
8561 struct drm_device *dev = crtc->base.dev;
8562 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008563 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008564
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008565 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8566 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8567 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8568 & ~TU_SIZE_MASK;
8569 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8570 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8571 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8572}
8573
8574static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8575 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008576 struct intel_link_m_n *m_n,
8577 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008578{
8579 struct drm_device *dev = crtc->base.dev;
8580 struct drm_i915_private *dev_priv = dev->dev_private;
8581 enum pipe pipe = crtc->pipe;
8582
8583 if (INTEL_INFO(dev)->gen >= 5) {
8584 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8585 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8586 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8587 & ~TU_SIZE_MASK;
8588 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8589 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8590 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008591 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8592 * gen < 8) and if DRRS is supported (to make sure the
8593 * registers are not unnecessarily read).
8594 */
8595 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008596 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008597 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8598 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8599 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8600 & ~TU_SIZE_MASK;
8601 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8602 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8603 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8604 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008605 } else {
8606 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8607 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8608 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8609 & ~TU_SIZE_MASK;
8610 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8611 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8612 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8613 }
8614}
8615
8616void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008617 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008618{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008619 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008620 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8621 else
8622 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008623 &pipe_config->dp_m_n,
8624 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008625}
8626
Daniel Vetter72419202013-04-04 13:28:53 +02008627static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008628 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008629{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008630 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008631 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008632}
8633
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008634static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008635 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008636{
8637 struct drm_device *dev = crtc->base.dev;
8638 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008639 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8640 uint32_t ps_ctrl = 0;
8641 int id = -1;
8642 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008643
Chandra Kondurua1b22782015-04-07 15:28:45 -07008644 /* find scaler attached to this pipe */
8645 for (i = 0; i < crtc->num_scalers; i++) {
8646 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8647 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8648 id = i;
8649 pipe_config->pch_pfit.enabled = true;
8650 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8651 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8652 break;
8653 }
8654 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008655
Chandra Kondurua1b22782015-04-07 15:28:45 -07008656 scaler_state->scaler_id = id;
8657 if (id >= 0) {
8658 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8659 } else {
8660 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008661 }
8662}
8663
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008664static void
8665skylake_get_initial_plane_config(struct intel_crtc *crtc,
8666 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008667{
8668 struct drm_device *dev = crtc->base.dev;
8669 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008670 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008671 int pipe = crtc->pipe;
8672 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008673 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008674 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008675 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008676
Damien Lespiaud9806c92015-01-21 14:07:19 +00008677 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008678 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008679 DRM_DEBUG_KMS("failed to alloc fb\n");
8680 return;
8681 }
8682
Damien Lespiau1b842c82015-01-21 13:50:54 +00008683 fb = &intel_fb->base;
8684
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008685 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008686 if (!(val & PLANE_CTL_ENABLE))
8687 goto error;
8688
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008689 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8690 fourcc = skl_format_to_fourcc(pixel_format,
8691 val & PLANE_CTL_ORDER_RGBX,
8692 val & PLANE_CTL_ALPHA_MASK);
8693 fb->pixel_format = fourcc;
8694 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8695
Damien Lespiau40f46282015-02-27 11:15:21 +00008696 tiling = val & PLANE_CTL_TILED_MASK;
8697 switch (tiling) {
8698 case PLANE_CTL_TILED_LINEAR:
8699 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8700 break;
8701 case PLANE_CTL_TILED_X:
8702 plane_config->tiling = I915_TILING_X;
8703 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8704 break;
8705 case PLANE_CTL_TILED_Y:
8706 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8707 break;
8708 case PLANE_CTL_TILED_YF:
8709 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8710 break;
8711 default:
8712 MISSING_CASE(tiling);
8713 goto error;
8714 }
8715
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008716 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8717 plane_config->base = base;
8718
8719 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8720
8721 val = I915_READ(PLANE_SIZE(pipe, 0));
8722 fb->height = ((val >> 16) & 0xfff) + 1;
8723 fb->width = ((val >> 0) & 0x1fff) + 1;
8724
8725 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00008726 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8727 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008728 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8729
8730 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008731 fb->pixel_format,
8732 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008733
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008734 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008735
8736 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8737 pipe_name(pipe), fb->width, fb->height,
8738 fb->bits_per_pixel, base, fb->pitches[0],
8739 plane_config->size);
8740
Damien Lespiau2d140302015-02-05 17:22:18 +00008741 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008742 return;
8743
8744error:
8745 kfree(fb);
8746}
8747
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008748static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008749 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008750{
8751 struct drm_device *dev = crtc->base.dev;
8752 struct drm_i915_private *dev_priv = dev->dev_private;
8753 uint32_t tmp;
8754
8755 tmp = I915_READ(PF_CTL(crtc->pipe));
8756
8757 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008758 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008759 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8760 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008761
8762 /* We currently do not free assignements of panel fitters on
8763 * ivb/hsw (since we don't use the higher upscaling modes which
8764 * differentiates them) so just WARN about this case for now. */
8765 if (IS_GEN7(dev)) {
8766 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8767 PF_PIPE_SEL_IVB(crtc->pipe));
8768 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008769 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008770}
8771
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008772static void
8773ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8774 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008775{
8776 struct drm_device *dev = crtc->base.dev;
8777 struct drm_i915_private *dev_priv = dev->dev_private;
8778 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008779 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008780 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008781 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008782 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008783 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008784
Damien Lespiau42a7b082015-02-05 19:35:13 +00008785 val = I915_READ(DSPCNTR(pipe));
8786 if (!(val & DISPLAY_PLANE_ENABLE))
8787 return;
8788
Damien Lespiaud9806c92015-01-21 14:07:19 +00008789 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008790 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008791 DRM_DEBUG_KMS("failed to alloc fb\n");
8792 return;
8793 }
8794
Damien Lespiau1b842c82015-01-21 13:50:54 +00008795 fb = &intel_fb->base;
8796
Daniel Vetter18c52472015-02-10 17:16:09 +00008797 if (INTEL_INFO(dev)->gen >= 4) {
8798 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008799 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008800 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8801 }
8802 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008803
8804 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008805 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008806 fb->pixel_format = fourcc;
8807 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008808
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008809 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008810 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008811 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008812 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00008813 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008814 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008815 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008816 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008817 }
8818 plane_config->base = base;
8819
8820 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008821 fb->width = ((val >> 16) & 0xfff) + 1;
8822 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008823
8824 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008825 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008826
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008827 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008828 fb->pixel_format,
8829 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008830
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008831 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008832
Damien Lespiau2844a922015-01-20 12:51:48 +00008833 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8834 pipe_name(pipe), fb->width, fb->height,
8835 fb->bits_per_pixel, base, fb->pitches[0],
8836 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008837
Damien Lespiau2d140302015-02-05 17:22:18 +00008838 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008839}
8840
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008841static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008842 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008843{
8844 struct drm_device *dev = crtc->base.dev;
8845 struct drm_i915_private *dev_priv = dev->dev_private;
8846 uint32_t tmp;
8847
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008848 if (!intel_display_power_is_enabled(dev_priv,
8849 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008850 return false;
8851
Daniel Vettere143a212013-07-04 12:01:15 +02008852 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008853 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008854
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008855 tmp = I915_READ(PIPECONF(crtc->pipe));
8856 if (!(tmp & PIPECONF_ENABLE))
8857 return false;
8858
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008859 switch (tmp & PIPECONF_BPC_MASK) {
8860 case PIPECONF_6BPC:
8861 pipe_config->pipe_bpp = 18;
8862 break;
8863 case PIPECONF_8BPC:
8864 pipe_config->pipe_bpp = 24;
8865 break;
8866 case PIPECONF_10BPC:
8867 pipe_config->pipe_bpp = 30;
8868 break;
8869 case PIPECONF_12BPC:
8870 pipe_config->pipe_bpp = 36;
8871 break;
8872 default:
8873 break;
8874 }
8875
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008876 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8877 pipe_config->limited_color_range = true;
8878
Daniel Vetterab9412b2013-05-03 11:49:46 +02008879 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008880 struct intel_shared_dpll *pll;
8881
Daniel Vetter88adfff2013-03-28 10:42:01 +01008882 pipe_config->has_pch_encoder = true;
8883
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008884 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8885 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8886 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008887
8888 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008889
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008890 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02008891 pipe_config->shared_dpll =
8892 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008893 } else {
8894 tmp = I915_READ(PCH_DPLL_SEL);
8895 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8896 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8897 else
8898 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8899 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008900
8901 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8902
8903 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8904 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008905
8906 tmp = pipe_config->dpll_hw_state.dpll;
8907 pipe_config->pixel_multiplier =
8908 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8909 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008910
8911 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008912 } else {
8913 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008914 }
8915
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008916 intel_get_pipe_timings(crtc, pipe_config);
8917
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008918 ironlake_get_pfit_config(crtc, pipe_config);
8919
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008920 return true;
8921}
8922
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008923static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8924{
8925 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008926 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008927
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008928 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008929 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008930 pipe_name(crtc->pipe));
8931
Rob Clarke2c719b2014-12-15 13:56:32 -05008932 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8933 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8934 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8935 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8936 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8937 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008938 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03008939 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05008940 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008941 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008942 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008943 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008944 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008945 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008946 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008947
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008948 /*
8949 * In theory we can still leave IRQs enabled, as long as only the HPD
8950 * interrupts remain enabled. We used to check for that, but since it's
8951 * gen-specific and since we only disable LCPLL after we fully disable
8952 * the interrupts, the check below should be enough.
8953 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008954 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008955}
8956
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008957static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8958{
8959 struct drm_device *dev = dev_priv->dev;
8960
8961 if (IS_HASWELL(dev))
8962 return I915_READ(D_COMP_HSW);
8963 else
8964 return I915_READ(D_COMP_BDW);
8965}
8966
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008967static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8968{
8969 struct drm_device *dev = dev_priv->dev;
8970
8971 if (IS_HASWELL(dev)) {
8972 mutex_lock(&dev_priv->rps.hw_lock);
8973 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8974 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03008975 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008976 mutex_unlock(&dev_priv->rps.hw_lock);
8977 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008978 I915_WRITE(D_COMP_BDW, val);
8979 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008980 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008981}
8982
8983/*
8984 * This function implements pieces of two sequences from BSpec:
8985 * - Sequence for display software to disable LCPLL
8986 * - Sequence for display software to allow package C8+
8987 * The steps implemented here are just the steps that actually touch the LCPLL
8988 * register. Callers should take care of disabling all the display engine
8989 * functions, doing the mode unset, fixing interrupts, etc.
8990 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008991static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8992 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008993{
8994 uint32_t val;
8995
8996 assert_can_disable_lcpll(dev_priv);
8997
8998 val = I915_READ(LCPLL_CTL);
8999
9000 if (switch_to_fclk) {
9001 val |= LCPLL_CD_SOURCE_FCLK;
9002 I915_WRITE(LCPLL_CTL, val);
9003
9004 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9005 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9006 DRM_ERROR("Switching to FCLK failed\n");
9007
9008 val = I915_READ(LCPLL_CTL);
9009 }
9010
9011 val |= LCPLL_PLL_DISABLE;
9012 I915_WRITE(LCPLL_CTL, val);
9013 POSTING_READ(LCPLL_CTL);
9014
9015 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9016 DRM_ERROR("LCPLL still locked\n");
9017
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009018 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009019 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009020 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009021 ndelay(100);
9022
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009023 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9024 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009025 DRM_ERROR("D_COMP RCOMP still in progress\n");
9026
9027 if (allow_power_down) {
9028 val = I915_READ(LCPLL_CTL);
9029 val |= LCPLL_POWER_DOWN_ALLOW;
9030 I915_WRITE(LCPLL_CTL, val);
9031 POSTING_READ(LCPLL_CTL);
9032 }
9033}
9034
9035/*
9036 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9037 * source.
9038 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009039static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009040{
9041 uint32_t val;
9042
9043 val = I915_READ(LCPLL_CTL);
9044
9045 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9046 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9047 return;
9048
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009049 /*
9050 * Make sure we're not on PC8 state before disabling PC8, otherwise
9051 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009052 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009053 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009054
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009055 if (val & LCPLL_POWER_DOWN_ALLOW) {
9056 val &= ~LCPLL_POWER_DOWN_ALLOW;
9057 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009058 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009059 }
9060
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009061 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009062 val |= D_COMP_COMP_FORCE;
9063 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009064 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009065
9066 val = I915_READ(LCPLL_CTL);
9067 val &= ~LCPLL_PLL_DISABLE;
9068 I915_WRITE(LCPLL_CTL, val);
9069
9070 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9071 DRM_ERROR("LCPLL not locked yet\n");
9072
9073 if (val & LCPLL_CD_SOURCE_FCLK) {
9074 val = I915_READ(LCPLL_CTL);
9075 val &= ~LCPLL_CD_SOURCE_FCLK;
9076 I915_WRITE(LCPLL_CTL, val);
9077
9078 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9079 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9080 DRM_ERROR("Switching back to LCPLL failed\n");
9081 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009082
Mika Kuoppala59bad942015-01-16 11:34:40 +02009083 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009084}
9085
Paulo Zanoni765dab672014-03-07 20:08:18 -03009086/*
9087 * Package states C8 and deeper are really deep PC states that can only be
9088 * reached when all the devices on the system allow it, so even if the graphics
9089 * device allows PC8+, it doesn't mean the system will actually get to these
9090 * states. Our driver only allows PC8+ when going into runtime PM.
9091 *
9092 * The requirements for PC8+ are that all the outputs are disabled, the power
9093 * well is disabled and most interrupts are disabled, and these are also
9094 * requirements for runtime PM. When these conditions are met, we manually do
9095 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9096 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9097 * hang the machine.
9098 *
9099 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9100 * the state of some registers, so when we come back from PC8+ we need to
9101 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9102 * need to take care of the registers kept by RC6. Notice that this happens even
9103 * if we don't put the device in PCI D3 state (which is what currently happens
9104 * because of the runtime PM support).
9105 *
9106 * For more, read "Display Sequences for Package C8" on the hardware
9107 * documentation.
9108 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009109void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009110{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009111 struct drm_device *dev = dev_priv->dev;
9112 uint32_t val;
9113
Paulo Zanonic67a4702013-08-19 13:18:09 -03009114 DRM_DEBUG_KMS("Enabling package C8+\n");
9115
Paulo Zanonic67a4702013-08-19 13:18:09 -03009116 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9117 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9118 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9119 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9120 }
9121
9122 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009123 hsw_disable_lcpll(dev_priv, true, true);
9124}
9125
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009126void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009127{
9128 struct drm_device *dev = dev_priv->dev;
9129 uint32_t val;
9130
Paulo Zanonic67a4702013-08-19 13:18:09 -03009131 DRM_DEBUG_KMS("Disabling package C8+\n");
9132
9133 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009134 lpt_init_pch_refclk(dev);
9135
9136 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9137 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9138 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9139 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9140 }
9141
9142 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009143}
9144
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009145static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309146{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009147 struct drm_device *dev = old_state->dev;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309148 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009149 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309150 int req_cdclk;
9151
9152 /* see the comment in valleyview_modeset_global_resources */
9153 if (WARN_ON(max_pixclk < 0))
9154 return;
9155
9156 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9157
9158 if (req_cdclk != dev_priv->cdclk_freq)
9159 broxton_set_cdclk(dev, req_cdclk);
9160}
9161
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009162static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9163 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009164{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009165 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009166 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009167
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009168 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009169
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009170 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009171}
9172
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309173static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9174 enum port port,
9175 struct intel_crtc_state *pipe_config)
9176{
9177 switch (port) {
9178 case PORT_A:
9179 pipe_config->ddi_pll_sel = SKL_DPLL0;
9180 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9181 break;
9182 case PORT_B:
9183 pipe_config->ddi_pll_sel = SKL_DPLL1;
9184 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9185 break;
9186 case PORT_C:
9187 pipe_config->ddi_pll_sel = SKL_DPLL2;
9188 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9189 break;
9190 default:
9191 DRM_ERROR("Incorrect port type\n");
9192 }
9193}
9194
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009195static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9196 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009197 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009198{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009199 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009200
9201 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9202 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9203
9204 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009205 case SKL_DPLL0:
9206 /*
9207 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9208 * of the shared DPLL framework and thus needs to be read out
9209 * separately
9210 */
9211 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9212 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9213 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009214 case SKL_DPLL1:
9215 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9216 break;
9217 case SKL_DPLL2:
9218 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9219 break;
9220 case SKL_DPLL3:
9221 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9222 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009223 }
9224}
9225
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009226static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9227 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009228 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009229{
9230 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9231
9232 switch (pipe_config->ddi_pll_sel) {
9233 case PORT_CLK_SEL_WRPLL1:
9234 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9235 break;
9236 case PORT_CLK_SEL_WRPLL2:
9237 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9238 break;
9239 }
9240}
9241
Daniel Vetter26804af2014-06-25 22:01:55 +03009242static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009243 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009244{
9245 struct drm_device *dev = crtc->base.dev;
9246 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009247 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009248 enum port port;
9249 uint32_t tmp;
9250
9251 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9252
9253 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9254
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009255 if (IS_SKYLAKE(dev))
9256 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309257 else if (IS_BROXTON(dev))
9258 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009259 else
9260 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009261
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009262 if (pipe_config->shared_dpll >= 0) {
9263 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9264
9265 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9266 &pipe_config->dpll_hw_state));
9267 }
9268
Daniel Vetter26804af2014-06-25 22:01:55 +03009269 /*
9270 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9271 * DDI E. So just check whether this pipe is wired to DDI E and whether
9272 * the PCH transcoder is on.
9273 */
Damien Lespiauca370452013-12-03 13:56:24 +00009274 if (INTEL_INFO(dev)->gen < 9 &&
9275 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009276 pipe_config->has_pch_encoder = true;
9277
9278 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9279 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9280 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9281
9282 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9283 }
9284}
9285
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009286static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009287 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009288{
9289 struct drm_device *dev = crtc->base.dev;
9290 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009291 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009292 uint32_t tmp;
9293
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009294 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009295 POWER_DOMAIN_PIPE(crtc->pipe)))
9296 return false;
9297
Daniel Vettere143a212013-07-04 12:01:15 +02009298 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009299 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9300
Daniel Vettereccb1402013-05-22 00:50:22 +02009301 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9302 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9303 enum pipe trans_edp_pipe;
9304 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9305 default:
9306 WARN(1, "unknown pipe linked to edp transcoder\n");
9307 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9308 case TRANS_DDI_EDP_INPUT_A_ON:
9309 trans_edp_pipe = PIPE_A;
9310 break;
9311 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9312 trans_edp_pipe = PIPE_B;
9313 break;
9314 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9315 trans_edp_pipe = PIPE_C;
9316 break;
9317 }
9318
9319 if (trans_edp_pipe == crtc->pipe)
9320 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9321 }
9322
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009323 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009324 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009325 return false;
9326
Daniel Vettereccb1402013-05-22 00:50:22 +02009327 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009328 if (!(tmp & PIPECONF_ENABLE))
9329 return false;
9330
Daniel Vetter26804af2014-06-25 22:01:55 +03009331 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009332
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009333 intel_get_pipe_timings(crtc, pipe_config);
9334
Chandra Kondurua1b22782015-04-07 15:28:45 -07009335 if (INTEL_INFO(dev)->gen >= 9) {
9336 skl_init_scalers(dev, crtc, pipe_config);
9337 }
9338
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009339 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009340
9341 if (INTEL_INFO(dev)->gen >= 9) {
9342 pipe_config->scaler_state.scaler_id = -1;
9343 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9344 }
9345
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009346 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009347 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009348 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009349 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009350 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009351 else
9352 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009353 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009354
Jesse Barnese59150d2014-01-07 13:30:45 -08009355 if (IS_HASWELL(dev))
9356 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9357 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009358
Clint Taylorebb69c92014-09-30 10:30:22 -07009359 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9360 pipe_config->pixel_multiplier =
9361 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9362 } else {
9363 pipe_config->pixel_multiplier = 1;
9364 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009365
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009366 return true;
9367}
9368
Chris Wilson560b85b2010-08-07 11:01:38 +01009369static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9370{
9371 struct drm_device *dev = crtc->dev;
9372 struct drm_i915_private *dev_priv = dev->dev_private;
9373 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009374 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009375
Ville Syrjälädc41c152014-08-13 11:57:05 +03009376 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009377 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9378 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009379 unsigned int stride = roundup_pow_of_two(width) * 4;
9380
9381 switch (stride) {
9382 default:
9383 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9384 width, stride);
9385 stride = 256;
9386 /* fallthrough */
9387 case 256:
9388 case 512:
9389 case 1024:
9390 case 2048:
9391 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009392 }
9393
Ville Syrjälädc41c152014-08-13 11:57:05 +03009394 cntl |= CURSOR_ENABLE |
9395 CURSOR_GAMMA_ENABLE |
9396 CURSOR_FORMAT_ARGB |
9397 CURSOR_STRIDE(stride);
9398
9399 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009400 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009401
Ville Syrjälädc41c152014-08-13 11:57:05 +03009402 if (intel_crtc->cursor_cntl != 0 &&
9403 (intel_crtc->cursor_base != base ||
9404 intel_crtc->cursor_size != size ||
9405 intel_crtc->cursor_cntl != cntl)) {
9406 /* On these chipsets we can only modify the base/size/stride
9407 * whilst the cursor is disabled.
9408 */
9409 I915_WRITE(_CURACNTR, 0);
9410 POSTING_READ(_CURACNTR);
9411 intel_crtc->cursor_cntl = 0;
9412 }
9413
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009414 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009415 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009416 intel_crtc->cursor_base = base;
9417 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009418
9419 if (intel_crtc->cursor_size != size) {
9420 I915_WRITE(CURSIZE, size);
9421 intel_crtc->cursor_size = size;
9422 }
9423
Chris Wilson4b0e3332014-05-30 16:35:26 +03009424 if (intel_crtc->cursor_cntl != cntl) {
9425 I915_WRITE(_CURACNTR, cntl);
9426 POSTING_READ(_CURACNTR);
9427 intel_crtc->cursor_cntl = cntl;
9428 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009429}
9430
9431static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9432{
9433 struct drm_device *dev = crtc->dev;
9434 struct drm_i915_private *dev_priv = dev->dev_private;
9435 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9436 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009437 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009438
Chris Wilson4b0e3332014-05-30 16:35:26 +03009439 cntl = 0;
9440 if (base) {
9441 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009442 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309443 case 64:
9444 cntl |= CURSOR_MODE_64_ARGB_AX;
9445 break;
9446 case 128:
9447 cntl |= CURSOR_MODE_128_ARGB_AX;
9448 break;
9449 case 256:
9450 cntl |= CURSOR_MODE_256_ARGB_AX;
9451 break;
9452 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009453 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309454 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009455 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009456 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009457
9458 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9459 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009460 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009461
Matt Roper8e7d6882015-01-21 16:35:41 -08009462 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009463 cntl |= CURSOR_ROTATE_180;
9464
Chris Wilson4b0e3332014-05-30 16:35:26 +03009465 if (intel_crtc->cursor_cntl != cntl) {
9466 I915_WRITE(CURCNTR(pipe), cntl);
9467 POSTING_READ(CURCNTR(pipe));
9468 intel_crtc->cursor_cntl = cntl;
9469 }
9470
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009471 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009472 I915_WRITE(CURBASE(pipe), base);
9473 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009474
9475 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009476}
9477
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009478/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009479static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9480 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009481{
9482 struct drm_device *dev = crtc->dev;
9483 struct drm_i915_private *dev_priv = dev->dev_private;
9484 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9485 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009486 int x = crtc->cursor_x;
9487 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009488 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009489
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009490 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009491 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009492
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009493 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009494 base = 0;
9495
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009496 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009497 base = 0;
9498
9499 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009500 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009501 base = 0;
9502
9503 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9504 x = -x;
9505 }
9506 pos |= x << CURSOR_X_SHIFT;
9507
9508 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009509 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009510 base = 0;
9511
9512 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9513 y = -y;
9514 }
9515 pos |= y << CURSOR_Y_SHIFT;
9516
Chris Wilson4b0e3332014-05-30 16:35:26 +03009517 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009518 return;
9519
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009520 I915_WRITE(CURPOS(pipe), pos);
9521
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009522 /* ILK+ do this automagically */
9523 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009524 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009525 base += (intel_crtc->base.cursor->state->crtc_h *
9526 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009527 }
9528
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009529 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009530 i845_update_cursor(crtc, base);
9531 else
9532 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009533}
9534
Ville Syrjälädc41c152014-08-13 11:57:05 +03009535static bool cursor_size_ok(struct drm_device *dev,
9536 uint32_t width, uint32_t height)
9537{
9538 if (width == 0 || height == 0)
9539 return false;
9540
9541 /*
9542 * 845g/865g are special in that they are only limited by
9543 * the width of their cursors, the height is arbitrary up to
9544 * the precision of the register. Everything else requires
9545 * square cursors, limited to a few power-of-two sizes.
9546 */
9547 if (IS_845G(dev) || IS_I865G(dev)) {
9548 if ((width & 63) != 0)
9549 return false;
9550
9551 if (width > (IS_845G(dev) ? 64 : 512))
9552 return false;
9553
9554 if (height > 1023)
9555 return false;
9556 } else {
9557 switch (width | height) {
9558 case 256:
9559 case 128:
9560 if (IS_GEN2(dev))
9561 return false;
9562 case 64:
9563 break;
9564 default:
9565 return false;
9566 }
9567 }
9568
9569 return true;
9570}
9571
Jesse Barnes79e53942008-11-07 14:24:08 -08009572static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01009573 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08009574{
James Simmons72034252010-08-03 01:33:19 +01009575 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08009576 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08009577
James Simmons72034252010-08-03 01:33:19 +01009578 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009579 intel_crtc->lut_r[i] = red[i] >> 8;
9580 intel_crtc->lut_g[i] = green[i] >> 8;
9581 intel_crtc->lut_b[i] = blue[i] >> 8;
9582 }
9583
9584 intel_crtc_load_lut(crtc);
9585}
9586
Jesse Barnes79e53942008-11-07 14:24:08 -08009587/* VESA 640x480x72Hz mode to set on the pipe */
9588static struct drm_display_mode load_detect_mode = {
9589 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9590 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9591};
9592
Daniel Vettera8bb6812014-02-10 18:00:39 +01009593struct drm_framebuffer *
9594__intel_framebuffer_create(struct drm_device *dev,
9595 struct drm_mode_fb_cmd2 *mode_cmd,
9596 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01009597{
9598 struct intel_framebuffer *intel_fb;
9599 int ret;
9600
9601 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9602 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009603 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01009604 return ERR_PTR(-ENOMEM);
9605 }
9606
9607 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009608 if (ret)
9609 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009610
9611 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009612err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009613 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009614 kfree(intel_fb);
9615
9616 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009617}
9618
Daniel Vetterb5ea6422014-03-02 21:18:00 +01009619static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01009620intel_framebuffer_create(struct drm_device *dev,
9621 struct drm_mode_fb_cmd2 *mode_cmd,
9622 struct drm_i915_gem_object *obj)
9623{
9624 struct drm_framebuffer *fb;
9625 int ret;
9626
9627 ret = i915_mutex_lock_interruptible(dev);
9628 if (ret)
9629 return ERR_PTR(ret);
9630 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9631 mutex_unlock(&dev->struct_mutex);
9632
9633 return fb;
9634}
9635
Chris Wilsond2dff872011-04-19 08:36:26 +01009636static u32
9637intel_framebuffer_pitch_for_width(int width, int bpp)
9638{
9639 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9640 return ALIGN(pitch, 64);
9641}
9642
9643static u32
9644intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9645{
9646 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009647 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009648}
9649
9650static struct drm_framebuffer *
9651intel_framebuffer_create_for_mode(struct drm_device *dev,
9652 struct drm_display_mode *mode,
9653 int depth, int bpp)
9654{
9655 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009656 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009657
9658 obj = i915_gem_alloc_object(dev,
9659 intel_framebuffer_size_for_mode(mode, bpp));
9660 if (obj == NULL)
9661 return ERR_PTR(-ENOMEM);
9662
9663 mode_cmd.width = mode->hdisplay;
9664 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009665 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9666 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009667 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009668
9669 return intel_framebuffer_create(dev, &mode_cmd, obj);
9670}
9671
9672static struct drm_framebuffer *
9673mode_fits_in_fbdev(struct drm_device *dev,
9674 struct drm_display_mode *mode)
9675{
Daniel Vetter4520f532013-10-09 09:18:51 +02009676#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01009677 struct drm_i915_private *dev_priv = dev->dev_private;
9678 struct drm_i915_gem_object *obj;
9679 struct drm_framebuffer *fb;
9680
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009681 if (!dev_priv->fbdev)
9682 return NULL;
9683
9684 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009685 return NULL;
9686
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009687 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009688 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009689
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009690 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009691 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9692 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01009693 return NULL;
9694
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009695 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009696 return NULL;
9697
9698 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009699#else
9700 return NULL;
9701#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009702}
9703
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009704static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9705 struct drm_crtc *crtc,
9706 struct drm_display_mode *mode,
9707 struct drm_framebuffer *fb,
9708 int x, int y)
9709{
9710 struct drm_plane_state *plane_state;
9711 int hdisplay, vdisplay;
9712 int ret;
9713
9714 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9715 if (IS_ERR(plane_state))
9716 return PTR_ERR(plane_state);
9717
9718 if (mode)
9719 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
9720 else
9721 hdisplay = vdisplay = 0;
9722
9723 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9724 if (ret)
9725 return ret;
9726 drm_atomic_set_fb_for_plane(plane_state, fb);
9727 plane_state->crtc_x = 0;
9728 plane_state->crtc_y = 0;
9729 plane_state->crtc_w = hdisplay;
9730 plane_state->crtc_h = vdisplay;
9731 plane_state->src_x = x << 16;
9732 plane_state->src_y = y << 16;
9733 plane_state->src_w = hdisplay << 16;
9734 plane_state->src_h = vdisplay << 16;
9735
9736 return 0;
9737}
9738
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009739bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01009740 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05009741 struct intel_load_detect_pipe *old,
9742 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009743{
9744 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009745 struct intel_encoder *intel_encoder =
9746 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009747 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009748 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009749 struct drm_crtc *crtc = NULL;
9750 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02009751 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009752 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009753 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009754 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009755 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009756 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009757
Chris Wilsond2dff872011-04-19 08:36:26 +01009758 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009759 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009760 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009761
Rob Clark51fd3712013-11-19 12:10:12 -05009762retry:
9763 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9764 if (ret)
9765 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009766
Jesse Barnes79e53942008-11-07 14:24:08 -08009767 /*
9768 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009769 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009770 * - if the connector already has an assigned crtc, use it (but make
9771 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009772 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009773 * - try to find the first unused crtc that can drive this connector,
9774 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009775 */
9776
9777 /* See if we already have a CRTC for this connector */
9778 if (encoder->crtc) {
9779 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009780
Rob Clark51fd3712013-11-19 12:10:12 -05009781 ret = drm_modeset_lock(&crtc->mutex, ctx);
9782 if (ret)
9783 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009784 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9785 if (ret)
9786 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01009787
Daniel Vetter24218aa2012-08-12 19:27:11 +02009788 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009789 old->load_detect_temp = false;
9790
9791 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009792 if (connector->dpms != DRM_MODE_DPMS_ON)
9793 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01009794
Chris Wilson71731882011-04-19 23:10:58 +01009795 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08009796 }
9797
9798 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009799 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009800 i++;
9801 if (!(encoder->possible_crtcs & (1 << i)))
9802 continue;
Matt Roper83d65732015-02-25 13:12:16 -08009803 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +03009804 continue;
9805 /* This can occur when applying the pipe A quirk on resume. */
9806 if (to_intel_crtc(possible_crtc)->new_enabled)
9807 continue;
9808
9809 crtc = possible_crtc;
9810 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009811 }
9812
9813 /*
9814 * If we didn't find an unused CRTC, don't use any.
9815 */
9816 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01009817 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05009818 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08009819 }
9820
Rob Clark51fd3712013-11-19 12:10:12 -05009821 ret = drm_modeset_lock(&crtc->mutex, ctx);
9822 if (ret)
9823 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009824 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9825 if (ret)
9826 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02009827 intel_encoder->new_crtc = to_intel_crtc(crtc);
9828 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009829
9830 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009831 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +02009832 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009833 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01009834 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08009835
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009836 state = drm_atomic_state_alloc(dev);
9837 if (!state)
9838 return false;
9839
9840 state->acquire_ctx = ctx;
9841
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009842 connector_state = drm_atomic_get_connector_state(state, connector);
9843 if (IS_ERR(connector_state)) {
9844 ret = PTR_ERR(connector_state);
9845 goto fail;
9846 }
9847
9848 connector_state->crtc = crtc;
9849 connector_state->best_encoder = &intel_encoder->base;
9850
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009851 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9852 if (IS_ERR(crtc_state)) {
9853 ret = PTR_ERR(crtc_state);
9854 goto fail;
9855 }
9856
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +02009857 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009858
Chris Wilson64927112011-04-20 07:25:26 +01009859 if (!mode)
9860 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08009861
Chris Wilsond2dff872011-04-19 08:36:26 +01009862 /* We need a framebuffer large enough to accommodate all accesses
9863 * that the plane may generate whilst we perform load detection.
9864 * We can not rely on the fbcon either being present (we get called
9865 * during its initialisation to detect all boot displays, or it may
9866 * not even exist) or that it is large enough to satisfy the
9867 * requested mode.
9868 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02009869 fb = mode_fits_in_fbdev(dev, mode);
9870 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009871 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009872 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9873 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009874 } else
9875 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009876 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009877 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009878 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009879 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009880
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009881 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
9882 if (ret)
9883 goto fail;
9884
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +03009885 drm_mode_copy(&crtc_state->base.mode, mode);
9886
9887 if (intel_set_mode(crtc, state)) {
Chris Wilson64927112011-04-20 07:25:26 +01009888 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01009889 if (old->release_fb)
9890 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009891 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009892 }
Daniel Vetter9128b042015-03-03 17:31:21 +01009893 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +01009894
Jesse Barnes79e53942008-11-07 14:24:08 -08009895 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009896 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01009897 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009898
9899 fail:
Matt Roper83d65732015-02-25 13:12:16 -08009900 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -05009901fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +03009902 drm_atomic_state_free(state);
9903 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009904
Rob Clark51fd3712013-11-19 12:10:12 -05009905 if (ret == -EDEADLK) {
9906 drm_modeset_backoff(ctx);
9907 goto retry;
9908 }
9909
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009910 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009911}
9912
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009913void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +02009914 struct intel_load_detect_pipe *old,
9915 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009916{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009917 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009918 struct intel_encoder *intel_encoder =
9919 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01009920 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01009921 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009922 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009923 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009924 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009925 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009926 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009927
Chris Wilsond2dff872011-04-19 08:36:26 +01009928 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009929 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009930 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009931
Chris Wilson8261b192011-04-19 23:18:09 +01009932 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009933 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009934 if (!state)
9935 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009936
9937 state->acquire_ctx = ctx;
9938
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009939 connector_state = drm_atomic_get_connector_state(state, connector);
9940 if (IS_ERR(connector_state))
9941 goto fail;
9942
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009943 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9944 if (IS_ERR(crtc_state))
9945 goto fail;
9946
Daniel Vetterfc303102012-07-09 10:40:58 +02009947 to_intel_connector(connector)->new_encoder = NULL;
9948 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009949 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009950
9951 connector_state->best_encoder = NULL;
9952 connector_state->crtc = NULL;
9953
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +02009954 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009955
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009956 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
9957 0, 0);
9958 if (ret)
9959 goto fail;
9960
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +03009961 ret = intel_set_mode(crtc, state);
9962 if (ret)
9963 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +01009964
Daniel Vetter36206362012-12-10 20:42:17 +01009965 if (old->release_fb) {
9966 drm_framebuffer_unregister_private(old->release_fb);
9967 drm_framebuffer_unreference(old->release_fb);
9968 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009969
Chris Wilson0622a532011-04-21 09:32:11 +01009970 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009971 }
9972
Eric Anholtc751ce42010-03-25 11:48:48 -07009973 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009974 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9975 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009976
9977 return;
9978fail:
9979 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9980 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -08009981}
9982
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009983static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009984 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009985{
9986 struct drm_i915_private *dev_priv = dev->dev_private;
9987 u32 dpll = pipe_config->dpll_hw_state.dpll;
9988
9989 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009990 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009991 else if (HAS_PCH_SPLIT(dev))
9992 return 120000;
9993 else if (!IS_GEN2(dev))
9994 return 96000;
9995 else
9996 return 48000;
9997}
9998
Jesse Barnes79e53942008-11-07 14:24:08 -08009999/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010000static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010001 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010002{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010003 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010004 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010005 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010006 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010007 u32 fp;
10008 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010009 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010010
10011 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010012 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010013 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010014 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010015
10016 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010017 if (IS_PINEVIEW(dev)) {
10018 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10019 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010020 } else {
10021 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10022 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10023 }
10024
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010025 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010026 if (IS_PINEVIEW(dev))
10027 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10028 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010029 else
10030 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010031 DPLL_FPA01_P1_POST_DIV_SHIFT);
10032
10033 switch (dpll & DPLL_MODE_MASK) {
10034 case DPLLB_MODE_DAC_SERIAL:
10035 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10036 5 : 10;
10037 break;
10038 case DPLLB_MODE_LVDS:
10039 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10040 7 : 14;
10041 break;
10042 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010043 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010044 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010045 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010046 }
10047
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010048 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010049 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010050 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010051 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010052 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010053 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010054 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010055
10056 if (is_lvds) {
10057 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10058 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010059
10060 if (lvds & LVDS_CLKB_POWER_UP)
10061 clock.p2 = 7;
10062 else
10063 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010064 } else {
10065 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10066 clock.p1 = 2;
10067 else {
10068 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10069 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10070 }
10071 if (dpll & PLL_P2_DIVIDE_BY_4)
10072 clock.p2 = 4;
10073 else
10074 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010075 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010076
10077 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010078 }
10079
Ville Syrjälä18442d02013-09-13 16:00:08 +030010080 /*
10081 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010082 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010083 * encoder's get_config() function.
10084 */
10085 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010086}
10087
Ville Syrjälä6878da02013-09-13 15:59:11 +030010088int intel_dotclock_calculate(int link_freq,
10089 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010090{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010091 /*
10092 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010093 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010094 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010095 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010096 *
10097 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010098 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010099 */
10100
Ville Syrjälä6878da02013-09-13 15:59:11 +030010101 if (!m_n->link_n)
10102 return 0;
10103
10104 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10105}
10106
Ville Syrjälä18442d02013-09-13 16:00:08 +030010107static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010108 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010109{
10110 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010111
10112 /* read out port_clock from the DPLL */
10113 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010114
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010115 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010116 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010117 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010118 * agree once we know their relationship in the encoder's
10119 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010120 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010121 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010122 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10123 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010124}
10125
10126/** Returns the currently programmed mode of the given pipe. */
10127struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10128 struct drm_crtc *crtc)
10129{
Jesse Barnes548f2452011-02-17 10:40:53 -080010130 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010131 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010132 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010133 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010134 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010135 int htot = I915_READ(HTOTAL(cpu_transcoder));
10136 int hsync = I915_READ(HSYNC(cpu_transcoder));
10137 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10138 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010139 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010140
10141 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10142 if (!mode)
10143 return NULL;
10144
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010145 /*
10146 * Construct a pipe_config sufficient for getting the clock info
10147 * back out of crtc_clock_get.
10148 *
10149 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10150 * to use a real value here instead.
10151 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010152 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010153 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010154 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10155 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10156 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010157 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10158
Ville Syrjälä773ae032013-09-23 17:48:20 +030010159 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010160 mode->hdisplay = (htot & 0xffff) + 1;
10161 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10162 mode->hsync_start = (hsync & 0xffff) + 1;
10163 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10164 mode->vdisplay = (vtot & 0xffff) + 1;
10165 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10166 mode->vsync_start = (vsync & 0xffff) + 1;
10167 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10168
10169 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010170
10171 return mode;
10172}
10173
Jesse Barnes652c3932009-08-17 13:31:43 -070010174static void intel_decrease_pllclock(struct drm_crtc *crtc)
10175{
10176 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010177 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010179
Sonika Jindalbaff2962014-07-22 11:16:35 +053010180 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010181 return;
10182
10183 if (!dev_priv->lvds_downclock_avail)
10184 return;
10185
10186 /*
10187 * Since this is called by a timer, we should never get here in
10188 * the manual case.
10189 */
10190 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010191 int pipe = intel_crtc->pipe;
10192 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010193 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010194
Zhao Yakui44d98a62009-10-09 11:39:40 +080010195 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010196
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010197 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010198
Chris Wilson074b5e12012-05-02 12:07:06 +010010199 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010200 dpll |= DISPLAY_RATE_SELECT_FPA1;
10201 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010202 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010203 dpll = I915_READ(dpll_reg);
10204 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010205 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010206 }
10207
10208}
10209
Chris Wilsonf047e392012-07-21 12:31:41 +010010210void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010211{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010212 struct drm_i915_private *dev_priv = dev->dev_private;
10213
Chris Wilsonf62a0072014-02-21 17:55:39 +000010214 if (dev_priv->mm.busy)
10215 return;
10216
Paulo Zanoni43694d62014-03-07 20:08:08 -030010217 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010218 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010219 if (INTEL_INFO(dev)->gen >= 6)
10220 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010221 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010222}
10223
10224void intel_mark_idle(struct drm_device *dev)
10225{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010226 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010227 struct drm_crtc *crtc;
10228
Chris Wilsonf62a0072014-02-21 17:55:39 +000010229 if (!dev_priv->mm.busy)
10230 return;
10231
10232 dev_priv->mm.busy = false;
10233
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010234 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010235 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010236 continue;
10237
10238 intel_decrease_pllclock(crtc);
10239 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010240
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010241 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010242 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010243
Paulo Zanoni43694d62014-03-07 20:08:08 -030010244 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010245}
10246
Jesse Barnes79e53942008-11-07 14:24:08 -080010247static void intel_crtc_destroy(struct drm_crtc *crtc)
10248{
10249 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010250 struct drm_device *dev = crtc->dev;
10251 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010252
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010253 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010254 work = intel_crtc->unpin_work;
10255 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010256 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010257
10258 if (work) {
10259 cancel_work_sync(&work->work);
10260 kfree(work);
10261 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010262
10263 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010264
Jesse Barnes79e53942008-11-07 14:24:08 -080010265 kfree(intel_crtc);
10266}
10267
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010268static void intel_unpin_work_fn(struct work_struct *__work)
10269{
10270 struct intel_unpin_work *work =
10271 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010272 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010273 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010274
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010275 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010276 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010277 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010278
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010279 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010280
10281 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010282 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010283 mutex_unlock(&dev->struct_mutex);
10284
Daniel Vetterf99d7062014-06-19 16:01:59 +020010285 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010286 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010287
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010288 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10289 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10290
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010291 kfree(work);
10292}
10293
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010294static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010295 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010296{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010297 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10298 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010299 unsigned long flags;
10300
10301 /* Ignore early vblank irqs */
10302 if (intel_crtc == NULL)
10303 return;
10304
Daniel Vetterf3260382014-09-15 14:55:23 +020010305 /*
10306 * This is called both by irq handlers and the reset code (to complete
10307 * lost pageflips) so needs the full irqsave spinlocks.
10308 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010309 spin_lock_irqsave(&dev->event_lock, flags);
10310 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010311
10312 /* Ensure we don't miss a work->pending update ... */
10313 smp_rmb();
10314
10315 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010316 spin_unlock_irqrestore(&dev->event_lock, flags);
10317 return;
10318 }
10319
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010320 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010321
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010322 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010323}
10324
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010325void intel_finish_page_flip(struct drm_device *dev, int pipe)
10326{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010327 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010328 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10329
Mario Kleiner49b14a52010-12-09 07:00:07 +010010330 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010331}
10332
10333void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10334{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010335 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010336 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10337
Mario Kleiner49b14a52010-12-09 07:00:07 +010010338 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010339}
10340
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010341/* Is 'a' after or equal to 'b'? */
10342static bool g4x_flip_count_after_eq(u32 a, u32 b)
10343{
10344 return !((a - b) & 0x80000000);
10345}
10346
10347static bool page_flip_finished(struct intel_crtc *crtc)
10348{
10349 struct drm_device *dev = crtc->base.dev;
10350 struct drm_i915_private *dev_priv = dev->dev_private;
10351
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010352 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10353 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10354 return true;
10355
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010356 /*
10357 * The relevant registers doen't exist on pre-ctg.
10358 * As the flip done interrupt doesn't trigger for mmio
10359 * flips on gmch platforms, a flip count check isn't
10360 * really needed there. But since ctg has the registers,
10361 * include it in the check anyway.
10362 */
10363 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10364 return true;
10365
10366 /*
10367 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10368 * used the same base address. In that case the mmio flip might
10369 * have completed, but the CS hasn't even executed the flip yet.
10370 *
10371 * A flip count check isn't enough as the CS might have updated
10372 * the base address just after start of vblank, but before we
10373 * managed to process the interrupt. This means we'd complete the
10374 * CS flip too soon.
10375 *
10376 * Combining both checks should get us a good enough result. It may
10377 * still happen that the CS flip has been executed, but has not
10378 * yet actually completed. But in case the base address is the same
10379 * anyway, we don't really care.
10380 */
10381 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10382 crtc->unpin_work->gtt_offset &&
10383 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10384 crtc->unpin_work->flip_count);
10385}
10386
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010387void intel_prepare_page_flip(struct drm_device *dev, int plane)
10388{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010389 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010390 struct intel_crtc *intel_crtc =
10391 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10392 unsigned long flags;
10393
Daniel Vetterf3260382014-09-15 14:55:23 +020010394
10395 /*
10396 * This is called both by irq handlers and the reset code (to complete
10397 * lost pageflips) so needs the full irqsave spinlocks.
10398 *
10399 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010400 * generate a page-flip completion irq, i.e. every modeset
10401 * is also accompanied by a spurious intel_prepare_page_flip().
10402 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010403 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010404 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010405 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010406 spin_unlock_irqrestore(&dev->event_lock, flags);
10407}
10408
Robin Schroereba905b2014-05-18 02:24:50 +020010409static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010410{
10411 /* Ensure that the work item is consistent when activating it ... */
10412 smp_wmb();
10413 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10414 /* and that it is marked active as soon as the irq could fire. */
10415 smp_wmb();
10416}
10417
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010418static int intel_gen2_queue_flip(struct drm_device *dev,
10419 struct drm_crtc *crtc,
10420 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010421 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010422 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010423 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010424{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010425 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010426 u32 flip_mask;
10427 int ret;
10428
Daniel Vetter6d90c952012-04-26 23:28:05 +020010429 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010430 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010431 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010432
10433 /* Can't queue multiple flips, so wait for the previous
10434 * one to finish before executing the next.
10435 */
10436 if (intel_crtc->plane)
10437 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10438 else
10439 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010440 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10441 intel_ring_emit(ring, MI_NOOP);
10442 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10443 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10444 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010445 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010446 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010447
10448 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010449 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010450 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010451}
10452
10453static int intel_gen3_queue_flip(struct drm_device *dev,
10454 struct drm_crtc *crtc,
10455 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010456 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010457 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010458 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010459{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010460 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010461 u32 flip_mask;
10462 int ret;
10463
Daniel Vetter6d90c952012-04-26 23:28:05 +020010464 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010465 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010466 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010467
10468 if (intel_crtc->plane)
10469 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10470 else
10471 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010472 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10473 intel_ring_emit(ring, MI_NOOP);
10474 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10475 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10476 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010477 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010478 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010479
Chris Wilsone7d841c2012-12-03 11:36:30 +000010480 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010481 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010482 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010483}
10484
10485static int intel_gen4_queue_flip(struct drm_device *dev,
10486 struct drm_crtc *crtc,
10487 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010488 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010489 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010490 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010491{
10492 struct drm_i915_private *dev_priv = dev->dev_private;
10493 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10494 uint32_t pf, pipesrc;
10495 int ret;
10496
Daniel Vetter6d90c952012-04-26 23:28:05 +020010497 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010498 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010499 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010500
10501 /* i965+ uses the linear or tiled offsets from the
10502 * Display Registers (which do not change across a page-flip)
10503 * so we need only reprogram the base address.
10504 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010505 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10506 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10507 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010508 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010509 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010510
10511 /* XXX Enabling the panel-fitter across page-flip is so far
10512 * untested on non-native modes, so ignore it for now.
10513 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10514 */
10515 pf = 0;
10516 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010517 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010518
10519 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010520 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010521 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010522}
10523
10524static int intel_gen6_queue_flip(struct drm_device *dev,
10525 struct drm_crtc *crtc,
10526 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010527 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010528 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010529 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010530{
10531 struct drm_i915_private *dev_priv = dev->dev_private;
10532 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10533 uint32_t pf, pipesrc;
10534 int ret;
10535
Daniel Vetter6d90c952012-04-26 23:28:05 +020010536 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010537 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010538 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010539
Daniel Vetter6d90c952012-04-26 23:28:05 +020010540 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10541 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10542 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010543 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010544
Chris Wilson99d9acd2012-04-17 20:37:00 +010010545 /* Contrary to the suggestions in the documentation,
10546 * "Enable Panel Fitter" does not seem to be required when page
10547 * flipping with a non-native mode, and worse causes a normal
10548 * modeset to fail.
10549 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10550 */
10551 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010552 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010553 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010554
10555 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010556 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010557 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010558}
10559
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010560static int intel_gen7_queue_flip(struct drm_device *dev,
10561 struct drm_crtc *crtc,
10562 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010563 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010564 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010565 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010566{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010567 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010568 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010569 int len, ret;
10570
Robin Schroereba905b2014-05-18 02:24:50 +020010571 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010572 case PLANE_A:
10573 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10574 break;
10575 case PLANE_B:
10576 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10577 break;
10578 case PLANE_C:
10579 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10580 break;
10581 default:
10582 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010583 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010584 }
10585
Chris Wilsonffe74d72013-08-26 20:58:12 +010010586 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010587 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010588 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010589 /*
10590 * On Gen 8, SRM is now taking an extra dword to accommodate
10591 * 48bits addresses, and we need a NOOP for the batch size to
10592 * stay even.
10593 */
10594 if (IS_GEN8(dev))
10595 len += 2;
10596 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010597
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010598 /*
10599 * BSpec MI_DISPLAY_FLIP for IVB:
10600 * "The full packet must be contained within the same cache line."
10601 *
10602 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10603 * cacheline, if we ever start emitting more commands before
10604 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10605 * then do the cacheline alignment, and finally emit the
10606 * MI_DISPLAY_FLIP.
10607 */
10608 ret = intel_ring_cacheline_align(ring);
10609 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010610 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010611
Chris Wilsonffe74d72013-08-26 20:58:12 +010010612 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010613 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010614 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010615
Chris Wilsonffe74d72013-08-26 20:58:12 +010010616 /* Unmask the flip-done completion message. Note that the bspec says that
10617 * we should do this for both the BCS and RCS, and that we must not unmask
10618 * more than one flip event at any time (or ensure that one flip message
10619 * can be sent by waiting for flip-done prior to queueing new flips).
10620 * Experimentation says that BCS works despite DERRMR masking all
10621 * flip-done completion events and that unmasking all planes at once
10622 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10623 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10624 */
10625 if (ring->id == RCS) {
10626 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10627 intel_ring_emit(ring, DERRMR);
10628 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10629 DERRMR_PIPEB_PRI_FLIP_DONE |
10630 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010010631 if (IS_GEN8(dev))
10632 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10633 MI_SRM_LRM_GLOBAL_GTT);
10634 else
10635 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10636 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010010637 intel_ring_emit(ring, DERRMR);
10638 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010010639 if (IS_GEN8(dev)) {
10640 intel_ring_emit(ring, 0);
10641 intel_ring_emit(ring, MI_NOOP);
10642 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010643 }
10644
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010645 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010646 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010647 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010648 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000010649
10650 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010651 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010652 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010653}
10654
Sourab Gupta84c33a62014-06-02 16:47:17 +053010655static bool use_mmio_flip(struct intel_engine_cs *ring,
10656 struct drm_i915_gem_object *obj)
10657{
10658 /*
10659 * This is not being used for older platforms, because
10660 * non-availability of flip done interrupt forces us to use
10661 * CS flips. Older platforms derive flip done using some clever
10662 * tricks involving the flip_pending status bits and vblank irqs.
10663 * So using MMIO flips there would disrupt this mechanism.
10664 */
10665
Chris Wilson8e09bf82014-07-08 10:40:30 +010010666 if (ring == NULL)
10667 return true;
10668
Sourab Gupta84c33a62014-06-02 16:47:17 +053010669 if (INTEL_INFO(ring->dev)->gen < 5)
10670 return false;
10671
10672 if (i915.use_mmio_flip < 0)
10673 return false;
10674 else if (i915.use_mmio_flip > 0)
10675 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010010676 else if (i915.enable_execlists)
10677 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010678 else
John Harrison41c52412014-11-24 18:49:43 +000010679 return ring != i915_gem_request_get_ring(obj->last_read_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010680}
10681
Damien Lespiauff944562014-11-20 14:58:16 +000010682static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10683{
10684 struct drm_device *dev = intel_crtc->base.dev;
10685 struct drm_i915_private *dev_priv = dev->dev_private;
10686 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000010687 const enum pipe pipe = intel_crtc->pipe;
10688 u32 ctl, stride;
10689
10690 ctl = I915_READ(PLANE_CTL(pipe, 0));
10691 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010692 switch (fb->modifier[0]) {
10693 case DRM_FORMAT_MOD_NONE:
10694 break;
10695 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000010696 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010697 break;
10698 case I915_FORMAT_MOD_Y_TILED:
10699 ctl |= PLANE_CTL_TILED_Y;
10700 break;
10701 case I915_FORMAT_MOD_Yf_TILED:
10702 ctl |= PLANE_CTL_TILED_YF;
10703 break;
10704 default:
10705 MISSING_CASE(fb->modifier[0]);
10706 }
Damien Lespiauff944562014-11-20 14:58:16 +000010707
10708 /*
10709 * The stride is either expressed as a multiple of 64 bytes chunks for
10710 * linear buffers or in number of tiles for tiled buffers.
10711 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010712 stride = fb->pitches[0] /
10713 intel_fb_stride_alignment(dev, fb->modifier[0],
10714 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000010715
10716 /*
10717 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10718 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10719 */
10720 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10721 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10722
10723 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10724 POSTING_READ(PLANE_SURF(pipe, 0));
10725}
10726
10727static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010728{
10729 struct drm_device *dev = intel_crtc->base.dev;
10730 struct drm_i915_private *dev_priv = dev->dev_private;
10731 struct intel_framebuffer *intel_fb =
10732 to_intel_framebuffer(intel_crtc->base.primary->fb);
10733 struct drm_i915_gem_object *obj = intel_fb->obj;
10734 u32 dspcntr;
10735 u32 reg;
10736
Sourab Gupta84c33a62014-06-02 16:47:17 +053010737 reg = DSPCNTR(intel_crtc->plane);
10738 dspcntr = I915_READ(reg);
10739
Damien Lespiauc5d97472014-10-25 00:11:11 +010010740 if (obj->tiling_mode != I915_TILING_NONE)
10741 dspcntr |= DISPPLANE_TILED;
10742 else
10743 dspcntr &= ~DISPPLANE_TILED;
10744
Sourab Gupta84c33a62014-06-02 16:47:17 +053010745 I915_WRITE(reg, dspcntr);
10746
10747 I915_WRITE(DSPSURF(intel_crtc->plane),
10748 intel_crtc->unpin_work->gtt_offset);
10749 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010750
Damien Lespiauff944562014-11-20 14:58:16 +000010751}
10752
10753/*
10754 * XXX: This is the temporary way to update the plane registers until we get
10755 * around to using the usual plane update functions for MMIO flips
10756 */
10757static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10758{
10759 struct drm_device *dev = intel_crtc->base.dev;
10760 bool atomic_update;
10761 u32 start_vbl_count;
10762
10763 intel_mark_page_flip_active(intel_crtc);
10764
10765 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10766
10767 if (INTEL_INFO(dev)->gen >= 9)
10768 skl_do_mmio_flip(intel_crtc);
10769 else
10770 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10771 ilk_do_mmio_flip(intel_crtc);
10772
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010773 if (atomic_update)
10774 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010775}
10776
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010777static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010778{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010779 struct intel_mmio_flip *mmio_flip =
10780 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010781
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010782 if (mmio_flip->rq)
10783 WARN_ON(__i915_wait_request(mmio_flip->rq,
10784 mmio_flip->crtc->reset_counter,
10785 false, NULL, NULL));
Sourab Gupta84c33a62014-06-02 16:47:17 +053010786
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010787 intel_do_mmio_flip(mmio_flip->crtc);
10788
10789 i915_gem_request_unreference__unlocked(mmio_flip->rq);
10790 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010791}
10792
10793static int intel_queue_mmio_flip(struct drm_device *dev,
10794 struct drm_crtc *crtc,
10795 struct drm_framebuffer *fb,
10796 struct drm_i915_gem_object *obj,
10797 struct intel_engine_cs *ring,
10798 uint32_t flags)
10799{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010800 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010801
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010802 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
10803 if (mmio_flip == NULL)
10804 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010805
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010806 mmio_flip->rq = i915_gem_request_reference(obj->last_write_req);
10807 mmio_flip->crtc = to_intel_crtc(crtc);
10808
10809 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
10810 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020010811
Sourab Gupta84c33a62014-06-02 16:47:17 +053010812 return 0;
10813}
10814
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010815static int intel_default_queue_flip(struct drm_device *dev,
10816 struct drm_crtc *crtc,
10817 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010818 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010819 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010820 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010821{
10822 return -ENODEV;
10823}
10824
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010825static bool __intel_pageflip_stall_check(struct drm_device *dev,
10826 struct drm_crtc *crtc)
10827{
10828 struct drm_i915_private *dev_priv = dev->dev_private;
10829 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10830 struct intel_unpin_work *work = intel_crtc->unpin_work;
10831 u32 addr;
10832
10833 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10834 return true;
10835
10836 if (!work->enable_stall_check)
10837 return false;
10838
10839 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010010840 if (work->flip_queued_req &&
10841 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010842 return false;
10843
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010844 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010845 }
10846
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010847 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010848 return false;
10849
10850 /* Potential stall - if we see that the flip has happened,
10851 * assume a missed interrupt. */
10852 if (INTEL_INFO(dev)->gen >= 4)
10853 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10854 else
10855 addr = I915_READ(DSPADDR(intel_crtc->plane));
10856
10857 /* There is a potential issue here with a false positive after a flip
10858 * to the same address. We could address this by checking for a
10859 * non-incrementing frame counter.
10860 */
10861 return addr == work->gtt_offset;
10862}
10863
10864void intel_check_page_flip(struct drm_device *dev, int pipe)
10865{
10866 struct drm_i915_private *dev_priv = dev->dev_private;
10867 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10868 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010869 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020010870
Dave Gordon6c51d462015-03-06 15:34:26 +000010871 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010872
10873 if (crtc == NULL)
10874 return;
10875
Daniel Vetterf3260382014-09-15 14:55:23 +020010876 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010877 work = intel_crtc->unpin_work;
10878 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010879 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010010880 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010881 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010882 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010883 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010010884 if (work != NULL &&
10885 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10886 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020010887 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010888}
10889
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010890static int intel_crtc_page_flip(struct drm_crtc *crtc,
10891 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010892 struct drm_pending_vblank_event *event,
10893 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010894{
10895 struct drm_device *dev = crtc->dev;
10896 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070010897 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070010898 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010899 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080010900 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020010901 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010902 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010903 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010904 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010010905 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010906
Matt Roper2ff8fde2014-07-08 07:50:07 -070010907 /*
10908 * drm_mode_page_flip_ioctl() should already catch this, but double
10909 * check to be safe. In the future we may enable pageflipping from
10910 * a disabled primary plane.
10911 */
10912 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10913 return -EBUSY;
10914
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010915 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070010916 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010917 return -EINVAL;
10918
10919 /*
10920 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10921 * Note that pitch changes could also affect these register.
10922 */
10923 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070010924 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10925 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010926 return -EINVAL;
10927
Chris Wilsonf900db42014-02-20 09:26:13 +000010928 if (i915_terminally_wedged(&dev_priv->gpu_error))
10929 goto out_hang;
10930
Daniel Vetterb14c5672013-09-19 12:18:32 +020010931 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010932 if (work == NULL)
10933 return -ENOMEM;
10934
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010935 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010936 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010937 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010938 INIT_WORK(&work->work, intel_unpin_work_fn);
10939
Daniel Vetter87b6b102014-05-15 15:33:46 +020010940 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010941 if (ret)
10942 goto free_work;
10943
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010944 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010945 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010946 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010947 /* Before declaring the flip queue wedged, check if
10948 * the hardware completed the operation behind our backs.
10949 */
10950 if (__intel_pageflip_stall_check(dev, crtc)) {
10951 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10952 page_flip_completed(intel_crtc);
10953 } else {
10954 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010955 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010010956
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010957 drm_crtc_vblank_put(crtc);
10958 kfree(work);
10959 return -EBUSY;
10960 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010961 }
10962 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010963 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010964
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010965 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10966 flush_workqueue(dev_priv->wq);
10967
Jesse Barnes75dfca82010-02-10 15:09:44 -080010968 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010969 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010970 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010971
Matt Roperf4510a22014-04-01 15:22:40 -070010972 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010973 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080010974
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010975 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010976
Chris Wilson89ed88b2015-02-16 14:31:49 +000010977 ret = i915_mutex_lock_interruptible(dev);
10978 if (ret)
10979 goto cleanup;
10980
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010981 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020010982 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010983
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010984 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020010985 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010986
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010987 if (IS_VALLEYVIEW(dev)) {
10988 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010989 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010010990 /* vlv: DISPLAY_FLIP fails to change tiling */
10991 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000010992 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010010993 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010994 } else if (INTEL_INFO(dev)->gen >= 7) {
John Harrison41c52412014-11-24 18:49:43 +000010995 ring = i915_gem_request_get_ring(obj->last_read_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010996 if (ring == NULL || ring->id != RCS)
10997 ring = &dev_priv->ring[BCS];
10998 } else {
10999 ring = &dev_priv->ring[RCS];
11000 }
11001
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011002 mmio_flip = use_mmio_flip(ring, obj);
11003
11004 /* When using CS flips, we want to emit semaphores between rings.
11005 * However, when using mmio flips we will create a task to do the
11006 * synchronisation, so all we want here is to pin the framebuffer
11007 * into the display plane and skip any waits.
11008 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011009 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011010 crtc->primary->state,
11011 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011012 if (ret)
11013 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011014
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011015 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11016 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011017
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011018 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011019 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11020 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011021 if (ret)
11022 goto cleanup_unpin;
11023
John Harrisonf06cc1b2014-11-24 18:49:37 +000011024 i915_gem_request_assign(&work->flip_queued_req,
11025 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011026 } else {
Chris Wilsond94b5032015-04-27 13:41:15 +010011027 if (obj->last_write_req) {
11028 ret = i915_gem_check_olr(obj->last_write_req);
11029 if (ret)
11030 goto cleanup_unpin;
11031 }
11032
Sourab Gupta84c33a62014-06-02 16:47:17 +053011033 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011034 page_flip_flags);
11035 if (ret)
11036 goto cleanup_unpin;
11037
John Harrisonf06cc1b2014-11-24 18:49:37 +000011038 i915_gem_request_assign(&work->flip_queued_req,
11039 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011040 }
11041
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011042 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011043 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011044
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011045 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020011046 INTEL_FRONTBUFFER_PRIMARY(pipe));
11047
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011048 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011049 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011050 mutex_unlock(&dev->struct_mutex);
11051
Jesse Barnese5510fa2010-07-01 16:48:37 -070011052 trace_i915_flip_request(intel_crtc->plane, obj);
11053
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011054 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011055
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011056cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011057 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011058cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011059 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011060 mutex_unlock(&dev->struct_mutex);
11061cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011062 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011063 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011064
Chris Wilson89ed88b2015-02-16 14:31:49 +000011065 drm_gem_object_unreference_unlocked(&obj->base);
11066 drm_framebuffer_unreference(work->old_fb);
11067
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011068 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011069 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011070 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011071
Daniel Vetter87b6b102014-05-15 15:33:46 +020011072 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011073free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011074 kfree(work);
11075
Chris Wilsonf900db42014-02-20 09:26:13 +000011076 if (ret == -EIO) {
11077out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080011078 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011079 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011080 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011081 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011082 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011083 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011084 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011085 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011086}
11087
Jani Nikula65b38e02015-04-13 11:26:56 +030011088static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011089 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11090 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011091 .atomic_begin = intel_begin_crtc_commit,
11092 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011093};
11094
Daniel Vetter9a935852012-07-05 22:34:27 +020011095/**
11096 * intel_modeset_update_staged_output_state
11097 *
11098 * Updates the staged output configuration state, e.g. after we've read out the
11099 * current hw state.
11100 */
11101static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11102{
Ville Syrjälä76688512014-01-10 11:28:06 +020011103 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011104 struct intel_encoder *encoder;
11105 struct intel_connector *connector;
11106
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011107 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011108 connector->new_encoder =
11109 to_intel_encoder(connector->base.encoder);
11110 }
11111
Damien Lespiaub2784e12014-08-05 11:29:37 +010011112 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011113 encoder->new_crtc =
11114 to_intel_crtc(encoder->base.crtc);
11115 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011116
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011117 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011118 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011119 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011120}
11121
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011122/* Transitional helper to copy current connector/encoder state to
11123 * connector->state. This is needed so that code that is partially
11124 * converted to atomic does the right thing.
11125 */
11126static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11127{
11128 struct intel_connector *connector;
11129
11130 for_each_intel_connector(dev, connector) {
11131 if (connector->base.encoder) {
11132 connector->base.state->best_encoder =
11133 connector->base.encoder;
11134 connector->base.state->crtc =
11135 connector->base.encoder->crtc;
11136 } else {
11137 connector->base.state->best_encoder = NULL;
11138 connector->base.state->crtc = NULL;
11139 }
11140 }
11141}
11142
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011143/* Fixup legacy state after an atomic state swap.
Daniel Vetter9a935852012-07-05 22:34:27 +020011144 */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011145static void intel_modeset_fixup_state(struct drm_atomic_state *state)
Daniel Vetter9a935852012-07-05 22:34:27 +020011146{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011147 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011148 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011149 struct intel_connector *connector;
Daniel Vetter9a935852012-07-05 22:34:27 +020011150
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011151 for_each_intel_connector(state->dev, connector) {
11152 connector->base.encoder = connector->base.state->best_encoder;
11153 if (connector->base.encoder)
11154 connector->base.encoder->crtc =
11155 connector->base.state->crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011156 }
11157
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011158 /* Update crtc of disabled encoders */
11159 for_each_intel_encoder(state->dev, encoder) {
11160 int num_connectors = 0;
11161
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011162 for_each_intel_connector(state->dev, connector)
11163 if (connector->base.encoder == &encoder->base)
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011164 num_connectors++;
11165
11166 if (num_connectors == 0)
11167 encoder->base.crtc = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020011168 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011169
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011170 for_each_intel_crtc(state->dev, crtc) {
11171 crtc->base.enabled = crtc->base.state->enable;
11172 crtc->config = to_intel_crtc_state(crtc->base.state);
Ville Syrjälä76688512014-01-10 11:28:06 +020011173 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011174
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011175 /* Copy the new configuration to the staged state, to keep the few
11176 * pieces of code that haven't been converted yet happy */
11177 intel_modeset_update_staged_output_state(state->dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020011178}
11179
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011180static void
Robin Schroereba905b2014-05-18 02:24:50 +020011181connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011182 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011183{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011184 int bpp = pipe_config->pipe_bpp;
11185
11186 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11187 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011188 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011189
11190 /* Don't use an invalid EDID bpc value */
11191 if (connector->base.display_info.bpc &&
11192 connector->base.display_info.bpc * 3 < bpp) {
11193 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11194 bpp, connector->base.display_info.bpc*3);
11195 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11196 }
11197
11198 /* Clamp bpp to 8 on screens without EDID 1.4 */
11199 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11200 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11201 bpp);
11202 pipe_config->pipe_bpp = 24;
11203 }
11204}
11205
11206static int
11207compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011208 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011209{
11210 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011211 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011212 struct drm_connector *connector;
11213 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011214 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011215
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011216 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011217 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011218 else if (INTEL_INFO(dev)->gen >= 5)
11219 bpp = 12*3;
11220 else
11221 bpp = 8*3;
11222
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011223
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011224 pipe_config->pipe_bpp = bpp;
11225
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011226 state = pipe_config->base.state;
11227
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011228 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011229 for_each_connector_in_state(state, connector, connector_state, i) {
11230 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011231 continue;
11232
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011233 connected_sink_compute_bpp(to_intel_connector(connector),
11234 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011235 }
11236
11237 return bpp;
11238}
11239
Daniel Vetter644db712013-09-19 14:53:58 +020011240static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11241{
11242 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11243 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011244 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011245 mode->crtc_hdisplay, mode->crtc_hsync_start,
11246 mode->crtc_hsync_end, mode->crtc_htotal,
11247 mode->crtc_vdisplay, mode->crtc_vsync_start,
11248 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11249}
11250
Daniel Vetterc0b03412013-05-28 12:05:54 +020011251static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011252 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011253 const char *context)
11254{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011255 struct drm_device *dev = crtc->base.dev;
11256 struct drm_plane *plane;
11257 struct intel_plane *intel_plane;
11258 struct intel_plane_state *state;
11259 struct drm_framebuffer *fb;
11260
11261 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11262 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011263
11264 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11265 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11266 pipe_config->pipe_bpp, pipe_config->dither);
11267 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11268 pipe_config->has_pch_encoder,
11269 pipe_config->fdi_lanes,
11270 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11271 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11272 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011273 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11274 pipe_config->has_dp_encoder,
11275 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11276 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11277 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011278
11279 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11280 pipe_config->has_dp_encoder,
11281 pipe_config->dp_m2_n2.gmch_m,
11282 pipe_config->dp_m2_n2.gmch_n,
11283 pipe_config->dp_m2_n2.link_m,
11284 pipe_config->dp_m2_n2.link_n,
11285 pipe_config->dp_m2_n2.tu);
11286
Daniel Vetter55072d12014-11-20 16:10:28 +010011287 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11288 pipe_config->has_audio,
11289 pipe_config->has_infoframe);
11290
Daniel Vetterc0b03412013-05-28 12:05:54 +020011291 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011292 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011293 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011294 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11295 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011296 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011297 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11298 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010011299 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11300 crtc->num_scalers,
11301 pipe_config->scaler_state.scaler_users,
11302 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011303 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11304 pipe_config->gmch_pfit.control,
11305 pipe_config->gmch_pfit.pgm_ratios,
11306 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011307 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011308 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011309 pipe_config->pch_pfit.size,
11310 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011311 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011312 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011313
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011314 if (IS_BROXTON(dev)) {
11315 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
11316 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
11317 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
11318 pipe_config->ddi_pll_sel,
11319 pipe_config->dpll_hw_state.ebb0,
11320 pipe_config->dpll_hw_state.pll0,
11321 pipe_config->dpll_hw_state.pll1,
11322 pipe_config->dpll_hw_state.pll2,
11323 pipe_config->dpll_hw_state.pll3,
11324 pipe_config->dpll_hw_state.pll6,
11325 pipe_config->dpll_hw_state.pll8,
11326 pipe_config->dpll_hw_state.pcsdw12);
11327 } else if (IS_SKYLAKE(dev)) {
11328 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11329 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11330 pipe_config->ddi_pll_sel,
11331 pipe_config->dpll_hw_state.ctrl1,
11332 pipe_config->dpll_hw_state.cfgcr1,
11333 pipe_config->dpll_hw_state.cfgcr2);
11334 } else if (HAS_DDI(dev)) {
11335 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11336 pipe_config->ddi_pll_sel,
11337 pipe_config->dpll_hw_state.wrpll);
11338 } else {
11339 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11340 "fp0: 0x%x, fp1: 0x%x\n",
11341 pipe_config->dpll_hw_state.dpll,
11342 pipe_config->dpll_hw_state.dpll_md,
11343 pipe_config->dpll_hw_state.fp0,
11344 pipe_config->dpll_hw_state.fp1);
11345 }
11346
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011347 DRM_DEBUG_KMS("planes on this crtc\n");
11348 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11349 intel_plane = to_intel_plane(plane);
11350 if (intel_plane->pipe != crtc->pipe)
11351 continue;
11352
11353 state = to_intel_plane_state(plane->state);
11354 fb = state->base.fb;
11355 if (!fb) {
11356 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11357 "disabled, scaler_id = %d\n",
11358 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11359 plane->base.id, intel_plane->pipe,
11360 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11361 drm_plane_index(plane), state->scaler_id);
11362 continue;
11363 }
11364
11365 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11366 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11367 plane->base.id, intel_plane->pipe,
11368 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11369 drm_plane_index(plane));
11370 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11371 fb->base.id, fb->width, fb->height, fb->pixel_format);
11372 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11373 state->scaler_id,
11374 state->src.x1 >> 16, state->src.y1 >> 16,
11375 drm_rect_width(&state->src) >> 16,
11376 drm_rect_height(&state->src) >> 16,
11377 state->dst.x1, state->dst.y1,
11378 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11379 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011380}
11381
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011382static bool encoders_cloneable(const struct intel_encoder *a,
11383 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011384{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011385 /* masks could be asymmetric, so check both ways */
11386 return a == b || (a->cloneable & (1 << b->type) &&
11387 b->cloneable & (1 << a->type));
11388}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011389
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011390static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11391 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011392 struct intel_encoder *encoder)
11393{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011394 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011395 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011396 struct drm_connector_state *connector_state;
11397 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011398
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011399 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011400 if (connector_state->crtc != &crtc->base)
11401 continue;
11402
11403 source_encoder =
11404 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011405 if (!encoders_cloneable(encoder, source_encoder))
11406 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011407 }
11408
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011409 return true;
11410}
11411
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011412static bool check_encoder_cloning(struct drm_atomic_state *state,
11413 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011414{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011415 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011416 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011417 struct drm_connector_state *connector_state;
11418 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011419
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011420 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011421 if (connector_state->crtc != &crtc->base)
11422 continue;
11423
11424 encoder = to_intel_encoder(connector_state->best_encoder);
11425 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011426 return false;
11427 }
11428
11429 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011430}
11431
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011432static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011433{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011434 struct drm_device *dev = state->dev;
11435 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011436 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011437 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011438 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011439 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011440
11441 /*
11442 * Walk the connector list instead of the encoder
11443 * list to detect the problem on ddi platforms
11444 * where there's just one encoder per digital port.
11445 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011446 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011447 if (!connector_state->best_encoder)
11448 continue;
11449
11450 encoder = to_intel_encoder(connector_state->best_encoder);
11451
11452 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011453
11454 switch (encoder->type) {
11455 unsigned int port_mask;
11456 case INTEL_OUTPUT_UNKNOWN:
11457 if (WARN_ON(!HAS_DDI(dev)))
11458 break;
11459 case INTEL_OUTPUT_DISPLAYPORT:
11460 case INTEL_OUTPUT_HDMI:
11461 case INTEL_OUTPUT_EDP:
11462 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11463
11464 /* the same port mustn't appear more than once */
11465 if (used_ports & port_mask)
11466 return false;
11467
11468 used_ports |= port_mask;
11469 default:
11470 break;
11471 }
11472 }
11473
11474 return true;
11475}
11476
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011477static void
11478clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11479{
11480 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011481 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011482 struct intel_dpll_hw_state dpll_hw_state;
11483 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011484 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011485
Chandra Konduru663a3642015-04-07 15:28:41 -070011486 /* Clear only the intel specific part of the crtc state excluding scalers */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011487 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070011488 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011489 shared_dpll = crtc_state->shared_dpll;
11490 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011491 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011492
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011493 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011494
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011495 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011496 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011497 crtc_state->shared_dpll = shared_dpll;
11498 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011499 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011500}
11501
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011502static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011503intel_modeset_pipe_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011504 struct drm_atomic_state *state,
11505 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020011506{
Daniel Vetter7758a112012-07-08 19:40:39 +020011507 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011508 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011509 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011510 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011511 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011512 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011513
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011514 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011515 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011516 return -EINVAL;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011517 }
11518
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011519 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011520 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011521 return -EINVAL;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011522 }
11523
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011524 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011525
Daniel Vettere143a212013-07-04 12:01:15 +020011526 pipe_config->cpu_transcoder =
11527 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011528
Imre Deak2960bc92013-07-30 13:36:32 +030011529 /*
11530 * Sanitize sync polarity flags based on requested ones. If neither
11531 * positive or negative polarity is requested, treat this as meaning
11532 * negative polarity.
11533 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011534 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011535 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011536 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011537
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011538 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011539 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011540 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011541
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011542 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11543 * plane pixel format and any sink constraints into account. Returns the
11544 * source plane bpp so that dithering can be selected on mismatches
11545 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011546 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11547 pipe_config);
11548 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011549 goto fail;
11550
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011551 /*
11552 * Determine the real pipe dimensions. Note that stereo modes can
11553 * increase the actual pipe size due to the frame doubling and
11554 * insertion of additional space for blanks between the frame. This
11555 * is stored in the crtc timings. We use the requested mode to do this
11556 * computation to clearly distinguish it from the adjusted mode, which
11557 * can be changed by the connectors in the below retry loop.
11558 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011559 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011560 &pipe_config->pipe_src_w,
11561 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011562
Daniel Vettere29c22c2013-02-21 00:00:16 +010011563encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011564 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011565 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011566 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011567
Daniel Vetter135c81b2013-07-21 21:37:09 +020011568 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011569 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11570 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011571
Daniel Vetter7758a112012-07-08 19:40:39 +020011572 /* Pass our mode to the connectors and the CRTC to give them a chance to
11573 * adjust it according to limitations or connector properties, and also
11574 * a chance to reject the mode entirely.
11575 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011576 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011577 if (connector_state->crtc != crtc)
11578 continue;
11579
11580 encoder = to_intel_encoder(connector_state->best_encoder);
11581
Daniel Vetterefea6e82013-07-21 21:36:59 +020011582 if (!(encoder->compute_config(encoder, pipe_config))) {
11583 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011584 goto fail;
11585 }
11586 }
11587
Daniel Vetterff9a6752013-06-01 17:16:21 +020011588 /* Set default port clock if not overwritten by the encoder. Needs to be
11589 * done afterwards in case the encoder adjusts the mode. */
11590 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011591 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011592 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011593
Daniel Vettera43f6e02013-06-07 23:10:32 +020011594 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011595 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011596 DRM_DEBUG_KMS("CRTC fixup failed\n");
11597 goto fail;
11598 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011599
11600 if (ret == RETRY) {
11601 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11602 ret = -EINVAL;
11603 goto fail;
11604 }
11605
11606 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11607 retry = false;
11608 goto encoder_retry;
11609 }
11610
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011611 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011612 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011613 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011614
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011615 return 0;
Daniel Vetter7758a112012-07-08 19:40:39 +020011616fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011617 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020011618}
11619
Daniel Vetterea9d7582012-07-10 10:42:52 +020011620static bool intel_crtc_in_use(struct drm_crtc *crtc)
11621{
11622 struct drm_encoder *encoder;
11623 struct drm_device *dev = crtc->dev;
11624
11625 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11626 if (encoder->crtc == crtc)
11627 return true;
11628
11629 return false;
11630}
11631
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011632static bool
11633needs_modeset(struct drm_crtc_state *state)
Daniel Vetterea9d7582012-07-10 10:42:52 +020011634{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011635 return state->mode_changed || state->active_changed;
11636}
11637
11638static void
11639intel_modeset_update_state(struct drm_atomic_state *state)
11640{
11641 struct drm_device *dev = state->dev;
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011642 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011643 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011644 struct drm_crtc *crtc;
11645 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011646 struct drm_connector *connector;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011647 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011648
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011649 intel_shared_dpll_commit(dev_priv);
11650
Damien Lespiaub2784e12014-08-05 11:29:37 +010011651 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020011652 if (!intel_encoder->base.crtc)
11653 continue;
11654
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011655 for_each_crtc_in_state(state, crtc, crtc_state, i)
11656 if (crtc == intel_encoder->base.crtc)
11657 break;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011658
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011659 if (crtc != intel_encoder->base.crtc)
11660 continue;
11661
11662 if (crtc_state->enable && needs_modeset(crtc_state))
Daniel Vetterea9d7582012-07-10 10:42:52 +020011663 intel_encoder->connectors_active = false;
11664 }
11665
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011666 drm_atomic_helper_swap_state(state->dev, state);
11667 intel_modeset_fixup_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011668
Ville Syrjälä76688512014-01-10 11:28:06 +020011669 /* Double check state. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011670 for_each_crtc(dev, crtc) {
11671 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Daniel Vetterea9d7582012-07-10 10:42:52 +020011672 }
11673
11674 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11675 if (!connector->encoder || !connector->encoder->crtc)
11676 continue;
11677
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011678 for_each_crtc_in_state(state, crtc, crtc_state, i)
11679 if (crtc == connector->encoder->crtc)
11680 break;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011681
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011682 if (crtc != connector->encoder->crtc)
11683 continue;
11684
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011685 if (crtc->state->enable && needs_modeset(crtc->state)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020011686 struct drm_property *dpms_property =
11687 dev->mode_config.dpms_property;
11688
Daniel Vetterea9d7582012-07-10 10:42:52 +020011689 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050011690 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020011691 dpms_property,
11692 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011693
11694 intel_encoder = to_intel_encoder(connector->encoder);
11695 intel_encoder->connectors_active = true;
11696 }
11697 }
11698
11699}
11700
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011701static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011702{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011703 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011704
11705 if (clock1 == clock2)
11706 return true;
11707
11708 if (!clock1 || !clock2)
11709 return false;
11710
11711 diff = abs(clock1 - clock2);
11712
11713 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11714 return true;
11715
11716 return false;
11717}
11718
Daniel Vetter25c5b262012-07-08 22:08:04 +020011719#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11720 list_for_each_entry((intel_crtc), \
11721 &(dev)->mode_config.crtc_list, \
11722 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020011723 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020011724
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011725static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011726intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011727 struct intel_crtc_state *current_config,
11728 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011729{
Daniel Vetter66e985c2013-06-05 13:34:20 +020011730#define PIPE_CONF_CHECK_X(name) \
11731 if (current_config->name != pipe_config->name) { \
11732 DRM_ERROR("mismatch in " #name " " \
11733 "(expected 0x%08x, found 0x%08x)\n", \
11734 current_config->name, \
11735 pipe_config->name); \
11736 return false; \
11737 }
11738
Daniel Vetter08a24032013-04-19 11:25:34 +020011739#define PIPE_CONF_CHECK_I(name) \
11740 if (current_config->name != pipe_config->name) { \
11741 DRM_ERROR("mismatch in " #name " " \
11742 "(expected %i, found %i)\n", \
11743 current_config->name, \
11744 pipe_config->name); \
11745 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011746 }
11747
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011748/* This is required for BDW+ where there is only one set of registers for
11749 * switching between high and low RR.
11750 * This macro can be used whenever a comparison has to be made between one
11751 * hw state and multiple sw state variables.
11752 */
11753#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11754 if ((current_config->name != pipe_config->name) && \
11755 (current_config->alt_name != pipe_config->name)) { \
11756 DRM_ERROR("mismatch in " #name " " \
11757 "(expected %i or %i, found %i)\n", \
11758 current_config->name, \
11759 current_config->alt_name, \
11760 pipe_config->name); \
11761 return false; \
11762 }
11763
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011764#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11765 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070011766 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011767 "(expected %i, found %i)\n", \
11768 current_config->name & (mask), \
11769 pipe_config->name & (mask)); \
11770 return false; \
11771 }
11772
Ville Syrjälä5e550652013-09-06 23:29:07 +030011773#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11774 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11775 DRM_ERROR("mismatch in " #name " " \
11776 "(expected %i, found %i)\n", \
11777 current_config->name, \
11778 pipe_config->name); \
11779 return false; \
11780 }
11781
Daniel Vetterbb760062013-06-06 14:55:52 +020011782#define PIPE_CONF_QUIRK(quirk) \
11783 ((current_config->quirks | pipe_config->quirks) & (quirk))
11784
Daniel Vettereccb1402013-05-22 00:50:22 +020011785 PIPE_CONF_CHECK_I(cpu_transcoder);
11786
Daniel Vetter08a24032013-04-19 11:25:34 +020011787 PIPE_CONF_CHECK_I(has_pch_encoder);
11788 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020011789 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11790 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11791 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11792 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11793 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020011794
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011795 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011796
11797 if (INTEL_INFO(dev)->gen < 8) {
11798 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11799 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11800 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11801 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11802 PIPE_CONF_CHECK_I(dp_m_n.tu);
11803
11804 if (current_config->has_drrs) {
11805 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11806 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11807 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11808 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11809 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11810 }
11811 } else {
11812 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11813 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11814 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11815 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11816 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11817 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011818
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011819 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11820 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11821 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11822 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11823 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11824 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011825
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011826 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11827 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11828 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11829 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11830 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11831 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011832
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011833 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020011834 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020011835 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11836 IS_VALLEYVIEW(dev))
11837 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080011838 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011839
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011840 PIPE_CONF_CHECK_I(has_audio);
11841
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011842 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011843 DRM_MODE_FLAG_INTERLACE);
11844
Daniel Vetterbb760062013-06-06 14:55:52 +020011845 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011846 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011847 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011848 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011849 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011850 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011851 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011852 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011853 DRM_MODE_FLAG_NVSYNC);
11854 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011855
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011856 PIPE_CONF_CHECK_I(pipe_src_w);
11857 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011858
Daniel Vetter99535992014-04-13 12:00:33 +020011859 /*
11860 * FIXME: BIOS likes to set up a cloned config with lvds+external
11861 * screen. Since we don't yet re-compute the pipe config when moving
11862 * just the lvds port away to another pipe the sw tracking won't match.
11863 *
11864 * Proper atomic modesets with recomputed global state will fix this.
11865 * Until then just don't check gmch state for inherited modes.
11866 */
11867 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11868 PIPE_CONF_CHECK_I(gmch_pfit.control);
11869 /* pfit ratios are autocomputed by the hw on gen4+ */
11870 if (INTEL_INFO(dev)->gen < 4)
11871 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11872 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11873 }
11874
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011875 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11876 if (current_config->pch_pfit.enabled) {
11877 PIPE_CONF_CHECK_I(pch_pfit.pos);
11878 PIPE_CONF_CHECK_I(pch_pfit.size);
11879 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011880
Chandra Kondurua1b22782015-04-07 15:28:45 -070011881 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
11882
Jesse Barnese59150d2014-01-07 13:30:45 -080011883 /* BDW+ don't expose a synchronous way to read the state */
11884 if (IS_HASWELL(dev))
11885 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011886
Ville Syrjälä282740f2013-09-04 18:30:03 +030011887 PIPE_CONF_CHECK_I(double_wide);
11888
Daniel Vetter26804af2014-06-25 22:01:55 +030011889 PIPE_CONF_CHECK_X(ddi_pll_sel);
11890
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011891 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011892 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011893 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011894 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11895 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011896 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011897 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11898 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11899 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011900
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011901 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11902 PIPE_CONF_CHECK_I(pipe_bpp);
11903
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011904 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011905 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011906
Daniel Vetter66e985c2013-06-05 13:34:20 +020011907#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011908#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011909#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011910#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011911#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011912#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011913
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011914 return true;
11915}
11916
Damien Lespiau08db6652014-11-04 17:06:52 +000011917static void check_wm_state(struct drm_device *dev)
11918{
11919 struct drm_i915_private *dev_priv = dev->dev_private;
11920 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11921 struct intel_crtc *intel_crtc;
11922 int plane;
11923
11924 if (INTEL_INFO(dev)->gen < 9)
11925 return;
11926
11927 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11928 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11929
11930 for_each_intel_crtc(dev, intel_crtc) {
11931 struct skl_ddb_entry *hw_entry, *sw_entry;
11932 const enum pipe pipe = intel_crtc->pipe;
11933
11934 if (!intel_crtc->active)
11935 continue;
11936
11937 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000011938 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000011939 hw_entry = &hw_ddb.plane[pipe][plane];
11940 sw_entry = &sw_ddb->plane[pipe][plane];
11941
11942 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11943 continue;
11944
11945 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11946 "(expected (%u,%u), found (%u,%u))\n",
11947 pipe_name(pipe), plane + 1,
11948 sw_entry->start, sw_entry->end,
11949 hw_entry->start, hw_entry->end);
11950 }
11951
11952 /* cursor */
11953 hw_entry = &hw_ddb.cursor[pipe];
11954 sw_entry = &sw_ddb->cursor[pipe];
11955
11956 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11957 continue;
11958
11959 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11960 "(expected (%u,%u), found (%u,%u))\n",
11961 pipe_name(pipe),
11962 sw_entry->start, sw_entry->end,
11963 hw_entry->start, hw_entry->end);
11964 }
11965}
11966
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011967static void
11968check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011969{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011970 struct intel_connector *connector;
11971
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011972 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011973 /* This also checks the encoder/connector hw state with the
11974 * ->get_hw_state callbacks. */
11975 intel_connector_check_state(connector);
11976
Rob Clarke2c719b2014-12-15 13:56:32 -050011977 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011978 "connector's staged encoder doesn't match current encoder\n");
11979 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011980}
11981
11982static void
11983check_encoder_state(struct drm_device *dev)
11984{
11985 struct intel_encoder *encoder;
11986 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011987
Damien Lespiaub2784e12014-08-05 11:29:37 +010011988 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011989 bool enabled = false;
11990 bool active = false;
11991 enum pipe pipe, tracked_pipe;
11992
11993 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11994 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011995 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011996
Rob Clarke2c719b2014-12-15 13:56:32 -050011997 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011998 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011999 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012000 "encoder's active_connectors set, but no crtc\n");
12001
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012002 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012003 if (connector->base.encoder != &encoder->base)
12004 continue;
12005 enabled = true;
12006 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12007 active = true;
12008 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012009 /*
12010 * for MST connectors if we unplug the connector is gone
12011 * away but the encoder is still connected to a crtc
12012 * until a modeset happens in response to the hotplug.
12013 */
12014 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12015 continue;
12016
Rob Clarke2c719b2014-12-15 13:56:32 -050012017 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012018 "encoder's enabled state mismatch "
12019 "(expected %i, found %i)\n",
12020 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012021 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012022 "active encoder with no crtc\n");
12023
Rob Clarke2c719b2014-12-15 13:56:32 -050012024 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012025 "encoder's computed active state doesn't match tracked active state "
12026 "(expected %i, found %i)\n", active, encoder->connectors_active);
12027
12028 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012029 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012030 "encoder's hw state doesn't match sw tracking "
12031 "(expected %i, found %i)\n",
12032 encoder->connectors_active, active);
12033
12034 if (!encoder->base.crtc)
12035 continue;
12036
12037 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012038 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012039 "active encoder's pipe doesn't match"
12040 "(expected %i, found %i)\n",
12041 tracked_pipe, pipe);
12042
12043 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012044}
12045
12046static void
12047check_crtc_state(struct drm_device *dev)
12048{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012049 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012050 struct intel_crtc *crtc;
12051 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012052 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012053
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012054 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012055 bool enabled = false;
12056 bool active = false;
12057
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012058 memset(&pipe_config, 0, sizeof(pipe_config));
12059
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012060 DRM_DEBUG_KMS("[CRTC:%d]\n",
12061 crtc->base.base.id);
12062
Matt Roper83d65732015-02-25 13:12:16 -080012063 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012064 "active crtc, but not enabled in sw tracking\n");
12065
Damien Lespiaub2784e12014-08-05 11:29:37 +010012066 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012067 if (encoder->base.crtc != &crtc->base)
12068 continue;
12069 enabled = true;
12070 if (encoder->connectors_active)
12071 active = true;
12072 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012073
Rob Clarke2c719b2014-12-15 13:56:32 -050012074 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012075 "crtc's computed active state doesn't match tracked active state "
12076 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012077 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012078 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012079 "(expected %i, found %i)\n", enabled,
12080 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012081
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012082 active = dev_priv->display.get_pipe_config(crtc,
12083 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012084
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012085 /* hw state is inconsistent with the pipe quirk */
12086 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12087 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012088 active = crtc->active;
12089
Damien Lespiaub2784e12014-08-05 11:29:37 +010012090 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012091 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012092 if (encoder->base.crtc != &crtc->base)
12093 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012094 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012095 encoder->get_config(encoder, &pipe_config);
12096 }
12097
Rob Clarke2c719b2014-12-15 13:56:32 -050012098 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012099 "crtc active state doesn't match with hw state "
12100 "(expected %i, found %i)\n", crtc->active, active);
12101
Daniel Vetterc0b03412013-05-28 12:05:54 +020012102 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012103 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012104 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012105 intel_dump_pipe_config(crtc, &pipe_config,
12106 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012107 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012108 "[sw state]");
12109 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012110 }
12111}
12112
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012113static void
12114check_shared_dpll_state(struct drm_device *dev)
12115{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012116 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012117 struct intel_crtc *crtc;
12118 struct intel_dpll_hw_state dpll_hw_state;
12119 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012120
12121 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12122 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12123 int enabled_crtcs = 0, active_crtcs = 0;
12124 bool active;
12125
12126 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12127
12128 DRM_DEBUG_KMS("%s\n", pll->name);
12129
12130 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12131
Rob Clarke2c719b2014-12-15 13:56:32 -050012132 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012133 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012134 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012135 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012136 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012137 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012138 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012139 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012140 "pll on state mismatch (expected %i, found %i)\n",
12141 pll->on, active);
12142
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012143 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012144 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012145 enabled_crtcs++;
12146 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12147 active_crtcs++;
12148 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012149 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012150 "pll active crtcs mismatch (expected %i, found %i)\n",
12151 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012152 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012153 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012154 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012155
Rob Clarke2c719b2014-12-15 13:56:32 -050012156 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012157 sizeof(dpll_hw_state)),
12158 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012159 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012160}
12161
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012162void
12163intel_modeset_check_state(struct drm_device *dev)
12164{
Damien Lespiau08db6652014-11-04 17:06:52 +000012165 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012166 check_connector_state(dev);
12167 check_encoder_state(dev);
12168 check_crtc_state(dev);
12169 check_shared_dpll_state(dev);
12170}
12171
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012172void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012173 int dotclock)
12174{
12175 /*
12176 * FDI already provided one idea for the dotclock.
12177 * Yell if the encoder disagrees.
12178 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012179 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012180 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012181 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012182}
12183
Ville Syrjälä80715b22014-05-15 20:23:23 +030012184static void update_scanline_offset(struct intel_crtc *crtc)
12185{
12186 struct drm_device *dev = crtc->base.dev;
12187
12188 /*
12189 * The scanline counter increments at the leading edge of hsync.
12190 *
12191 * On most platforms it starts counting from vtotal-1 on the
12192 * first active line. That means the scanline counter value is
12193 * always one less than what we would expect. Ie. just after
12194 * start of vblank, which also occurs at start of hsync (on the
12195 * last active line), the scanline counter will read vblank_start-1.
12196 *
12197 * On gen2 the scanline counter starts counting from 1 instead
12198 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12199 * to keep the value positive), instead of adding one.
12200 *
12201 * On HSW+ the behaviour of the scanline counter depends on the output
12202 * type. For DP ports it behaves like most other platforms, but on HDMI
12203 * there's an extra 1 line difference. So we need to add two instead of
12204 * one to the value.
12205 */
12206 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012207 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012208 int vtotal;
12209
12210 vtotal = mode->crtc_vtotal;
12211 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12212 vtotal /= 2;
12213
12214 crtc->scanline_offset = vtotal - 1;
12215 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012216 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012217 crtc->scanline_offset = 2;
12218 } else
12219 crtc->scanline_offset = 1;
12220}
12221
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012222static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012223intel_modeset_compute_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012224 struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012225{
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012226 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012227 int ret = 0;
12228
12229 ret = drm_atomic_add_affected_connectors(state, crtc);
12230 if (ret)
12231 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012232
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012233 ret = drm_atomic_helper_check_modeset(state->dev, state);
12234 if (ret)
12235 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012236
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012237 /*
12238 * Note this needs changes when we start tracking multiple modes
12239 * and crtcs. At that point we'll need to compute the whole config
12240 * (i.e. one pipe_config for each crtc) rather than just the one
12241 * for this crtc.
12242 */
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012243 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
12244 if (IS_ERR(pipe_config))
12245 return pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012246
Ander Conselvan de Oliveira4fed33f2015-04-21 17:13:03 +030012247 if (!pipe_config->base.enable)
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012248 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012249
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012250 ret = intel_modeset_pipe_config(crtc, state, pipe_config);
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012251 if (ret)
12252 return ERR_PTR(ret);
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012253
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012254 /* Check things that can only be changed through modeset */
12255 if (pipe_config->has_audio !=
12256 to_intel_crtc(crtc)->config->has_audio)
12257 pipe_config->base.mode_changed = true;
12258
12259 /*
12260 * Note we have an issue here with infoframes: current code
12261 * only updates them on the full mode set path per hw
12262 * requirements. So here we should be checking for any
12263 * required changes and forcing a mode set.
12264 */
12265
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012266 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,"[modeset]");
12267
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012268 ret = drm_atomic_helper_check_planes(state->dev, state);
12269 if (ret)
12270 return ERR_PTR(ret);
12271
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012272 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012273}
12274
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012275static int __intel_set_mode_setup_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012276{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012277 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012278 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012279 unsigned clear_pipes = 0;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012280 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012281 struct intel_crtc_state *intel_crtc_state;
12282 struct drm_crtc *crtc;
12283 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012284 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012285 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012286
12287 if (!dev_priv->display.crtc_compute_clock)
12288 return 0;
12289
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012290 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12291 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012292 intel_crtc_state = to_intel_crtc_state(crtc_state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012293
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012294 if (needs_modeset(crtc_state)) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012295 clear_pipes |= 1 << intel_crtc->pipe;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012296 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012297 }
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012298 }
12299
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012300 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12301 if (ret)
12302 goto done;
12303
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012304 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12305 if (!needs_modeset(crtc_state) || !crtc_state->enable)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012306 continue;
12307
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012308 intel_crtc = to_intel_crtc(crtc);
12309 intel_crtc_state = to_intel_crtc_state(crtc_state);
12310
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012311 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012312 intel_crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012313 if (ret) {
12314 intel_shared_dpll_abort_config(dev_priv);
12315 goto done;
12316 }
12317 }
12318
12319done:
12320 return ret;
12321}
12322
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012323/* Code that should eventually be part of atomic_check() */
12324static int __intel_set_mode_checks(struct drm_atomic_state *state)
12325{
12326 struct drm_device *dev = state->dev;
12327 int ret;
12328
12329 /*
12330 * See if the config requires any additional preparation, e.g.
12331 * to adjust global state with pipes off. We need to do this
12332 * here so we can get the modeset_pipe updated config for the new
12333 * mode set on this crtc. For other crtcs we need to use the
12334 * adjusted_mode bits in the crtc directly.
12335 */
12336 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
12337 ret = valleyview_modeset_global_pipes(state);
12338 if (ret)
12339 return ret;
12340 }
12341
12342 ret = __intel_set_mode_setup_plls(state);
12343 if (ret)
12344 return ret;
12345
12346 return 0;
12347}
12348
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012349static int __intel_set_mode(struct drm_crtc *modeset_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012350 struct intel_crtc_state *pipe_config)
Daniel Vettera6778b32012-07-02 09:56:42 +020012351{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012352 struct drm_device *dev = modeset_crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012353 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012354 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012355 struct drm_crtc *crtc;
12356 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012357 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012358 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020012359
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012360 ret = __intel_set_mode_checks(state);
12361 if (ret < 0)
12362 return ret;
12363
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012364 ret = drm_atomic_helper_prepare_planes(dev, state);
12365 if (ret)
12366 return ret;
12367
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012368 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12369 if (!needs_modeset(crtc_state))
12370 continue;
Daniel Vetter460da9162013-03-27 00:44:51 +010012371
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012372 if (!crtc_state->enable) {
12373 intel_crtc_disable(crtc);
12374 } else if (crtc->state->enable) {
12375 intel_crtc_disable_planes(crtc);
12376 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030012377 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012378 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012379
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012380 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12381 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012382 *
12383 * Note we'll need to fix this up when we start tracking multiple
12384 * pipes; here we assume a single modeset_pipe and only track the
12385 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012386 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012387 if (pipe_config->base.enable && needs_modeset(&pipe_config->base)) {
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012388 modeset_crtc->mode = pipe_config->base.mode;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020012389
12390 /*
12391 * Calculate and store various constants which
12392 * are later needed by vblank and swap-completion
12393 * timestamping. They are derived from true hwmode.
12394 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012395 drm_calc_timestamping_constants(modeset_crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012396 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012397 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012398
Daniel Vetterea9d7582012-07-10 10:42:52 +020012399 /* Only after disabling all output pipelines that will be changed can we
12400 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012401 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012402
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030012403 /* The state has been swaped above, so state actually contains the
12404 * old state now. */
12405
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012406 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020012407
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012408 drm_atomic_helper_commit_planes(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012409
12410 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012411 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030012412 if (!needs_modeset(crtc->state) || !crtc->state->enable)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012413 continue;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012414
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012415 update_scanline_offset(to_intel_crtc(crtc));
12416
12417 dev_priv->display.crtc_enable(crtc);
12418 intel_crtc_enable_planes(crtc);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012419 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012420
Daniel Vettera6778b32012-07-02 09:56:42 +020012421 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012422
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012423 drm_atomic_helper_cleanup_planes(dev, state);
12424
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012425 drm_atomic_state_free(state);
12426
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030012427 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020012428}
12429
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012430static int intel_set_mode_with_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012431 struct intel_crtc_state *pipe_config)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012432{
12433 int ret;
12434
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012435 ret = __intel_set_mode(crtc, pipe_config);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012436
12437 if (ret == 0)
12438 intel_modeset_check_state(crtc->dev);
12439
12440 return ret;
12441}
12442
Damien Lespiaue7457a92013-08-08 22:28:59 +010012443static int intel_set_mode(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012444 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020012445{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012446 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012447 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020012448
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012449 pipe_config = intel_modeset_compute_config(crtc, state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012450 if (IS_ERR(pipe_config)) {
12451 ret = PTR_ERR(pipe_config);
12452 goto out;
12453 }
Daniel Vetterf30da182013-04-11 20:22:50 +020012454
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012455 ret = intel_set_mode_with_config(crtc, pipe_config);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012456 if (ret)
12457 goto out;
12458
12459out:
12460 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020012461}
12462
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012463void intel_crtc_restore_mode(struct drm_crtc *crtc)
12464{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012465 struct drm_device *dev = crtc->dev;
12466 struct drm_atomic_state *state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012467 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012468 struct intel_encoder *encoder;
12469 struct intel_connector *connector;
12470 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012471 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012472 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012473
12474 state = drm_atomic_state_alloc(dev);
12475 if (!state) {
12476 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12477 crtc->base.id);
12478 return;
12479 }
12480
12481 state->acquire_ctx = dev->mode_config.acquire_ctx;
12482
12483 /* The force restore path in the HW readout code relies on the staged
12484 * config still keeping the user requested config while the actual
12485 * state has been overwritten by the configuration read from HW. We
12486 * need to copy the staged config to the atomic state, otherwise the
12487 * mode set will just reapply the state the HW is already in. */
12488 for_each_intel_encoder(dev, encoder) {
12489 if (&encoder->new_crtc->base != crtc)
12490 continue;
12491
12492 for_each_intel_connector(dev, connector) {
12493 if (connector->new_encoder != encoder)
12494 continue;
12495
12496 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12497 if (IS_ERR(connector_state)) {
12498 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12499 connector->base.base.id,
12500 connector->base.name,
12501 PTR_ERR(connector_state));
12502 continue;
12503 }
12504
12505 connector_state->crtc = crtc;
12506 connector_state->best_encoder = &encoder->base;
12507 }
12508 }
12509
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012510 for_each_intel_crtc(dev, intel_crtc) {
12511 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
12512 continue;
12513
12514 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
12515 if (IS_ERR(crtc_state)) {
12516 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
12517 intel_crtc->base.base.id,
12518 PTR_ERR(crtc_state));
12519 continue;
12520 }
12521
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020012522 crtc_state->base.active = crtc_state->base.enable =
12523 intel_crtc->new_enabled;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012524
12525 if (&intel_crtc->base == crtc)
12526 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012527 }
12528
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030012529 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
12530 crtc->primary->fb, crtc->x, crtc->y);
12531
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012532 ret = intel_set_mode(crtc, state);
12533 if (ret)
12534 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012535}
12536
Daniel Vetter25c5b262012-07-08 22:08:04 +020012537#undef for_each_intel_crtc_masked
12538
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012539static bool intel_connector_in_mode_set(struct intel_connector *connector,
12540 struct drm_mode_set *set)
12541{
12542 int ro;
12543
12544 for (ro = 0; ro < set->num_connectors; ro++)
12545 if (set->connectors[ro] == &connector->base)
12546 return true;
12547
12548 return false;
12549}
12550
Daniel Vetter2e431052012-07-04 22:42:15 +020012551static int
Daniel Vetter9a935852012-07-05 22:34:27 +020012552intel_modeset_stage_output_state(struct drm_device *dev,
12553 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012554 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020012555{
Daniel Vetter9a935852012-07-05 22:34:27 +020012556 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012557 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012558 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012559 struct drm_crtc *crtc;
12560 struct drm_crtc_state *crtc_state;
12561 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020012562
Damien Lespiau9abdda72013-02-13 13:29:23 +000012563 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020012564 * of connectors. For paranoia, double-check this. */
12565 WARN_ON(!set->fb && (set->num_connectors != 0));
12566 WARN_ON(set->fb && (set->num_connectors == 0));
12567
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012568 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012569 bool in_mode_set = intel_connector_in_mode_set(connector, set);
12570
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012571 if (!in_mode_set && connector->base.state->crtc != set->crtc)
12572 continue;
12573
12574 connector_state =
12575 drm_atomic_get_connector_state(state, &connector->base);
12576 if (IS_ERR(connector_state))
12577 return PTR_ERR(connector_state);
12578
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012579 if (in_mode_set) {
12580 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012581 connector_state->best_encoder =
12582 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020012583 }
12584
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012585 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012586 continue;
12587
Daniel Vetter9a935852012-07-05 22:34:27 +020012588 /* If we disable the crtc, disable all its connectors. Also, if
12589 * the connector is on the changing crtc but not on the new
12590 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012591 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012592 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020012593
12594 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12595 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012596 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020012597 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012598 }
12599 /* connector->new_encoder is now updated for all connectors. */
12600
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012601 for_each_connector_in_state(state, drm_connector, connector_state, i) {
12602 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020012603
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012604 if (!connector_state->best_encoder) {
12605 ret = drm_atomic_set_crtc_for_connector(connector_state,
12606 NULL);
12607 if (ret)
12608 return ret;
12609
Daniel Vetter50f56112012-07-02 09:35:43 +020012610 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012611 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012612
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012613 if (intel_connector_in_mode_set(connector, set)) {
12614 struct drm_crtc *crtc = connector->base.state->crtc;
12615
12616 /* If this connector was in a previous crtc, add it
12617 * to the state. We might need to disable it. */
12618 if (crtc) {
12619 crtc_state =
12620 drm_atomic_get_crtc_state(state, crtc);
12621 if (IS_ERR(crtc_state))
12622 return PTR_ERR(crtc_state);
12623 }
12624
12625 ret = drm_atomic_set_crtc_for_connector(connector_state,
12626 set->crtc);
12627 if (ret)
12628 return ret;
12629 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012630
12631 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012632 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
12633 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012634 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012635 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012636
Daniel Vetter9a935852012-07-05 22:34:27 +020012637 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12638 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012639 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012640 connector_state->crtc->base.id);
12641
12642 if (connector_state->best_encoder != &connector->encoder->base)
12643 connector->encoder =
12644 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020012645 }
12646
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012647 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020012648 bool has_connectors;
12649
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012650 ret = drm_atomic_add_affected_connectors(state, crtc);
12651 if (ret)
12652 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012653
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020012654 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
12655 if (has_connectors != crtc_state->enable)
12656 crtc_state->enable =
12657 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020012658 }
12659
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012660 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
12661 set->fb, set->x, set->y);
12662 if (ret)
12663 return ret;
12664
12665 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
12666 if (IS_ERR(crtc_state))
12667 return PTR_ERR(crtc_state);
12668
12669 if (set->mode)
12670 drm_mode_copy(&crtc_state->mode, set->mode);
12671
12672 if (set->num_connectors)
12673 crtc_state->active = true;
12674
Daniel Vetter2e431052012-07-04 22:42:15 +020012675 return 0;
12676}
12677
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012678static bool primary_plane_visible(struct drm_crtc *crtc)
12679{
12680 struct intel_plane_state *plane_state =
12681 to_intel_plane_state(crtc->primary->state);
12682
12683 return plane_state->visible;
12684}
12685
Daniel Vetter2e431052012-07-04 22:42:15 +020012686static int intel_crtc_set_config(struct drm_mode_set *set)
12687{
12688 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012689 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012690 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012691 bool primary_plane_was_visible;
Daniel Vetter2e431052012-07-04 22:42:15 +020012692 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020012693
Daniel Vetter8d3e3752012-07-05 16:09:09 +020012694 BUG_ON(!set);
12695 BUG_ON(!set->crtc);
12696 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020012697
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010012698 /* Enforce sane interface api - has been abused by the fb helper. */
12699 BUG_ON(!set->mode && set->fb);
12700 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020012701
Daniel Vetter2e431052012-07-04 22:42:15 +020012702 if (set->fb) {
12703 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12704 set->crtc->base.id, set->fb->base.id,
12705 (int)set->num_connectors, set->x, set->y);
12706 } else {
12707 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020012708 }
12709
12710 dev = set->crtc->dev;
12711
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012712 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012713 if (!state)
12714 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012715
12716 state->acquire_ctx = dev->mode_config.acquire_ctx;
12717
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030012718 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020012719 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012720 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020012721
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012722 pipe_config = intel_modeset_compute_config(set->crtc, state);
Jesse Barnes20664592014-11-05 14:26:09 -080012723 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080012724 ret = PTR_ERR(pipe_config);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012725 goto out;
Jesse Barnes20664592014-11-05 14:26:09 -080012726 }
Jesse Barnes50f52752014-11-07 13:11:00 -080012727
Jesse Barnes1f9954d2014-11-05 14:26:10 -080012728 intel_update_pipe_size(to_intel_crtc(set->crtc));
12729
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012730 primary_plane_was_visible = primary_plane_visible(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012731
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012732 ret = intel_set_mode_with_config(set->crtc, pipe_config);
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012733
12734 if (ret == 0 &&
12735 pipe_config->base.enable &&
12736 pipe_config->base.planes_changed &&
12737 !needs_modeset(&pipe_config->base)) {
12738 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012739
12740 /*
12741 * We need to make sure the primary plane is re-enabled if it
12742 * has previously been turned off.
12743 */
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012744 if (ret == 0 && !primary_plane_was_visible &&
12745 primary_plane_visible(set->crtc)) {
Matt Roper3b150f02014-05-29 08:06:53 -070012746 WARN_ON(!intel_crtc->active);
Maarten Lankhorst87d43002015-04-21 17:12:54 +030012747 intel_post_enable_primary(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012748 }
12749
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012750 /*
12751 * In the fastboot case this may be our only check of the
12752 * state after boot. It would be better to only do it on
12753 * the first update, but we don't have a nice way of doing that
12754 * (and really, set_config isn't used much for high freq page
12755 * flipping, so increasing its cost here shouldn't be a big
12756 * deal).
12757 */
Jani Nikulad330a952014-01-21 11:24:25 +020012758 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012759 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020012760 }
12761
Chris Wilson2d05eae2013-05-03 17:36:25 +010012762 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020012763 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12764 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010012765 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012766
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012767out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012768 if (ret)
12769 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020012770 return ret;
12771}
12772
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012773static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012774 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020012775 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012776 .destroy = intel_crtc_destroy,
12777 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080012778 .atomic_duplicate_state = intel_crtc_duplicate_state,
12779 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012780};
12781
Daniel Vetter53589012013-06-05 13:34:16 +020012782static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
12783 struct intel_shared_dpll *pll,
12784 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012785{
Daniel Vetter53589012013-06-05 13:34:16 +020012786 uint32_t val;
12787
Daniel Vetterf458ebb2014-09-30 10:56:39 +020012788 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030012789 return false;
12790
Daniel Vetter53589012013-06-05 13:34:16 +020012791 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020012792 hw_state->dpll = val;
12793 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
12794 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020012795
12796 return val & DPLL_VCO_ENABLE;
12797}
12798
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012799static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
12800 struct intel_shared_dpll *pll)
12801{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012802 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
12803 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012804}
12805
Daniel Vettere7b903d2013-06-05 13:34:14 +020012806static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
12807 struct intel_shared_dpll *pll)
12808{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012809 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020012810 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020012811
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012812 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012813
12814 /* Wait for the clocks to stabilize. */
12815 POSTING_READ(PCH_DPLL(pll->id));
12816 udelay(150);
12817
12818 /* The pixel multiplier can only be updated once the
12819 * DPLL is enabled and the clocks are stable.
12820 *
12821 * So write it again.
12822 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012823 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012824 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012825 udelay(200);
12826}
12827
12828static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
12829 struct intel_shared_dpll *pll)
12830{
12831 struct drm_device *dev = dev_priv->dev;
12832 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020012833
12834 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012835 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020012836 if (intel_crtc_to_shared_dpll(crtc) == pll)
12837 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
12838 }
12839
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012840 I915_WRITE(PCH_DPLL(pll->id), 0);
12841 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012842 udelay(200);
12843}
12844
Daniel Vetter46edb022013-06-05 13:34:12 +020012845static char *ibx_pch_dpll_names[] = {
12846 "PCH DPLL A",
12847 "PCH DPLL B",
12848};
12849
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012850static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012851{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012852 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012853 int i;
12854
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012855 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012856
Daniel Vettere72f9fb2013-06-05 13:34:06 +020012857 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020012858 dev_priv->shared_dplls[i].id = i;
12859 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012860 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020012861 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
12862 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020012863 dev_priv->shared_dplls[i].get_hw_state =
12864 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012865 }
12866}
12867
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012868static void intel_shared_dpll_init(struct drm_device *dev)
12869{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012870 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012871
Daniel Vetter9cd86932014-06-25 22:01:57 +030012872 if (HAS_DDI(dev))
12873 intel_ddi_pll_init(dev);
12874 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012875 ibx_pch_dpll_init(dev);
12876 else
12877 dev_priv->num_shared_dpll = 0;
12878
12879 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012880}
12881
Matt Roper6beb8c232014-12-01 15:40:14 -080012882/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000012883 * intel_wm_need_update - Check whether watermarks need updating
12884 * @plane: drm plane
12885 * @state: new plane state
12886 *
12887 * Check current plane state versus the new one to determine whether
12888 * watermarks need to be recalculated.
12889 *
12890 * Returns true or false.
12891 */
12892bool intel_wm_need_update(struct drm_plane *plane,
12893 struct drm_plane_state *state)
12894{
12895 /* Update watermarks on tiling changes. */
12896 if (!plane->state->fb || !state->fb ||
12897 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
12898 plane->state->rotation != state->rotation)
12899 return true;
12900
12901 return false;
12902}
12903
12904/**
Matt Roper6beb8c232014-12-01 15:40:14 -080012905 * intel_prepare_plane_fb - Prepare fb for usage on plane
12906 * @plane: drm plane to prepare for
12907 * @fb: framebuffer to prepare for presentation
12908 *
12909 * Prepares a framebuffer for usage on a display plane. Generally this
12910 * involves pinning the underlying object and updating the frontbuffer tracking
12911 * bits. Some older platforms need special physical address handling for
12912 * cursor planes.
12913 *
12914 * Returns 0 on success, negative error code on failure.
12915 */
12916int
12917intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000012918 struct drm_framebuffer *fb,
12919 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070012920{
12921 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080012922 struct intel_plane *intel_plane = to_intel_plane(plane);
12923 enum pipe pipe = intel_plane->pipe;
12924 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12925 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
12926 unsigned frontbuffer_bits = 0;
12927 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070012928
Matt Roperea2c67b2014-12-23 10:41:52 -080012929 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070012930 return 0;
12931
Matt Roper6beb8c232014-12-01 15:40:14 -080012932 switch (plane->type) {
12933 case DRM_PLANE_TYPE_PRIMARY:
12934 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
12935 break;
12936 case DRM_PLANE_TYPE_CURSOR:
12937 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
12938 break;
12939 case DRM_PLANE_TYPE_OVERLAY:
12940 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
12941 break;
12942 }
Matt Roper465c1202014-05-29 08:06:54 -070012943
Matt Roper4c345742014-07-09 16:22:10 -070012944 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070012945
Matt Roper6beb8c232014-12-01 15:40:14 -080012946 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
12947 INTEL_INFO(dev)->cursor_needs_physical) {
12948 int align = IS_I830(dev) ? 16 * 1024 : 256;
12949 ret = i915_gem_object_attach_phys(obj, align);
12950 if (ret)
12951 DRM_DEBUG_KMS("failed to attach phys object\n");
12952 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000012953 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080012954 }
12955
12956 if (ret == 0)
12957 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
12958
12959 mutex_unlock(&dev->struct_mutex);
12960
12961 return ret;
12962}
12963
Matt Roper38f3ce32014-12-02 07:45:25 -080012964/**
12965 * intel_cleanup_plane_fb - Cleans up an fb after plane use
12966 * @plane: drm plane to clean up for
12967 * @fb: old framebuffer that was on plane
12968 *
12969 * Cleans up a framebuffer that has just been removed from a plane.
12970 */
12971void
12972intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000012973 struct drm_framebuffer *fb,
12974 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080012975{
12976 struct drm_device *dev = plane->dev;
12977 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12978
12979 if (WARN_ON(!obj))
12980 return;
12981
12982 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
12983 !INTEL_INFO(dev)->cursor_needs_physical) {
12984 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000012985 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080012986 mutex_unlock(&dev->struct_mutex);
12987 }
Matt Roper465c1202014-05-29 08:06:54 -070012988}
12989
Chandra Konduru6156a452015-04-27 13:48:39 -070012990int
12991skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
12992{
12993 int max_scale;
12994 struct drm_device *dev;
12995 struct drm_i915_private *dev_priv;
12996 int crtc_clock, cdclk;
12997
12998 if (!intel_crtc || !crtc_state)
12999 return DRM_PLANE_HELPER_NO_SCALING;
13000
13001 dev = intel_crtc->base.dev;
13002 dev_priv = dev->dev_private;
13003 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13004 cdclk = dev_priv->display.get_display_clock_speed(dev);
13005
13006 if (!crtc_clock || !cdclk)
13007 return DRM_PLANE_HELPER_NO_SCALING;
13008
13009 /*
13010 * skl max scale is lower of:
13011 * close to 3 but not 3, -1 is for that purpose
13012 * or
13013 * cdclk/crtc_clock
13014 */
13015 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13016
13017 return max_scale;
13018}
13019
Matt Roper465c1202014-05-29 08:06:54 -070013020static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013021intel_check_primary_plane(struct drm_plane *plane,
13022 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013023{
Matt Roper32b7eee2014-12-24 07:59:06 -080013024 struct drm_device *dev = plane->dev;
13025 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013026 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013027 struct intel_crtc *intel_crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013028 struct intel_crtc_state *crtc_state;
Matt Roper2b875c22014-12-01 15:40:13 -080013029 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013030 struct drm_rect *dest = &state->dst;
13031 struct drm_rect *src = &state->src;
13032 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013033 bool can_position = false;
Chandra Konduru6156a452015-04-27 13:48:39 -070013034 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13035 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013036 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013037
Matt Roperea2c67b2014-12-23 10:41:52 -080013038 crtc = crtc ? crtc : plane->crtc;
13039 intel_crtc = to_intel_crtc(crtc);
Chandra Konduru6156a452015-04-27 13:48:39 -070013040 crtc_state = state->base.state ?
13041 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
Matt Roperea2c67b2014-12-23 10:41:52 -080013042
Chandra Konduru6156a452015-04-27 13:48:39 -070013043 if (INTEL_INFO(dev)->gen >= 9) {
13044 min_scale = 1;
13045 max_scale = skl_max_scale(intel_crtc, crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013046 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013047 }
Sonika Jindald8106362015-04-10 14:37:28 +053013048
Matt Roperc59cb172014-12-01 15:40:16 -080013049 ret = drm_plane_helper_check_update(plane, crtc, fb,
13050 src, dest, clip,
Chandra Konduru6156a452015-04-27 13:48:39 -070013051 min_scale,
13052 max_scale,
Sonika Jindald8106362015-04-10 14:37:28 +053013053 can_position, true,
13054 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013055 if (ret)
13056 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013057
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013058 if (intel_crtc->active) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013059 struct intel_plane_state *old_state =
13060 to_intel_plane_state(plane->state);
13061
Matt Roper32b7eee2014-12-24 07:59:06 -080013062 intel_crtc->atomic.wait_for_flips = true;
13063
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013064 /*
13065 * FBC does not work on some platforms for rotated
13066 * planes, so disable it when rotation is not 0 and
13067 * update it when rotation is set back to 0.
13068 *
13069 * FIXME: This is redundant with the fbc update done in
13070 * the primary plane enable function except that that
13071 * one is done too late. We eventually need to unify
13072 * this.
13073 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013074 if (state->visible &&
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013075 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013076 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013077 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013078 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013079 }
13080
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013081 if (state->visible && !old_state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013082 /*
13083 * BDW signals flip done immediately if the plane
13084 * is disabled, even if the plane enable is already
13085 * armed to occur at the next vblank :(
13086 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013087 if (IS_BROADWELL(dev))
Matt Roper32b7eee2014-12-24 07:59:06 -080013088 intel_crtc->atomic.wait_vblank = true;
13089 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013090
Matt Roper32b7eee2014-12-24 07:59:06 -080013091 intel_crtc->atomic.fb_bits |=
13092 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13093
13094 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013095
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013096 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013097 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013098 }
13099
Chandra Konduru6156a452015-04-27 13:48:39 -070013100 if (INTEL_INFO(dev)->gen >= 9) {
13101 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13102 to_intel_plane(plane), state, 0);
13103 if (ret)
13104 return ret;
13105 }
13106
Matt Roperc59cb172014-12-01 15:40:16 -080013107 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013108}
13109
Sonika Jindal48404c12014-08-22 14:06:04 +053013110static void
13111intel_commit_primary_plane(struct drm_plane *plane,
13112 struct intel_plane_state *state)
13113{
Matt Roper2b875c22014-12-01 15:40:13 -080013114 struct drm_crtc *crtc = state->base.crtc;
13115 struct drm_framebuffer *fb = state->base.fb;
13116 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013117 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013118 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013119 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013120
Matt Roperea2c67b2014-12-23 10:41:52 -080013121 crtc = crtc ? crtc : plane->crtc;
13122 intel_crtc = to_intel_crtc(crtc);
13123
Matt Ropercf4c7c12014-12-04 10:27:42 -080013124 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013125 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013126 crtc->y = src->y1 >> 16;
13127
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013128 if (intel_crtc->active) {
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013129 if (state->visible)
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013130 /* FIXME: kill this fastboot hack */
13131 intel_update_pipe_size(intel_crtc);
13132
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013133 dev_priv->display.update_primary_plane(crtc, plane->fb,
13134 crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013135 }
13136}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013137
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013138static void
13139intel_disable_primary_plane(struct drm_plane *plane,
13140 struct drm_crtc *crtc,
13141 bool force)
13142{
13143 struct drm_device *dev = plane->dev;
13144 struct drm_i915_private *dev_priv = dev->dev_private;
13145
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013146 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13147}
13148
Matt Roper32b7eee2014-12-24 07:59:06 -080013149static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13150{
13151 struct drm_device *dev = crtc->dev;
13152 struct drm_i915_private *dev_priv = dev->dev_private;
13153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013154 struct intel_plane *intel_plane;
13155 struct drm_plane *p;
13156 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013157
Matt Roperea2c67b2014-12-23 10:41:52 -080013158 /* Track fb's for any planes being disabled */
13159 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13160 intel_plane = to_intel_plane(p);
13161
13162 if (intel_crtc->atomic.disabled_planes &
13163 (1 << drm_plane_index(p))) {
13164 switch (p->type) {
13165 case DRM_PLANE_TYPE_PRIMARY:
13166 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13167 break;
13168 case DRM_PLANE_TYPE_CURSOR:
13169 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13170 break;
13171 case DRM_PLANE_TYPE_OVERLAY:
13172 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13173 break;
13174 }
13175
13176 mutex_lock(&dev->struct_mutex);
13177 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13178 mutex_unlock(&dev->struct_mutex);
13179 }
13180 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013181
Matt Roper32b7eee2014-12-24 07:59:06 -080013182 if (intel_crtc->atomic.wait_for_flips)
13183 intel_crtc_wait_for_pending_flips(crtc);
13184
13185 if (intel_crtc->atomic.disable_fbc)
13186 intel_fbc_disable(dev);
13187
13188 if (intel_crtc->atomic.pre_disable_primary)
13189 intel_pre_disable_primary(crtc);
13190
13191 if (intel_crtc->atomic.update_wm)
13192 intel_update_watermarks(crtc);
13193
13194 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013195
13196 /* Perform vblank evasion around commit operation */
13197 if (intel_crtc->active)
13198 intel_crtc->atomic.evade =
13199 intel_pipe_update_start(intel_crtc,
13200 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013201}
13202
13203static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13204{
13205 struct drm_device *dev = crtc->dev;
13206 struct drm_i915_private *dev_priv = dev->dev_private;
13207 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13208 struct drm_plane *p;
13209
Matt Roperc34c9ee2014-12-23 10:41:50 -080013210 if (intel_crtc->atomic.evade)
13211 intel_pipe_update_end(intel_crtc,
13212 intel_crtc->atomic.start_vbl_count);
13213
Matt Roper32b7eee2014-12-24 07:59:06 -080013214 intel_runtime_pm_put(dev_priv);
13215
13216 if (intel_crtc->atomic.wait_vblank)
13217 intel_wait_for_vblank(dev, intel_crtc->pipe);
13218
13219 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13220
13221 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013222 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013223 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013224 mutex_unlock(&dev->struct_mutex);
13225 }
Matt Roper465c1202014-05-29 08:06:54 -070013226
Matt Roper32b7eee2014-12-24 07:59:06 -080013227 if (intel_crtc->atomic.post_enable_primary)
13228 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013229
Matt Roper32b7eee2014-12-24 07:59:06 -080013230 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13231 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13232 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13233 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013234
Matt Roper32b7eee2014-12-24 07:59:06 -080013235 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013236}
13237
Matt Ropercf4c7c12014-12-04 10:27:42 -080013238/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013239 * intel_plane_destroy - destroy a plane
13240 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013241 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013242 * Common destruction function for all types of planes (primary, cursor,
13243 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013244 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013245void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013246{
13247 struct intel_plane *intel_plane = to_intel_plane(plane);
13248 drm_plane_cleanup(plane);
13249 kfree(intel_plane);
13250}
13251
Matt Roper65a3fea2015-01-21 16:35:42 -080013252const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013253 .update_plane = drm_atomic_helper_update_plane,
13254 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013255 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013256 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013257 .atomic_get_property = intel_plane_atomic_get_property,
13258 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013259 .atomic_duplicate_state = intel_plane_duplicate_state,
13260 .atomic_destroy_state = intel_plane_destroy_state,
13261
Matt Roper465c1202014-05-29 08:06:54 -070013262};
13263
13264static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13265 int pipe)
13266{
13267 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013268 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013269 const uint32_t *intel_primary_formats;
13270 int num_formats;
13271
13272 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13273 if (primary == NULL)
13274 return NULL;
13275
Matt Roper8e7d6882015-01-21 16:35:41 -080013276 state = intel_create_plane_state(&primary->base);
13277 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013278 kfree(primary);
13279 return NULL;
13280 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013281 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013282
Matt Roper465c1202014-05-29 08:06:54 -070013283 primary->can_scale = false;
13284 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013285 if (INTEL_INFO(dev)->gen >= 9) {
13286 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013287 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013288 }
Matt Roper465c1202014-05-29 08:06:54 -070013289 primary->pipe = pipe;
13290 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013291 primary->check_plane = intel_check_primary_plane;
13292 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013293 primary->disable_plane = intel_disable_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013294 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013295 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13296 primary->plane = !pipe;
13297
13298 if (INTEL_INFO(dev)->gen <= 3) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013299 intel_primary_formats = i8xx_primary_formats;
13300 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013301 } else {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013302 intel_primary_formats = i965_primary_formats;
13303 num_formats = ARRAY_SIZE(i965_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013304 }
13305
13306 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013307 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013308 intel_primary_formats, num_formats,
13309 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013310
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013311 if (INTEL_INFO(dev)->gen >= 4)
13312 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013313
Matt Roperea2c67b2014-12-23 10:41:52 -080013314 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13315
Matt Roper465c1202014-05-29 08:06:54 -070013316 return &primary->base;
13317}
13318
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013319void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13320{
13321 if (!dev->mode_config.rotation_property) {
13322 unsigned long flags = BIT(DRM_ROTATE_0) |
13323 BIT(DRM_ROTATE_180);
13324
13325 if (INTEL_INFO(dev)->gen >= 9)
13326 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13327
13328 dev->mode_config.rotation_property =
13329 drm_mode_create_rotation_property(dev, flags);
13330 }
13331 if (dev->mode_config.rotation_property)
13332 drm_object_attach_property(&plane->base.base,
13333 dev->mode_config.rotation_property,
13334 plane->base.state->rotation);
13335}
13336
Matt Roper3d7d6512014-06-10 08:28:13 -070013337static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013338intel_check_cursor_plane(struct drm_plane *plane,
13339 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013340{
Matt Roper2b875c22014-12-01 15:40:13 -080013341 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013342 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013343 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013344 struct drm_rect *dest = &state->dst;
13345 struct drm_rect *src = &state->src;
13346 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013347 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013348 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013349 unsigned stride;
13350 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013351
Matt Roperea2c67b2014-12-23 10:41:52 -080013352 crtc = crtc ? crtc : plane->crtc;
13353 intel_crtc = to_intel_crtc(crtc);
13354
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013355 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013356 src, dest, clip,
13357 DRM_PLANE_HELPER_NO_SCALING,
13358 DRM_PLANE_HELPER_NO_SCALING,
13359 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013360 if (ret)
13361 return ret;
13362
13363
13364 /* if we want to turn off the cursor ignore width and height */
13365 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013366 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013367
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013368 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013369 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13370 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13371 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013372 return -EINVAL;
13373 }
13374
Matt Roperea2c67b2014-12-23 10:41:52 -080013375 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13376 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013377 DRM_DEBUG_KMS("buffer is too small\n");
13378 return -ENOMEM;
13379 }
13380
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013381 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013382 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13383 ret = -EINVAL;
13384 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013385
Matt Roper32b7eee2014-12-24 07:59:06 -080013386finish:
13387 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013388 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013389 intel_crtc->atomic.update_wm = true;
13390
13391 intel_crtc->atomic.fb_bits |=
13392 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13393 }
13394
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013395 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013396}
13397
Matt Roperf4a2cf22014-12-01 15:40:12 -080013398static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013399intel_disable_cursor_plane(struct drm_plane *plane,
13400 struct drm_crtc *crtc,
13401 bool force)
13402{
13403 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13404
13405 if (!force) {
13406 plane->fb = NULL;
13407 intel_crtc->cursor_bo = NULL;
13408 intel_crtc->cursor_addr = 0;
13409 }
13410
13411 intel_crtc_update_cursor(crtc, false);
13412}
13413
13414static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013415intel_commit_cursor_plane(struct drm_plane *plane,
13416 struct intel_plane_state *state)
13417{
Matt Roper2b875c22014-12-01 15:40:13 -080013418 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013419 struct drm_device *dev = plane->dev;
13420 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013421 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013422 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013423
Matt Roperea2c67b2014-12-23 10:41:52 -080013424 crtc = crtc ? crtc : plane->crtc;
13425 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013426
Matt Roperea2c67b2014-12-23 10:41:52 -080013427 plane->fb = state->base.fb;
13428 crtc->cursor_x = state->base.crtc_x;
13429 crtc->cursor_y = state->base.crtc_y;
13430
Gustavo Padovana912f122014-12-01 15:40:10 -080013431 if (intel_crtc->cursor_bo == obj)
13432 goto update;
13433
Matt Roperf4a2cf22014-12-01 15:40:12 -080013434 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013435 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013436 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013437 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013438 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013439 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013440
Gustavo Padovana912f122014-12-01 15:40:10 -080013441 intel_crtc->cursor_addr = addr;
13442 intel_crtc->cursor_bo = obj;
13443update:
Gustavo Padovana912f122014-12-01 15:40:10 -080013444
Matt Roper32b7eee2014-12-24 07:59:06 -080013445 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013446 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013447}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013448
Matt Roper3d7d6512014-06-10 08:28:13 -070013449static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13450 int pipe)
13451{
13452 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013453 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013454
13455 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13456 if (cursor == NULL)
13457 return NULL;
13458
Matt Roper8e7d6882015-01-21 16:35:41 -080013459 state = intel_create_plane_state(&cursor->base);
13460 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013461 kfree(cursor);
13462 return NULL;
13463 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013464 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013465
Matt Roper3d7d6512014-06-10 08:28:13 -070013466 cursor->can_scale = false;
13467 cursor->max_downscale = 1;
13468 cursor->pipe = pipe;
13469 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013470 cursor->check_plane = intel_check_cursor_plane;
13471 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013472 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013473
13474 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013475 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013476 intel_cursor_formats,
13477 ARRAY_SIZE(intel_cursor_formats),
13478 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013479
13480 if (INTEL_INFO(dev)->gen >= 4) {
13481 if (!dev->mode_config.rotation_property)
13482 dev->mode_config.rotation_property =
13483 drm_mode_create_rotation_property(dev,
13484 BIT(DRM_ROTATE_0) |
13485 BIT(DRM_ROTATE_180));
13486 if (dev->mode_config.rotation_property)
13487 drm_object_attach_property(&cursor->base.base,
13488 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013489 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013490 }
13491
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013492 if (INTEL_INFO(dev)->gen >=9)
13493 state->scaler_id = -1;
13494
Matt Roperea2c67b2014-12-23 10:41:52 -080013495 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13496
Matt Roper3d7d6512014-06-10 08:28:13 -070013497 return &cursor->base;
13498}
13499
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013500static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13501 struct intel_crtc_state *crtc_state)
13502{
13503 int i;
13504 struct intel_scaler *intel_scaler;
13505 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13506
13507 for (i = 0; i < intel_crtc->num_scalers; i++) {
13508 intel_scaler = &scaler_state->scalers[i];
13509 intel_scaler->in_use = 0;
13510 intel_scaler->id = i;
13511
13512 intel_scaler->mode = PS_SCALER_MODE_DYN;
13513 }
13514
13515 scaler_state->scaler_id = -1;
13516}
13517
Hannes Ederb358d0a2008-12-18 21:18:47 +010013518static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013519{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013520 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013521 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013522 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013523 struct drm_plane *primary = NULL;
13524 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013525 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013526
Daniel Vetter955382f2013-09-19 14:05:45 +020013527 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013528 if (intel_crtc == NULL)
13529 return;
13530
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013531 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13532 if (!crtc_state)
13533 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030013534 intel_crtc->config = crtc_state;
13535 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080013536 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013537
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013538 /* initialize shared scalers */
13539 if (INTEL_INFO(dev)->gen >= 9) {
13540 if (pipe == PIPE_C)
13541 intel_crtc->num_scalers = 1;
13542 else
13543 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13544
13545 skl_init_scalers(dev, intel_crtc, crtc_state);
13546 }
13547
Matt Roper465c1202014-05-29 08:06:54 -070013548 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013549 if (!primary)
13550 goto fail;
13551
13552 cursor = intel_cursor_plane_create(dev, pipe);
13553 if (!cursor)
13554 goto fail;
13555
Matt Roper465c1202014-05-29 08:06:54 -070013556 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013557 cursor, &intel_crtc_funcs);
13558 if (ret)
13559 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013560
13561 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013562 for (i = 0; i < 256; i++) {
13563 intel_crtc->lut_r[i] = i;
13564 intel_crtc->lut_g[i] = i;
13565 intel_crtc->lut_b[i] = i;
13566 }
13567
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013568 /*
13569 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013570 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013571 */
Jesse Barnes80824002009-09-10 15:28:06 -070013572 intel_crtc->pipe = pipe;
13573 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013574 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013575 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013576 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013577 }
13578
Chris Wilson4b0e3332014-05-30 16:35:26 +030013579 intel_crtc->cursor_base = ~0;
13580 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013581 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013582
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013583 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13584 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13585 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13586 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13587
Jesse Barnes79e53942008-11-07 14:24:08 -080013588 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013589
13590 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013591 return;
13592
13593fail:
13594 if (primary)
13595 drm_plane_cleanup(primary);
13596 if (cursor)
13597 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013598 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013599 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013600}
13601
Jesse Barnes752aa882013-10-31 18:55:49 +020013602enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13603{
13604 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013605 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013606
Rob Clark51fd3712013-11-19 12:10:12 -050013607 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013608
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013609 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013610 return INVALID_PIPE;
13611
13612 return to_intel_crtc(encoder->crtc)->pipe;
13613}
13614
Carl Worth08d7b3d2009-04-29 14:43:54 -070013615int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013616 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013617{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013618 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013619 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013620 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013621
Rob Clark7707e652014-07-17 23:30:04 -040013622 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013623
Rob Clark7707e652014-07-17 23:30:04 -040013624 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013625 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013626 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013627 }
13628
Rob Clark7707e652014-07-17 23:30:04 -040013629 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013630 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013631
Daniel Vetterc05422d2009-08-11 16:05:30 +020013632 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013633}
13634
Daniel Vetter66a92782012-07-12 20:08:18 +020013635static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013636{
Daniel Vetter66a92782012-07-12 20:08:18 +020013637 struct drm_device *dev = encoder->base.dev;
13638 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013639 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013640 int entry = 0;
13641
Damien Lespiaub2784e12014-08-05 11:29:37 +010013642 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013643 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013644 index_mask |= (1 << entry);
13645
Jesse Barnes79e53942008-11-07 14:24:08 -080013646 entry++;
13647 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013648
Jesse Barnes79e53942008-11-07 14:24:08 -080013649 return index_mask;
13650}
13651
Chris Wilson4d302442010-12-14 19:21:29 +000013652static bool has_edp_a(struct drm_device *dev)
13653{
13654 struct drm_i915_private *dev_priv = dev->dev_private;
13655
13656 if (!IS_MOBILE(dev))
13657 return false;
13658
13659 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13660 return false;
13661
Damien Lespiaue3589902014-02-07 19:12:50 +000013662 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013663 return false;
13664
13665 return true;
13666}
13667
Jesse Barnes84b4e042014-06-25 08:24:29 -070013668static bool intel_crt_present(struct drm_device *dev)
13669{
13670 struct drm_i915_private *dev_priv = dev->dev_private;
13671
Damien Lespiau884497e2013-12-03 13:56:23 +000013672 if (INTEL_INFO(dev)->gen >= 9)
13673 return false;
13674
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013675 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013676 return false;
13677
13678 if (IS_CHERRYVIEW(dev))
13679 return false;
13680
13681 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13682 return false;
13683
13684 return true;
13685}
13686
Jesse Barnes79e53942008-11-07 14:24:08 -080013687static void intel_setup_outputs(struct drm_device *dev)
13688{
Eric Anholt725e30a2009-01-22 13:01:02 -080013689 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013690 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013691 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013692
Daniel Vetterc9093352013-06-06 22:22:47 +020013693 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013694
Jesse Barnes84b4e042014-06-25 08:24:29 -070013695 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013696 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013697
Vandana Kannanc776eb22014-08-19 12:05:01 +053013698 if (IS_BROXTON(dev)) {
13699 /*
13700 * FIXME: Broxton doesn't support port detection via the
13701 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13702 * detect the ports.
13703 */
13704 intel_ddi_init(dev, PORT_A);
13705 intel_ddi_init(dev, PORT_B);
13706 intel_ddi_init(dev, PORT_C);
13707 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013708 int found;
13709
Jesse Barnesde31fac2015-03-06 15:53:32 -080013710 /*
13711 * Haswell uses DDI functions to detect digital outputs.
13712 * On SKL pre-D0 the strap isn't connected, so we assume
13713 * it's there.
13714 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013715 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013716 /* WaIgnoreDDIAStrap: skl */
13717 if (found ||
13718 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013719 intel_ddi_init(dev, PORT_A);
13720
13721 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13722 * register */
13723 found = I915_READ(SFUSE_STRAP);
13724
13725 if (found & SFUSE_STRAP_DDIB_DETECTED)
13726 intel_ddi_init(dev, PORT_B);
13727 if (found & SFUSE_STRAP_DDIC_DETECTED)
13728 intel_ddi_init(dev, PORT_C);
13729 if (found & SFUSE_STRAP_DDID_DETECTED)
13730 intel_ddi_init(dev, PORT_D);
13731 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013732 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013733 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013734
13735 if (has_edp_a(dev))
13736 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013737
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013738 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013739 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013740 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013741 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013742 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013743 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013744 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013745 }
13746
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013747 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013748 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013749
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013750 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013751 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013752
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013753 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013754 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013755
Daniel Vetter270b3042012-10-27 15:52:05 +020013756 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013757 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013758 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013759 /*
13760 * The DP_DETECTED bit is the latched state of the DDC
13761 * SDA pin at boot. However since eDP doesn't require DDC
13762 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13763 * eDP ports may have been muxed to an alternate function.
13764 * Thus we can't rely on the DP_DETECTED bit alone to detect
13765 * eDP ports. Consult the VBT as well as DP_DETECTED to
13766 * detect eDP ports.
13767 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013768 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13769 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013770 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13771 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013772 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13773 intel_dp_is_edp(dev, PORT_B))
13774 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013775
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013776 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13777 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070013778 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13779 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013780 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13781 intel_dp_is_edp(dev, PORT_C))
13782 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053013783
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013784 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013785 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013786 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
13787 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013788 /* eDP not supported on port D, so don't check VBT */
13789 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
13790 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013791 }
13792
Jani Nikula3cfca972013-08-27 15:12:26 +030013793 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080013794 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013795 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080013796
Paulo Zanonie2debe92013-02-18 19:00:27 -030013797 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013798 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013799 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013800 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
13801 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013802 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013803 }
Ma Ling27185ae2009-08-24 13:50:23 +080013804
Imre Deake7281ea2013-05-08 13:14:08 +030013805 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013806 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080013807 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013808
13809 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013810
Paulo Zanonie2debe92013-02-18 19:00:27 -030013811 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013812 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013813 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013814 }
Ma Ling27185ae2009-08-24 13:50:23 +080013815
Paulo Zanonie2debe92013-02-18 19:00:27 -030013816 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013817
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013818 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
13819 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013820 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013821 }
Imre Deake7281ea2013-05-08 13:14:08 +030013822 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013823 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080013824 }
Ma Ling27185ae2009-08-24 13:50:23 +080013825
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013826 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030013827 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013828 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070013829 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013830 intel_dvo_init(dev);
13831
Zhenyu Wang103a1962009-11-27 11:44:36 +080013832 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013833 intel_tv_init(dev);
13834
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080013835 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070013836
Damien Lespiaub2784e12014-08-05 11:29:37 +010013837 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010013838 encoder->base.possible_crtcs = encoder->crtc_mask;
13839 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020013840 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080013841 }
Chris Wilson47356eb2011-01-11 17:06:04 +000013842
Paulo Zanonidde86e22012-12-01 12:04:25 -020013843 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020013844
13845 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013846}
13847
13848static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
13849{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013850 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080013851 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080013852
Daniel Vetteref2d6332014-02-10 18:00:38 +010013853 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013854 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010013855 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013856 drm_gem_object_unreference(&intel_fb->obj->base);
13857 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013858 kfree(intel_fb);
13859}
13860
13861static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000013862 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080013863 unsigned int *handle)
13864{
13865 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000013866 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080013867
Chris Wilson05394f32010-11-08 19:18:58 +000013868 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080013869}
13870
13871static const struct drm_framebuffer_funcs intel_fb_funcs = {
13872 .destroy = intel_user_framebuffer_destroy,
13873 .create_handle = intel_user_framebuffer_create_handle,
13874};
13875
Damien Lespiaub3218032015-02-27 11:15:18 +000013876static
13877u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
13878 uint32_t pixel_format)
13879{
13880 u32 gen = INTEL_INFO(dev)->gen;
13881
13882 if (gen >= 9) {
13883 /* "The stride in bytes must not exceed the of the size of 8K
13884 * pixels and 32K bytes."
13885 */
13886 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
13887 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
13888 return 32*1024;
13889 } else if (gen >= 4) {
13890 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13891 return 16*1024;
13892 else
13893 return 32*1024;
13894 } else if (gen >= 3) {
13895 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13896 return 8*1024;
13897 else
13898 return 16*1024;
13899 } else {
13900 /* XXX DSPC is limited to 4k tiled */
13901 return 8*1024;
13902 }
13903}
13904
Daniel Vetterb5ea6422014-03-02 21:18:00 +010013905static int intel_framebuffer_init(struct drm_device *dev,
13906 struct intel_framebuffer *intel_fb,
13907 struct drm_mode_fb_cmd2 *mode_cmd,
13908 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080013909{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000013910 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080013911 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000013912 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080013913
Daniel Vetterdd4916c2013-10-09 21:23:51 +020013914 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
13915
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013916 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
13917 /* Enforce that fb modifier and tiling mode match, but only for
13918 * X-tiled. This is needed for FBC. */
13919 if (!!(obj->tiling_mode == I915_TILING_X) !=
13920 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
13921 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
13922 return -EINVAL;
13923 }
13924 } else {
13925 if (obj->tiling_mode == I915_TILING_X)
13926 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
13927 else if (obj->tiling_mode == I915_TILING_Y) {
13928 DRM_DEBUG("No Y tiling for legacy addfb\n");
13929 return -EINVAL;
13930 }
13931 }
13932
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000013933 /* Passed in modifier sanity checking. */
13934 switch (mode_cmd->modifier[0]) {
13935 case I915_FORMAT_MOD_Y_TILED:
13936 case I915_FORMAT_MOD_Yf_TILED:
13937 if (INTEL_INFO(dev)->gen < 9) {
13938 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
13939 mode_cmd->modifier[0]);
13940 return -EINVAL;
13941 }
13942 case DRM_FORMAT_MOD_NONE:
13943 case I915_FORMAT_MOD_X_TILED:
13944 break;
13945 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070013946 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
13947 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010013948 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013949 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013950
Damien Lespiaub3218032015-02-27 11:15:18 +000013951 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
13952 mode_cmd->pixel_format);
13953 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
13954 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
13955 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010013956 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013957 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013958
Damien Lespiaub3218032015-02-27 11:15:18 +000013959 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
13960 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010013961 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000013962 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
13963 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013964 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010013965 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013966 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013967 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013968
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013969 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013970 mode_cmd->pitches[0] != obj->stride) {
13971 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
13972 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013973 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013974 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013975
Ville Syrjälä57779d02012-10-31 17:50:14 +020013976 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013977 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020013978 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020013979 case DRM_FORMAT_RGB565:
13980 case DRM_FORMAT_XRGB8888:
13981 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020013982 break;
13983 case DRM_FORMAT_XRGB1555:
13984 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013985 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013986 DRM_DEBUG("unsupported pixel format: %s\n",
13987 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013988 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013989 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020013990 break;
13991 case DRM_FORMAT_XBGR8888:
13992 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020013993 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020013994 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013995 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013996 DRM_DEBUG("unsupported pixel format: %s\n",
13997 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013998 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013999 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014000 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014001 case DRM_FORMAT_ABGR2101010:
14002 if (!IS_VALLEYVIEW(dev)) {
14003 DRM_DEBUG("unsupported pixel format: %s\n",
14004 drm_get_format_name(mode_cmd->pixel_format));
14005 return -EINVAL;
14006 }
14007 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014008 case DRM_FORMAT_YUYV:
14009 case DRM_FORMAT_UYVY:
14010 case DRM_FORMAT_YVYU:
14011 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014012 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014013 DRM_DEBUG("unsupported pixel format: %s\n",
14014 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014015 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014016 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014017 break;
14018 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014019 DRM_DEBUG("unsupported pixel format: %s\n",
14020 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014021 return -EINVAL;
14022 }
14023
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014024 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14025 if (mode_cmd->offsets[0] != 0)
14026 return -EINVAL;
14027
Damien Lespiauec2c9812015-01-20 12:51:45 +000014028 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014029 mode_cmd->pixel_format,
14030 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014031 /* FIXME drm helper for size checks (especially planar formats)? */
14032 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14033 return -EINVAL;
14034
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014035 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14036 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014037 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014038
Jesse Barnes79e53942008-11-07 14:24:08 -080014039 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14040 if (ret) {
14041 DRM_ERROR("framebuffer init failed %d\n", ret);
14042 return ret;
14043 }
14044
Jesse Barnes79e53942008-11-07 14:24:08 -080014045 return 0;
14046}
14047
Jesse Barnes79e53942008-11-07 14:24:08 -080014048static struct drm_framebuffer *
14049intel_user_framebuffer_create(struct drm_device *dev,
14050 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014051 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014052{
Chris Wilson05394f32010-11-08 19:18:58 +000014053 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014054
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014055 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14056 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014057 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014058 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014059
Chris Wilsond2dff872011-04-19 08:36:26 +010014060 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014061}
14062
Daniel Vetter4520f532013-10-09 09:18:51 +020014063#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014064static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014065{
14066}
14067#endif
14068
Jesse Barnes79e53942008-11-07 14:24:08 -080014069static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014070 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014071 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014072 .atomic_check = intel_atomic_check,
14073 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080014074};
14075
Jesse Barnese70236a2009-09-21 10:42:27 -070014076/* Set up chip specific display functions */
14077static void intel_init_display(struct drm_device *dev)
14078{
14079 struct drm_i915_private *dev_priv = dev->dev_private;
14080
Daniel Vetteree9300b2013-06-03 22:40:22 +020014081 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14082 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014083 else if (IS_CHERRYVIEW(dev))
14084 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014085 else if (IS_VALLEYVIEW(dev))
14086 dev_priv->display.find_dpll = vlv_find_best_dpll;
14087 else if (IS_PINEVIEW(dev))
14088 dev_priv->display.find_dpll = pnv_find_best_dpll;
14089 else
14090 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14091
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014092 if (INTEL_INFO(dev)->gen >= 9) {
14093 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014094 dev_priv->display.get_initial_plane_config =
14095 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014096 dev_priv->display.crtc_compute_clock =
14097 haswell_crtc_compute_clock;
14098 dev_priv->display.crtc_enable = haswell_crtc_enable;
14099 dev_priv->display.crtc_disable = haswell_crtc_disable;
14100 dev_priv->display.off = ironlake_crtc_off;
14101 dev_priv->display.update_primary_plane =
14102 skylake_update_primary_plane;
14103 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014104 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014105 dev_priv->display.get_initial_plane_config =
14106 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014107 dev_priv->display.crtc_compute_clock =
14108 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014109 dev_priv->display.crtc_enable = haswell_crtc_enable;
14110 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030014111 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014112 dev_priv->display.update_primary_plane =
14113 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014114 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014115 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014116 dev_priv->display.get_initial_plane_config =
14117 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014118 dev_priv->display.crtc_compute_clock =
14119 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014120 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14121 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014122 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014123 dev_priv->display.update_primary_plane =
14124 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014125 } else if (IS_VALLEYVIEW(dev)) {
14126 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014127 dev_priv->display.get_initial_plane_config =
14128 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014129 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014130 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14131 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14132 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014133 dev_priv->display.update_primary_plane =
14134 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014135 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014136 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014137 dev_priv->display.get_initial_plane_config =
14138 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014139 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014140 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14141 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014142 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014143 dev_priv->display.update_primary_plane =
14144 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014145 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014146
Jesse Barnese70236a2009-09-21 10:42:27 -070014147 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014148 if (IS_SKYLAKE(dev))
14149 dev_priv->display.get_display_clock_speed =
14150 skylake_get_display_clock_speed;
14151 else if (IS_BROADWELL(dev))
14152 dev_priv->display.get_display_clock_speed =
14153 broadwell_get_display_clock_speed;
14154 else if (IS_HASWELL(dev))
14155 dev_priv->display.get_display_clock_speed =
14156 haswell_get_display_clock_speed;
14157 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014158 dev_priv->display.get_display_clock_speed =
14159 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014160 else if (IS_GEN5(dev))
14161 dev_priv->display.get_display_clock_speed =
14162 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014163 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14164 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070014165 dev_priv->display.get_display_clock_speed =
14166 i945_get_display_clock_speed;
14167 else if (IS_I915G(dev))
14168 dev_priv->display.get_display_clock_speed =
14169 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014170 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014171 dev_priv->display.get_display_clock_speed =
14172 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014173 else if (IS_PINEVIEW(dev))
14174 dev_priv->display.get_display_clock_speed =
14175 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014176 else if (IS_I915GM(dev))
14177 dev_priv->display.get_display_clock_speed =
14178 i915gm_get_display_clock_speed;
14179 else if (IS_I865G(dev))
14180 dev_priv->display.get_display_clock_speed =
14181 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014182 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014183 dev_priv->display.get_display_clock_speed =
14184 i855_get_display_clock_speed;
14185 else /* 852, 830 */
14186 dev_priv->display.get_display_clock_speed =
14187 i830_get_display_clock_speed;
14188
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014189 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014190 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014191 } else if (IS_GEN6(dev)) {
14192 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014193 } else if (IS_IVYBRIDGE(dev)) {
14194 /* FIXME: detect B0+ stepping and use auto training */
14195 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014196 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014197 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014198 } else if (IS_VALLEYVIEW(dev)) {
14199 dev_priv->display.modeset_global_resources =
14200 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014201 } else if (IS_BROXTON(dev)) {
14202 dev_priv->display.modeset_global_resources =
14203 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014204 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014205
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014206 switch (INTEL_INFO(dev)->gen) {
14207 case 2:
14208 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14209 break;
14210
14211 case 3:
14212 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14213 break;
14214
14215 case 4:
14216 case 5:
14217 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14218 break;
14219
14220 case 6:
14221 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14222 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014223 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014224 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014225 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14226 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014227 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014228 /* Drop through - unsupported since execlist only. */
14229 default:
14230 /* Default just returns -ENODEV to indicate unsupported */
14231 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014232 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014233
14234 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014235
14236 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014237}
14238
Jesse Barnesb690e962010-07-19 13:53:12 -070014239/*
14240 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14241 * resume, or other times. This quirk makes sure that's the case for
14242 * affected systems.
14243 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014244static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014245{
14246 struct drm_i915_private *dev_priv = dev->dev_private;
14247
14248 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014249 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014250}
14251
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014252static void quirk_pipeb_force(struct drm_device *dev)
14253{
14254 struct drm_i915_private *dev_priv = dev->dev_private;
14255
14256 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14257 DRM_INFO("applying pipe b force quirk\n");
14258}
14259
Keith Packard435793d2011-07-12 14:56:22 -070014260/*
14261 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14262 */
14263static void quirk_ssc_force_disable(struct drm_device *dev)
14264{
14265 struct drm_i915_private *dev_priv = dev->dev_private;
14266 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014267 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014268}
14269
Carsten Emde4dca20e2012-03-15 15:56:26 +010014270/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014271 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14272 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014273 */
14274static void quirk_invert_brightness(struct drm_device *dev)
14275{
14276 struct drm_i915_private *dev_priv = dev->dev_private;
14277 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014278 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014279}
14280
Scot Doyle9c72cc62014-07-03 23:27:50 +000014281/* Some VBT's incorrectly indicate no backlight is present */
14282static void quirk_backlight_present(struct drm_device *dev)
14283{
14284 struct drm_i915_private *dev_priv = dev->dev_private;
14285 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14286 DRM_INFO("applying backlight present quirk\n");
14287}
14288
Jesse Barnesb690e962010-07-19 13:53:12 -070014289struct intel_quirk {
14290 int device;
14291 int subsystem_vendor;
14292 int subsystem_device;
14293 void (*hook)(struct drm_device *dev);
14294};
14295
Egbert Eich5f85f172012-10-14 15:46:38 +020014296/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14297struct intel_dmi_quirk {
14298 void (*hook)(struct drm_device *dev);
14299 const struct dmi_system_id (*dmi_id_list)[];
14300};
14301
14302static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14303{
14304 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14305 return 1;
14306}
14307
14308static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14309 {
14310 .dmi_id_list = &(const struct dmi_system_id[]) {
14311 {
14312 .callback = intel_dmi_reverse_brightness,
14313 .ident = "NCR Corporation",
14314 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14315 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14316 },
14317 },
14318 { } /* terminating entry */
14319 },
14320 .hook = quirk_invert_brightness,
14321 },
14322};
14323
Ben Widawskyc43b5632012-04-16 14:07:40 -070014324static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014325 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14326 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14327
Jesse Barnesb690e962010-07-19 13:53:12 -070014328 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14329 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14330
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014331 /* 830 needs to leave pipe A & dpll A up */
14332 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14333
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014334 /* 830 needs to leave pipe B & dpll B up */
14335 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14336
Keith Packard435793d2011-07-12 14:56:22 -070014337 /* Lenovo U160 cannot use SSC on LVDS */
14338 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014339
14340 /* Sony Vaio Y cannot use SSC on LVDS */
14341 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014342
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014343 /* Acer Aspire 5734Z must invert backlight brightness */
14344 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14345
14346 /* Acer/eMachines G725 */
14347 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14348
14349 /* Acer/eMachines e725 */
14350 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14351
14352 /* Acer/Packard Bell NCL20 */
14353 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14354
14355 /* Acer Aspire 4736Z */
14356 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014357
14358 /* Acer Aspire 5336 */
14359 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014360
14361 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14362 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014363
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014364 /* Acer C720 Chromebook (Core i3 4005U) */
14365 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14366
jens steinb2a96012014-10-28 20:25:53 +010014367 /* Apple Macbook 2,1 (Core 2 T7400) */
14368 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14369
Scot Doyled4967d82014-07-03 23:27:52 +000014370 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14371 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014372
14373 /* HP Chromebook 14 (Celeron 2955U) */
14374 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014375
14376 /* Dell Chromebook 11 */
14377 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014378};
14379
14380static void intel_init_quirks(struct drm_device *dev)
14381{
14382 struct pci_dev *d = dev->pdev;
14383 int i;
14384
14385 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14386 struct intel_quirk *q = &intel_quirks[i];
14387
14388 if (d->device == q->device &&
14389 (d->subsystem_vendor == q->subsystem_vendor ||
14390 q->subsystem_vendor == PCI_ANY_ID) &&
14391 (d->subsystem_device == q->subsystem_device ||
14392 q->subsystem_device == PCI_ANY_ID))
14393 q->hook(dev);
14394 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014395 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14396 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14397 intel_dmi_quirks[i].hook(dev);
14398 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014399}
14400
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014401/* Disable the VGA plane that we never use */
14402static void i915_disable_vga(struct drm_device *dev)
14403{
14404 struct drm_i915_private *dev_priv = dev->dev_private;
14405 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014406 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014407
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014408 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014409 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014410 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014411 sr1 = inb(VGA_SR_DATA);
14412 outb(sr1 | 1<<5, VGA_SR_DATA);
14413 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14414 udelay(300);
14415
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014416 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014417 POSTING_READ(vga_reg);
14418}
14419
Daniel Vetterf8175862012-04-10 15:50:11 +020014420void intel_modeset_init_hw(struct drm_device *dev)
14421{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014422 intel_prepare_ddi(dev);
14423
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030014424 if (IS_VALLEYVIEW(dev))
14425 vlv_update_cdclk(dev);
14426
Daniel Vetterf8175862012-04-10 15:50:11 +020014427 intel_init_clock_gating(dev);
14428
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014429 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014430}
14431
Jesse Barnes79e53942008-11-07 14:24:08 -080014432void intel_modeset_init(struct drm_device *dev)
14433{
Jesse Barnes652c3932009-08-17 13:31:43 -070014434 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014435 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014436 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014437 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014438
14439 drm_mode_config_init(dev);
14440
14441 dev->mode_config.min_width = 0;
14442 dev->mode_config.min_height = 0;
14443
Dave Airlie019d96c2011-09-29 16:20:42 +010014444 dev->mode_config.preferred_depth = 24;
14445 dev->mode_config.prefer_shadow = 1;
14446
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014447 dev->mode_config.allow_fb_modifiers = true;
14448
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014449 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014450
Jesse Barnesb690e962010-07-19 13:53:12 -070014451 intel_init_quirks(dev);
14452
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014453 intel_init_pm(dev);
14454
Ben Widawskye3c74752013-04-05 13:12:39 -070014455 if (INTEL_INFO(dev)->num_pipes == 0)
14456 return;
14457
Jesse Barnese70236a2009-09-21 10:42:27 -070014458 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014459 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014460
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014461 if (IS_GEN2(dev)) {
14462 dev->mode_config.max_width = 2048;
14463 dev->mode_config.max_height = 2048;
14464 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014465 dev->mode_config.max_width = 4096;
14466 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014467 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014468 dev->mode_config.max_width = 8192;
14469 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014470 }
Damien Lespiau068be562014-03-28 14:17:49 +000014471
Ville Syrjälädc41c152014-08-13 11:57:05 +030014472 if (IS_845G(dev) || IS_I865G(dev)) {
14473 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14474 dev->mode_config.cursor_height = 1023;
14475 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014476 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14477 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14478 } else {
14479 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14480 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14481 }
14482
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014483 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014484
Zhao Yakui28c97732009-10-09 11:39:41 +080014485 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014486 INTEL_INFO(dev)->num_pipes,
14487 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014488
Damien Lespiau055e3932014-08-18 13:49:10 +010014489 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014490 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014491 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014492 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014493 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014494 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014495 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014496 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014497 }
14498
Jesse Barnesf42bb702013-12-16 16:34:23 -080014499 intel_init_dpio(dev);
14500
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014501 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014502
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014503 /* Just disable it once at startup */
14504 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014505 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014506
14507 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014508 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014509
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014510 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014511 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014512 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014513
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014514 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080014515 if (!crtc->active)
14516 continue;
14517
Jesse Barnes46f297f2014-03-07 08:57:48 -080014518 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014519 * Note that reserving the BIOS fb up front prevents us
14520 * from stuffing other stolen allocations like the ring
14521 * on top. This prevents some ugliness at boot time, and
14522 * can even allow for smooth boot transitions if the BIOS
14523 * fb is large enough for the active pipe configuration.
14524 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014525 if (dev_priv->display.get_initial_plane_config) {
14526 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080014527 &crtc->plane_config);
14528 /*
14529 * If the fb is shared between multiple heads, we'll
14530 * just get the first one.
14531 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010014532 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014533 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080014534 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014535}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014536
Daniel Vetter7fad7982012-07-04 17:51:47 +020014537static void intel_enable_pipe_a(struct drm_device *dev)
14538{
14539 struct intel_connector *connector;
14540 struct drm_connector *crt = NULL;
14541 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014542 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014543
14544 /* We can't just switch on the pipe A, we need to set things up with a
14545 * proper mode and output configuration. As a gross hack, enable pipe A
14546 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014547 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014548 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14549 crt = &connector->base;
14550 break;
14551 }
14552 }
14553
14554 if (!crt)
14555 return;
14556
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014557 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014558 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014559}
14560
Daniel Vetterfa555832012-10-10 23:14:00 +020014561static bool
14562intel_check_plane_mapping(struct intel_crtc *crtc)
14563{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014564 struct drm_device *dev = crtc->base.dev;
14565 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014566 u32 reg, val;
14567
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014568 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014569 return true;
14570
14571 reg = DSPCNTR(!crtc->plane);
14572 val = I915_READ(reg);
14573
14574 if ((val & DISPLAY_PLANE_ENABLE) &&
14575 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14576 return false;
14577
14578 return true;
14579}
14580
Daniel Vetter24929352012-07-02 20:28:59 +020014581static void intel_sanitize_crtc(struct intel_crtc *crtc)
14582{
14583 struct drm_device *dev = crtc->base.dev;
14584 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014585 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014586
Daniel Vetter24929352012-07-02 20:28:59 +020014587 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014588 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014589 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14590
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014591 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014592 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014593 if (crtc->active) {
14594 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014595 drm_crtc_vblank_on(&crtc->base);
14596 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014597
Daniel Vetter24929352012-07-02 20:28:59 +020014598 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014599 * disable the crtc (and hence change the state) if it is wrong. Note
14600 * that gen4+ has a fixed plane -> pipe mapping. */
14601 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014602 struct intel_connector *connector;
14603 bool plane;
14604
Daniel Vetter24929352012-07-02 20:28:59 +020014605 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14606 crtc->base.base.id);
14607
14608 /* Pipe has the wrong plane attached and the plane is active.
14609 * Temporarily change the plane mapping and disable everything
14610 * ... */
14611 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014612 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014613 crtc->plane = !plane;
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030014614 intel_crtc_disable_planes(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020014615 dev_priv->display.crtc_disable(&crtc->base);
14616 crtc->plane = plane;
14617
14618 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014619 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014620 if (connector->encoder->base.crtc != &crtc->base)
14621 continue;
14622
Egbert Eich7f1950f2014-04-25 10:56:22 +020014623 connector->base.dpms = DRM_MODE_DPMS_OFF;
14624 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014625 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014626 /* multiple connectors may have the same encoder:
14627 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014628 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020014629 if (connector->encoder->base.crtc == &crtc->base) {
14630 connector->encoder->base.crtc = NULL;
14631 connector->encoder->connectors_active = false;
14632 }
Daniel Vetter24929352012-07-02 20:28:59 +020014633
14634 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080014635 crtc->base.state->enable = false;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014636 crtc->base.state->active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014637 crtc->base.enabled = false;
14638 }
Daniel Vetter24929352012-07-02 20:28:59 +020014639
Daniel Vetter7fad7982012-07-04 17:51:47 +020014640 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14641 crtc->pipe == PIPE_A && !crtc->active) {
14642 /* BIOS forgot to enable pipe A, this mostly happens after
14643 * resume. Force-enable the pipe to fix this, the update_dpms
14644 * call below we restore the pipe to the right state, but leave
14645 * the required bits on. */
14646 intel_enable_pipe_a(dev);
14647 }
14648
Daniel Vetter24929352012-07-02 20:28:59 +020014649 /* Adjust the state of the output pipe according to whether we
14650 * have active connectors/encoders. */
14651 intel_crtc_update_dpms(&crtc->base);
14652
Matt Roper83d65732015-02-25 13:12:16 -080014653 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020014654 struct intel_encoder *encoder;
14655
14656 /* This can happen either due to bugs in the get_hw_state
14657 * functions or because the pipe is force-enabled due to the
14658 * pipe A quirk. */
14659 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14660 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014661 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014662 crtc->active ? "enabled" : "disabled");
14663
Matt Roper83d65732015-02-25 13:12:16 -080014664 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014665 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014666 crtc->base.enabled = crtc->active;
14667
14668 /* Because we only establish the connector -> encoder ->
14669 * crtc links if something is active, this means the
14670 * crtc is now deactivated. Break the links. connector
14671 * -> encoder links are only establish when things are
14672 * actually up, hence no need to break them. */
14673 WARN_ON(crtc->active);
14674
14675 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14676 WARN_ON(encoder->connectors_active);
14677 encoder->base.crtc = NULL;
14678 }
14679 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014680
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014681 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014682 /*
14683 * We start out with underrun reporting disabled to avoid races.
14684 * For correct bookkeeping mark this on active crtcs.
14685 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014686 * Also on gmch platforms we dont have any hardware bits to
14687 * disable the underrun reporting. Which means we need to start
14688 * out with underrun reporting disabled also on inactive pipes,
14689 * since otherwise we'll complain about the garbage we read when
14690 * e.g. coming up after runtime pm.
14691 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014692 * No protection against concurrent access is required - at
14693 * worst a fifo underrun happens which also sets this to false.
14694 */
14695 crtc->cpu_fifo_underrun_disabled = true;
14696 crtc->pch_fifo_underrun_disabled = true;
14697 }
Daniel Vetter24929352012-07-02 20:28:59 +020014698}
14699
14700static void intel_sanitize_encoder(struct intel_encoder *encoder)
14701{
14702 struct intel_connector *connector;
14703 struct drm_device *dev = encoder->base.dev;
14704
14705 /* We need to check both for a crtc link (meaning that the
14706 * encoder is active and trying to read from a pipe) and the
14707 * pipe itself being active. */
14708 bool has_active_crtc = encoder->base.crtc &&
14709 to_intel_crtc(encoder->base.crtc)->active;
14710
14711 if (encoder->connectors_active && !has_active_crtc) {
14712 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14713 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014714 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014715
14716 /* Connector is active, but has no active pipe. This is
14717 * fallout from our resume register restoring. Disable
14718 * the encoder manually again. */
14719 if (encoder->base.crtc) {
14720 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14721 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014722 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014723 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014724 if (encoder->post_disable)
14725 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014726 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014727 encoder->base.crtc = NULL;
14728 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014729
14730 /* Inconsistent output/port/pipe state happens presumably due to
14731 * a bug in one of the get_hw_state functions. Or someplace else
14732 * in our code, like the register restore mess on resume. Clamp
14733 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014734 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014735 if (connector->encoder != encoder)
14736 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014737 connector->base.dpms = DRM_MODE_DPMS_OFF;
14738 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014739 }
14740 }
14741 /* Enabled encoders without active connectors will be fixed in
14742 * the crtc fixup. */
14743}
14744
Imre Deak04098752014-02-18 00:02:16 +020014745void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014746{
14747 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014748 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014749
Imre Deak04098752014-02-18 00:02:16 +020014750 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14751 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14752 i915_disable_vga(dev);
14753 }
14754}
14755
14756void i915_redisable_vga(struct drm_device *dev)
14757{
14758 struct drm_i915_private *dev_priv = dev->dev_private;
14759
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014760 /* This function can be called both from intel_modeset_setup_hw_state or
14761 * at a very early point in our resume sequence, where the power well
14762 * structures are not yet restored. Since this function is at a very
14763 * paranoid "someone might have enabled VGA while we were not looking"
14764 * level, just check if the power well is enabled instead of trying to
14765 * follow the "don't touch the power well if we don't need it" policy
14766 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014767 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014768 return;
14769
Imre Deak04098752014-02-18 00:02:16 +020014770 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014771}
14772
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014773static bool primary_get_hw_state(struct intel_crtc *crtc)
14774{
14775 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14776
14777 if (!crtc->active)
14778 return false;
14779
14780 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
14781}
14782
Daniel Vetter30e984d2013-06-05 13:34:17 +020014783static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020014784{
14785 struct drm_i915_private *dev_priv = dev->dev_private;
14786 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020014787 struct intel_crtc *crtc;
14788 struct intel_encoder *encoder;
14789 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020014790 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020014791
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014792 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014793 struct drm_plane *primary = crtc->base.primary;
14794 struct intel_plane_state *plane_state;
14795
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014796 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020014797
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014798 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020014799
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014800 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014801 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014802
Matt Roper83d65732015-02-25 13:12:16 -080014803 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014804 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014805 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014806
14807 plane_state = to_intel_plane_state(primary->state);
14808 plane_state->visible = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020014809
14810 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
14811 crtc->base.base.id,
14812 crtc->active ? "enabled" : "disabled");
14813 }
14814
Daniel Vetter53589012013-06-05 13:34:16 +020014815 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14816 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14817
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014818 pll->on = pll->get_hw_state(dev_priv, pll,
14819 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020014820 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014821 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014822 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014823 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020014824 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014825 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014826 }
Daniel Vetter53589012013-06-05 13:34:16 +020014827 }
Daniel Vetter53589012013-06-05 13:34:16 +020014828
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014829 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014830 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014831
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014832 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014833 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020014834 }
14835
Damien Lespiaub2784e12014-08-05 11:29:37 +010014836 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014837 pipe = 0;
14838
14839 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070014840 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14841 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014842 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014843 } else {
14844 encoder->base.crtc = NULL;
14845 }
14846
14847 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014848 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020014849 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014850 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014851 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014852 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020014853 }
14854
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014855 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014856 if (connector->get_hw_state(connector)) {
14857 connector->base.dpms = DRM_MODE_DPMS_ON;
14858 connector->encoder->connectors_active = true;
14859 connector->base.encoder = &connector->encoder->base;
14860 } else {
14861 connector->base.dpms = DRM_MODE_DPMS_OFF;
14862 connector->base.encoder = NULL;
14863 }
14864 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
14865 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030014866 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014867 connector->base.encoder ? "enabled" : "disabled");
14868 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020014869}
14870
14871/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
14872 * and i915 state tracking structures. */
14873void intel_modeset_setup_hw_state(struct drm_device *dev,
14874 bool force_restore)
14875{
14876 struct drm_i915_private *dev_priv = dev->dev_private;
14877 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014878 struct intel_crtc *crtc;
14879 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020014880 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014881
14882 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020014883
Jesse Barnesbabea612013-06-26 18:57:38 +030014884 /*
14885 * Now that we have the config, copy it to each CRTC struct
14886 * Note that this could go away if we move to using crtc_config
14887 * checking everywhere.
14888 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014889 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020014890 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014891 intel_mode_from_pipe_config(&crtc->base.mode,
14892 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030014893 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
14894 crtc->base.base.id);
14895 drm_mode_debug_printmodeline(&crtc->base.mode);
14896 }
14897 }
14898
Daniel Vetter24929352012-07-02 20:28:59 +020014899 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010014900 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014901 intel_sanitize_encoder(encoder);
14902 }
14903
Damien Lespiau055e3932014-08-18 13:49:10 +010014904 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020014905 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14906 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014907 intel_dump_pipe_config(crtc, crtc->config,
14908 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020014909 }
Daniel Vetter9a935852012-07-05 22:34:27 +020014910
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020014911 intel_modeset_update_connector_atomic_state(dev);
14912
Daniel Vetter35c95372013-07-17 06:55:04 +020014913 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14914 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14915
14916 if (!pll->on || pll->active)
14917 continue;
14918
14919 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
14920
14921 pll->disable(dev_priv, pll);
14922 pll->on = false;
14923 }
14924
Pradeep Bhat30789992014-11-04 17:06:45 +000014925 if (IS_GEN9(dev))
14926 skl_wm_get_hw_state(dev);
14927 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030014928 ilk_wm_get_hw_state(dev);
14929
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010014930 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030014931 i915_redisable_vga(dev);
14932
Daniel Vetterf30da182013-04-11 20:22:50 +020014933 /*
14934 * We need to use raw interfaces for restoring state to avoid
14935 * checking (bogus) intermediate states.
14936 */
Damien Lespiau055e3932014-08-18 13:49:10 +010014937 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070014938 struct drm_crtc *crtc =
14939 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020014940
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014941 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010014942 }
14943 } else {
14944 intel_modeset_update_staged_output_state(dev);
14945 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020014946
14947 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010014948}
14949
14950void intel_modeset_gem_init(struct drm_device *dev)
14951{
Jesse Barnes92122782014-10-09 12:57:42 -070014952 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080014953 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070014954 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010014955 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080014956
Imre Deakae484342014-03-31 15:10:44 +030014957 mutex_lock(&dev->struct_mutex);
14958 intel_init_gt_powersave(dev);
14959 mutex_unlock(&dev->struct_mutex);
14960
Jesse Barnes92122782014-10-09 12:57:42 -070014961 /*
14962 * There may be no VBT; and if the BIOS enabled SSC we can
14963 * just keep using it to avoid unnecessary flicker. Whereas if the
14964 * BIOS isn't using it, don't assume it will work even if the VBT
14965 * indicates as much.
14966 */
14967 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
14968 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14969 DREF_SSC1_ENABLE);
14970
Chris Wilson1833b132012-05-09 11:56:28 +010014971 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020014972
14973 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080014974
14975 /*
14976 * Make sure any fbs we allocated at startup are properly
14977 * pinned & fenced. When we do the allocation it's too early
14978 * for this.
14979 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010014980 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070014981 obj = intel_fb_obj(c->primary->fb);
14982 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080014983 continue;
14984
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010014985 mutex_lock(&dev->struct_mutex);
14986 ret = intel_pin_and_fence_fb_obj(c->primary,
14987 c->primary->fb,
14988 c->primary->state,
14989 NULL);
14990 mutex_unlock(&dev->struct_mutex);
14991 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080014992 DRM_ERROR("failed to pin boot fb on pipe %d\n",
14993 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100014994 drm_framebuffer_unreference(c->primary->fb);
14995 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080014996 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080014997 }
14998 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020014999
15000 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015001}
15002
Imre Deak4932e2c2014-02-11 17:12:48 +020015003void intel_connector_unregister(struct intel_connector *intel_connector)
15004{
15005 struct drm_connector *connector = &intel_connector->base;
15006
15007 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015008 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015009}
15010
Jesse Barnes79e53942008-11-07 14:24:08 -080015011void intel_modeset_cleanup(struct drm_device *dev)
15012{
Jesse Barnes652c3932009-08-17 13:31:43 -070015013 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015014 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015015
Imre Deak2eb52522014-11-19 15:30:05 +020015016 intel_disable_gt_powersave(dev);
15017
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015018 intel_backlight_unregister(dev);
15019
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015020 /*
15021 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015022 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015023 * experience fancy races otherwise.
15024 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015025 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015026
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015027 /*
15028 * Due to the hpd irq storm handling the hotplug work can re-arm the
15029 * poll handlers. Hence disable polling after hpd handling is shut down.
15030 */
Keith Packardf87ea762010-10-03 19:36:26 -070015031 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015032
Jesse Barnes652c3932009-08-17 13:31:43 -070015033 mutex_lock(&dev->struct_mutex);
15034
Jesse Barnes723bfd72010-10-07 16:01:13 -070015035 intel_unregister_dsm_handler();
15036
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015037 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015038
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015039 mutex_unlock(&dev->struct_mutex);
15040
Chris Wilson1630fe72011-07-08 12:22:42 +010015041 /* flush any delayed tasks or pending work */
15042 flush_scheduled_work();
15043
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015044 /* destroy the backlight and sysfs files before encoders/connectors */
15045 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015046 struct intel_connector *intel_connector;
15047
15048 intel_connector = to_intel_connector(connector);
15049 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015050 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015051
Jesse Barnes79e53942008-11-07 14:24:08 -080015052 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015053
15054 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015055
15056 mutex_lock(&dev->struct_mutex);
15057 intel_cleanup_gt_powersave(dev);
15058 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015059}
15060
Dave Airlie28d52042009-09-21 14:33:58 +100015061/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015062 * Return which encoder is currently attached for connector.
15063 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015064struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015065{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015066 return &intel_attached_encoder(connector)->base;
15067}
Jesse Barnes79e53942008-11-07 14:24:08 -080015068
Chris Wilsondf0e9242010-09-09 16:20:55 +010015069void intel_connector_attach_encoder(struct intel_connector *connector,
15070 struct intel_encoder *encoder)
15071{
15072 connector->encoder = encoder;
15073 drm_mode_connector_attach_encoder(&connector->base,
15074 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015075}
Dave Airlie28d52042009-09-21 14:33:58 +100015076
15077/*
15078 * set vga decode state - true == enable VGA decode
15079 */
15080int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15081{
15082 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015083 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015084 u16 gmch_ctrl;
15085
Chris Wilson75fa0412014-02-07 18:37:02 -020015086 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15087 DRM_ERROR("failed to read control word\n");
15088 return -EIO;
15089 }
15090
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015091 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15092 return 0;
15093
Dave Airlie28d52042009-09-21 14:33:58 +100015094 if (state)
15095 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15096 else
15097 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015098
15099 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15100 DRM_ERROR("failed to write control word\n");
15101 return -EIO;
15102 }
15103
Dave Airlie28d52042009-09-21 14:33:58 +100015104 return 0;
15105}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015106
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015107struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015108
15109 u32 power_well_driver;
15110
Chris Wilson63b66e52013-08-08 15:12:06 +020015111 int num_transcoders;
15112
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015113 struct intel_cursor_error_state {
15114 u32 control;
15115 u32 position;
15116 u32 base;
15117 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015118 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015119
15120 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015121 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015122 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015123 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015124 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015125
15126 struct intel_plane_error_state {
15127 u32 control;
15128 u32 stride;
15129 u32 size;
15130 u32 pos;
15131 u32 addr;
15132 u32 surface;
15133 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015134 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015135
15136 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015137 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015138 enum transcoder cpu_transcoder;
15139
15140 u32 conf;
15141
15142 u32 htotal;
15143 u32 hblank;
15144 u32 hsync;
15145 u32 vtotal;
15146 u32 vblank;
15147 u32 vsync;
15148 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015149};
15150
15151struct intel_display_error_state *
15152intel_display_capture_error_state(struct drm_device *dev)
15153{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015154 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015155 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015156 int transcoders[] = {
15157 TRANSCODER_A,
15158 TRANSCODER_B,
15159 TRANSCODER_C,
15160 TRANSCODER_EDP,
15161 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015162 int i;
15163
Chris Wilson63b66e52013-08-08 15:12:06 +020015164 if (INTEL_INFO(dev)->num_pipes == 0)
15165 return NULL;
15166
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015167 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015168 if (error == NULL)
15169 return NULL;
15170
Imre Deak190be112013-11-25 17:15:31 +020015171 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015172 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15173
Damien Lespiau055e3932014-08-18 13:49:10 +010015174 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015175 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015176 __intel_display_power_is_enabled(dev_priv,
15177 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015178 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015179 continue;
15180
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015181 error->cursor[i].control = I915_READ(CURCNTR(i));
15182 error->cursor[i].position = I915_READ(CURPOS(i));
15183 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015184
15185 error->plane[i].control = I915_READ(DSPCNTR(i));
15186 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015187 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015188 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015189 error->plane[i].pos = I915_READ(DSPPOS(i));
15190 }
Paulo Zanonica291362013-03-06 20:03:14 -030015191 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15192 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015193 if (INTEL_INFO(dev)->gen >= 4) {
15194 error->plane[i].surface = I915_READ(DSPSURF(i));
15195 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15196 }
15197
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015198 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015199
Sonika Jindal3abfce72014-07-21 15:23:43 +053015200 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015201 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015202 }
15203
15204 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15205 if (HAS_DDI(dev_priv->dev))
15206 error->num_transcoders++; /* Account for eDP. */
15207
15208 for (i = 0; i < error->num_transcoders; i++) {
15209 enum transcoder cpu_transcoder = transcoders[i];
15210
Imre Deakddf9c532013-11-27 22:02:02 +020015211 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015212 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015213 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015214 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015215 continue;
15216
Chris Wilson63b66e52013-08-08 15:12:06 +020015217 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15218
15219 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15220 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15221 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15222 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15223 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15224 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15225 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015226 }
15227
15228 return error;
15229}
15230
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015231#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15232
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015233void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015234intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015235 struct drm_device *dev,
15236 struct intel_display_error_state *error)
15237{
Damien Lespiau055e3932014-08-18 13:49:10 +010015238 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015239 int i;
15240
Chris Wilson63b66e52013-08-08 15:12:06 +020015241 if (!error)
15242 return;
15243
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015244 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015245 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015246 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015247 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015248 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015249 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015250 err_printf(m, " Power: %s\n",
15251 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015252 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015253 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015254
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015255 err_printf(m, "Plane [%d]:\n", i);
15256 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15257 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015258 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015259 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15260 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015261 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015262 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015263 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015264 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015265 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15266 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015267 }
15268
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015269 err_printf(m, "Cursor [%d]:\n", i);
15270 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15271 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15272 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015273 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015274
15275 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015276 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015277 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015278 err_printf(m, " Power: %s\n",
15279 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015280 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15281 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15282 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15283 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15284 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15285 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15286 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15287 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015288}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015289
15290void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15291{
15292 struct intel_crtc *crtc;
15293
15294 for_each_intel_crtc(dev, crtc) {
15295 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015296
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015297 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015298
15299 work = crtc->unpin_work;
15300
15301 if (work && work->event &&
15302 work->event->base.file_priv == file) {
15303 kfree(work->event);
15304 work->event = NULL;
15305 }
15306
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015307 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015308 }
15309}